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 {
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//!         ..sentry::ClientOptions::default()
50//!     },
51//! ));
52//!
53//! // Register the Sentry propagator to enable distributed tracing
54//! global::set_text_map_propagator(sentry_opentelemetry::SentryPropagator::new());
55//!
56//! let tracer_provider = SdkTracerProvider::builder()
57//!     // Register the Sentry span processor to send OpenTelemetry spans to Sentry
58//!     .with_span_processor(sentry_opentelemetry::SentrySpanProcessor::new())
59//!     .build();
60//!
61//! global::set_tracer_provider(tracer_provider);
62//! ```
63//!
64//! # Usage
65//!
66//! Use the OpenTelemetry API to create spans. They will be captured by Sentry:
67//!
68//! ```
69//! # use opentelemetry::{
70//! #     global,
71//! #     trace::{TraceContextExt, Tracer},
72//! #     KeyValue,
73//! # };
74//!
75//! let tracer = global::tracer("tracer");
76//! // Creates a Sentry span (transaction) with the name set to "example"
77//! tracer.in_span("example", |_| {
78//!     // Creates a Sentry child span with the name set to "child"
79//!     tracer.in_span("child", |cx| {
80//!         // OTEL span attributes are captured as data attributes on the Sentry span
81//!         cx.span().set_attribute(KeyValue::new("my", "attribute"));
82//!
83//!         // Captures a Sentry error message and associates it with the ongoing child span
84//!         sentry::capture_message("Everything is on fire!", sentry::Level::Error);
85//!     });
86//! });
87//! ```
88
89mod converters;
90mod processor;
91mod propagator;
92
93pub use processor::*;
94pub use propagator::*;