From c254961cf586b4f5b1c56e51a5a2cb6c5e41f0ee Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Mon, 13 Sep 2021 01:13:04 +0200 Subject: [PATCH 1/2] Extract Sentry initialization code into dedicated function --- src/bin/server.rs | 20 ++------------------ src/lib.rs | 1 + src/sentry.rs | 22 ++++++++++++++++++++++ 3 files changed, 25 insertions(+), 18 deletions(-) create mode 100644 src/sentry.rs 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..ae7a1b8829c --- /dev/null +++ b/src/sentry.rs @@ -0,0 +1,22 @@ +use sentry::{ClientInitGuard, ClientOptions, IntoDsn}; +use std::borrow::Cow; + +#[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) + }) +} From b571353d1eb5f450b445f3b9577f0559cd5b60b1 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Mon, 13 Sep 2021 01:16:31 +0200 Subject: [PATCH 2/2] sentry: Add documentation --- src/sentry.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/sentry.rs b/src/sentry.rs index ae7a1b8829c..1467e71b4c7 100644 --- a/src/sentry.rs +++ b/src/sentry.rs @@ -1,6 +1,14 @@ 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")