Hi all,
I’ve been trying to integrate Sentry into a Python app that is built with FastAPI and thus runs on Starlette/Uvicorn. The initialisation code looks something like this:
def initialize_sentry_sdk():
"""Initialize Sentry according to current configuration."""
kwargs = {
'dsn': settings.sentry_dsn,
'transport': HttpTransportWithTimeout,
'environment': settings.environment,
# 'debug': True,
}
if settings.release_id:
kwargs['release'] = settings.release_id
sentry_sdk.init(**kwargs)
app.add_event_handler('startup', initialize_sentry_sdk)
And I was quite surprised to see that it was not working. After a quite long debugging session I figured out that “hub.client” was not configured in the request handlers. And that happened because the “startup” event was handled inside a Sentry scope, and thus “sentry_sdk.init” was not applied to the “Hub.main” instance which would be inherited for each newly created request scope.
Is this intended?
I can probably initialize Sentry during loading the module instead of server startup, or I could maybe make another middleware that would load Sentry settings right before starting the app, or I could copy the settings from “Hub.current” to “Hub.main” manually, but none of these methods feel right. Is there a better way?