Skip to content

Commit d6d323f

Browse files
committed
extract logging config into subcrate
1 parent 0b9816c commit d6d323f

File tree

5 files changed

+108
-72
lines changed

5 files changed

+108
-72
lines changed

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,17 @@ opentelemetry-otlp = { version = "0.31.0", features = ["grpc-tonic", "metrics"]
2929
opentelemetry-resource-detectors = "0.10.0"
3030
opentelemetry_sdk = { version = "0.31.0", features = ["rt-tokio"] }
3131
regex = "1"
32+
sentry = { version = "0.46.0", features = ["panic", "tracing", "tower-http", "anyhow", "backtrace"] }
3233
tokio = { version = "1.0", features = ["rt-multi-thread", "signal", "macros", "process", "sync"] }
3334
tracing = "0.1.37"
3435
url = { version = "2.1.1", features = ["serde"] }
3536

3637
[dependencies]
3738
docs_rs_env_vars = { path = "crates/lib/docs_rs_env_vars" }
39+
docs_rs_logging = { path = "crates/lib/docs_rs_logging" }
3840
docs_rs_opentelemetry = { path = "crates/lib/docs_rs_opentelemetry" }
3941
docs_rs_utils = { path = "crates/lib/docs_rs_utils" }
40-
sentry = { version = "0.46.0", features = ["panic", "tracing", "tower-http", "anyhow", "backtrace"] }
42+
sentry = { workspace = true }
4143
log = "0.4"
4244
tracing = { workspace = true }
4345
tracing-subscriber = { version = "0.3.20", default-features = false, features = ["ansi", "fmt", "json", "env-filter", "tracing-log"] }
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "docs_rs_logging"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
anyhow = { workspace = true }
8+
docs_rs_utils = { path = "../docs_rs_utils" }
9+
sentry = { workspace = true }
10+
tracing = { workspace = true }
11+
tracing-subscriber = { version = "0.3.20", default-features = false, features = ["ansi", "fmt", "json", "env-filter", "tracing-log"] }
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
use sentry::{
2+
TransactionContext, integrations::panic as sentry_panic,
3+
integrations::tracing as sentry_tracing,
4+
};
5+
use std::{env, str::FromStr as _, sync::Arc};
6+
use tracing_subscriber::{EnvFilter, filter::Directive, prelude::*};
7+
8+
pub struct Guard {
9+
#[allow(dead_code)]
10+
sentry_guard: Option<sentry::ClientInitGuard>,
11+
}
12+
13+
pub fn init() -> anyhow::Result<Guard> {
14+
let log_formatter = {
15+
let log_format = env::var("DOCSRS_LOG_FORMAT").unwrap_or_default();
16+
17+
if log_format == "json" {
18+
tracing_subscriber::fmt::layer().json().boxed()
19+
} else {
20+
tracing_subscriber::fmt::layer().boxed()
21+
}
22+
};
23+
24+
let tracing_registry = tracing_subscriber::registry().with(log_formatter).with(
25+
EnvFilter::builder()
26+
.with_default_directive(Directive::from_str("docs_rs=info")?)
27+
.with_env_var("DOCSRS_LOG")
28+
.from_env_lossy(),
29+
);
30+
31+
let sentry_guard = if let Ok(sentry_dsn) = env::var("SENTRY_DSN") {
32+
tracing::subscriber::set_global_default(tracing_registry.with(
33+
sentry_tracing::layer().event_filter(|md| {
34+
if md.fields().field("reported_to_sentry").is_some() {
35+
sentry_tracing::EventFilter::Ignore
36+
} else {
37+
sentry_tracing::default_event_filter(md)
38+
}
39+
}),
40+
))?;
41+
42+
let traces_sample_rate = env::var("SENTRY_TRACES_SAMPLE_RATE")
43+
.ok()
44+
.and_then(|v| v.parse().ok())
45+
.unwrap_or(0.0);
46+
47+
let traces_sampler = move |ctx: &TransactionContext| -> f32 {
48+
if let Some(sampled) = ctx.sampled() {
49+
// if the transaction was already marked as "to be sampled" by
50+
// the JS/frontend SDK, we want to sample it in the backend too.
51+
return if sampled { 1.0 } else { 0.0 };
52+
}
53+
54+
let op = ctx.operation();
55+
if op == "docbuilder.build_package" {
56+
// record all transactions for builds
57+
1.
58+
} else {
59+
traces_sample_rate
60+
}
61+
};
62+
63+
Some(sentry::init((
64+
sentry_dsn,
65+
sentry::ClientOptions {
66+
release: Some(docs_rs_utils::BUILD_VERSION.into()),
67+
attach_stacktrace: true,
68+
traces_sampler: Some(Arc::new(traces_sampler)),
69+
..Default::default()
70+
}
71+
.add_integration(sentry_panic::PanicIntegration::default()),
72+
)))
73+
} else {
74+
tracing::subscriber::set_global_default(tracing_registry)?;
75+
None
76+
};
77+
78+
Ok(Guard { sentry_guard })
79+
}

src/bin/cratesfyi.rs

Lines changed: 3 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -12,84 +12,16 @@ use docs_rs::{
1212
},
1313
};
1414
use futures_util::StreamExt;
15-
use sentry::{
16-
TransactionContext, integrations::panic as sentry_panic,
17-
integrations::tracing as sentry_tracing,
18-
};
19-
use std::{env, fmt::Write, net::SocketAddr, path::PathBuf, str::FromStr, sync::Arc};
15+
use std::{env, fmt::Write, net::SocketAddr, path::PathBuf, sync::Arc};
2016
use tokio::runtime;
2117
use tracing_log::LogTracer;
22-
use tracing_subscriber::{EnvFilter, filter::Directive, prelude::*};
2318

2419
fn main() {
2520
// set the global log::logger for backwards compatibility
2621
// through rustwide.
2722
rustwide::logging::init_with(LogTracer::new());
2823

29-
let log_formatter = {
30-
let log_format = env::var("DOCSRS_LOG_FORMAT").unwrap_or_default();
31-
32-
if log_format == "json" {
33-
tracing_subscriber::fmt::layer().json().boxed()
34-
} else {
35-
tracing_subscriber::fmt::layer().boxed()
36-
}
37-
};
38-
39-
let tracing_registry = tracing_subscriber::registry().with(log_formatter).with(
40-
EnvFilter::builder()
41-
.with_default_directive(Directive::from_str("docs_rs=info").unwrap())
42-
.with_env_var("DOCSRS_LOG")
43-
.from_env_lossy(),
44-
);
45-
46-
let _sentry_guard = if let Ok(sentry_dsn) = env::var("SENTRY_DSN") {
47-
tracing::subscriber::set_global_default(tracing_registry.with(
48-
sentry_tracing::layer().event_filter(|md| {
49-
if md.fields().field("reported_to_sentry").is_some() {
50-
sentry_tracing::EventFilter::Ignore
51-
} else {
52-
sentry_tracing::default_event_filter(md)
53-
}
54-
}),
55-
))
56-
.unwrap();
57-
58-
let traces_sample_rate = env::var("SENTRY_TRACES_SAMPLE_RATE")
59-
.ok()
60-
.and_then(|v| v.parse().ok())
61-
.unwrap_or(0.0);
62-
63-
let traces_sampler = move |ctx: &TransactionContext| -> f32 {
64-
if let Some(sampled) = ctx.sampled() {
65-
// if the transaction was already marked as "to be sampled" by
66-
// the JS/frontend SDK, we want to sample it in the backend too.
67-
return if sampled { 1.0 } else { 0.0 };
68-
}
69-
70-
let op = ctx.operation();
71-
if op == "docbuilder.build_package" {
72-
// record all transactions for builds
73-
1.
74-
} else {
75-
traces_sample_rate
76-
}
77-
};
78-
79-
Some(sentry::init((
80-
sentry_dsn,
81-
sentry::ClientOptions {
82-
release: Some(docs_rs::BUILD_VERSION.into()),
83-
attach_stacktrace: true,
84-
traces_sampler: Some(Arc::new(traces_sampler)),
85-
..Default::default()
86-
}
87-
.add_integration(sentry_panic::PanicIntegration::default()),
88-
)))
89-
} else {
90-
tracing::subscriber::set_global_default(tracing_registry).unwrap();
91-
None
92-
};
24+
let guard = docs_rs_logging::init().expect("error initializing logging");
9325

9426
if let Err(err) = CommandLine::parse().handle_args() {
9527
let mut msg = format!("Error: {err}");
@@ -106,7 +38,7 @@ fn main() {
10638
// we need to drop the sentry guard here so all unsent
10739
// errors are sent to sentry before
10840
// process::exit kills everything.
109-
drop(_sentry_guard);
41+
drop(guard);
11042
std::process::exit(1);
11143
}
11244
}

0 commit comments

Comments
 (0)