-
Notifications
You must be signed in to change notification settings - Fork 488
/
Copy pathcontext.rs
160 lines (147 loc) · 5.15 KB
/
context.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use futures_util::future::BoxFuture;
use opentelemetry::{
global::BoxedTracer,
trace::{
noop::NoopTracer, SpanContext, SpanId, TraceContextExt, TraceFlags, TraceId, TraceState,
TracerProvider as _,
},
Context, ContextGuard,
};
use opentelemetry_sdk::{
export::trace::{ExportResult, SpanData, SpanExporter},
trace,
trace::{Sampler, TracerProvider},
};
#[cfg(not(target_os = "windows"))]
use pprof::criterion::{Output, PProfProfiler};
use std::fmt::Display;
fn criterion_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("context");
group.throughput(Throughput::Elements(1));
for env in [
Environment::InContext,
Environment::NoContext,
Environment::NoSdk,
] {
let (_provider, _tracer, _guard) = env.setup();
for api in [Api::Alt, Api::Spec] {
let param = format!("{env}/{api}");
group.bench_function(
BenchmarkId::new("has_active_span", param.clone()),
|b| match api {
Api::Alt => b.iter(|| Context::map_current(TraceContextExt::has_active_span)),
Api::Spec => b.iter(|| Context::current().has_active_span()),
},
);
group.bench_function(
BenchmarkId::new("is_sampled", param.clone()),
|b| match api {
Api::Alt => {
b.iter(|| Context::map_current(|cx| cx.span().span_context().is_sampled()))
}
Api::Spec => b.iter(|| Context::current().span().span_context().is_sampled()),
},
);
group.bench_function(BenchmarkId::new("is_recording", param), |b| match api {
Api::Alt => b.iter(|| Context::map_current(|cx| cx.span().is_recording())),
Api::Spec => b.iter(|| Context::current().span().is_recording()),
});
}
}
}
#[derive(Copy, Clone)]
enum Api {
/// An alternative way which may be faster than what the spec recommends.
Alt,
/// The recommended way as proposed by the current opentelemetry specification.
Spec,
}
impl Api {
const fn as_str(self) -> &'static str {
match self {
Api::Alt => "alt",
Api::Spec => "spec",
}
}
}
impl Display for Api {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_str())
}
}
#[derive(Copy, Clone)]
enum Environment {
/// There is an active span being sampled in the current context.
InContext,
/// There is no span in context (or there is not context).
NoContext,
/// An SDK has not been configured, so instrumentation should be noop.
NoSdk,
}
impl Environment {
const fn as_str(self) -> &'static str {
match self {
Environment::InContext => "in-cx",
Environment::NoContext => "no-cx",
Environment::NoSdk => "no-sdk",
}
}
fn setup(&self) -> (Option<TracerProvider>, BoxedTracer, Option<ContextGuard>) {
match self {
Environment::InContext => {
let guard = Context::current()
.with_remote_span_context(SpanContext::new(
TraceId::from(0x09251969),
SpanId::from(0x08171969),
TraceFlags::SAMPLED,
true,
TraceState::default(),
))
.attach();
let (provider, tracer) = parent_sampled_tracer(Sampler::AlwaysOff);
(Some(provider), tracer, Some(guard))
}
Environment::NoContext => {
let (provider, tracer) = parent_sampled_tracer(Sampler::AlwaysOff);
(Some(provider), tracer, None)
}
Environment::NoSdk => (None, BoxedTracer::new(Box::new(NoopTracer::new())), None),
}
}
}
impl Display for Environment {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_str())
}
}
fn parent_sampled_tracer(inner_sampler: Sampler) -> (TracerProvider, BoxedTracer) {
let provider = TracerProvider::builder()
.with_config(
trace::Config::default().with_sampler(Sampler::ParentBased(Box::new(inner_sampler))),
)
.with_simple_exporter(NoopExporter)
.build();
let tracer = provider.tracer(module_path!());
(provider, BoxedTracer::new(Box::new(tracer)))
}
#[derive(Debug)]
struct NoopExporter;
impl SpanExporter for NoopExporter {
fn export(&mut self, _spans: Vec<SpanData>) -> BoxFuture<'static, ExportResult> {
Box::pin(futures_util::future::ready(Ok(())))
}
}
#[cfg(not(target_os = "windows"))]
criterion_group! {
name = benches;
config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flamegraph(None)));
targets = criterion_benchmark
}
#[cfg(target_os = "windows")]
criterion_group! {
name = benches;
config = Criterion::default();
targets = criterion_benchmark
}
criterion_main!(benches);