diff --git a/src/main/main.rs b/src/main/main.rs index 1bb46c8e..d579cd4a 100644 --- a/src/main/main.rs +++ b/src/main/main.rs @@ -1,6 +1,7 @@ pub(crate) mod clap; mod mods; mod restart; +mod sentry; mod server; mod signal; diff --git a/src/main/sentry.rs b/src/main/sentry.rs new file mode 100644 index 00000000..6ed4bb8a --- /dev/null +++ b/src/main/sentry.rs @@ -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 { + 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> { + trace!("Sending sentry event: {event:?}"); + Some(event) +} + +fn before_breadcrumb(crumb: Breadcrumb) -> Option { + trace!("Adding sentry breadcrumb: {crumb:?}"); + Some(crumb) +} diff --git a/src/main/server.rs b/src/main/server.rs index 456a8756..064357c1 100644 --- a/src/main/server.rs +++ b/src/main/server.rs @@ -21,7 +21,7 @@ pub(crate) struct Server { _tracing_flame_guard: TracingFlameGuard, #[cfg(feature = "sentry_telemetry")] - _sentry_guard: Option, + _sentry_guard: Option<::sentry::ClientInitGuard>, #[cfg(conduit_mods)] // Module instances; TODO: move to mods::loaded mgmt vector @@ -33,10 +33,12 @@ impl Server { let config = Config::new(args.config)?; #[cfg(feature = "sentry_telemetry")] - let sentry_guard = init_sentry(&config); + let sentry_guard = crate::sentry::init(&config); + let (tracing_reload_handle, tracing_flame_guard, capture) = init_tracing(&config); config.check()?; + #[cfg(unix)] sys::maximize_fd_limit().expect("Unable to increase maximum soft and hard file descriptor limit"); hash::init(); @@ -70,35 +72,6 @@ impl Server { } } -#[cfg(feature = "sentry_telemetry")] -fn init_sentry(config: &Config) -> Option { - if !config.sentry { - return None; - } - - let sentry_endpoint = config - .sentry_endpoint - .as_ref() - .expect("init_sentry should only be called if sentry is enabled and this is not None") - .as_str(); - - let server_name = if config.sentry_send_server_name { - Some(config.server_name.to_string().into()) - } else { - None - }; - - Some(sentry::init(( - sentry_endpoint, - sentry::ClientOptions { - release: sentry::release_name!(), - traces_sample_rate: config.sentry_traces_sample_rate, - server_name, - ..Default::default() - }, - ))) -} - #[cfg(feature = "perf_measurements")] type TracingFlameGuard = Option>>; #[cfg(not(feature = "perf_measurements"))]