You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(middleware): Enhance SQL profiling with request tracing
This enhances the `SQLProfilingMiddleware` to provide deeper observability by integrating it with a new request tracing context system.
It introduces `TraceContextMiddleware`, which establishes a unique `trace_id` for each request using `contextvars`. This trace ID is then leveraged by the `SQLProfilingMiddleware` to inject a detailed comment, including the trace ID, route, and origin, into every SQL query. This allows for direct correlation between a web request and its database activity.
Key changes include:
- A new `TraceContextMiddleware` that sets and safely resets the request context.
- The `SQLProfilingMiddleware` is updated to inject contextual SQL comments.
- A new, isolated test suite for the `TraceContextMiddleware` to ensure it correctly handles the `X-Request-ID` header and prevents context bleeding between requests.
- Existing tests were made more robust, and a state pollution issue was fixed by ensuring proper context cleanup in the test suite.
To use it, add both the `TraceContextMiddleware` and the `SQLProfilingMiddleware` to your `MIDDLEWARE` list in your Django settings. The `TraceContextMiddleware` should come before the `SQLProfilingMiddleware`.
0 commit comments