use std::{borrow::Cow, str::FromStr};
use anyhow::Context;
use sentry::{types::Dsn, ClientInitGuard};
use tracing_subscriber::{self, layer::SubscriberExt, registry::Registry};
#[must_use]
pub struct Instruments {
_client_guard: Option<ClientInitGuard>,
}
impl Instruments {
pub fn new(release_name: Option<Cow<'static, str>>) -> anyhow::Result<Self> {
let should_activate = std::env::var("SWARMD_TELEMETRY_LEVEL")
.ok()
.and_then(|x| x.parse::<u8>().ok())
.unwrap_or(1);
let guard = if should_activate > 0 {
Some(sentry::init(sentry::ClientOptions {
dsn: Some(Dsn::from_str("https://3ca89d7f605a902ddd39375c2a5b1509@o4506304538345472.ingest.sentry.io/4506304540311552")?),
debug: false,
release: release_name,
traces_sample_rate: 1.0,
..sentry::ClientOptions::default()
}))
} else {
None
};
let subscriber = Registry::default()
.with(tracing_subscriber::EnvFilter::from_default_env())
.with(sentry_tracing::layer())
.with(tracing_subscriber::fmt::layer());
tracing::subscriber::set_global_default(subscriber)
.with_context(|| "cannot set global default subscriber")?;
Ok(Self {
_client_guard: guard,
})
}
}