Skip to content

Commit 296818b

Browse files
committed
WIP
0 parents  commit 296818b

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+
Cargo.lock

Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "tracing-tree"
3+
version = "0.1.0"
4+
authors = ["David Barsky <me@davidbarsky.com>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
tracing = "0.1.11"
11+
tracing-subscriber = { git = "https://github.com/tokio-rs/tracing" }
12+
quanta = "0.3.1"
13+
termcolor = "1.0.5"

examples/basic.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use tracing::{info, instrument, span, Level};
2+
use tracing_subscriber::layer::SubscriberExt;
3+
use tracing_subscriber::{registry::Registry, Layer};
4+
use tracing_tree::TreeLayer;
5+
6+
fn main() {
7+
let subscriber = Registry::default().with(TreeLayer::default());
8+
tracing::subscriber::set_global_default(subscriber).expect("Unable to set a global default");
9+
call_a("david");
10+
}
11+
12+
#[instrument]
13+
fn call_a(name: &str) {
14+
info!(name, "got a name");
15+
call_b(name)
16+
}
17+
18+
#[instrument]
19+
fn call_b(name: &str) {
20+
info!(name, "got a name");
21+
}

src/lib.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use quanta::Clock;
2+
use std::fmt::Debug;
3+
use tracing::{
4+
span::{Attributes, Id, Record},
5+
Event, Level, Metadata, Subscriber,
6+
};
7+
use tracing_subscriber::{
8+
layer::{Context, Layer},
9+
registry::LookupSpan,
10+
};
11+
12+
#[derive(Debug, Default)]
13+
pub struct TreeLayer {
14+
clock: Clock,
15+
}
16+
17+
#[derive(Debug)]
18+
struct Data {
19+
start: u64,
20+
level: Level,
21+
}
22+
23+
impl<S> Layer<S> for TreeLayer
24+
where
25+
S: Subscriber + for<'span> LookupSpan<'span> + Debug,
26+
{
27+
fn new_span(&self, attrs: &Attributes, id: &Id, ctx: Context<S>) {
28+
let start = self.clock.now();
29+
let level = attrs.metadata().level();
30+
dbg!(attrs);
31+
let data = Data {
32+
start,
33+
level: level.clone(),
34+
};
35+
let span = ctx.span(id).expect("in new_span but span does not exist");
36+
span.extensions_mut().insert(data);
37+
}
38+
39+
fn on_event(&self, event: &Event<'_>, _ctx: Context<S>) {
40+
dbg!(event);
41+
}
42+
43+
fn on_close(&self, id: Id, ctx: Context<S>) {
44+
let end = self.clock.now();
45+
let span = ctx.span(&id).expect("in on_close but span does not exist");
46+
let mut ext = span.extensions_mut();
47+
let data = ext
48+
.get_mut::<Data>()
49+
.expect("span does not have metric data");
50+
51+
let elapsed = self.clock.delta(data.start, end);
52+
}
53+
}

0 commit comments

Comments
 (0)