Skip to content
This repository has been archived by the owner on Nov 23, 2024. It is now read-only.

Commit

Permalink
🎉: Initial commit of example remote-tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
czy-29 committed Nov 21, 2024
1 parent afabc7b commit 545a746
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ serde = { version = "1.0.215", features = ["derive"] }
tracing-core = "0.1.32"

[dev-dependencies]
nu-ansi-term = "0.50.1"
rand = "0.8.5"
serde_json = "1.0.133"
tokio = { version = "1.4.11", features = ["full"] }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["chrono"] }

[build-dependencies]
version_check = "0.9.5"
130 changes: 130 additions & 0 deletions examples/remote-tracing/emit_expand.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
const CCC: &str = "ccc";

fn emit_span() -> tracing::Span {
let ccc = 333;

use tracing::__macro_support::Callsite as _;
static __CALLSITE: tracing::callsite::DefaultCallsite = {
static META: tracing::Metadata<'static> = {
tracing_core::metadata::Metadata::new(
"my-span",
"span-target",
tracing::Level::INFO,
Some("examples\\remote-tracing\\emit_macro.rs"),
Some(6u32),
Some("remote_tracing::emit_macro"),
tracing_core::field::FieldSet::new(
&["aaa", "bbb", CCC, "ddd"],
tracing_core::callsite::Identifier(&__CALLSITE),
),
tracing::metadata::Kind::SPAN,
)
};

tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = tracing::subscriber::Interest::never();

if tracing::Level::INFO <= tracing::level_filters::STATIC_MAX_LEVEL
&& tracing::Level::INFO <= tracing::level_filters::LevelFilter::current()
&& {
interest = __CALLSITE.interest();
!interest.is_never()
}
&& tracing::__macro_support::__is_enabled(__CALLSITE.metadata(), interest)
{
let meta = __CALLSITE.metadata();

tracing::Span::new(meta, &{
let mut iter = meta.fields().iter();

meta.fields().value_set(&[
(
&Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
Some(&111 as &dyn tracing::field::Value),
),
(
&Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
Some(&222 as &dyn tracing::field::Value),
),
(
&Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
Some(&tracing::field::debug(&ccc) as &dyn tracing::field::Value),
),
(
&Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
Some(&tracing::field::Empty as &dyn tracing::field::Value),
),
])
})
} else {
tracing::__macro_support::__disabled_span(__CALLSITE.metadata())
}
}

fn emit_event(myspan: tracing::Span) {
let ccc = 333;
let ddd = 444;

use tracing::__macro_support::Callsite as _;
static __CALLSITE: tracing::callsite::DefaultCallsite = {
static META: tracing::Metadata<'static> = {
tracing_core::metadata::Metadata::new(
"event examples\\remote-tracing\\emit_macro.rs:18",
"remote_tracing::emit_macro",
tracing::Level::INFO,
Some("examples\\remote-tracing\\emit_macro.rs"),
Some(18u32),
Some("remote_tracing::emit_macro"),
tracing_core::field::FieldSet::new(
&["message", "aaa", "bbb", CCC],
tracing_core::callsite::Identifier(&__CALLSITE),
),
tracing::metadata::Kind::EVENT,
)
};

tracing::callsite::DefaultCallsite::new(&META)
};
let enabled = tracing::Level::INFO <= tracing::level_filters::STATIC_MAX_LEVEL
&& tracing::Level::INFO <= tracing::level_filters::LevelFilter::current()
&& {
let interest = __CALLSITE.interest();
!interest.is_never()
&& tracing::__macro_support::__is_enabled(__CALLSITE.metadata(), interest)
};

if enabled {
(|value_set: tracing::field::ValueSet| {
let meta = __CALLSITE.metadata();
tracing::Event::child_of(&myspan, meta, &value_set);
})({
let mut iter = __CALLSITE.metadata().fields().iter();

__CALLSITE.metadata().fields().value_set(&[
(
&Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
Some(&format_args!("ddd = {0}", ddd) as &dyn tracing::field::Value),
),
(
&Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
Some(&111 as &dyn tracing::field::Value),
),
(
&Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
Some(&222 as &dyn tracing::field::Value),
),
(
&Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
Some(&tracing::field::debug(&ccc) as &dyn tracing::field::Value),
),
])
});
}
}

pub fn emit_tracing() {
let myspan = emit_span();
myspan.record("ddd", 444);
emit_event(myspan);
}
26 changes: 26 additions & 0 deletions examples/remote-tracing/emit_macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
pub fn emit_tracing() {
let ccc = 333;
const CCC: &str = "ccc";
let ddd = 444;

let myspan = tracing::span!(
target: "span-target",
tracing::Level::INFO,
"my-span",
aaa = 111,
"bbb" = 222,
{ CCC } = ?ccc,
ddd = tracing::field::Empty
);

myspan.record("ddd", 444);

tracing::event!(
parent: &myspan,
tracing::Level::INFO,
aaa = 111,
"bbb" = 222,
{ CCC } = ?ccc,
"ddd = {}", ddd
);
}
84 changes: 84 additions & 0 deletions examples/remote-tracing/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use std::fmt::Debug;
use tracing::{
span::{Attributes, Id, Record},
Event, Subscriber,
};
use tracing_subscriber::{
filter::LevelFilter,
fmt::{self, format::FmtSpan, time::ChronoLocal},
layer::{Context, SubscriberExt},
registry::LookupSpan,
util::SubscriberInitExt,
Layer,
};

mod emit_macro;
use emit_macro::emit_tracing;

struct DebugLayer;

impl<S> Layer<S> for DebugLayer
where
S: Subscriber + for<'a> LookupSpan<'a> + Debug,
{
fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, _ctx: Context<'_, S>) {
println!("on_new_span: {{\r\nattrs: {:#?},\r\nid: {:#?}}}", attrs, id);
}

fn on_record(&self, span: &Id, values: &Record<'_>, _ctx: Context<'_, S>) {
println!(
"on_record: {{\r\nspan: {:#?},\r\nvalues: {:#?}}}",
span, values
);
}

fn on_follows_from(&self, span: &Id, follows: &Id, _ctx: Context<'_, S>) {
println!(
"on_follows_from: {{\r\nspan: {:#?},\r\nfollows: {:#?}}}",
span, follows
);
}

fn on_event(&self, event: &Event<'_>, _ctx: Context<'_, S>) {
println!("on_event: {{\r\nevent: {:#?}}}", event);
}

fn on_enter(&self, id: &Id, _ctx: Context<'_, S>) {
println!("on_enter: {{\r\nid: {:#?}}}", id);
}

fn on_exit(&self, id: &Id, _ctx: Context<'_, S>) {
println!("on_exit: {{\r\nid: {:#?}}}", id);
}

fn on_close(&self, id: Id, _ctx: Context<'_, S>) {
println!("on_close: {{\r\nid: {:#?}}}", id);
}

fn on_id_change(&self, old: &Id, new: &Id, _ctx: Context<'_, S>) {
println!("on_id_change: {{\r\nold: {:#?},\r\nnew: {:#?}}}", old, new);
}
}

fn init_subscriber() {
// See: https://github.com/tokio-rs/tracing/issues/3068
#[cfg(windows)]
nu_ansi_term::enable_ansi_support().ok();

tracing_subscriber::registry()
.with(
fmt::layer()
.with_timer(ChronoLocal::new("%m-%d %H:%M:%S".into()))
.with_span_events(FmtSpan::FULL)
.with_thread_names(true)
.with_filter(LevelFilter::INFO),
)
.with(DebugLayer)
.init();
}

#[tokio::main]
async fn main() {
init_subscriber();
emit_tracing();
}

0 comments on commit 545a746

Please sign in to comment.