Rewrite FastAPI instrumentor middleware stack to be failsafe#3664
Rewrite FastAPI instrumentor middleware stack to be failsafe#3664xrmx merged 39 commits intoopen-telemetry:mainfrom
Conversation
Also improve code documentation and add another test.
Co-authored-by: Alex Hall <[email protected]>
Co-authored-by: Alex Hall <[email protected]>
alexmojaki
left a comment
There was a problem hiding this comment.
Looks solid, thank you so much!
|
Thank you for the thorough review and the persistence @alexmojaki! |
There was a problem hiding this comment.
Right. I gave it a try to run the repro and now I can see the recorded exception. Overall, it sounds good.
If it helps to others to review, before we had:
ServerErrorMiddleware (outermost) -> OpenTelemetryMiddleware -> ServerErrorMiddleware (innermost)
now we have:
ServerErrorMiddleware (outer -- same as before) -> OpenTelemetryMiddleware -> ServerErrorMiddleware (with original handler/debug) -> ExceptionHandlerMiddleware (with access to the span context)
But I'm afraid we are not seeing some issues in the current structure of tests for FastAPI. Noticed that while reviewing #3701
…lemetry#3664) * rewrite FastAPIInstrumentor:build_middleware_stack to become failsafe * add test cases for FastAPI failsafe handling * add CHANGELOG entry * remove unused import * [lint] don't return from failsafe wrapper * [lint] allow broad exceptions * [lint] more allowing * record FastAPI hook exceptions in active span * remove comment * properly deal with hooks not being set * add custom FastAPI exception recording * move failsafe hook handling down to OpenTelemetryMiddleware * shut up pylint * optimize failsafe to check for `None` only once * remove confusing comment and simplify wrapper logic * add clarifying comment * test proper exception / status code recording * add HTTP status code check * test HTTP status on the exception recording span * improve test by removing TypeError * rectify comment/explanation on inner middleware for exception handling * minor typo * move ExceptionHandlingMiddleware as the outermost inner middleware Also improve code documentation and add another test. * use distinct status code in test * improve comemnt Co-authored-by: Alex Hall <[email protected]> * narrow down exception handling Co-authored-by: Alex Hall <[email protected]> * narrow down FastAPI exception tests to relevant spans * collapse tests, more narrow exceptions * move failsafe hook tests to ASGI test suite * update CHANGELOG * satisfy linter * don't record exception if span is not recording * add test for unhappy instrumentation codepath * make inject fixtures private * give up and shut up pylint * improve instrumentation failure error message and add test --------- Co-authored-by: Alex Hall <[email protected]>
…lemetry#3664) * rewrite FastAPIInstrumentor:build_middleware_stack to become failsafe * add test cases for FastAPI failsafe handling * add CHANGELOG entry * remove unused import * [lint] don't return from failsafe wrapper * [lint] allow broad exceptions * [lint] more allowing * record FastAPI hook exceptions in active span * remove comment * properly deal with hooks not being set * add custom FastAPI exception recording * move failsafe hook handling down to OpenTelemetryMiddleware * shut up pylint * optimize failsafe to check for `None` only once * remove confusing comment and simplify wrapper logic * add clarifying comment * test proper exception / status code recording * add HTTP status code check * test HTTP status on the exception recording span * improve test by removing TypeError * rectify comment/explanation on inner middleware for exception handling * minor typo * move ExceptionHandlingMiddleware as the outermost inner middleware Also improve code documentation and add another test. * use distinct status code in test * improve comemnt Co-authored-by: Alex Hall <[email protected]> * narrow down exception handling Co-authored-by: Alex Hall <[email protected]> * narrow down FastAPI exception tests to relevant spans * collapse tests, more narrow exceptions * move failsafe hook tests to ASGI test suite * update CHANGELOG * satisfy linter * don't record exception if span is not recording * add test for unhappy instrumentation codepath * make inject fixtures private * give up and shut up pylint * improve instrumentation failure error message and add test --------- Co-authored-by: Alex Hall <[email protected]>
Description
Change the way the FastAPI instrumentor deals with the FastAPI middleware stack so that exception handling code doesn't get executed twice, but still has a valid OTEL context available. At the same time, make sure instrumentor hooks failures cannot crash the service itself.
Fixes #3642
Fixes #3637
Type of change
How Has This Been Tested?
Using the MRE in the linked issue, and added unit tests.
Does This PR Require a Core Repo Change?
Checklist:
Documentation has been updated(not needed)