Trait tracing::instrument::WithCollector
source · pub trait WithCollector: Sized {
// Provided methods
fn with_collector<C>(self, collector: C) -> WithDispatch<Self> ⓘ
where C: Into<Dispatch> { ... }
fn with_current_collector(self) -> WithDispatch<Self> ⓘ { ... }
}
Available on crate feature
std
only.Expand description
Extension trait allowing futures to be instrumented with
a tracing
collector.
Provided Methods§
sourcefn with_collector<C>(self, collector: C) -> WithDispatch<Self> ⓘ
fn with_collector<C>(self, collector: C) -> WithDispatch<Self> ⓘ
Attaches the provided collector to this type, returning a
WithDispatch
wrapper.
The attached collector will be set as the default when the returned
Future
is polled.
§Examples
use tracing::instrument::WithCollector;
// Set the default collector
let _default = tracing::collect::set_default(MyCollector::new());
tracing::info!("this event will be recorded by the default collector");
// Create a different collector and attach it to a future.
let other_collector = MyCollector::new();
let future = async {
tracing::info!("this event will be recorded by the other collector");
// ...
};
future
// Attach the other collector to the future before awaiting it
.with_collector(other_collector)
.await;
// Once the future has completed, we return to the default collector.
tracing::info!("this event will be recorded by the default collector");
sourcefn with_current_collector(self) -> WithDispatch<Self> ⓘ
fn with_current_collector(self) -> WithDispatch<Self> ⓘ
Attaches the current default collector to this type, returning a
WithDispatch
wrapper.
The attached collector will be set as the default when the returned
Future
is polled.
This can be used to propagate the current dispatcher context when spawning a new future that may run on a different thread.
§Examples
use tracing::instrument::WithCollector;
// Using `set_default` (rather than `set_global_default`) sets the
// default collector for *this* thread only.
let _default = tracing::collect::set_default(MyCollector::new());
let future = async {
// ...
};
// If a multi-threaded async runtime is in use, this spawned task may
// run on a different thread, in a different default collector's context.
tokio::spawn(future);
// However, calling `with_current_collector` on the future before
// spawning it, ensures that the current thread's default collector is
// propagated to the spawned task, regardless of where it executes:
tokio::spawn(future.with_current_collector());
Object Safety§
This trait is not object safe.