Created
April 8, 2021 16:21
-
-
Save nguqtruong/8cce344577df2b392008d15fc92a1ff6 to your computer and use it in GitHub Desktop.
Integrate Sentry to FastAPI
This file contains hidden or 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
Hello,
Thanks for this snapshot,
I have an issue while integrating sentry to FastAPI. All the errors are being duplicated. They appear each twice on the dashboard with minor differences:
Do you have any idea how to solve this ?