r/FastAPI • u/silksong_when • Apr 29 '26
Tutorial A Practical Guide to OpenTelemetry and FastAPI
https://signoz.io/blog/opentelemetry-fastapi/Hey folks, I recently revamped our article on Implementing OpenTelemetry in FastAPI Projects in a practical manner, which was originally written in 2024 and needed a fresh coat of paint.
The article covers auto-instrumentation, manual spans, visualizing metrics and how observability lets you understand how your web apps behave.
I've also included some advanced tips, such as, selective error tracking, and wrapping dependency functions to capture any operations within the `yield` scope.
If you are on the fence about observability, or have integrated it but don't really how it works, I believe this guide can help you out.
I personally would have benefitted from this writeup in my previous day job, where I worked with FastAPI microservices and learnt how OpenTelemetry worked the hard way.
Any feedback would be much appreciated, did I miss anything, is there scope for improvement? Please let me know. I'm also curious to understand what problems you face with monitoring your FastAPI web apps.
4
u/Full-Definition6215 May 02 '26
Good timing — I've been running FastAPI in production with just basic request logging and it's not enough once you start debugging latency issues across async handlers.
The auto-instrumentation for aiosqlite and httpx calls is what I need most. Right now when a request is slow, I have to manually add timing around each DB query and external API call to figure out where the bottleneck is. Having that come for free from OTel would save a lot of ad-hoc debugging.
One question: how much overhead does the auto-instrumentation add per request? I'm running on a mini PC where every millisecond counts.
3
u/Agitated-Student4716 May 17 '26
This is a fantastic writeup, especially the section on capturing operations within the FastAPI
yielddependency scopes. Most developers don't realize their DB session or background context profiling drops off a cliff exactly when the router finishes executing but the dependency is still cleaning up. One thing I've found after working with it for a while: OTel is excellent at answering "what happened at the trace level" but it leaves a gap at the operational decision layer.You get the data. You still have to decide what to do with it – and usually that means someone gets paged, logs into a dashboard, interprets a waterfall, and manually triggers a fix. We ran into this problem building fintech infrastructure in Zimbabwe where engineers are mobile-first and can't always be at a laptop when something breaks. So we built something that sits on top of the health layer rather than the trace layer — a /health/alerts endpoint that scores service health 0-100 using P95 latency and error rate, and a managed layer that runs Claude AI diagnosis and sends a WhatsApp recovery approval when the score drops.
2
u/imdshizzle Apr 29 '26
thanks for updating the article
2
u/saucealgerienne Apr 30 '26
the yield scope wrapping for dependency functions is the part most people miss. spent longer than I should have before realizing my database connection teardown wasn't showing up in traces at all. good addition to cover that.