Skip to content
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

Set http status of spans in FastAPI integration #1588

Closed
antonpirker opened this issue Aug 30, 2022 · 1 comment
Closed

Set http status of spans in FastAPI integration #1588

antonpirker opened this issue Aug 30, 2022 · 1 comment

Comments

@antonpirker
Copy link
Member

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:


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)
@antonpirker
Copy link
Member Author

Closing. This was fixed here: #2312

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants