FastAPI: emit structured RequestHandled event per HTTP request - #202
Merged
Conversation
Adds RequestLifecycleMiddleware to the FastAPI integration, registered automatically by FastAPIProvider. For every request it fires a RequestHandled event (method, path, status_code, duration_ms, request_id) through the existing Event dispatcher/facade — no new event mechanism, no storage, no UI. A request id is generated per request (or echoed back if the client already sent one) and returned via the X-Request-Id header.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
A bare (non-HTTPException) exception is handled by Starlette's ServerErrorMiddleware, which sits outside RequestLifecycleMiddleware — so the middleware's local response stays None on that path and the header never gets set there. HTTPException stays inside the middleware via ExceptionMiddleware and was already fine. Stamp the header from the shared exception handler instead (covers Exception, HTTPException, and RequestValidationError alike), using the request_id RequestLifecycleMiddleware already stashed on request.state before the route ran. This keeps the RequestHandled event's request_id correlated with the response the client actually receives, including on the 500 path.
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.
Summary
Implements task #1324 — Telescope-style visibility into FastAPI requests, scoped down to structured events only (no storage, no UI, no other watchers).
RequestLifecycleMiddleware(fastapi_startkit/fastapi/middleware.py), registered automatically byFastAPIProvider.boot().RequestHandledevent (fastapi_startkit/fastapi/events.py) through the existing Event dispatcher/facade from task #940 — no parallel event mechanism was built.method,path,status_code,duration_ms,request_id.uuid4), or echoed back if the client already sent anX-Request-Idheader. The id is also returned on the response viaX-Request-Id.Event.listen(RequestHandled, ...).try/finallyso the event still fires (withstatus_code=500) even if an unhandled exception escapes the router.Scope confirmation / overlap check
on_event("startup"/"shutdown")/lifespan hooks (Application.add_event_handler), not per-request events. No overlap.RequestHandledevent via the Event dispatcher. Worth closing/merging #1262 into this PR's context.Out of scope (left as extension points, not implemented)
Test plan
tests/fastapi/test_request_lifecycle_events.py: boots a realApplication+FastAPIProvider, hits real routes viaTestClient, and asserts the event payload, theX-Request-Idheader (generated + echoed), error-status propagation, andEvent.fake()interception.uv run pytest --ignore=tests/masoniteorm/postgres→ 2046 passed, 7 skipped.ruff check/ruff format --checkclean.