Skip to main content

sentry_opentelemetry/
lib.rs

1//! OpenTelemetry support for Sentry.
2//!
3//! This integration allows you to capture spans from your existing OpenTelemetry setup and send
4//! them to Sentry, with support for distributed tracing.
5//!
6//! It's assumed that only the [OpenTelemetry tracing
7//! API](https://opentelemetry.io/docs/specs/otel/trace/api/) is used to start/end/modify Spans.
8//! Mixing it with the Sentry tracing API (e.g. `sentry_core::start_transaction(ctx)`) will not
9//! work, as the spans created with the two methods will not be nested properly.
10//!
11//! Capturing events with `sentry::capture_event` will send them to Sentry with the correct
12//! trace and span association.
13//!
14//! # Configuration
15//!
16//! Add the necessary dependencies to your Cargo.toml:
17//!
18//! ```toml
19//! [dependencies]
20//! opentelemetry = { version = "0.29.1", features = ["trace"] }
21//! opentelemetry_sdk = { version = "0.29.0", features = ["trace"] }
22//! sentry = { version = "0.38.0", features = ["opentelemetry"] }
23//! ```
24//!
25//! Initialize Sentry with a `traces_sample_rate`, then register the [`SentryPropagator`] and the
26//! [`SentrySpanProcessor`]:
27//!
28//! ```
29//! use opentelemetry::{
30//!     global,
31//!     trace::{TraceContextExt, Tracer},
32//!     KeyValue,
33//! };
34//! use opentelemetry_sdk::trace::SdkTracerProvider;
35//! use sentry::integrations::opentelemetry as sentry_opentelemetry;
36//!
37//! // Initialize the Sentry SDK
38//! let _guard = sentry::init((
39//!     "https://your-dsn@sentry.io/0",
40//!     sentry::ClientOptions::new()
41//!         // Enable capturing of traces; set this a to lower value in production.
42//!         // For more sophisticated behavior use a custom
43//!         // [`sentry::ClientOptions::traces_sampler`] instead.
44//!         // That's the equivalent of a tail sampling processor in OpenTelemetry.
45//!         // These options will only affect sampling of the spans that are sent to Sentry,
46//!         // not of the underlying OpenTelemetry spans.
47//!         .traces_sample_rate(1.0)
48//!         .debug(true),
49//! ));
50//!
51//! // Register the Sentry propagator to enable distributed tracing
52//! global::set_text_map_propagator(sentry_opentelemetry::SentryPropagator::new());
53//!
54//! let tracer_provider = SdkTracerProvider::builder()
55//!     // Register the Sentry span processor to send OpenTelemetry spans to Sentry
56//!     .with_span_processor(sentry_opentelemetry::SentrySpanProcessor::new())
57//!     .build();
58//!
59//! global::set_tracer_provider(tracer_provider);
60//! ```
61//!
62//! # Usage
63//!
64//! Use the OpenTelemetry API to create spans. They will be captured by Sentry:
65//!
66//! ```
67//! # use opentelemetry::{
68//! #     global,
69//! #     trace::{TraceContextExt, Tracer},
70//! #     KeyValue,
71//! # };
72//!
73//! let tracer = global::tracer("tracer");
74//! // Creates a Sentry span (transaction) with the name set to "example"
75//! tracer.in_span("example", |_| {
76//!     // Creates a Sentry child span with the name set to "child"
77//!     tracer.in_span("child", |cx| {
78//!         // OTEL span attributes are captured as data attributes on the Sentry span
79//!         cx.span().set_attribute(KeyValue::new("my", "attribute"));
80//!
81//!         // Captures a Sentry error message and associates it with the ongoing child span
82//!         sentry::capture_message("Everything is on fire!", sentry::Level::Error);
83//!     });
84//! });
85//! ```
86
87mod converters;
88mod processor;
89mod propagator;
90
91pub use processor::*;
92pub use propagator::*;