-
Notifications
You must be signed in to change notification settings - Fork 614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fastapi: fix wrapping of middlewares #3012
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice comment and test!
@adriangb Please take a look at the failing tests |
# are handled. | ||
# This should not happen unless there is a bug in OpenTelemetryMiddleware, but if there is we don't want that | ||
# to impact the user's application just because we wrapped the middlewares in this order. | ||
stack = ServerErrorMiddleware(stack) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to clarify, is the purpose of this pr to handle exceptions generated from the OpenTelemetryMiddleware
itself (if there is a bug) or unhandled exceptions thrown from the request?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep! If there is a bug in OpenTelemetryMiddleware
without this double wrapping then the server might not send a 500 to the client and instead abruptly disconnect. In practice I believe every ASGI server I know of does also have it's own handling of unhandled exceptions such that this would not be the case, but I'm not sure we want to rely on that + there would still be a noticeable change (different response content at least, maybe different headers, etc.)
fixes #795 |
e406be8
to
3d8dd4f
Compare
@@ -170,7 +170,8 @@ def setUp(self): | |||
self._instrumentor = otel_fastapi.FastAPIInstrumentor() | |||
self._app = self._create_app() | |||
self._app.add_middleware(HTTPSRedirectMiddleware) | |||
self._client = TestClient(self._app) | |||
self._client = TestClient(self._app, base_url="https://testserver:443") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was the root cause of tests failures. It turns out every request was being made twice because it was getting redirected to https. Before this PR that wasn't being instrumented correctly, so this was not being caught! I think that's another major bug this PR is fixing.
@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... 👀 |
app = ServerErrorMiddleware(app) | ||
return app | ||
|
||
app._original_build_middleware_stack = app.build_middleware_stack |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: do you think it would be possible to use wrapt for monkeypatching instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather not. This is simpler and works just fine.
instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py
Outdated
Show resolved
Hide resolved
Co-authored-by: Riccardo Magliocchetti <[email protected]>
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. |
MRE:
With this change it shows up properly
Run this and you'll see that there is no
"http.status_code": 500
in the logs.