Skip to content

feat: add per-request DB query tracing with ContextVar#476

Open
ahmedelgyar1 wants to merge 1 commit intoNetflix:masterfrom
ahmedelgyar1:feat/query-tracing-per-request
Open

feat: add per-request DB query tracing with ContextVar#476
ahmedelgyar1 wants to merge 1 commit intoNetflix:masterfrom
ahmedelgyar1:feat/query-tracing-per-request

Conversation

@ahmedelgyar1
Copy link
Copy Markdown

@ahmedelgyar1 ahmedelgyar1 commented Mar 27, 2026

Summary

Added opt-in query tracing to the metadata service. When QUERY_TRACING_ENABLED=1 is 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

Screenshot from 2026-03-27 10-24-35

Changes Made

  • Added a new query_tracing module with an aiohttp middleware.
  • Used contextvars.ContextVar to keep track of queries per request. This prevents logs from getting interleaved when there are many concurrent requests.
  • Added timing and row counting logic inside execute_sql().
  • Added QUERY_TRACING_ENABLED env variable (defaults to 0 / off).
  • Zero overhead when disabled: If the env var is not set, the tracing logic is completely bypassed.

Testing

  • Added unit tests in query_trace_test.py to verify:
    • Metrics are aggregated correctly per request.
    • SQL strings are truncated to 200 chars to avoid log spam.
    • Tracing context is properly cleaned up after the request finishes.
    • The feature is a complete no-op when disabled.
  • Manually tested locally via Docker: Ran QUERY_TRACING_ENABLED=1 docker-compose up and hit /flows and /runs endpoints using the Swagger UI to verify the logs appear correctly in stdout.

- 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
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.

Instrument the metadata service to trace database queries per request

1 participant