Skip to content

Commit d4f4976

Browse files
committed
Don't trace tasks spawned through the console server
The console subscriber's server generates a lot of async activity, introspecting or logging that drowns out the activity the program has outside of instrumentation. Set a thread-local subscriber that drops events coming from the server thread.
1 parent 9178ecf commit d4f4976

File tree

1 file changed

+66
-11
lines changed

1 file changed

+66
-11
lines changed

console-subscriber/src/builder.rs

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,59 @@ use tracing_subscriber::{
1414
registry::LookupSpan,
1515
};
1616

17+
/// A subscriber that discards everything
18+
///
19+
/// NoSubscriber seems like it would do that, but setting it for a
20+
/// single thread won't have the desired effect. The dispatch logic
21+
/// uses NoSubscriber (through Dispatcher::none()) as a default and as a
22+
/// sentinel value. When dispatching, it checks the thread-local
23+
/// subscriber using Any::is<NoSubscriber>, and has the global
24+
/// subscriber take over the thread-local subscriber if so.
25+
struct NoNoSubscriber(tracing_core::subscriber::NoSubscriber);
26+
27+
impl Default for NoNoSubscriber {
28+
fn default() -> Self {
29+
Self(Default::default())
30+
}
31+
}
32+
33+
impl tracing_core::subscriber::Subscriber for NoNoSubscriber {
34+
fn register_callsite(
35+
&self,
36+
metadata: &'static tracing::Metadata<'static>,
37+
) -> tracing_core::Interest {
38+
self.0.register_callsite(metadata)
39+
}
40+
41+
fn new_span(&self, span: &tracing_core::span::Attributes<'_>) -> tracing_core::span::Id {
42+
self.0.new_span(span)
43+
}
44+
45+
fn event(&self, event: &tracing::Event<'_>) {
46+
self.0.event(event)
47+
}
48+
49+
fn record(&self, span: &tracing_core::span::Id, values: &tracing_core::span::Record<'_>) {
50+
self.0.record(span, values)
51+
}
52+
53+
fn record_follows_from(&self, span: &tracing_core::span::Id, follows: &tracing_core::span::Id) {
54+
self.0.record_follows_from(span, follows)
55+
}
56+
57+
fn enabled(&self, metadata: &tracing::Metadata<'_>) -> bool {
58+
self.0.enabled(metadata)
59+
}
60+
61+
fn enter(&self, span: &tracing_core::span::Id) {
62+
self.0.enter(span)
63+
}
64+
65+
fn exit(&self, span: &tracing_core::span::Id) {
66+
self.0.exit(span)
67+
}
68+
}
69+
1770
/// Builder for configuring [`ConsoleLayer`]s.
1871
#[derive(Clone, Debug)]
1972
pub struct Builder {
@@ -376,17 +429,19 @@ impl Builder {
376429
thread::Builder::new()
377430
.name("console_subscriber".into())
378431
.spawn(move || {
379-
let runtime = runtime::Builder::new_current_thread()
380-
.enable_io()
381-
.enable_time()
382-
.build()
383-
.expect("console subscriber runtime initialization failed");
384-
385-
runtime.block_on(async move {
386-
server
387-
.serve()
388-
.await
389-
.expect("console subscriber server failed")
432+
tracing::subscriber::with_default(NoNoSubscriber::default(), || {
433+
let runtime = runtime::Builder::new_current_thread()
434+
.enable_io()
435+
.enable_time()
436+
.build()
437+
.expect("console subscriber runtime initialization failed");
438+
439+
runtime.block_on(async move {
440+
server
441+
.serve()
442+
.await
443+
.expect("console subscriber server failed")
444+
});
390445
});
391446
})
392447
.expect("console subscriber could not spawn thread");

0 commit comments

Comments
 (0)