|
| 1 | +--- |
| 2 | +title: tracing |
| 3 | +description: "Learn about monitoring your Rust application with Sentry's tracing integration." |
| 4 | +--- |
| 5 | + |
| 6 | +The Sentry SDK offers an integration for tokio's [tracing](https://github.com/tokio-rs/tracing) ecosystem that supports: |
| 7 | + |
| 8 | +- Reporting `tracing` events to Sentry as events, breadcrumbs, or logs. |
| 9 | +- Reporting `tracing` spans to Sentry. |
| 10 | +- Reporting errors and panics with the correct trace correlation. |
| 11 | + |
| 12 | +## Install |
| 13 | + |
| 14 | +<OnboardingOptionButtons |
| 15 | + options={["error-monitoring", "performance", "logs"]} |
| 16 | +/> |
| 17 | + |
| 18 | +To add Sentry with the `tracing` integration to your Rust project, add the necessary dependencies to your `Cargo.toml`: |
| 19 | + |
| 20 | +```toml {filename:Cargo.toml} |
| 21 | +[dependencies] |
| 22 | +tracing = "0.1.41" |
| 23 | +tracing-subscriber = "0.3.19" |
| 24 | +sentry = { version = "{{@inject packages.version('sentry.rust') }}", features = [ |
| 25 | + "tracing", |
| 26 | + # ___PRODUCT_OPTION_START___ logs |
| 27 | + "logs", |
| 28 | + # ___PRODUCT_OPTION_END___ logs |
| 29 | +] } |
| 30 | +``` |
| 31 | + |
| 32 | +## Configure |
| 33 | + |
| 34 | +Initialize the Sentry SDK and register the Sentry layer to start sending `tracing` events and spans to Sentry: |
| 35 | + |
| 36 | +```rust |
| 37 | +use tracing_subscriber::prelude::*; |
| 38 | + |
| 39 | +fn main() { |
| 40 | + // Initialize Sentry |
| 41 | + let _guard = sentry::init(( |
| 42 | + "___PUBLIC_DSN___", |
| 43 | + sentry::ClientOptions { |
| 44 | + release: sentry::release_name!(), |
| 45 | + # ___PRODUCT_OPTION_START___ performance |
| 46 | + // Capture all traces and spans. Set to a lower value in production |
| 47 | + traces_sample_rate: 1.0, |
| 48 | + # ___PRODUCT_OPTION_END___ performance |
| 49 | + # ___PRODUCT_OPTION_START___ logs |
| 50 | + enable_logs:true |
| 51 | + # ___PRODUCT_OPTION_END___ logs |
| 52 | + ..sentry::ClientOptions::default() |
| 53 | + }, |
| 54 | + }); |
| 55 | + |
| 56 | + // Register the Sentry tracing layer |
| 57 | + tracing_subscriber::registry() |
| 58 | + .with(tracing_subscriber::fmt::layer()) |
| 59 | + .with(sentry_layer) |
| 60 | + .init(); |
| 61 | + |
| 62 | + // Futures need to be bound to a Hub |
| 63 | + // Learn more at https://docs.rs/sentry-core/latest/sentry_core/#parallelism-concurrency-and-async |
| 64 | + fail().bind_hub(sentry::Hub::current()).await; |
| 65 | +} |
| 66 | + |
| 67 | +# ___PRODUCT_OPTION_START___ performance |
| 68 | +#[tracing::instrument] // Captures a root span (transaction) around this function execution |
| 69 | +# ___PRODUCT_OPTION_END___ performance |
| 70 | +async fn fail() { |
| 71 | + # ___PRODUCT_OPTION_START___ performance |
| 72 | + let _span = tracing::info_span("Child span").entered(); // Captures a child span |
| 73 | + # ___PRODUCT_OPTION_END___ performance |
| 74 | + tracing::error!("Everything is on fire!"); |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +{/* ___PRODUCT_OPTION_START___ logs */} |
| 79 | + |
| 80 | +To capture logs for `tracing` events, you need to set up a custom event filter, like so: |
| 81 | + |
| 82 | +```rs |
| 83 | +use sentry::integrations::tracing::EventFilter; |
| 84 | + |
| 85 | +sentry::integrations::tracing::layer().event_filter(|md| match *md.level() { |
| 86 | + // Capture error level events as Sentry events |
| 87 | + tracing::Level::ERROR => EventFilter::Event, |
| 88 | + // Ignore trace level events, as they're too verbose |
| 89 | + tracing::Level::TRACE => EventFilter::Ignore, |
| 90 | + // Capture everything else as a structured log |
| 91 | + _ => EventFilter::Log, |
| 92 | +}); |
| 93 | +``` |
| 94 | + |
| 95 | +{/* ___PRODUCT_OPTION_END___ logs */} |
| 96 | + |
| 97 | +## Verify |
| 98 | + |
| 99 | +<Alert> |
| 100 | + |
| 101 | +Learn more about manually capturing an error or message in our <PlatformLink to="/usage/">Usage documentation</PlatformLink>. |
| 102 | + |
| 103 | +</Alert> |
| 104 | + |
| 105 | +To view and resolve the recorded error, log into [sentry.io](https://sentry.io) and select your project. Select Issues, and then Errors & Outages in the sidebar, where you will find the newly created issue. Clicking on the issue's title will open a page where you can see detailed information and mark it as resolved. |
0 commit comments