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