stratus/infra/sentry/
sentry_config.rs

1use clap::Parser;
2use display_json::DebugAsJson;
3use sentry::ClientInitGuard;
4
5use crate::config::Environment;
6use crate::ext::not;
7use crate::infra::build_info;
8
9#[derive(DebugAsJson, Clone, Parser, serde::Serialize)]
10pub struct SentryConfig {
11    /// Sentry server URL.
12    #[arg(long = "sentry-url", env = "SENTRY_URL", required = false)]
13    pub sentry_url: String,
14}
15
16impl SentryConfig {
17    /// Inits application global Sentry exporter.
18    pub fn init(&self, env: Environment) -> anyhow::Result<ClientInitGuard> {
19        let release = build_info::service_name_with_version();
20        tracing::info!(url = %self.sentry_url, %env, %release, "creating sentry exporter");
21
22        let guard = sentry::init((
23            self.sentry_url.clone(),
24            sentry::ClientOptions {
25                release: Some(release.clone().into()),
26                environment: Some(env.to_string().into()),
27                ..Default::default()
28            },
29        ));
30        if not(guard.is_enabled()) {
31            tracing::error!(url = %self.sentry_url, %env, %release, "failed to create sentry exporter");
32        }
33
34        Ok(guard)
35    }
36}