fastapi icon indicating copy to clipboard operation
fastapi copied to clipboard

Sentry ASG integration not working for FastAPI

Open raminsj13 opened this issue 2 years ago • 3 comments

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: https://github.com/tiangolo/fastapi/pull/4603 https://github.com/getsentry/sentry-python/pull/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

raminsj13 avatar Mar 29 '22 02:03 raminsj13

try this one https://github.com/encode/sentry-asgi

slavugan avatar Apr 19 '22 21:04 slavugan

same issue here T_T

daun-io avatar Jun 16 '22 11:06 daun-io

Sentry released this: https://docs.sentry.io/platforms/python/guides/fastapi/

Kludex avatar Aug 02 '22 10:08 Kludex