Skip to content

Instantly share code, notes, and snippets.

@encryptblockr
Forked from nguqtruong/fastapi_app.py
Created May 14, 2021 21:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save encryptblockr/f87b7e6b50f0db0db1b248e8f3a86d2e to your computer and use it in GitHub Desktop.
Save encryptblockr/f87b7e6b50f0db0db1b248e8f3a86d2e 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')
@Kludex
Copy link

Kludex commented Mar 24, 2023

The SentryASGIMiddleware is not needed anymore, please check https://docs.sentry.io/platforms/python/guides/fastapi/.

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