sentry-opentelemetry

Crates.iosentry-opentelemetry
lib.rssentry-opentelemetry
version0.43.0
created_at2025-05-09 14:24:51.266226+00
updated_at2025-09-24 09:26:04.822991+00
descriptionSentry integration for OpenTelemetry.
homepagehttps://sentry.io/welcome/
repositoryhttps://github.com/getsentry/sentry-rust
max_upload_size
id1667122
size61,484
Sentry Bot (getsentry-bot)

documentation

README

Sentry

Sentry Rust SDK: sentry-opentelemetry

OpenTelemetry support for Sentry.

This integration allows you to capture spans from your existing OpenTelemetry setup and send them to Sentry, with support for distributed tracing.

It's assumed that only the OpenTelemetry tracing API is used to start/end/modify Spans. Mixing it with the Sentry tracing API (e.g. sentry_core::start_transaction(ctx)) will not work, as the spans created with the two methods will not be nested properly.

Capturing events with sentry::capture_event will send them to Sentry with the correct trace and span association.

Configuration

Add the necessary dependencies to your Cargo.toml:

[dependencies]
opentelemetry = { version = "0.29.1", features = ["trace"] }
opentelemetry_sdk = { version = "0.29.0", features = ["trace"] }
sentry = { version = "0.38.0", features = ["opentelemetry"] }

Initialize Sentry with a traces_sample_rate, then register the [SentryPropagator] and the [SentrySpanProcessor]:

use opentelemetry::{
    global,
    trace::{TraceContextExt, Tracer},
    KeyValue,
};
use opentelemetry_sdk::trace::SdkTracerProvider;
use sentry::integrations::opentelemetry as sentry_opentelemetry;

// Initialize the Sentry SDK
let _guard = sentry::init((
    "https://your-dsn@sentry.io/0",
    sentry::ClientOptions {
        // Enable capturing of traces; set this a to lower value in production.
        // For more sophisticated behavior use a custom
        // [`sentry::ClientOptions::traces_sampler`] instead.
        // That's the equivalent of a tail sampling processor in OpenTelemetry.
        // These options will only affect sampling of the spans that are sent to Sentry,
        // not of the underlying OpenTelemetry spans.
        traces_sample_rate: 1.0,
        debug: true,
        ..sentry::ClientOptions::default()
    },
));

// Register the Sentry propagator to enable distributed tracing
global::set_text_map_propagator(sentry_opentelemetry::SentryPropagator::new());

let tracer_provider = SdkTracerProvider::builder()
    // Register the Sentry span processor to send OpenTelemetry spans to Sentry
    .with_span_processor(sentry_opentelemetry::SentrySpanProcessor::new())
    .build();

global::set_tracer_provider(tracer_provider);

Usage

Use the OpenTelemetry API to create spans. They will be captured by Sentry:


let tracer = global::tracer("tracer");
// Creates a Sentry span (transaction) with the name set to "example"
tracer.in_span("example", |_| {
    // Creates a Sentry child span with the name set to "child"
    tracer.in_span("child", |cx| {
        // OTEL span attributes are captured as data attributes on the Sentry span
        cx.span().set_attribute(KeyValue::new("my", "attribute"));

        // Captures a Sentry error message and associates it with the ongoing child span
        sentry::capture_message("Everything is on fire!", sentry::Level::Error);
    });
});

Resources

License: MIT

Commit count: 1168

cargo fmt