observability_traces.user_email is always NULL: ObservabilityMiddleware registered after AuthContextMiddleware runs before auth
Summary
On v1.0.6, observability_traces.user_email is NULL for 100% of rows (verified on a production deployment: 96,101 traces, 0 with user_email), regardless of auth mode (email auth, JWT bearer, virtual servers).
Root cause
The only writer of user_email is ObservabilityService.start_trace() (mcpgateway/services/observability_service.py:346), called from ObservabilityMiddleware, which reads it from request.state.user (mcpgateway/middleware/observability_middleware.py:137-138).
request.state.user is set by AuthContextMiddleware (mcpgateway/middleware/auth_middleware.py:188). In mcpgateway/main.py the registration order is:
- line ~3406:
AuthContextMiddleware - line ~3451:
ObservabilityMiddleware
Starlette's add_middleware does self.user_middleware.insert(0, ...), so the later-registered middleware is outermost and runs first. The effective chain is Observability → ... → AuthContext → handler, meaning start_trace() always runs before any authentication — request.state.user cannot exist yet. The comment at main.py:3441-3443 ("Execution order will be: AuthContext -> Observability -> Request Handler") states the opposite of the actual behavior.
end_trace() (observability_service.py:364-420) never writes user_email back, so there is no backfill path.
Note: OTEL_CAPTURE_IDENTITY_ATTRIBUTES only affects the OTLP span path (mcpgateway/observability.py:1297-1330 via ContextVars in utils/trace_context.py); it never feeds the observability_traces table.
Suggested fix
- Register
ObservabilityMiddlewarebeforeAuthContextMiddlewareinmain.py(so Observability ends up inside auth), or setuser_emailatend_trace()time. - In
observability_middleware.py:137-138, also fall back torequest.state.user_email, which is what the JWT/API-token paths set (auth.py:1956,auth_context.py:858) — the current code only handles an object-shapedrequest.state.user.email.
Environment
- mcp-context-forge v1.0.6 (
ghcr.io/ibm/mcp-context-forge:v1.0.6), Postgres backend AUTH_REQUIRED=true,EMAIL_AUTH_ENABLED=true,MCPGATEWAY_ADMIN_API_ENABLED=true,OBSERVABILITY_ENABLED=true,OTEL_CAPTURE_IDENTITY_ATTRIBUTES=true
Happy to provide more detail. As a workaround we run a small custom plugin on tool_pre_invoke that UPDATEs the running trace row once the verified identity is available.
Confirmed the diagnosis locally — this is a middleware-registration-order bug, not a store/schema problem:
Starlette.add_middlewaredoesuser_middleware.insert(0, ...), so the later-registeredObservabilityMiddlewareends up outermost and runs beforeAuthContextMiddleware. The comment atmain.py(~line 3441) states the opposite of the actual execution order.start_trace()therefore readsrequest.state.userbefore any authentication has run, andend_trace()has no backfill, souser_emailstays NULL for every trace.
I'd like to take this with the following approach:
- Swap the two
add_middlewareregistrations inmain.pysoObservabilityMiddlewareis registered first (ends up innermost, i.e. runs after auth), and correct the ordering comment to describe the real semantics ofadd_middleware(last registered = outermost) so this doesn't regress. - In
observability_middleware.py, read the identity defensively: preferrequest.state.user(object with.email) and fall back torequest.state.user_email, which is what the JWT and API-token paths actually set. - Add a regression test that pins the effective execution order (auth-before-observability) and asserts
user_emaillands on the trace for both identity shapes.
Will send a PR signed with DCO shortly.