Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Sentry ASG integration not working for FastAPI #4735

@raminsj13

Description

@raminsj13
import sentry_sdk
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
from core.app import app
sentry_sdk.init(dsn="https://xxx.ingest.sentry.io/xxxx")
asgi_app = SentryAsgiMiddleware(app)
raise ValueError()

and

app = FastAPI()
class CustomSentryAsgiMiddleware(SentryAsgiMiddleware):
    def event_processor(
        self, event: "Event", hint: "Hint", asgi_scope: Any
    ) -> "Optional[Event]":
        result_event = super().event_processor(event, hint, asgi_scope)
        route: Optional[APIRoute] = asgi_scope.get("route")
        if route and result_event:
            result_event["transaction"] = route.path
        return result_event

app.add_middleware(CustomSentryAsgiMiddleware)
raise ValueError()

Description

Unable to make Sentry starter code send exceptions (not HTTPExceptions) from FasAPI starter template.

I followed instructions:
here: https://docs.sentry.io/platforms/python/guides/asgi/

here: https://fastapi.tiangolo.com/advanced/middleware/#httpsredirectmiddleware

And traced related issues & latest contribution to Sentry's github:
#4603
getsentry/sentry-python#1349

I can confirm that I was able to push errors using basic sentry as described here but this was a workaround from a while back: https://philstories.medium.com/integrate-sentry-to-fastapi-7250603c070f

Any suggestions are appreciated.

macOS
fastapi==0.75.0
Python 3.9.10
sentry-sdk==1.5.8

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions