Skip to content

feat: CorrelationIdMiddleware with ContextVar - #8

Open
HenriBlacksmith wants to merge 5 commits into
mainfrom
feat/correlation-id-middleware
Open

HenriBlacksmith wants to merge 5 commits into
mainfrom
feat/correlation-id-middleware

Conversation

@HenriBlacksmith

Copy link
Copy Markdown
Owner

Summary

  • Add pure ASGI CorrelationIdMiddleware:
    • reads X-Correlation-ID from the request headers, or generates a uuid4 when missing;
    • stores the value in a module-level ContextVar (app.api.middlewares.correlation_id.correlation_id) so any code in the request path (controllers, adapters, log filters) can read it with correlation_id.get();
    • echoes the value back in the X-Correlation-ID response header;
    • resets the ContextVar token in a finally block and leaves non-HTTP scopes (lifespan, websockets) untouched.
  • Register the middleware in configure logic of app/main.py.
  • Tests: client-provided id is echoed, missing id is generated as a UUIDv4 on /api/v1/health (public route).

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.

1 participant