Repository navigation
Conversation
0717lee
requested review from
Lang-Akshay,
brian-hussey,
crivetimihai,
ja8zyjits and
msureshkumar88
as code owners
August 31, 2026 00:55
0717lee
force-pushed
the
fix/observability-user-email-middleware-order
branch
2 times, most recently
from
September 12, 2026 00:19
f58ca0f to
2db1231
Compare
…ser_email is captured ObservabilityMiddleware was registered after AuthContextMiddleware, and Starlette's add_middleware() inserts at position 0, so the later-registered middleware is outermost and executes first. start_trace() therefore ran before any authentication and read request.state.user before AuthContextMiddleware had set it, leaving observability_traces.user_email NULL on every row (IBM#6473). - Register ObservabilityMiddleware before AuthContextMiddleware so it ends up inside auth (auth runs first), and replace the ordering comment that described the opposite of the actual execution semantics. - Read the identity defensively in the middleware: prefer request.state.user.email, falling back to request.state.user_email, which is what the JWT and API-token paths set. - Regression tests: a registration-order test pinning auth before observability on the assembled app, and dispatch tests covering both identity shapes reaching start_trace(user_email=...). Signed-off-by: Li Fengmin <2080291162@qq.com>
0717lee
force-pushed
the
fix/observability-user-email-middleware-order
branch
from
October 24, 2026 04:55
2db1231 to
5b3625d
Compare
Author
|
Replayed onto the rewritten Re-verified locally: the registration-order regression test fails without the change and passes with it; the observability middleware suite (17 tests) passes; ruff is clean on the touched files. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #6473
Problem
observability_traces.user_emailwas NULL on 100% of rows regardless of auth mode.ObservabilityMiddlewarewas registered afterAuthContextMiddlewareinmain.py, and Starlette'sadd_middleware()inserts at position 0 — the later-registered middleware is outermost and executes first. Sostart_trace()ran before any authentication had setrequest.state.user, andend_trace()has no backfill. The comment inmain.pyclaimed the opposite execution order, which is what let this slip through.Fix
ObservabilityMiddlewarebeforeAuthContextMiddlewareso it ends up inside auth:AuthContextMiddlewareexecutes first and populatesrequest.state.user/request.state.user_email, thenstart_trace()can read the identity. The ordering comment now describes the realadd_middlewaresemantics (last registered = outermost = first) so the order doesn't silently regress.observability_middleware.py, read the identity defensively: preferrequest.state.user.email, falling back torequest.state.user_email, which is what the JWT and API-token paths set.AsyncMockimport in the touched test file.Tests
test_main_observability_order.py: pins on the assembled app thatAuthContextMiddlewaresits closer to the front ofuser_middlewarethanObservabilityMiddleware(i.e. executes first).test_observability_middleware.py:request.state.user.emailand therequest.state.user_emailfallback both reachstart_trace(user_email=...).pytest tests/unit/mcpgateway/middleware/test_observability_middleware.py tests/unit/mcpgateway/test_main_observability_order.py— 17 passed; relatedtest_main*.py/ observability-migration suites (444 tests) pass;ruff check/ruff formatclean.