-
-
Save encryptblockr/f87b7e6b50f0db0db1b248e8f3a86d2e to your computer and use it in GitHub Desktop.
Integrate Sentry to FastAPI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from fastapi import FastAPI | |
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware | |
import sentry_sdk | |
sentry_sdk.init( | |
dsn='your Sentry dns', # CHANGE HERE | |
environment=os.getenv('ENV', 'dev'), # You should read it from environment variable | |
) | |
app = FastAPI( | |
title='My FastAPI App', | |
description='Demo Sentry Integration', | |
version='1.0.0', | |
) | |
try: | |
app.add_middleware(SentryAsgiMiddleware) | |
except Exception: | |
# pass silently if the Sentry integration failed | |
pass | |
@app.get('/') | |
async def root(): | |
return {'message': 'success!'} | |
# Calling this endpoint to see if the setup works. If yes, an error message will show in Sentry dashboard | |
@app.get('/sentry') | |
async def sentry(): | |
raise Exception('Test sentry integration') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
SentryASGIMiddleware
is not needed anymore, please check https://docs.sentry.io/platforms/python/guides/fastapi/.