mcp-context-forge icon indicating copy to clipboard operation
mcp-context-forge copied to clipboard

observability_traces.user_email is always NULL: ObservabilityMiddleware registered after AuthContextMiddleware runs before auth

Open JanvanHout opened this issue 3 months ago • 1 comments

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

  1. Register ObservabilityMiddleware before AuthContextMiddleware in main.py (so Observability ends up inside auth), or set user_email at end_trace() time.
  2. In observability_middleware.py:137-138, also fall back to request.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-shaped request.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.

JanvanHout avatar Aug 29 '26 12:08 JanvanHout

Confirmed the diagnosis locally — this is a middleware-registration-order bug, not a store/schema problem:

  • Starlette.add_middleware does user_middleware.insert(0, ...), so the later-registered ObservabilityMiddleware ends up outermost and runs before AuthContextMiddleware. The comment at main.py (~line 3441) states the opposite of the actual execution order.
  • start_trace() therefore reads request.state.user before any authentication has run, and end_trace() has no backfill, so user_email stays NULL for every trace.

I'd like to take this with the following approach:

  1. Swap the two add_middleware registrations in main.py so ObservabilityMiddleware is registered first (ends up innermost, i.e. runs after auth), and correct the ordering comment to describe the real semantics of add_middleware (last registered = outermost) so this doesn't regress.
  2. In observability_middleware.py, read the identity defensively: prefer request.state.user (object with .email) and fall back to request.state.user_email, which is what the JWT and API-token paths actually set.
  3. Add a regression test that pins the effective execution order (auth-before-observability) and asserts user_email lands on the trace for both identity shapes.

Will send a PR signed with DCO shortly.

0717lee avatar Aug 31 '26 00:08 0717lee