Is there an open-source way to measure Python code coverage from production traffic?
Asked by
MellowCedar42
Our backend services are mostly built with FastAPI, and I'm looking for open-source runtime coverage instrumentation for Python. Ideally, it would show which lines of code were executed by real production traffic over something like the last 30 days. I've seen solutions for this in Go and Java, but I'm not sure what the best Python options are. I'm specifically interested in runtime line coverage, rather than only request tracing or performance profiling.
Answers
coverage.py is the standard tool to investigate, but itโs primarily designed for test-time coverage. Running it continuously across production traffic for 30 days could add overhead and produce a lot of data, so youโd need to evaluate the performance and storage impact carefully.
Reply to this answer
If you want production observability rather than strict coverage data, Pydantic Logfire is worth considering. It integrates nicely with Python and FastAPI applications and can help reveal what your services are actually doing in production.
Reply to this answer
OpenTelemetry has FastAPI instrumentation and is a good foundation for tracing requests through your services. It wonโt directly provide line-by-line code coverage, but it can show which endpoints and execution paths production traffic is using without tying you to one observability vendor.
Reply to this answer
Grafana Pyroscope may be useful if profiling is close enough to what you need. It collects runtime profiles from Python and can show which parts of the application consume execution time. Keep in mind that profiling is different from exact line coverage: it tells you what was running and how often, not necessarily every line that executed.
Reply to this answer
Add your answer
Answers are reviewed before publication.
AmberKite28: Iโve had a good experience with Logfire as well. It was straightforward to integrate and gave us useful visibility into the applicationโs runtime behavior.