Skip to content

Commit 42a8afe

Browse files
committed
Don't trace tasks spawned through the console subscriber by default
Enabling the console subscriber starts a server that generates a lot of async activity. Introspecting or logging that can drown out the activity the program has outside of instrumentation. Provide a new builder method, enable_self_trace, to control whether to also trace events that come with enabling the tokio console. If self trace is not enabled, set a thread-local subscriber that drops events coming from the server thread. Raise the requirement on tracing-core so that setting a per-thread NoSubscriber is effective.
1 parent 83d8a87 commit 42a8afe

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

console-subscriber/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ tokio-stream = "0.1"
3636
thread_local = "1.1.3"
3737
console-api = { version = "0.2.0", path = "../console-api", features = ["transport"] }
3838
tonic = { version = "0.7", features = ["transport"] }
39-
tracing-core = "0.1.18"
39+
tracing-core = "0.1.24"
4040
tracing = "0.1.26"
4141
tracing-subscriber = { version = "0.3.0", default-features = false, features = ["fmt", "registry"] }
4242
futures = { version = "0.3", default-features = false }
@@ -58,4 +58,4 @@ futures = "0.3"
5858

5959
[package.metadata.docs.rs]
6060
all-features = true
61-
rustdoc-args = ["--cfg", "docsrs"]
61+
rustdoc-args = ["--cfg", "docsrs"]

console-subscriber/src/builder.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ pub struct Builder {
3939

4040
/// The filter environment variable to use for `tracing` events.
4141
pub(super) filter_env_var: String,
42+
43+
/// Whether to trace events coming from the subscriber thread
44+
self_trace: bool,
4245
}
4346

4447
impl Default for Builder {
@@ -51,6 +54,7 @@ impl Default for Builder {
5154
server_addr: SocketAddr::new(Server::DEFAULT_IP, Server::DEFAULT_PORT),
5255
recording_path: None,
5356
filter_env_var: "RUST_LOG".to_string(),
57+
self_trace: false,
5458
}
5559
}
5660
}
@@ -176,6 +180,13 @@ impl Builder {
176180
}
177181
}
178182

183+
/// Sets whether we are are tracing events coming from the console subscriber
184+
///
185+
/// The default is to drop events coming from the console subscriber thread.
186+
pub fn enable_self_trace(self, self_trace: bool) -> Self {
187+
Self { self_trace, ..self }
188+
}
189+
179190
/// Completes the builder, returning a [`ConsoleLayer`] and [`Server`] task.
180191
pub fn build(self) -> (ConsoleLayer, Server) {
181192
ConsoleLayer::build(self)
@@ -368,6 +379,8 @@ impl Builder {
368379
meta.name().starts_with("runtime.") || meta.target().starts_with("tokio")
369380
}
370381

382+
let self_trace = self.self_trace;
383+
371384
let (layer, server) = self.build();
372385
let filter =
373386
FilterFn::new(console_filter as for<'r, 's> fn(&'r tracing::Metadata<'s>) -> bool);
@@ -376,6 +389,12 @@ impl Builder {
376389
thread::Builder::new()
377390
.name("console_subscriber".into())
378391
.spawn(move || {
392+
let _subscriber_guard;
393+
if !self_trace {
394+
_subscriber_guard = tracing::subscriber::set_default(
395+
tracing_core::subscriber::NoSubscriber::default(),
396+
);
397+
}
379398
let runtime = runtime::Builder::new_current_thread()
380399
.enable_io()
381400
.enable_time()

0 commit comments

Comments
 (0)