split sentry init; add user-agent, trace hooks.

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-06-24 22:10:34 +00:00
parent c05f00661b
commit 703c275266
3 changed files with 50 additions and 31 deletions

45
src/main/sentry.rs Normal file
View file

@ -0,0 +1,45 @@
#![cfg(feature = "sentry_telemetry")]
use std::{str::FromStr, sync::Arc};
use conduit::{config::Config, trace};
use sentry::{
types::{protocol::v7::Event, Dsn},
Breadcrumb, ClientOptions,
};
pub(crate) fn init(config: &Config) -> Option<sentry::ClientInitGuard> {
config.sentry.then(|| sentry::init(options(config)))
}
fn options(config: &Config) -> ClientOptions {
let dsn = config
.sentry_endpoint
.as_ref()
.expect("init_sentry should only be called if sentry is enabled and this is not None")
.as_str();
ClientOptions {
dsn: Some(Dsn::from_str(dsn).expect("sentry_endpoint must be a valid URL")),
server_name: config
.sentry_send_server_name
.then(|| config.server_name.to_string().into()),
traces_sample_rate: config.sentry_traces_sample_rate,
debug: cfg!(debug_assertions),
release: sentry::release_name!(),
user_agent: conduit::version::user_agent().into(),
before_send: Some(Arc::new(before_send)),
before_breadcrumb: Some(Arc::new(before_breadcrumb)),
..Default::default()
}
}
fn before_send(event: Event<'static>) -> Option<Event<'static>> {
trace!("Sending sentry event: {event:?}");
Some(event)
}
fn before_breadcrumb(crumb: Breadcrumb) -> Option<Breadcrumb> {
trace!("Adding sentry breadcrumb: {crumb:?}");
Some(crumb)
}