Skip to content

Commit 5f13e07

Browse files
committed
Auto merge of #3920 - Turbo87:sentry, r=JohnTitor
Extract Sentry initialization code into dedicated function This will allow us to more easily enable Sentry error reporting for the other binaries too.
2 parents ea78227 + b571353 commit 5f13e07

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

src/bin/server.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,20 @@
11
#![warn(clippy::all, rust_2018_idioms)]
22

33
use cargo_registry::{metrics::LogEncoder, util::errors::AppResult, App, Env};
4-
use std::{borrow::Cow, fs::File, process::Command, sync::Arc, time::Duration};
4+
use std::{fs::File, process::Command, sync::Arc, time::Duration};
55

66
use conduit_hyper::Service;
77
use futures_util::future::FutureExt;
88
use prometheus::Encoder;
99
use reqwest::blocking::Client;
10-
use sentry::{ClientOptions, IntoDsn};
1110
use std::io::Write;
1211
use tokio::io::AsyncWriteExt;
1312
use tokio::signal::unix::{signal, SignalKind};
1413

1514
const CORE_THREADS: usize = 4;
1615

1716
fn main() -> Result<(), Box<dyn std::error::Error>> {
18-
let _sentry = dotenv::var("SENTRY_DSN_API")
19-
.ok()
20-
.into_dsn()
21-
.expect("SENTRY_DSN_API is not a valid Sentry DSN value")
22-
.map(|dsn| {
23-
let mut opts = ClientOptions::from(dsn);
24-
opts.environment = Some(
25-
dotenv::var("SENTRY_ENV_API")
26-
.map(Cow::Owned)
27-
.expect("SENTRY_ENV_API must be set when using SENTRY_DSN_API"),
28-
);
29-
30-
opts.release = dotenv::var("HEROKU_SLUG_COMMIT").ok().map(Into::into);
31-
32-
sentry::init(opts)
33-
});
17+
let _sentry = cargo_registry::sentry::init();
3418

3519
// Initialize logging
3620
tracing_subscriber::fmt::init();

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub mod util;
5555
pub mod controllers;
5656
pub mod models;
5757
mod router;
58+
pub mod sentry;
5859
pub mod views;
5960

6061
/// Used for setting different values depending on whether the app is being run in production,

src/sentry.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use sentry::{ClientInitGuard, ClientOptions, IntoDsn};
2+
use std::borrow::Cow;
3+
4+
/// Initializes the Sentry SDK from the environment variables.
5+
///
6+
/// If `SENTRY_DSN_API` is not set then Sentry will not be initialized,
7+
/// otherwise it is required to be a valid DSN string. `SENTRY_ENV_API` must
8+
/// be set if a DSN is provided.
9+
///
10+
/// `HEROKU_SLUG_COMMIT`, if present, will be used as the `release` property
11+
/// on all events.
12+
#[must_use]
13+
pub fn init() -> Option<ClientInitGuard> {
14+
dotenv::var("SENTRY_DSN_API")
15+
.ok()
16+
.into_dsn()
17+
.expect("SENTRY_DSN_API is not a valid Sentry DSN value")
18+
.map(|dsn| {
19+
let mut opts = ClientOptions::from(dsn);
20+
opts.environment = Some(
21+
dotenv::var("SENTRY_ENV_API")
22+
.map(Cow::Owned)
23+
.expect("SENTRY_ENV_API must be set when using SENTRY_DSN_API"),
24+
);
25+
26+
opts.release = dotenv::var("HEROKU_SLUG_COMMIT").ok().map(Into::into);
27+
28+
sentry::init(opts)
29+
})
30+
}

0 commit comments

Comments
 (0)