FastAPI | Sentry for Python (original) (raw)

Learn about using Sentry with FastAPI.

The FastAPI integration adds support for the FastAPI Framework.

Install sentry-sdk from PyPI with the fastapi extra:

Copied

pip install "sentry-sdk[fastapi]"

If you have the fastapi package in your dependencies, the FastAPI integration will be enabled automatically when you initialize the Sentry SDK.

Error MonitoringTracingProfiling

Copied

import sentry_sdk

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
    # Add data like request headers and IP for users, if applicable;
    # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
    send_default_pii=True,
    #  performance
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    traces_sample_rate=1.0,
    #  performance
    #  profiling
    # To collect profiles for all profile sessions,
    # set `profile_session_sample_rate` to 1.0.
    profile_session_sample_rate=1.0,
    # Profiles will be automatically collected while
    # there is an active span.
    profile_lifecycle="trace",
    #  profiling
)

Copied

from fastapi import FastAPI

sentry_sdk.init(...)  # same as above

app = FastAPI()

@app.get("/sentry-debug")
async def trigger_error():
    division_by_zero = 1 / 0

When you point your browser to http://localhost:8000/sentry-debug a transaction will be created in the Performance section of sentry.io. Additionally, an error event will be sent to sentry.io and will be connected to the transaction.

It takes a couple of moments for the data to appear in sentry.io.

The following information about your FastAPI project will be available to you on Sentry.io:

Issues List

The following parts of your FastAPI project are monitored:

Performance details are shown as waterfall diagram

By adding FastApiIntegration to your sentry_sdk.init() call explicitly, you can set options for FastApiIntegration to change its behavior. Because FastAPI is based on the Starlette framework, both integrations, StarletteIntegration and FastApiIntegration, must be instantiated.

Copied

from sentry_sdk.integrations.starlette import StarletteIntegration
from sentry_sdk.integrations.fastapi import FastApiIntegration

sentry_sdk.init(
    # same as above
    integrations=[
        StarletteIntegration(
            transaction_style="endpoint",
            failed_request_status_codes={403, *range(500, 599)},
            http_methods_to_capture=("GET",),
        ),
        FastApiIntegration(
            transaction_style="endpoint",
            failed_request_status_codes={403, *range(500, 599)},
            http_methods_to_capture=("GET",),
        ),
    ]
)

You can pass the following keyword arguments to StarletteIntegration() and FastApiIntegration():

import sentry_sdk  
from sentry_sdk.integrations.starlette import StarletteIntegration  
from sentry_sdk.integrations.fastapi import FastApiIntegration  
sentry_sdk.init(  
    # ...  
    integrations=[  
        StarletteIntegration(  
            transaction_style="endpoint",  
        ),  
        FastApiIntegration(  
            transaction_style="endpoint",  
        ),  
    ],  
)  
app = FastAPI()  
@app.get("/catalog/product/{product_id}")  
async def product_detail(product_id):  
    return {...}  

In the above code, the transaction name will be:

Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").