feat: carry resolved identity into request_completed logs - #267
Conversation
Auth stashes user_id, auth_method, key_id, key_prefix, and app_id on request.state; the logging middleware merges it into request_completed. Contextvars bound in the handler task don't propagate back to the middleware dispatch context, so the main request log had no identity.
|
Warning Review limit reached
Next review available in: 33 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Zingzy
left a comment
There was a problem hiding this comment.
Mergeable, nothing blocking, and the mechanism is right.
- The task-boundary claim checks out: BaseHTTPMiddleware runs downstream in a child task, contextvars copied at spawn never propagate back, and
request.staterides the shared ASGI scope.request.state.tenantfrom the tenant middleware is the existing precedent, and thegetattr(..., None)read matches how the redirect routes consume that one. - The
**auth_ctxmerge cannot collide with the explicit kwargs ofrequest_completed, and the keys come from a fixed internal set, never from anything user-controlled. key_prefix/key_idinrequest_completedsurvive redaction via the #265 safe list, same coupling as #266.- The comments here are the kind worth having: they state a constraint the code cannot show (why
request.stateinstead of contextvars). Keep them.
Middleware and auth test files pass at this head (118).
# Conflicts: # tests/unit/middleware/test_logging.py
Stacked on #266 (which stacks on #265).
What
The auth dependency stashes the resolved identity (
user_id,auth_method, and for API keyskey_id/key_prefix, for app tokensapp_id) onrequest.state; the request logging middleware merges it into therequest_completedevent.Why
request_completedcurrently carries no identity at all. The auth dependency binds structlog contextvars, butBaseHTTPMiddlewareruns the downstream handler in a child task, so those bindings never propagate back to the middleware's dispatch context whererequest_completedis emitted. Service events get identity, the request log does not.request.staterides the shared ASGI scope, so it crosses that boundary.Performance and security
No extra queries or allocations beyond a small dict on authenticated requests. Fields match exactly what service-layer logs already carry; nothing new is exposed. Anonymous requests and requests rejected before auth runs (rate-limited 429s) are unchanged.
Unit tests cover both the authenticated and anonymous shapes of the event.