Python

Python SDK

Installation

pip install sentry-sdk

Setup

import sentry_sdk

sentry_sdk.init(
    dsn="https://your-key@your-hostname.tindra.sh/1",
    environment="production",
    release="1.4.2",
    traces_sample_rate=0.1,
)

Framework integrations

The SDK includes integrations for common Python frameworks. Pass them in integrations=[].

Django

pip install sentry-sdk[django]
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration

sentry_sdk.init(
    dsn="...",
    integrations=[DjangoIntegration()],
    traces_sample_rate=0.1,
    send_default_pii=True,
)

Flask

pip install sentry-sdk[flask]
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration

sentry_sdk.init(
    dsn="...",
    integrations=[FlaskIntegration()],
    traces_sample_rate=0.1,
)

FastAPI

pip install sentry-sdk[fastapi]
import sentry_sdk
from sentry_sdk.integrations.fastapi import FastApiIntegration
from sentry_sdk.integrations.starlette import StarletteIntegration

sentry_sdk.init(
    dsn="...",
    integrations=[StarletteIntegration(), FastApiIntegration()],
    traces_sample_rate=0.1,
)

Manual error capturing

try:
    process_order(order)
except Exception as e:
    sentry_sdk.capture_exception(e)
    raise

Capture a message

sentry_sdk.capture_message("User exceeded quota", level="warning")

User context

with sentry_sdk.configure_scope() as scope:
    scope.set_user({"id": user.id, "email": user.email})

Custom spans

with sentry_sdk.start_span(op="db.query", description="Fetch user orders"):
    orders = db.execute("SELECT * FROM orders WHERE user_id = %s", (user_id,))

Testing

sentry_sdk.capture_exception(Exception("Hello Tindra"))

Check your dashboard. The event should appear within a few seconds.