Sentry · Template docs
Sending data

Python

Send errors, structured logs and linked spans from Python.

Prepare the application

Use the template's examples/python/ directory. Set SENTRY_DSN in the runtime environment using the private DSN. The pinned SDK version is 2.39.0, from requirements.txt.

Run in the same Railway environment as the template, or use a locally reachable installation. A laptop cannot reach the private Railway listener through railway run.

Run the example

From the template repository root:

python3 -m venv .venv
. .venv/bin/activate
pip install -r examples/python/requirements.txt
python examples/python/app.py

Example source

This is the included app.py, reproduced without changes.

import os
import sentry_sdk

sentry_sdk.init(dsn=os.environ["SENTRY_DSN"], traces_sample_rate=1.0,
                enable_logs=True, send_default_pii=False)
with sentry_sdk.start_transaction(name="railway-example", op="task"):
    with sentry_sdk.start_span(op="work", description="child span"):
        sentry_sdk.logger.info("Railway structured log from Python")
        try:
            raise RuntimeError("Railway error example from Python")
        except RuntimeError:
            sentry_sdk.capture_exception()


def check_flush(pending, timeout):
    raise RuntimeError("Telemetry flush timed out")


sentry_sdk.flush(timeout=30, callback=check_flush)
print("SDK flush completed; verify Issues, Logs, and Traces")

Verify in Sentry

Open Issues, Logs and Traces for the same project and environment. Look for Railway error example from Python, Railway structured log from Python and railway-example. Confirm the child span belongs to its parent.

The sample uses full trace sampling for verification. Choose sampling for your workload. A completed flush does not prove server indexing. Use Troubleshooting if any telemetry type is missing.

Unofficial community template, not affiliated with or endorsed by Sentry.

Sentry is Fair Source (FSL-1.1-Apache-2.0), source-available software. Copyright Functional Software, Inc. dba Sentry.

For monitoring your own applications, not for offering Sentry as a hosted service to third parties.

Licensing · Upstream LICENSE.md

On this page