1 unstable release
new 0.1.0 | Mar 24, 2025 |
---|
#1044 in Debugging
22KB
360 lines
fastrace-tracing
A compatibility layer that connects toiok-tracing with the fastrace tracing library.
Overview
fastrace-tracing
allows you to capture spans and events from libraries that use tokio-tracing
and forward them to fastrace
. This is particularly useful when:
- You're using
fastrace
in your application but depend on libraries instrumented withtokio-tracing
- You want to migrate from
tokio-tracing
tofastrace
incrementally
Getting Started
Add fastrace-tracing
to your project:
[dependencies]
fastrace = { version = "0.7", features = ["enable"] }
fastrace-tracing = "0.1"
tracing = "0.1"
tracing-subscriber = "0.3"
Set up the compatibility layer:
use fastrace::collector::{Config, ConsoleReporter};
use fastrace::prelude::*;
use tracing_subscriber::layer::SubscriberExt;
// Initialize fastrace.
fastrace::set_reporter(ConsoleReporter, Config::default());
// Set up tokio-tracing with the fastrace compatibility layer.
let subscriber = tracing_subscriber::Registry::default()
.with(fastrace_tracing::FastraceCompatLayer::new());
tracing::subscriber::set_global_default(subscriber).unwrap();
{
// Create a fastrace root span.
let root = Span::root("my-application", SpanContext::random());
// Set a fastrace span as the local parent - this is critical for connecting the
// tokio-tracing spans with the fastrace span.
let _guard = root.set_local_parent();
// Spans from tokio-tracing will be captured by fastrace.
let span = tracing::span!(tracing::Level::INFO, "my_operation");
let _enter = span.enter();
// Events from tokio-tracing will also be captured by fastrace.
tracing::info!("This will be captured by fastrace");
}
// Don't forget to flush before your application exits.
fastrace::flush();
Examples
Check out the examples directory for more detailed usage examples.
License
This project is licensed under the Apache-2.0 license.
Dependencies
~3.5–9MB
~78K SLoC