Note – I have gone through Suppressing external library logs in a tokio tracing subscriber and How to turn off tracing events emitted by other crates?, and they don’t answer this question.
Context:
I’m creating a Tokio tracing subscriber that captures logs from tracing instrumented libraries and exports them to a backend service using the hyper crate. However, hyper
is also instrumented with Tokio tracing, which causes an infinite logging loop because the logs generated by hyper
are captured and exported by my subscriber.
Here’s a simplified version of my code:
use pin_project::pin_project;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use tracing::info;
use tracing::Event;
use tracing::Metadata;
use tracing::Subscriber;
use tracing_core::span::Id;
use tracing_core::span::Record;
// Define a task-local variable for suppression
tokio::task_local! {
static SUPPRESSED: bool;
}
struct SimpleSubscriber;
impl Subscriber for SimpleSubscriber {
fn enabled(&self, _metadata: &Metadata<'_>) -> bool {
!is_logging_suppressed()
}
fn new_span(&self, _: &tracing::span::Attributes<'_>) -> Id {
Id::from_u64(10)
}
fn event(&self, event: &Event<'_>) {
if is_logging_suppressed() {
return;
}
// Extract the required metadata before moving into the async context
let target = event.metadata().target().to_string();
let name = event.metadata().name().to_string();
let level = event.metadata().level().clone();
let suppressed_future = SUPPRESSED.scope(true, async move {
// the actual logging event, the hyper tracing event generated here should be suppressed
hyper_wrapper::make_hyper_call("mock_upload_event", &target, &name, &level).await;
});
let suppress_logging_future = SuppressLogging {
inner: suppressed_future,
};
// Use tokio::spawn instead of spawn_local
tokio::task::spawn(suppress_logging_future);
}
fn record(&self, _: &Id, _: &Record<'_>) {}
fn record_follows_from(&self, _: &Id, _: &Id) {}
fn enter(&self, _: &Id) {}
fn exit(&self, _: &Id) {}
}
#[pin_project]
struct SuppressLogging<F> {
#[pin]
inner: F,
}
impl<F: Future> Future for SuppressLogging<F> {
type Output = F::Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
this.inner.poll(cx)
}
}
fn is_logging_suppressed() -> bool {
SUPPRESSED.try_with(|val| *val).unwrap_or(false)
}
#[tokio::main]
async fn main() {
let subscriber = SimpleSubscriber;
tracing::subscriber::set_global_default(subscriber)
.expect("Setting default subscriber failed.");
// Make a small Hyper call in the main method
lib::some_func().await;
}
// Define the module within the same file
mod lib {
use tracing::info;
use tracing_core::Level;
pub async fn some_func() {
// this should be logged
info!(name: "this-is-also-logged", "test");
// the tracing event generated by hyper crate should not be suppressed.
super::hyper_wrapper::make_hyper_call("main method", "main", "startup", &Level::INFO).await;
}
}
mod hyper_wrapper {
use hyper::Client;
use hyper::Uri;
use tracing_core::Level;
pub async fn make_hyper_call(context: &str, target: &str, name: &str, level: &Level) {
let client = Client::new();
let uri = "http://www.google.com".parse::<Uri>().unwrap();
let response = client.get(uri).await;
match response {
Ok(_response) => {
//let _body = hyper::body::to_bytes(response).await.unwrap();
println!(
"{} - [{}] - {} - {} ",
context,
level,
target,
name,
);
}
Err(e) => {
println!("{} - [{}] - {} - {} - Error: {:?}", context, level, target, name, e);
}
}
}
}
Problem Statement:
- I am creating a Tokio tracing subscriber, which gets the logs from the tokio-tracing instrumented libraries, and exports them to some backend service using (say)
hyper
. - Now the
hyper
crate is also instrumented with Tokio tracing, which results in the logs from this crate being received by the event() method of my subscriber and getting exported. This results in an infinite logging loop. - While I can try to create a filter to remove all the logs from the hyper crate, this crate could also be used by applications using my crate or the instrumented libraries used by this application. The application user/developer may not want the logs from the usage of
hyper
within their application to be filtered. - So, I want to filter only the logs generated by the
hyper
crate from within my subscriber implementation.
I tried usingtokio::task_local
to set the suppression flag within my subscriber, and then use that to decide whether to export the logs or not. However, it seems that task_local won’t be propagated across multiple async calls made within the hyper crate.
Question:
What is the best way to achieve this? How can I filter out the logs generated by the hyper
crate within my subscriber implementation without affecting the logs generated by the hyper
crate in other parts of the application?
Please note that hyper
is just one of those crates. My Subscriber would be using other crates like reqwest
and tonic
and it would be same issue with them.