-
Notifications
You must be signed in to change notification settings - Fork 849
fix(fastapi): Fix FastAPI Instrumentation Compatibility with Middleware-Wrapped Apps #4041
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -265,6 +265,8 @@ def instrument_app( | |
| http_capture_headers_sanitize_fields: Optional list of HTTP headers to sanitize. | ||
| exclude_spans: Optionally exclude HTTP `send` and/or `receive` spans from the trace. | ||
| """ | ||
| # unwraps any middleware to get to the FastAPI or Starlette app | ||
| app = _unwrap_middleware(app) | ||
| if not hasattr(app, "_is_instrumented_by_opentelemetry"): | ||
| app._is_instrumented_by_opentelemetry = False | ||
|
|
||
|
|
@@ -391,6 +393,12 @@ async def __call__( | |
| app=otel_middleware, | ||
| ) | ||
|
|
||
| # add check if the app object has build_middleware_stack method | ||
| if not hasattr(app, "build_middleware_stack"): | ||
| _logger.error( | ||
| "Skipping FastAPI instrumentation due to missing build_middleware_stack method on app object." | ||
| ) | ||
| return | ||
| app._original_build_middleware_stack = app.build_middleware_stack | ||
| app.build_middleware_stack = types.MethodType( | ||
| functools.wraps(app.build_middleware_stack)( | ||
|
|
@@ -409,6 +417,9 @@ async def __call__( | |
|
|
||
| @staticmethod | ||
| def uninstrument_app(app: fastapi.FastAPI): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here and in |
||
| # Unwraps any middleware to get to the FastAPI or Starlette app | ||
| app = _unwrap_middleware(app) | ||
|
|
||
| original_build_middleware_stack = getattr( | ||
| app, "_original_build_middleware_stack", None | ||
| ) | ||
|
|
@@ -514,3 +525,17 @@ def _get_default_span_details(scope): | |
| else: # fallback | ||
| span_name = method | ||
| return span_name, attributes | ||
|
|
||
|
|
||
| def _unwrap_middleware(app): | ||
| """ | ||
| Unwraps the middleware stack to find the underlying FastAPI or Starlette app. | ||
|
|
||
| Args: | ||
| app: The ASGI application potentially wrapped in middleware. | ||
| Returns: | ||
| The unwrapped FastAPI or Starlette application. | ||
| """ | ||
| while hasattr(app, "app"): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we break once app is |
||
| app = app.app | ||
| return app | ||
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 should go under
Fixed