Skip to content

Commit 1b3f7fd

Browse files
authored
fix(layer): correctly track entered and exited state for timings (#212)
## Motivation The timings were not properly tracked when multiple enters or exits occurred out of order. Closes #123 ## Solution Count how many times the span has been entered and exited. This copies what `tracing-subscriber` does in its `fmt::Layer`.
1 parent c5631d7 commit 1b3f7fd

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/layer.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -944,9 +944,12 @@ where
944944
let mut extensions = span.extensions_mut();
945945

946946
if let Some(timings) = extensions.get_mut::<Timings>() {
947-
let now = Instant::now();
948-
timings.idle += (now - timings.last).as_nanos() as i64;
949-
timings.last = now;
947+
if timings.entered_count == 0 {
948+
let now = Instant::now();
949+
timings.idle += (now - timings.last).as_nanos() as i64;
950+
timings.last = now;
951+
}
952+
timings.entered_count += 1;
950953
}
951954
}
952955

@@ -963,9 +966,12 @@ where
963966
}
964967

965968
if let Some(timings) = extensions.get_mut::<Timings>() {
966-
let now = Instant::now();
967-
timings.busy += (now - timings.last).as_nanos() as i64;
968-
timings.last = now;
969+
timings.entered_count -= 1;
970+
if timings.entered_count == 0 {
971+
let now = Instant::now();
972+
timings.busy += (now - timings.last).as_nanos() as i64;
973+
timings.last = now;
974+
}
969975
}
970976
}
971977

@@ -1192,6 +1198,7 @@ struct Timings {
11921198
idle: i64,
11931199
busy: i64,
11941200
last: Instant,
1201+
entered_count: u64,
11951202
}
11961203

11971204
impl Timings {
@@ -1200,6 +1207,7 @@ impl Timings {
12001207
idle: 0,
12011208
busy: 0,
12021209
last: Instant::now(),
1210+
entered_count: 0,
12031211
}
12041212
}
12051213
}

0 commit comments

Comments
 (0)