OpenTelemetry Support
You can configure your OpenTelemetry SDK to send traces and spans to Sentry.
Sentry requires opentelemetry-distro
version 0.350b0
or higher.
pip install --upgrade 'sentry-sdk[opentelemetry]'
Initialize Sentry for tracing and set the instrumenter
to otel
:
import sentry_sdk
sentry_sdk.init(
dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
enable_tracing=True,
# set the instrumenter to use OpenTelemetry instead of Sentry
instrumenter="otel",
)
This disables all Sentry instrumentation and relies on the chosen OpenTelemetry tracers for creating spans.
Next, configure OpenTelemetry as you need and hook in the Sentry span processor and propagator. Here's a minimal example that doesn't require any additional OpenTelemetry setup:
from opentelemetry import trace
from opentelemetry.propagate import set_global_textmap
from opentelemetry.sdk.trace import TracerProvider
from sentry_sdk.integrations.opentelemetry import SentrySpanProcessor, SentryPropagator
provider = TracerProvider()
provider.add_span_processor(SentrySpanProcessor())
trace.set_tracer_provider(provider)
set_global_textmap(SentryPropagator())
Finally, try it out with:
import time
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("test_otel_span"):
print("Processing some data...")
# Simulate some processing
time.sleep(3)
With Sentry’s OpenTelemetry SDK, an OpenTelemetry Span
becomes a Sentry Transaction
or Span
. The first Span
sent through the Sentry SpanProcessor
is a Transaction
, and any child Span
gets attached to the first Transaction
upon checking the parent Span
context. This is true for the OpenTelemetry root Span
and any top level Span
in the system. For example, a request sent from frontend to backend will create an OpenTelemetry root Span
with a corresponding Sentry Transaction
. The backend request will create a new Sentry Transaction
for the OpenTelemetry Span
. The Sentry Transaction
and Span
are linked as a trace for navigation and error tracking purposes.
If you need more fine grained control over Sentry, take a look at the Configuration page. In case you'd like to filter out transactions before sending them to Sentry (to get rid of health checks, for example), you may find the Filtering page helpful.
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").
- Package:
- pypi:sentry-sdk
- Version:
- 1.45.0
- Repository:
- https://github.com/getsentry/sentry-python
- API Documentation:
- https://getsentry.github.io/sentry-python/