Skip to content

Commit

Permalink
Don't trace tasks spawned through the console server
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
g2p committed Mar 24, 2022
1 parent 9178ecf commit baeca4f
Showing 1 changed file with 61 additions and 11 deletions.
72 changes: 61 additions & 11 deletions console-subscriber/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,54 @@ use tracing_subscriber::{
registry::LookupSpan,
};

/// A subscriber that discards everything
///
/// NoSubscriber seems like it would do that, but setting it for a
/// single thread won't have the desired effect. The dispatch logic
/// uses NoSubscriber (through Dispatcher::none()) as a default and as a
/// sentinel value. When dispatching, it checks the thread-local
/// subscriber using Any::is<NoSubscriber>, and has the global
/// subscriber take over the thread-local subscriber if so.
#[derive(Default)]
struct NoNoSubscriber(tracing_core::subscriber::NoSubscriber);

impl tracing_core::subscriber::Subscriber for NoNoSubscriber {
fn register_callsite(
&self,
metadata: &'static tracing::Metadata<'static>,
) -> tracing_core::Interest {
self.0.register_callsite(metadata)
}

fn new_span(&self, span: &tracing_core::span::Attributes<'_>) -> tracing_core::span::Id {
self.0.new_span(span)
}

fn event(&self, event: &tracing::Event<'_>) {
self.0.event(event)
}

fn record(&self, span: &tracing_core::span::Id, values: &tracing_core::span::Record<'_>) {
self.0.record(span, values)
}

fn record_follows_from(&self, span: &tracing_core::span::Id, follows: &tracing_core::span::Id) {
self.0.record_follows_from(span, follows)
}

fn enabled(&self, metadata: &tracing::Metadata<'_>) -> bool {
self.0.enabled(metadata)
}

fn enter(&self, span: &tracing_core::span::Id) {
self.0.enter(span)
}

fn exit(&self, span: &tracing_core::span::Id) {
self.0.exit(span)
}
}

/// Builder for configuring [`ConsoleLayer`]s.
#[derive(Clone, Debug)]
pub struct Builder {
Expand Down Expand Up @@ -376,17 +424,19 @@ impl Builder {
thread::Builder::new()
.name("console_subscriber".into())
.spawn(move || {
let runtime = runtime::Builder::new_current_thread()
.enable_io()
.enable_time()
.build()
.expect("console subscriber runtime initialization failed");

runtime.block_on(async move {
server
.serve()
.await
.expect("console subscriber server failed")
tracing::subscriber::with_default(NoNoSubscriber::default(), || {
let runtime = runtime::Builder::new_current_thread()
.enable_io()
.enable_time()
.build()
.expect("console subscriber runtime initialization failed");

runtime.block_on(async move {
server
.serve()
.await
.expect("console subscriber server failed")
});
});
})
.expect("console subscriber could not spawn thread");
Expand Down

0 comments on commit baeca4f

Please sign in to comment.