fastapi
fastapi copied to clipboard
Sentry ASG integration not working for FastAPI
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
try this one https://github.com/encode/sentry-asgi
same issue here T_T
Sentry released this: https://docs.sentry.io/platforms/python/guides/fastapi/