feat: add per-request DB query tracing with ContextVar#476
Open
ahmedelgyar1 wants to merge 1 commit intoNetflix:masterfrom
Open
feat: add per-request DB query tracing with ContextVar#476ahmedelgyar1 wants to merge 1 commit intoNetflix:masterfrom
ahmedelgyar1 wants to merge 1 commit intoNetflix:masterfrom
Conversation
- Introduce query tracing using ContextVar for per-request isolation - Track query count, total rows, and execution time - Add SQL truncation (200 chars) to avoid excessive logs - Support DEBUG-level logging for individual queries - Ensure feature is opt-in and no-op when disabled - Add unit tests for enabled/disabled behavior and edge cases
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Added opt-in query tracing to the metadata service. When
QUERY_TRACING_ENABLED=1is set, it logs a single summary line per HTTP request showing the total DB queries, rows returned, and execution time.Context
Fixes #5
Right now, we can't tell how many database queries are triggered by a single API request or how long they take. This makes it hard to debug performance issues, especially when a single client operation fans out into many HTTP requests. This PR adds server-side observability to measure the exact DB cost of any request.
Screenshots
Changes Made
aiohttpmiddleware.contextvars.ContextVarto keep track of queries per request. This prevents logs from getting interleaved when there are many concurrent requests.QUERY_TRACING_ENABLEDenv variable (defaults to 0 / off).Testing
QUERY_TRACING_ENABLED=1 docker-compose upand hit/flowsand/runsendpoints using the Swagger UI to verify the logs appear correctly in stdout.