Skip to content

Instantly share code, notes, and snippets.

@nguqtruong
Created April 8, 2021 16:21
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save nguqtruong/8cce344577df2b392008d15fc92a1ff6 to your computer and use it in GitHub Desktop.
Save nguqtruong/8cce344577df2b392008d15fc92a1ff6 to your computer and use it in GitHub Desktop.
Integrate Sentry to FastAPI
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')
@VianneyMI
Copy link

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:

  • First Version : Handled / Mechanism : Logging
  • Second Version : Non Handled / Mechanism : ASGI

Do you have any idea how to solve this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment