sentry_tracing/
lib.rs

1//! Support for automatic breadcrumb, event, and trace capturing from `tracing` events.
2//!
3//! The `tracing` crate is supported in three ways. First, events can be captured as breadcrumbs for
4//! later. Secondly, error events can be captured as events to Sentry. Finally, spans can be
5//! recorded as structured transaction events. By default, events above `Info` are recorded as
6//! breadcrumbs, events above `Error` are captured as error events, and spans above `Info` are
7//! recorded as transactions.
8//!
9//! # Configuration
10//!
11//! To fully enable the tracing integration, set the traces sample rate and add a layer to the
12//! tracing subscriber:
13//!
14//! ```
15//! use tracing_subscriber::prelude::*;
16//!
17//! let _guard = sentry::init(sentry::ClientOptions {
18//!     // Enable capturing of traces; set this a to lower value in production:
19//!     traces_sample_rate: 1.0,
20//!     ..sentry::ClientOptions::default()
21//! });
22//!
23//! // Register the Sentry tracing layer to capture breadcrumbs, events, and spans:
24//! tracing_subscriber::registry()
25//!     .with(tracing_subscriber::fmt::layer())
26//!     .with(sentry_tracing::layer())
27//!     .init();
28//! ```
29//!
30//! It is also possible to set an explicit filter, to customize which log events are captured by
31//! Sentry:
32//!
33//! ```
34//! use sentry_tracing::EventFilter;
35//! use tracing_subscriber::prelude::*;
36//!
37//! let sentry_layer = sentry_tracing::layer().event_filter(|md| match md.level() {
38//!     &tracing::Level::ERROR => EventFilter::Event,
39//!     _ => EventFilter::Ignore,
40//! });
41//!
42//! tracing_subscriber::registry()
43//!     .with(tracing_subscriber::fmt::layer())
44//!     .with(sentry_layer)
45//!     .init();
46//! ```
47//!
48//! # Logging Messages
49//!
50//! Tracing events automatically create breadcrumbs that are attached to the current scope in
51//! Sentry. They show up on errors and transactions captured within this scope as shown in the
52//! examples below.
53//!
54//! Fields passed to the event macro are automatically tracked as structured data in Sentry. For
55//! breadcrumbs, they are shown directly with the breadcrumb message. For other types of data, read
56//! below.
57//!
58//! ```
59//! for i in 0..10 {
60//!     tracing::debug!(number = i, "Generates a breadcrumb");
61//! }
62//! ```
63//!
64//! # Tracking Errors
65//!
66//! The easiest way to emit errors is by logging an event with `ERROR` level. This will create a
67//! grouped issue in Sentry. To add custom information, prepend the message with fields. It is also
68//! possible to add Sentry tags if a field is prefixed with `"tags."`
69//!
70//! ```
71//! tracing::error!(
72//!     field = "value",                  // will become a context field
73//!     tags.custom = "value",            // will become a tag in Sentry
74//!     "this is an error with a custom tag",
75//! );
76//! ```
77//!
78//! To track [error structs](std::error::Error), assign a reference to error trait object as field
79//! in one of the logging macros. By convention, it is recommended to use the `ERROR` level and
80//! assign it to a field called `error`, although the integration will also work with all other
81//! levels and field names.
82//!
83//! All other fields passed to the macro are captured in a custom "Tracing Fields" context in
84//! Sentry.
85//!
86//! ```
87//! use std::error::Error;
88//! use std::io;
89//!
90//! let custom_error = io::Error::new(io::ErrorKind::Other, "oh no");
91//! tracing::error!(error = &custom_error as &dyn Error);
92//! ```
93//!
94//! It is also possible to combine error messages with error structs. In Sentry, this creates issues
95//! grouped by the message and location of the error log, and adds the passed error as nested
96//! source.
97//!
98//! ```
99//! use std::error::Error;
100//! use std::io;
101//!
102//! let custom_error = io::Error::new(io::ErrorKind::Other, "oh no");
103//! tracing::error!(error = &custom_error as &dyn Error, "my operation failed");
104//! ```
105//!
106//! # Tracing Spans
107//!
108//! The integration automatically tracks `tracing` spans as spans in Sentry. A convenient way to do
109//! this is with the `#[instrument]` attribute macro, which creates a transaction for the function
110//! in Sentry.
111//!
112//! Function arguments are added as context fields automatically, which can be configured through
113//! attribute arguments. Refer to documentation of the macro for more information.
114//!
115//! ```
116//! use std::time::Duration;
117//!
118//! use tracing_subscriber::prelude::*;
119//!
120//! // Functions instrumented by tracing automatically report
121//! // their span as transactions.
122//! #[tracing::instrument]
123//! async fn outer() {
124//!     for i in 0..10 {
125//!         inner(i).await;
126//!     }
127//! }
128//!
129//! // This creates spans inside the outer transaction, unless called directly.
130//! #[tracing::instrument]
131//! async fn inner(i: u32) {
132//!     // Also works, since log events are ingested by the tracing system
133//!     tracing::debug!(number = i, "Generates a breadcrumb");
134//!
135//!     tokio::time::sleep(Duration::from_millis(100)).await;
136//! }
137//! ```
138
139#![doc(html_favicon_url = "https://sentry-brand.storage.googleapis.com/favicon.ico")]
140#![doc(html_logo_url = "https://sentry-brand.storage.googleapis.com/sentry-glyph-black.png")]
141#![warn(missing_docs)]
142
143mod converters;
144mod layer;
145
146pub use converters::*;
147pub use layer::*;
148
149const TAGS_PREFIX: &str = "tags.";