with one click
sentry-python-performance
// Configure Sentry tracing, custom spans, and distributed tracing for Python applications to monitor throughput, latency, and cross-service dependencies.
// Configure Sentry tracing, custom spans, and distributed tracing for Python applications to monitor throughput, latency, and cross-service dependencies.
[HINT] Download the complete skill directory including SKILL.md and all related files
| name | sentry-python-performance |
| description | Configure Sentry tracing, custom spans, and distributed tracing for Python applications to monitor throughput, latency, and cross-service dependencies. |
| tech_stack | ["observability"] |
| language | ["python"] |
| capability | ["observability"] |
| version | sentry-python-sdk unversioned |
| collected_at | "2025-07-21T00:00:00.000Z" |
Source: https://docs.sentry.io/platforms/python/tracing/, https://docs.sentry.io/platforms/python/tracing/instrumentation/, https://docs.sentry.io/platforms/python/tracing/distributed-tracing/
Sentry Tracing captures transactions (top-level operations like HTTP requests) and spans (sub-operations like DB queries) to measure throughput and latency. Distributed tracing extends this across service boundaries by propagating sentry-trace and baggage HTTP headers, so a single request can be followed from frontend → API → worker → database.
Enable tracing (required — no default):
import sentry_sdk
sentry_sdk.init(
dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
traces_sample_rate=1.0, # 100% — adjust for production
)
Dynamic sampling — drop health checks, sample 10% of rest:
def traces_sampler(sampling_context):
name = sampling_context["transaction_context"]["name"]
if "health" in name:
return 0.0
return 0.1
sentry_sdk.init(dsn="...", traces_sampler=traces_sampler)
Custom span around business logic:
from sentry_sdk import start_span
with start_span(op="task", description="process_order") as span:
process_order(order_id)
span.set_data("order_id", order_id)
| Option | Signature | Precedence |
|---|---|---|
traces_sample_rate | float 0.0–1.0 | Uniform rate; must be set (no default) |
traces_sampler | (sampling_context) -> float 0–1 | Overrides traces_sample_rate; return 0 to drop |
trace_propagation_targets | list[str] | Restrict which origins receive propagated headers |
The sampling_context dict provides:
transaction_context — name, operation, source of the transactionparent_sampled — whether the upstream caller was sampled (enables head-based decisions)from sentry_sdk import start_span
# Context-manager span
with start_span(op="db", description="query_users") as span:
users = db.query("SELECT * FROM users")
span.set_data("row_count", len(users))
# Decorator pattern (manual)
@sentry_sdk.trace
def process_payment(amount: float):
...
Supported frameworks get distributed tracing out of the box (SDK >1.25.x): Django, FastAPI, Flask, Bottle, Falcon, Pyramid, Quart, Starlette, Tornado.
The SDK automatically reads incoming sentry-trace and baggage headers and propagates them on outgoing HTTP requests. No additional code required.
Two headers must survive the network path:
| Header | Purpose |
|---|---|
sentry-trace | Trace ID + span ID + sampling decision |
baggage | Key-value context carried across services |
traces_sample_rate or traces_sampler is mandatory — there is no default. Forgetting this means zero performance data.traces_sample_rate=1.0 sends every transaction. Test before deploying to a busy production service; prefer traces_sampler for fine-grained control.traces_sampler replaces, not combines with, traces_sample_rate: if both are set, the sampler wins.start_span() call has a cost. Instrument hot-path code sparingly.dsn, release, environment).sentry-trace + baggage pass CORS.trace_propagation_targets to prevent leaking trace headers to third-party APIs.traces_sampler + parent_sampled enables head-based sampling: if the frontend decided to sample, the backend follows that decision.