Repository navigation
feat(observability): Bind request context and record routes for telemetry - #299
Conversation
Add bind_request_context, which describes the current request for every telemetry signal: structlog events pick the attributes up through merge_contextvars, and the current OpenTelemetry span gets them as span attributes. get_request_context reads them back without depending on tracing. RequestContextMiddleware handles each request in a copy of the current context, so whatever gets bound while handling it doesn't leak into the next request served by the same thread, and binds the request's OpenAPI route and method once the URL has been resolved.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #299 +/- ##
==========================================
+ Coverage 97.67% 97.72% +0.04%
==========================================
Files 112 114 +2
Lines 4997 5096 +99
==========================================
+ Hits 4881 4980 +99
Misses 116 116 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Compute the OpenAPI route once, in RequestContextMiddleware.process_view, and pass it to every consumer: the request context, the current span's name and http.route attribute, and Gunicorn's access log and HTTP metrics through the WSGI environ, which is all that's left when Gunicorn logs the request. RouteLoggerMiddleware becomes a deprecated alias, so services listing it get the new middleware in the same place.
b9dfc66 to
1ad440b
Compare
…tMiddleware Build RequestContextMiddleware on MiddlewareMixin, so it runs natively in async (ASGI) chains as well as sync ones. A coroutine runs in the context of whatever awaits it, so copy_context().run() can't isolate an async request: handle it in a task with its own copy of the context instead.
There was a problem hiding this comment.
All reported issues were addressed across 8 files
Reply with feedback, questions, or to request a fix.
Turn on auto-fix | Re-trigger cubic
…WARE Request context only exists while the middleware handles the request, so middleware listed before it can neither bind nor read any. Spell that out in the docstring and README, along with the process_view ordering the route needs.
There was a problem hiding this comment.
0 issues found across 2 files (changes from recent commits).
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Requires human review: Auto-approval blocked by 1 unresolved issue from previous reviews.
Turn on auto-fix | Re-trigger cubic
There was a problem hiding this comment.
No issues found across 8 files
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Auto-approved: Adds request-scoped telemetry context middleware and bind/get APIs, deprecating RouteLoggerMiddleware and updating docs/dev settings. Bounded to observability, test-covered; the dismissed context-lifetime concern is intentionally documented.
Turn on auto-fix | Re-trigger cubic
Contributes to ENG-611
In this PR, we add
RequestContextMiddleware, superseding and deprecatingRouteLoggerMiddleware, andbind_request_context/get_request_contextAPIs which abstract structlog's context.bind_request_contextwill be used to add domain-specific attributes to all structured logs emitted during Django request processing.