Skip to content

Commit e9ca158

Browse files
authored
perf: Compare different span creation scenarios (#3079)
1 parent 643c645 commit e9ca158

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed

opentelemetry-sdk/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ required-features = ["experimental_metrics_custom_reader"]
8585
name = "trace"
8686
harness = false
8787

88+
[[bench]]
89+
name = "span"
90+
harness = false
91+
8892
[[bench]]
8993
name = "log_processor"
9094
harness = false

opentelemetry-sdk/benches/span.rs

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/*
2+
Span Creation scenarios.
3+
This benchmark measures the performance cost of different
4+
span creation patterns in OpenTelemetry when using On/Off
5+
sampling strategies.
6+
7+
TODO: Cover the impact of the presence of ActiveSpan in the context.
8+
9+
The benchmark results:
10+
criterion = "0.5.1"
11+
rustc 1.83.0 (90b35a623 2024-11-26)
12+
Hardware: M4Pro
13+
| Test | Always Sample | Never Sample |
14+
|-------------------------------------------------------|---------------|--------------|
15+
| span-creation-simple | 236.38 ns | 77.155 ns |
16+
| span-creation-span-builder | 234.48 ns | 109.22 ns |
17+
| span-creation-tracer-in-span | 417.24 ns | 221.93 ns |
18+
| span-creation-simple-context-activation | 408.40 ns | 59.426 ns |
19+
| span-creation-span-builder-context-activation | 414.39 ns | 90.575 ns |
20+
*/
21+
22+
use criterion::{criterion_group, criterion_main, Criterion};
23+
use opentelemetry::{
24+
trace::{mark_span_as_active, Span, TraceContextExt, Tracer, TracerProvider},
25+
Context, KeyValue,
26+
};
27+
use opentelemetry_sdk::{
28+
error::OTelSdkResult,
29+
trace::{self as sdktrace, SpanData, SpanExporter},
30+
};
31+
#[cfg(not(target_os = "windows"))]
32+
use pprof::criterion::{Output, PProfProfiler};
33+
34+
fn criterion_benchmark(c: &mut Criterion) {
35+
trace_benchmark_group(c, "span-creation-simple", |tracer| {
36+
// Simple span creation
37+
// There is not ability to specify anything other than the name.
38+
// Attributes are set after creation, and automatically gets
39+
// ignored if sampling is unfavorable.
40+
let mut span = tracer.start("span-name");
41+
span.set_attribute(KeyValue::new("key1", false));
42+
span.set_attribute(KeyValue::new("key2", "hello"));
43+
span.set_attribute(KeyValue::new("key3", 123.456));
44+
span.set_attribute(KeyValue::new("key4", "world"));
45+
span.set_attribute(KeyValue::new("key5", 123));
46+
span.end();
47+
});
48+
49+
trace_benchmark_group(c, "span-creation-span-builder", |tracer| {
50+
// This is similar to the simple span creation, but allows
51+
// attributes and other properties to be set during creation.
52+
// It is slightly slower than the simple span creation due to the fact that
53+
// attributes are collected into a vec! and allocated, even before sampling
54+
// decision is made.
55+
let mut span = tracer
56+
.span_builder("span-name")
57+
.with_attributes([
58+
KeyValue::new("key1", false),
59+
KeyValue::new("key2", "hello"),
60+
KeyValue::new("key3", 123.456),
61+
])
62+
.start(tracer);
63+
span.set_attribute(KeyValue::new("key4", "world"));
64+
span.set_attribute(KeyValue::new("key5", 123));
65+
span.end();
66+
});
67+
68+
trace_benchmark_group(c, "span-creation-tracer-in-span", |tracer| {
69+
// This is similar to the simple span creation, but also does the job of activating
70+
// the span in the current context.
71+
// It is slower than other approaches of activation due to the fact that
72+
// context activation is done, irrespective of sampling decision.
73+
tracer.in_span("span-name", |ctx| {
74+
let span = ctx.span();
75+
span.set_attribute(KeyValue::new("key1", false));
76+
span.set_attribute(KeyValue::new("key2", "hello"));
77+
span.set_attribute(KeyValue::new("key3", 123.456));
78+
span.set_attribute(KeyValue::new("key4", "world"));
79+
span.set_attribute(KeyValue::new("key5", 123));
80+
});
81+
});
82+
83+
trace_benchmark_group(c, "span-creation-simple-context-activation", |tracer| {
84+
// This optimizes by bypassing the context activation
85+
// based on sampling decision, and hence it is faster than the
86+
// tracer.in_span approach.
87+
let mut span = tracer.start("span-name");
88+
span.set_attribute(KeyValue::new("key1", false));
89+
span.set_attribute(KeyValue::new("key2", "hello"));
90+
span.set_attribute(KeyValue::new("key3", 123.456));
91+
if span.is_recording() {
92+
let _guard = mark_span_as_active(span);
93+
Context::map_current(|cx| {
94+
let span_from_context = cx.span();
95+
span_from_context.set_attribute(KeyValue::new("key4", "world"));
96+
span_from_context.set_attribute(KeyValue::new("key5", 123));
97+
});
98+
}
99+
});
100+
101+
trace_benchmark_group(
102+
c,
103+
"span-creation-span-builder-context-activation",
104+
|tracer| {
105+
// This optimizes by bypassing the context activation
106+
// based on sampling decision, and hence it is faster than the
107+
// tracer.in_span approach.
108+
let span = tracer
109+
.span_builder("span-name")
110+
.with_attributes([
111+
KeyValue::new("key1", false),
112+
KeyValue::new("key2", "hello"),
113+
KeyValue::new("key3", 123.456),
114+
])
115+
.start(tracer);
116+
if span.is_recording() {
117+
let _guard = mark_span_as_active(span);
118+
Context::map_current(|cx| {
119+
let span_from_context = cx.span();
120+
span_from_context.set_attribute(KeyValue::new("key4", "world"));
121+
span_from_context.set_attribute(KeyValue::new("key5", 123));
122+
});
123+
}
124+
},
125+
);
126+
}
127+
128+
#[derive(Debug)]
129+
struct VoidExporter;
130+
131+
impl SpanExporter for VoidExporter {
132+
async fn export(&self, _spans: Vec<SpanData>) -> OTelSdkResult {
133+
Ok(())
134+
}
135+
}
136+
137+
fn trace_benchmark_group<F: Fn(&sdktrace::SdkTracer)>(c: &mut Criterion, name: &str, f: F) {
138+
let mut group = c.benchmark_group(name);
139+
140+
group.bench_function("always-sample", |b| {
141+
let provider = sdktrace::SdkTracerProvider::builder()
142+
.with_sampler(sdktrace::Sampler::AlwaysOn)
143+
.with_simple_exporter(VoidExporter)
144+
.build();
145+
let always_sample = provider.tracer("always-sample");
146+
147+
b.iter(|| f(&always_sample));
148+
});
149+
150+
group.bench_function("never-sample", |b| {
151+
let provider = sdktrace::SdkTracerProvider::builder()
152+
.with_sampler(sdktrace::Sampler::AlwaysOff)
153+
.with_simple_exporter(VoidExporter)
154+
.build();
155+
let never_sample = provider.tracer("never-sample");
156+
b.iter(|| f(&never_sample));
157+
});
158+
159+
group.finish();
160+
}
161+
162+
#[cfg(not(target_os = "windows"))]
163+
criterion_group! {
164+
name = benches;
165+
config = Criterion::default()
166+
.warm_up_time(std::time::Duration::from_secs(1))
167+
.measurement_time(std::time::Duration::from_secs(2))
168+
.with_profiler(PProfProfiler::new(100, Output::Flamegraph(None)));
169+
targets = criterion_benchmark
170+
}
171+
#[cfg(target_os = "windows")]
172+
criterion_group! {
173+
name = benches;
174+
config = Criterion::default()
175+
.warm_up_time(std::time::Duration::from_secs(1))
176+
.measurement_time(std::time::Duration::from_secs(2));
177+
targets = criterion_benchmark
178+
}
179+
criterion_main!(benches);

0 commit comments

Comments
 (0)