Skip to main content

sentry_tracing/
lib.rs

1//! Capture `tracing` spans and events as Sentry traces and structured logs.
2//!
3//! We recommend enabling these two layers:
4//!
5//! - [`SpanLayer`] captures `tracing` spans as Sentry transactions and spans.
6//! - [`LogLayer`] captures `tracing` events as Sentry structured logs. Requires the `logs` feature.
7//!
8//! If you would like Sentry issues for error-level `tracing` events, you can also enable
9//! [`ErrorLayer`], which captures `tracing` events as Sentry error events, with support for
10//! tags and exception information. Error events count against your error quota, which is
11//! typically more expensive than logs. See [Errors](#errors).
12//!
13//! We also provide [`BreadcrumbLayer`], which adds `tracing` events as breadcrumbs on subsequent
14//! Sentry error events. This layer primarily exists to support legacy workflows; we recommend
15//! [`LogLayer`] instead.
16//!
17//! For migration from the legacy combined layer, see [`SentryLayer`].
18//!
19//! # Getting started
20//!
21//! Enable the `tracing` and `logs` features on `sentry` to use its tracing integration re-export.
22//! Set a traces sample rate to capture transactions, then install the span and log layers with
23//! explicit level filters:
24//!
25//! ```
26//! # #[cfg(feature = "logs")]
27//! # {
28//! use sentry::integrations::tracing::{LogLayer, SpanLayer};
29//! use tracing_subscriber::filter::LevelFilter;
30//! use tracing_subscriber::prelude::*;
31//!
32//! let _guard = sentry::init(
33//!     sentry::ClientOptions::new()
34//!         // Use a lower sample rate in production.
35//!         .traces_sample_rate(1.0),
36//! );
37//!
38//! tracing_subscriber::registry()
39//!     .with(SpanLayer::new().with_filter(LevelFilter::INFO))
40//!     .with(LogLayer::new().with_filter(LevelFilter::INFO))
41//!     .init();
42//! # }
43//! ```
44//!
45//! The layers do not filter by level internally. The filters above capture `INFO`, `WARN`, and
46//! `ERROR` telemetry, excluding `DEBUG` and `TRACE`. Adjust each layer's filter independently to
47//! control volume. Global subscriber filters also apply and can suppress telemetry before the
48//! Sentry layers receive it.
49//!
50//! # Spans
51//!
52//! `SpanLayer` creates a Sentry transaction when no Sentry span is active, or a child span
53//! otherwise. For example,
54//! [`tracing::instrument`](https://docs.rs/tracing/latest/tracing/attr.instrument.html) creates
55//! spans for instrumented functions; arguments become span data unless skipped.
56//!
57//! With the log layer installed, the `info!` event below produces a log associated with the same
58//! trace as the instrumented functions:
59//!
60//! ```
61//! #[tracing::instrument]
62//! async fn outer() {
63//!     inner(42).await;
64//! }
65//!
66//! #[tracing::instrument]
67//! async fn inner(value: i32) {
68//!     tracing::info!(value, "completed step");
69//! }
70//! ```
71//!
72//! By default, the Sentry span name matches the `tracing` span name, and its operation is
73//! `<target>::<span name>`.
74//!
75//! ## Special span fields
76//!
77//! - `sentry.name` overrides the Sentry span name, including when recorded after creation.
78//! - `sentry.op` overrides the Sentry span operation, including when recorded after creation.
79//! - `sentry.trace` continues the trace from a `sentry-trace` header. It must be set when a
80//!   **root Sentry span** is created, meaning no Sentry span is active in the current scope.
81//!   Applying it to a child Sentry span or recording it after creation has no effect.
82//!
83//! ```
84//! #[tracing::instrument(skip_all, fields(
85//!     sentry.name = "GET /payments",
86//!     sentry.op = "http.server",
87//!     sentry.trace = headers.get("sentry-trace").unwrap_or(&"".to_owned()),
88//! ))]
89//! async fn handle_request(headers: std::collections::HashMap<String, String>) {
90//!     // ...
91//! }
92//! ```
93//!
94//! # Logs
95//!
96//! `LogLayer` sends events accepted by its filter as structured logs. Event fields become log
97//! attributes.
98//!
99//! ```
100//! tracing::info!(number = 42, my.key = "value", "Processed request");
101//! ```
102//!
103//! # Errors
104//!
105//! `ErrorLayer` sends events accepted by its filter as Sentry error events, which create Sentry
106//! issues. If you would like Sentry issues for `ERROR` events in addition to logs, you can enable
107//! the error layer alongside the span and log layers, with an `ERROR` filter:
108//!
109//! ```
110//! # #[cfg(feature = "logs")]
111//! # {
112//! use sentry::integrations::tracing::{ErrorLayer, LogLayer, SpanLayer};
113//! use tracing_subscriber::filter::LevelFilter;
114//! use tracing_subscriber::prelude::*;
115//!
116//! tracing_subscriber::registry()
117//!     .with(SpanLayer::new().with_filter(LevelFilter::INFO))
118//!     .with(LogLayer::new().with_filter(LevelFilter::INFO))
119//!     .with(ErrorLayer::new().with_filter(LevelFilter::ERROR))
120//!     .init();
121//! # }
122//! ```
123
124#![cfg_attr(doc_cfg, feature(doc_cfg))]
125#![doc(html_favicon_url = "https://sentry-brand.storage.googleapis.com/favicon.ico")]
126#![doc(html_logo_url = "https://sentry-brand.storage.googleapis.com/sentry-glyph-black.png")]
127
128mod converters;
129mod layer;
130
131#[cfg(feature = "logs")]
132#[expect(deprecated, reason = "re-exporting deprecated legacy mapper helpers")]
133pub use converters::log_from_event;
134#[expect(deprecated, reason = "re-exporting deprecated legacy mapper helpers")]
135pub use converters::{breadcrumb_from_event, event_from_event};
136pub use layer::*;
137
138const TAGS_PREFIX: &str = "tags.";
139const SENTRY_OP_FIELD: &str = "sentry.op";
140const SENTRY_NAME_FIELD: &str = "sentry.name";
141const SENTRY_TRACE_FIELD: &str = "sentry.trace";