diff --git a/src/bin/server.rs b/src/bin/server.rs index 79907e5e039..1d69c17a834 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -1,13 +1,12 @@ #![warn(clippy::all, rust_2018_idioms)] use cargo_registry::{metrics::LogEncoder, util::errors::AppResult, App, Env}; -use std::{borrow::Cow, fs::File, process::Command, sync::Arc, time::Duration}; +use std::{fs::File, process::Command, sync::Arc, time::Duration}; use conduit_hyper::Service; use futures_util::future::FutureExt; use prometheus::Encoder; use reqwest::blocking::Client; -use sentry::{ClientOptions, IntoDsn}; use std::io::Write; use tokio::io::AsyncWriteExt; use tokio::signal::unix::{signal, SignalKind}; @@ -15,22 +14,7 @@ use tokio::signal::unix::{signal, SignalKind}; const CORE_THREADS: usize = 4; fn main() -> Result<(), Box> { - let _sentry = dotenv::var("SENTRY_DSN_API") - .ok() - .into_dsn() - .expect("SENTRY_DSN_API is not a valid Sentry DSN value") - .map(|dsn| { - let mut opts = ClientOptions::from(dsn); - opts.environment = Some( - dotenv::var("SENTRY_ENV_API") - .map(Cow::Owned) - .expect("SENTRY_ENV_API must be set when using SENTRY_DSN_API"), - ); - - opts.release = dotenv::var("HEROKU_SLUG_COMMIT").ok().map(Into::into); - - sentry::init(opts) - }); + let _sentry = cargo_registry::sentry::init(); // Initialize logging tracing_subscriber::fmt::init(); diff --git a/src/lib.rs b/src/lib.rs index 40168c77785..5b433601094 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -55,6 +55,7 @@ pub mod util; pub mod controllers; pub mod models; mod router; +pub mod sentry; pub mod views; /// Used for setting different values depending on whether the app is being run in production, diff --git a/src/sentry.rs b/src/sentry.rs new file mode 100644 index 00000000000..1467e71b4c7 --- /dev/null +++ b/src/sentry.rs @@ -0,0 +1,30 @@ +use sentry::{ClientInitGuard, ClientOptions, IntoDsn}; +use std::borrow::Cow; + +/// Initializes the Sentry SDK from the environment variables. +/// +/// If `SENTRY_DSN_API` is not set then Sentry will not be initialized, +/// otherwise it is required to be a valid DSN string. `SENTRY_ENV_API` must +/// be set if a DSN is provided. +/// +/// `HEROKU_SLUG_COMMIT`, if present, will be used as the `release` property +/// on all events. +#[must_use] +pub fn init() -> Option { + dotenv::var("SENTRY_DSN_API") + .ok() + .into_dsn() + .expect("SENTRY_DSN_API is not a valid Sentry DSN value") + .map(|dsn| { + let mut opts = ClientOptions::from(dsn); + opts.environment = Some( + dotenv::var("SENTRY_ENV_API") + .map(Cow::Owned) + .expect("SENTRY_ENV_API must be set when using SENTRY_DSN_API"), + ); + + opts.release = dotenv::var("HEROKU_SLUG_COMMIT").ok().map(Into::into); + + sentry::init(opts) + }) +}