Skip to content

Commit ad4cc77

Browse files
apollo_infra: return ReloadHandle from configure_tracing
1 parent 8b2e64b commit ad4cc77

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed

crates/apollo_infra/src/trace_util.rs

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,35 @@ use tokio::sync::OnceCell;
33
use tracing::metadata::LevelFilter;
44
use tracing_subscriber::fmt::time::UtcTime;
55
use tracing_subscriber::prelude::*;
6-
use tracing_subscriber::{fmt, EnvFilter};
6+
use tracing_subscriber::{fmt, reload, EnvFilter};
7+
8+
// Crates we always keep at INFO regardless of operator-supplied spec.
9+
const QUIET_LIBS: &[&str] = &[
10+
"alloy_provider",
11+
"alloy_transport_http",
12+
"alloy_rpc_client",
13+
"futures-util",
14+
"hickory-proto",
15+
"hyper",
16+
"hyper_util",
17+
"h2",
18+
"libp2p",
19+
"libp2p-gossipsub",
20+
"multistream_select",
21+
"netlink_proto",
22+
"reqwest",
23+
"yamux",
24+
];
725

826
const DEFAULT_LEVEL: LevelFilter = LevelFilter::INFO;
9-
// Define a OnceCell to ensure the configuration is initialized only once
10-
static TRACING_INITIALIZED: OnceCell<()> = OnceCell::const_new();
27+
type ReloadHandle = reload::Handle<EnvFilter, tracing_subscriber::Registry>;
28+
29+
static TRACING_INITIALIZED: OnceCell<ReloadHandle> = OnceCell::const_new();
1130

1231
pub static PID: std::sync::LazyLock<u32> = std::sync::LazyLock::new(std::process::id);
1332

14-
pub async fn configure_tracing() {
15-
TRACING_INITIALIZED
33+
pub async fn configure_tracing() -> ReloadHandle {
34+
let reload_handle = TRACING_INITIALIZED
1635
.get_or_init(|| async {
1736
// Use default time formatting with sub-second precision limited to three digits.
1837
let time_format = format_description!(
@@ -29,31 +48,25 @@ pub async fn configure_tracing() {
2948
.with_line_number(true)
3049
.flatten_event(true);
3150

32-
let level_filter_layer = EnvFilter::builder()
33-
.with_default_directive(DEFAULT_LEVEL.into())
34-
.from_env_lossy()
35-
.add_directive("alloy_provider=info".parse().unwrap())
36-
.add_directive("alloy_transport_http=info".parse().unwrap())
37-
.add_directive("alloy_rpc_client=info".parse().unwrap())
38-
.add_directive("futures-util=info".parse().unwrap())
39-
.add_directive("hickory-proto=info".parse().unwrap())
40-
.add_directive("hyper=info".parse().unwrap())
41-
.add_directive("hyper_util=info".parse().unwrap())
42-
.add_directive("h2=info".parse().unwrap())
43-
.add_directive("libp2p=info".parse().unwrap())
44-
.add_directive("libp2p-gossipsub=info".parse().unwrap())
45-
.add_directive("multistream_select=info".parse().unwrap())
46-
.add_directive("netlink_proto=info".parse().unwrap())
47-
.add_directive("reqwest=info".parse().unwrap())
48-
.add_directive("yamux=info".parse().unwrap());
51+
let level_filter_layer = QUIET_LIBS.iter().fold(
52+
EnvFilter::builder().with_default_directive(DEFAULT_LEVEL.into()).from_env_lossy(),
53+
|layer, lib| layer.add_directive(format!("{lib}=info").parse().unwrap()),
54+
);
55+
56+
// Wrap the EnvFilter in a reloadable layer so that it can be updated at runtime.
57+
let (filtered_layer, reload_handle) = reload::Layer::new(level_filter_layer);
4958

5059
// This sets a single subscriber to all of the threads. We may want to implement
5160
// different subscriber for some threads and use set_global_default instead
5261
// of init.
53-
tracing_subscriber::registry().with(fmt_layer).with(level_filter_layer).init();
62+
tracing_subscriber::registry().with(filtered_layer).with(fmt_layer).init();
5463
tracing::info!("Tracing has been successfully initialized.");
64+
65+
reload_handle
5566
})
5667
.await;
68+
69+
reload_handle.clone()
5770
}
5871

5972
#[macro_export]

0 commit comments

Comments
 (0)