Skip to content

Extract Sentry initialization code into dedicated function #3920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 2 additions & 18 deletions src/bin/server.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,20 @@
#![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};

const CORE_THREADS: usize = 4;

fn main() -> Result<(), Box<dyn std::error::Error>> {
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();
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
30 changes: 30 additions & 0 deletions src/sentry.rs
Original file line number Diff line number Diff line change
@@ -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<ClientInitGuard> {
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)
})
}