The FastAPI (and Starlette) integrations should set the http status of the current span to the http status of the request. Nice information to have!
class SentrySpanStatusMiddleware:
"""
Currently Sentry doesn't automatically detect the span status, so manually set it here
Ref: https://github.com/getsentry/sentry-python/blob/master/sentry_sdk/integrations/asgi.py#L147-L149
This middleware has to be added *before* CustomSentryMiddleware so that it's called
*inside* of it and the current span is available
"""
def __init__(
self,
app: ASGIApp,
) -> None:
self.app = app
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
if scope["type"] != "http":
await self.app(scope, receive, send)
return
request = Request(scope, receive, send)
sentry_trace = request.headers.get("custom-sentry-trace")
if sentry_trace is not None:
with sentry_sdk.configure_scope() as sentry_scope:
sentry_scope.set_tag("trace_id", sentry_trace)
async def send_wrapper(message: Message) -> None:
if message["type"] == "http.response.start":
span = sentry_sdk.Hub.current.scope.span
if span:
span.set_http_status(message["status"])
await send(message)
return await self.app(scope, receive, send_wrapper)
Problem Statement
The FastAPI (and Starlette) integrations should set the http status of the current span to the http status of the request. Nice information to have!
Solution Brainstorm
How @tiangolo does this: