mirror of
https://github.com/tokio-rs/tracing.git
synced 2026-03-31 03:39:04 +00:00
## Motivation Currently, most crates in this repository have `examples` directories that contain demonstrations of how to use that crate. In many cases, these examples also use APIs from _other_ crates in this repository --- for example, the `tracing-subscriber` `FmtSubscriber` is used by many examples to format the output generated by instrumentation APIs, so that users can see the generated output. The disadvantage to this approach is that it requires crates in this repository to have dev-dependencies on each other. This is problematic for a few reasons: * It makes it easy to inadvertently create cyclic dependencies between `tracing` crates. * When building a crate that depends on a crate with dev-dependencies, those dev-dependencies are downloaded and compiled, despite not being linked into the dependent crate. This means that users depending on a tracing crate will pull in a larger dependency tree, since the dev-dependencies used by examples are also downloaded, although they are not required by the dependent. This makes the tracing crate appear to be a much larger dependency than it actually is, and can make users' build times slower. * Finally, it means that the examples are scattered throughout the repository. In some cases, the examples demonstrate the functionality of multiple crates, but only live in one crate's `examples` dir. This can hurt the discoverability of the examples, especially for new users. ## Solution This branch moves all the examples out of individual crates and into a new `examples` crate (which is otherwise empty and cannot be published to crates.io). The `examples` crate is part of the root `tracing` workspace, so it's still possible to `cargo run --example WHATEVER`. Similarly, `cargo test --all` will still build the examples. All dev-dependencies which were only required by examples and not tests have been removed, which has a drastic impact on the dependency footprint of some crates. Finally, I've added a README to the `examples` crate, listing the available examples and what functionality they demonstrate. Refs: #315, #308 Signed-off-by: Eliza Weisman <eliza@buoyant.io>
144 lines
3.8 KiB
Rust
144 lines
3.8 KiB
Rust
#![deny(rust_2018_idioms)]
|
|
|
|
use tracing::{
|
|
field::{Field, Visit},
|
|
info, span,
|
|
subscriber::{self, Subscriber},
|
|
warn, Event, Id, Level, Metadata,
|
|
};
|
|
|
|
use std::{
|
|
collections::HashMap,
|
|
fmt,
|
|
sync::{
|
|
atomic::{AtomicUsize, Ordering},
|
|
Arc, RwLock, RwLockReadGuard,
|
|
},
|
|
};
|
|
|
|
#[derive(Clone)]
|
|
struct Counters(Arc<RwLock<HashMap<String, AtomicUsize>>>);
|
|
|
|
struct CounterSubscriber {
|
|
ids: AtomicUsize,
|
|
counters: Counters,
|
|
}
|
|
|
|
struct Count<'a> {
|
|
counters: RwLockReadGuard<'a, HashMap<String, AtomicUsize>>,
|
|
}
|
|
|
|
impl<'a> Visit for Count<'a> {
|
|
fn record_i64(&mut self, field: &Field, value: i64) {
|
|
if let Some(counter) = self.counters.get(field.name()) {
|
|
if value > 0 {
|
|
counter.fetch_add(value as usize, Ordering::Release);
|
|
} else {
|
|
counter.fetch_sub((value * -1) as usize, Ordering::Release);
|
|
}
|
|
};
|
|
}
|
|
|
|
fn record_u64(&mut self, field: &Field, value: u64) {
|
|
if let Some(counter) = self.counters.get(field.name()) {
|
|
counter.fetch_add(value as usize, Ordering::Release);
|
|
};
|
|
}
|
|
|
|
fn record_bool(&mut self, _: &Field, _: bool) {}
|
|
fn record_str(&mut self, _: &Field, _: &str) {}
|
|
fn record_debug(&mut self, _: &Field, _: &dyn fmt::Debug) {}
|
|
}
|
|
|
|
impl CounterSubscriber {
|
|
fn visitor(&self) -> Count<'_> {
|
|
Count {
|
|
counters: self.counters.0.read().unwrap(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Subscriber for CounterSubscriber {
|
|
fn register_callsite(&self, meta: &Metadata<'_>) -> subscriber::Interest {
|
|
let mut interest = subscriber::Interest::never();
|
|
for key in meta.fields() {
|
|
let name = key.name();
|
|
if name.contains("count") {
|
|
self.counters
|
|
.0
|
|
.write()
|
|
.unwrap()
|
|
.entry(name.to_owned())
|
|
.or_insert_with(|| AtomicUsize::new(0));
|
|
interest = subscriber::Interest::always();
|
|
}
|
|
}
|
|
interest
|
|
}
|
|
|
|
fn new_span(&self, new_span: &span::Attributes<'_>) -> Id {
|
|
new_span.record(&mut self.visitor());
|
|
let id = self.ids.fetch_add(1, Ordering::SeqCst);
|
|
Id::from_u64(id as u64)
|
|
}
|
|
|
|
fn record_follows_from(&self, _span: &Id, _follows: &Id) {
|
|
// unimplemented
|
|
}
|
|
|
|
fn record(&self, _: &Id, values: &span::Record<'_>) {
|
|
values.record(&mut self.visitor())
|
|
}
|
|
|
|
fn event(&self, event: &Event<'_>) {
|
|
event.record(&mut self.visitor())
|
|
}
|
|
|
|
fn enabled(&self, metadata: &Metadata<'_>) -> bool {
|
|
metadata.fields().iter().any(|f| f.name().contains("count"))
|
|
}
|
|
|
|
fn enter(&self, _span: &Id) {}
|
|
fn exit(&self, _span: &Id) {}
|
|
}
|
|
|
|
impl Counters {
|
|
fn print_counters(&self) {
|
|
for (k, v) in self.0.read().unwrap().iter() {
|
|
println!("{}: {}", k, v.load(Ordering::Acquire));
|
|
}
|
|
}
|
|
|
|
fn new() -> (Self, CounterSubscriber) {
|
|
let counters = Counters(Arc::new(RwLock::new(HashMap::new())));
|
|
let subscriber = CounterSubscriber {
|
|
ids: AtomicUsize::new(1),
|
|
counters: counters.clone(),
|
|
};
|
|
(counters, subscriber)
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let (counters, subscriber) = Counters::new();
|
|
|
|
tracing::subscriber::set_global_default(subscriber).unwrap();
|
|
|
|
let mut foo: u64 = 2;
|
|
span!(Level::TRACE, "my_great_span", foo_count = &foo).in_scope(|| {
|
|
foo += 1;
|
|
info!(yak_shaved = true, yak_count = 1, "hi from inside my span");
|
|
span!(
|
|
Level::TRACE,
|
|
"my other span",
|
|
foo_count = &foo,
|
|
baz_count = 5
|
|
)
|
|
.in_scope(|| {
|
|
warn!(yak_shaved = false, yak_count = -1, "failed to shave yak");
|
|
});
|
|
});
|
|
|
|
counters.print_counters();
|
|
}
|