opentelemetry: disabled tracked inactivity perf (#1315)

## Motivation

Optional inactivity tracking should have minimal overhead when disabled.

## Solution

Check to see if inactivity tracking is enabled before doing more
expensive operations. Performance improvement of ~6% using current
benchmarks.

* Address comments and switch to i64 timing values

Authored-by: Julian Tescher
Co-authored-by: Eliza Weisman <eliza@buoyant.io>
This commit is contained in:
Julian Tescher 2021-03-30 09:47:40 -07:00 committed by Eliza Weisman
parent bab71acb28
commit 151255691b

View File

@ -425,23 +425,31 @@ where
}
fn on_enter(&self, id: &span::Id, ctx: Context<'_, S>) {
if !self.tracked_inactivity {
return;
}
let span = ctx.span(id).expect("Span not found, this is a bug");
let mut extensions = span.extensions_mut();
if let Some(timings) = extensions.get_mut::<Timings>() {
let now = Instant::now();
timings.idle += (now - timings.last).as_nanos() as u64;
timings.idle += (now - timings.last).as_nanos() as i64;
timings.last = now;
}
}
fn on_exit(&self, id: &span::Id, ctx: Context<'_, S>) {
if !self.tracked_inactivity {
return;
}
let span = ctx.span(id).expect("Span not found, this is a bug");
let mut extensions = span.extensions_mut();
if let Some(timings) = extensions.get_mut::<Timings>() {
let now = Instant::now();
timings.busy += (now - timings.last).as_nanos() as u64;
timings.busy += (now - timings.last).as_nanos() as i64;
timings.last = now;
}
}
@ -537,16 +545,18 @@ where
let span = ctx.span(&id).expect("Span not found, this is a bug");
let mut extensions = span.extensions_mut();
if let Some(mut builder) = extensions.remove::<otel::SpanBuilder>() {
// Append busy/idle timings when enabled.
if let Some(timings) = extensions.get_mut::<Timings>() {
let mut timings_attributes = vec![
KeyValue::new("busy_ns", timings.busy.to_string()),
KeyValue::new("idle_ns", timings.idle.to_string()),
];
if self.tracked_inactivity {
// Append busy/idle timings when enabled.
if let Some(timings) = extensions.get_mut::<Timings>() {
let busy_ns = KeyValue::new("busy_ns", timings.busy);
let idle_ns = KeyValue::new("idle_ns", timings.idle);
match builder.attributes {
Some(ref mut attributes) => attributes.append(&mut timings_attributes),
None => builder.attributes = Some(timings_attributes),
if let Some(ref mut attributes) = builder.attributes {
attributes.push(busy_ns);
attributes.push(idle_ns);
} else {
builder.attributes = Some(vec![busy_ns, idle_ns]);
}
}
}
@ -569,8 +579,8 @@ where
}
struct Timings {
idle: u64,
busy: u64,
idle: i64,
busy: i64,
last: Instant,
}