This repository has been archived on 2026-03-27. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
tracing/examples/examples/serde-yak-shave.rs
Hayden Stainsby bdbaf80073
examples: add note to examples that they are for tracing 0.2.0 (#3099)
It is not uncommon that users who are new to tracing look at the
examples in the `master` branch of the repository and find that they
don't compile. This is because they are examples which compile with the
code from the master branch, which is for the as yet unreleased tracing
0.2.0 ecosystem.

Users should instead go to the `v0.1.x` branch to find examples
compatible with the crates published on crates.io.

This change adds a doc-comment to the beginning of every example file
informing the user of this fact and suggesting that they check out the
`v0.1.x` branch instead.
2024-10-09 15:31:11 -04:00

105 lines
2.6 KiB
Rust

//! NOTE: This is pre-release documentation for the upcoming tracing 0.2.0 ecosystem. For the
//! release examples, please see the `v0.1.x` branch instead.
use std::sync::atomic::{AtomicUsize, Ordering};
use tracing::debug;
use tracing_core::{
collect::Collect,
event::Event,
metadata::Metadata,
span::{Attributes, Current, Id, Record},
};
use tracing_serde::AsSerde;
use serde_json::json;
#[path = "fmt/yak_shave.rs"]
mod yak_shave;
pub struct JsonCollector {
next_id: AtomicUsize, // you need to assign span IDs, so you need a counter
}
impl Collect for JsonCollector {
fn enabled(&self, metadata: &Metadata<'_>) -> bool {
let json = json!({
"enabled": {
"metadata": metadata.as_serde(),
}});
println!("{}", json);
true
}
fn new_span(&self, attrs: &Attributes<'_>) -> Id {
let id = self.next_id.fetch_add(1, Ordering::Relaxed);
let id = Id::from_u64(id as u64);
let json = json!({
"new_span": {
"attributes": attrs.as_serde(),
"id": id.as_serde(),
}});
println!("{}", json);
id
}
fn record(&self, span: &Id, values: &Record<'_>) {
let json = json!({
"record": {
"span": span.as_serde(),
"values": values.as_serde(),
}});
println!("{}", json);
}
fn record_follows_from(&self, span: &Id, follows: &Id) {
let json = json!({
"record_follows_from": {
"span": span.as_serde(),
"follows": follows.as_serde(),
}});
println!("{}", json);
}
fn event(&self, event: &Event<'_>) {
let json = json!({
"event": event.as_serde(),
});
println!("{}", json);
}
fn enter(&self, span: &Id) {
let json = json!({
"enter": span.as_serde(),
});
println!("{}", json);
}
fn exit(&self, span: &Id) {
let json = json!({
"exit": span.as_serde(),
});
println!("{}", json);
}
fn current_span(&self) -> Current {
Current::unknown()
}
}
fn main() {
let collector = JsonCollector {
next_id: AtomicUsize::new(1),
};
tracing::collect::with_default(collector, || {
let number_of_yaks = 3;
debug!("preparing to shave {} yaks", number_of_yaks);
let number_shaved = yak_shave::shave_all(number_of_yaks);
debug!(
message = "yak shaving completed.",
all_yaks_shaved = number_shaved == number_of_yaks,
);
});
}