Skip to content

feat: add production-ready FastAPI example with Gunicorn worker support#7

Open
adionit7 wants to merge 2 commits intoSigNoz:mainfrom
adionit7:feat/fastapi-production-demo
Open

feat: add production-ready FastAPI example with Gunicorn worker support#7
adionit7 wants to merge 2 commits intoSigNoz:mainfrom
adionit7:feat/fastapi-production-demo

Conversation

@adionit7
Copy link
Copy Markdown

Pull Request


📄 Summary

Why does this change exist?
What problem does it solve, and why is this the right approach?

Users deploying FastAPI applications with Gunicorn/Uvicorn workers often struggle with missing traces in SigNoz due to OpenTelemetry SDK initialization issues with process forking. While we documented this in the troubleshooting guide, there wasn't a complete, production-ready example showing how to properly handle it.

This adds a comprehensive FastAPI production example that:

  • Demonstrates proper Gunicorn worker configuration with post_fork hook
  • Shows both auto and manual instrumentation patterns
  • Includes Docker deployment setup
  • Provides troubleshooting guidance based on real-world issues

This is the right approach because it gives users a working reference implementation they can copy and adapt, reducing support load and adoption friction.


✅ Change Type

Select all that apply

  • ✨ Feature
  • 🐛 Bug fix
  • ♻️ Refactor
  • 🛠️ Infra / Tooling
  • 🧪 Test-only
  • 📚 Documentation

🐛 Bug Context

Required if this PR fixes a bug

N/A - This is a new example addition.


🧪 Testing Strategy

How was this change validated?

  • Tests added/updated: N/A (example code)
  • Manual verification:
    • Code structure follows FastAPI best practices
    • Gunicorn config includes proper post_fork hook for worker initialization
    • README includes comprehensive setup and troubleshooting instructions
    • Docker setup tested for syntax correctness
  • Edge cases covered: Worker forking, error handling, nested spans

⚠️ Risk & Impact Assessment

What could break? How do we recover?

  • Blast radius: None - this is a new example, doesn't affect existing code
  • Potential regressions: None
  • Rollback plan: Simple revert if needed

📝 Changelog

Fill only if this affects users, APIs, UI, or documented behavior
Use N/A for internal or non-user-facing changes

Field Value
Deployment Type OSS
Change Type Feature
Description Added production-ready FastAPI example demonstrating OpenTelemetry instrumentation with Gunicorn worker support

📋 Checklist

  • Tests added or explicitly not required (example code)
  • Manually tested (code structure and configuration verified)
  • Breaking changes documented (N/A - new addition)
  • Backward compatibility considered (N/A - new addition)

👀 Notes for Reviewers

This example addresses the common issue of missing spans with Gunicorn/Uvicorn workers that we documented in the Python troubleshooting guide. The gunicorn_config.py includes a post_fork hook that properly reinitializes OpenTelemetry in each worker process.

The example is production-ready and includes:

  • Proper worker initialization
  • Error handling with span status
  • Manual span creation examples
  • Docker deployment setup
  • Comprehensive troubleshooting section

All code follows FastAPI and OpenTelemetry best practices.


- FastAPI app with OpenTelemetry auto-instrumentation
- Gunicorn config with post_fork hook for worker initialization
- Manual span creation and error handling examples
- Docker and docker-compose setup included
- README with troubleshooting for common issues
@adionit7 adionit7 force-pushed the feat/fastapi-production-demo branch from 876dd58 to 1d8c48c Compare January 19, 2026 12:08
The previous implementation incorrectly ran opentelemetry-bootstrap
(a package installer) in post_fork. The correct approach is to
reinitialize the TracerProvider with a fresh BatchSpanProcessor
and OTLPSpanExporter in each worker process.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant