Skip to content

Commit

Permalink
Bench optional span ref
Browse files Browse the repository at this point in the history
  • Loading branch information
jtescher committed Jul 19, 2023
1 parent 0b979a6 commit beabfcd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
7 changes: 7 additions & 0 deletions opentelemetry-api/src/trace/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ pub trait TraceContextExt {
/// ```
fn span(&self) -> SpanRef<'_>;

/// TODO
fn span_opt(&self) -> Option<SpanRef<'_>>;

/// Returns whether or not an active span has been set.
///
/// # Examples
Expand Down Expand Up @@ -273,6 +276,10 @@ impl TraceContextExt for Context {
}
}

fn span_opt(&self) -> Option<SpanRef<'_>> {
self.get::<SynchronizedSpan>().map(SpanRef)
}

fn has_active_span(&self) -> bool {
self.get::<SynchronizedSpan>().is_some()
}
Expand Down
24 changes: 11 additions & 13 deletions opentelemetry-sdk/benches/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,25 @@ fn benchmark_group(c: &mut Criterion, p: BenchmarkParameter) {

let mut group = c.benchmark_group("context");

group.bench_function(BenchmarkId::new("baseline current()", p), |b| {
group.bench_function(BenchmarkId::new("noop", p), |b| {
b.iter(|| {
black_box(Context::current());
black_box(Context::map_current(|cx| {
let span = cx.span();
drop(span)
}))
})
});

group.bench_function(BenchmarkId::new("current().has_active_span()", p), |b| {
group.bench_function(BenchmarkId::new("option", p), |b| {
b.iter(|| {
black_box(Context::current().has_active_span());
black_box(Context::map_current(|cx| {
if let Some(span) = cx.span_opt() {
drop(span)
}
}))
})
});

group.bench_function(
BenchmarkId::new("map_current(|cx| cx.has_active_span())", p),
|b| {
b.iter(|| {
black_box(Context::map_current(|cx| cx.has_active_span()));
})
},
);

group.finish();
}

Expand Down

0 comments on commit beabfcd

Please sign in to comment.