objectstore_server/
observability.rs

1//! Initialization of Sentry error reporting.
2//!
3//! Call [`init_sentry`] during server startup before creating the Tokio runtime so it can
4//! instrument async tasks from the start. Tracing subscriber initialization (including the
5//! Sentry tracing layer) is handled by [`objectstore_log::init`].
6
7use secrecy::ExposeSecret;
8
9use crate::config::Config;
10
11/// The full release name including the objectstore version and SHA.
12const RELEASE: &str = std::env!("OBJECTSTORE_RELEASE");
13
14/// Initializes the Sentry error-reporting client, if a DSN is configured.
15///
16/// Returns `None` when `config.sentry.dsn` is not set. The returned
17/// [`sentry::ClientInitGuard`] must be kept alive for the duration of the process;
18/// dropping it flushes the event queue and shuts down the Sentry client.
19pub fn init_sentry(config: &Config) -> Option<sentry::ClientInitGuard> {
20    let config = &config.sentry;
21    let dsn = config.dsn.as_ref()?;
22
23    let guard = sentry::init(sentry::ClientOptions {
24        dsn: dsn.expose_secret().parse().ok(),
25        release: Some(RELEASE.into()),
26        environment: config.environment.clone(),
27        server_name: config.server_name.clone(),
28        sample_rate: config.sample_rate,
29        traces_sampler: {
30            let traces_sample_rate = config.traces_sample_rate;
31            let inherit_sampling_decision = config.inherit_sampling_decision;
32            Some(std::sync::Arc::new(move |ctx| {
33                if let Some(sampled) = ctx.sampled()
34                    && inherit_sampling_decision
35                {
36                    f32::from(sampled)
37                } else {
38                    traces_sample_rate
39                }
40            }))
41        },
42        enable_logs: true,
43        debug: config.debug,
44        ..Default::default()
45    });
46
47    sentry::configure_scope(|scope| {
48        for (k, v) in &config.tags {
49            scope.set_tag(k, v);
50        }
51    });
52
53    Some(guard)
54}