Skip to content

feat(observability): Bind request context and record routes for telemetry - #299

Merged
khvn26 merged 6 commits into
mainfrom
feat/request-context
Nov 20, 2026
Merged

khvn26 merged 6 commits into
mainfrom
feat/request-context

Conversation

@khvn26

@khvn26 khvn26 commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Contributes to ENG-611

In this PR, we add RequestContextMiddleware, superseding and deprecating RouteLoggerMiddleware, and bind_request_context / get_request_context APIs which abstract structlog's context.

bind_request_context will be used to add domain-specific attributes to all structured logs emitted during Django request processing.

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.
@linear-code

linear-code Bot commented Nov 20, 2026

Copy link
Copy Markdown

ENG-611

@codecov-commenter

codecov-commenter commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.72%. Comparing base (33c0bcb) to head (a99655b).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.
@khvn26 khvn26 changed the title feat(observability): Bind request context for telemetry feat(observability): Bind request context and record routes for telemetry Nov 20, 2026
@khvn26
khvn26 force-pushed the feat/request-context branch from b9dfc66 to 1ad440b Compare November 20, 2026 16:55
…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.
@khvn26
khvn26 marked this pull request as ready for review November 20, 2026 17:30
@khvn26
khvn26 requested a review from a team as a code owner November 20, 2026 17:30
@khvn26
khvn26 requested review from emyller and removed request for a team November 20, 2026 17:30

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 8 files

Reply with feedback, questions, or to request a fix.

Turn on auto-fix | Re-trigger cubic

Comment thread README.md Resolved
Comment thread src/common/core/request_context.py Resolved
…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.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@khvn26
khvn26 merged commit d571083 into main Nov 20, 2026
4 checks passed
@khvn26
khvn26 deleted the feat/request-context branch November 20, 2026 17:44
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.

2 participants