fastapi: fix wrapping of middlewares#3012
Conversation
codefromthecrypt
left a comment
There was a problem hiding this comment.
nice comment and test!
|
@adriangb Please take a look at the failing tests |
|
fixes #795 |
e406be8 to
3d8dd4f
Compare
@xrmx see #3012 (comment) |
|
@Kludex could you update the Starlette instrumentations after we finish up this PR? I'm assuming they have the same bugs. |
|
The pipeline has been running for some hours... 👀 |
|
Hello! Just wondering if you'd expect this to enable getting the This has been discussed in more detail here: open-telemetry/opentelemetry-python#3477, but currently OpenTelemetryMiddleware's context seems to be removed by the time an exception gets to the Would be great to enable users to report back a [Update]: Have now verified this achieves the above behaviour - this fix is greatly appreciated! |
|
Fixed issue with test shutdown hanging. All checks passing now. |
xrmx
left a comment
There was a problem hiding this comment.
I trust you on the fastapi knowledge but would be nice to add a test to check that the change does what it's supposed to fix
|
Ups looks like that got lost, I had added it in 4220b09 |
7497d32 to
f825c76
Compare
|
Added back and updated the branch |
|
ICYMI the added test is red |
|
Thanks. I hadn't understood how the tests are parametrized and that |
|
Anything else in the way of merging now? |
Signed-off-by: emdneto <[email protected]>
Signed-off-by: emdneto <[email protected]>
|
I gave it a shot, lets see what CI says |
|
@adriangb we merged a PR stopping using SpanAttributes in favor of semconv._incubating.attributes.* |
|
Ok sounds like this 2 year old PR now needs another round of adjusting for upstream changes 😢. Any chance you can make the change here since you're familiar with the implications? |
Signed-off-by: emdneto <[email protected]>
|
Does @codefromthecrypt need to approve? |
|
@alexmojaki github was saying so but your approval was good enough 😅 |
|
Thanks everyone for getting this across! |
|
Amazing folks thanks so much!! |
|
Finally! Thanks to everyone involved, especially @adriangb! |
| if isinstance( | ||
| inner_server_error_middleware, ServerErrorMiddleware | ||
| ): # usually true | ||
| outer_server_error_middleware = ServerErrorMiddleware( | ||
| app=otel_middleware, | ||
| ) | ||
| else: | ||
| # Something else seems to have patched things, or maybe Starlette changed. | ||
| # Just create a default ServerErrorMiddleware. | ||
| outer_server_error_middleware = ServerErrorMiddleware( | ||
| app=otel_middleware | ||
| ) |
There was a problem hiding this comment.
these branches are the same now
There was a problem hiding this comment.
I feel stupid for not realizing this when I changed the code
* fastapi: fix wrapping of middlewares * fix import, super * add test * changelog * lint * lint * fix * ci * fix wip * fix * fix * lint * lint * Exit? * Update test_fastapi_instrumentation.py Co-authored-by: Riccardo Magliocchetti <[email protected]> * remove break * fix * remove dunders * add test * lint * add endpoint to class * fmt * pr feedback * move type ignores * fix sphinx? * Update CHANGELOG.md * update fastapi versions * fix? * generate * stop passing on user-supplied error handler This prevents potential side effects, such as logging, to be executed more than once per request handler exception. * fix ci Signed-off-by: emdneto <[email protected]> * fix ruff Signed-off-by: emdneto <[email protected]> * remove unused funcs Co-authored-by: Emídio Neto <[email protected]> * fix lint,ruff Signed-off-by: emdneto <[email protected]> * fix changelog Signed-off-by: emdneto <[email protected]> * add changelog note Signed-off-by: emdneto <[email protected]> * fix conflicts with main Signed-off-by: emdneto <[email protected]> --------- Signed-off-by: emdneto <[email protected]> Co-authored-by: Riccardo Magliocchetti <[email protected]> Co-authored-by: Alexander Dorn <[email protected]> Co-authored-by: Emídio Neto <[email protected]>
MRE:
With this change it shows up properly
Run this and you'll see that there is no
"http.status_code": 500in the logs.