forked from tokio-rs/tracing-opentelemetry
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: produce measurement with attributes (tokio-rs#43)
## Motivation Support tokio-rs#32 This change will allow user to associate Attributes with metrics from tracing. ## Solution 1. MetricsLayer is only interested in events that contain fields with metrics prefixes such as `counter`, so I have utilized the [tracing_subscriber Filter trait](https://docs.rs/tracing-subscriber/latest/tracing_subscriber/layer/#per-layer-filtering). 2. Since it's necessary to pass Attributes when outputting Measurements, it's required to complete the visit for all fields first before invoking the Metrics API(`Counter:add()`). `SmallVec` is used to hold Attribute KeyValues, Metrics names, and Instruments for invocation, aiming to avoid allocations in typical use cases.
- Loading branch information
Showing
4 changed files
with
661 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use opentelemetry::metrics::noop::NoopMeterProvider; | ||
use tracing_opentelemetry::MetricsLayer; | ||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; | ||
|
||
fn metrics_events(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("otel_metrics_events"); | ||
{ | ||
let _subscriber = tracing_subscriber::registry().set_default(); | ||
group.bench_function("no_metrics_layer", |b| { | ||
b.iter(|| { | ||
tracing::info!(key_1 = "va", "msg"); | ||
}) | ||
}); | ||
} | ||
|
||
{ | ||
let _subscriber = tracing_subscriber::registry() | ||
.with(MetricsLayer::new(NoopMeterProvider::new())) | ||
.set_default(); | ||
group.bench_function("metrics_events_0_attr_0", |b| { | ||
b.iter(|| { | ||
tracing::info!(key_1 = "va", "msg"); | ||
}) | ||
}); | ||
} | ||
|
||
{ | ||
let _subscriber = tracing_subscriber::registry() | ||
.with(MetricsLayer::new(NoopMeterProvider::new())) | ||
.set_default(); | ||
group.bench_function("metrics_events_1_attr_0", |b| { | ||
b.iter(|| { | ||
tracing::info!(monotonic_counter.c1 = 1, "msg"); | ||
}) | ||
}); | ||
} | ||
|
||
{ | ||
let _subscriber = tracing_subscriber::registry() | ||
.with(MetricsLayer::new(NoopMeterProvider::new())) | ||
.set_default(); | ||
group.bench_function("metrics_events_2_attr_0", |b| { | ||
b.iter(|| { | ||
tracing::info!(monotonic_counter.c1 = 1, monotonic_counter.c2 = 1, "msg"); | ||
}) | ||
}); | ||
} | ||
|
||
{ | ||
let _subscriber = tracing_subscriber::registry() | ||
.with(MetricsLayer::new(NoopMeterProvider::new())) | ||
.set_default(); | ||
group.bench_function("metrics_events_4_attr_0", |b| { | ||
b.iter(|| { | ||
tracing::info!( | ||
monotonic_counter.c1 = 1, | ||
monotonic_counter.c2 = 1, | ||
monotonic_counter.c3 = 1, | ||
monotonic_counter.c4 = 1, | ||
"msg" | ||
); | ||
}) | ||
}); | ||
} | ||
|
||
{ | ||
let _subscriber = tracing_subscriber::registry() | ||
.with(MetricsLayer::new(NoopMeterProvider::new())) | ||
.set_default(); | ||
group.bench_function("metrics_events_8_attr_0", |b| { | ||
b.iter(|| { | ||
tracing::info!( | ||
monotonic_counter.c1 = 1, | ||
monotonic_counter.c2 = 1, | ||
monotonic_counter.c3 = 1, | ||
monotonic_counter.c4 = 1, | ||
monotonic_counter.c5 = 1, | ||
monotonic_counter.c6 = 1, | ||
monotonic_counter.c7 = 1, | ||
monotonic_counter.c8 = 1, | ||
"msg" | ||
); | ||
}) | ||
}); | ||
} | ||
|
||
{ | ||
let _subscriber = tracing_subscriber::registry() | ||
.with(MetricsLayer::new(NoopMeterProvider::new())) | ||
.set_default(); | ||
group.bench_function("metrics_events_1_attr_1", |b| { | ||
b.iter(|| { | ||
tracing::info!(monotonic_counter.c1 = 1, key_1 = 1_i64, "msg"); | ||
}) | ||
}); | ||
} | ||
|
||
{ | ||
let _subscriber = tracing_subscriber::registry() | ||
.with(MetricsLayer::new(NoopMeterProvider::new())) | ||
.set_default(); | ||
group.bench_function("metrics_events_1_attr_2", |b| { | ||
b.iter(|| { | ||
tracing::info!( | ||
monotonic_counter.c1 = 1, | ||
key_1 = 1_i64, | ||
key_2 = 1_i64, | ||
"msg" | ||
); | ||
}) | ||
}); | ||
} | ||
|
||
{ | ||
let _subscriber = tracing_subscriber::registry() | ||
.with(MetricsLayer::new(NoopMeterProvider::new())) | ||
.set_default(); | ||
group.bench_function("metrics_events_1_attr_4", |b| { | ||
b.iter(|| { | ||
tracing::info!( | ||
monotonic_counter.c1 = 1, | ||
key_1 = 1_i64, | ||
key_2 = 1_i64, | ||
key_3 = 1_i64, | ||
key_4 = 1_i64, | ||
"msg" | ||
); | ||
}) | ||
}); | ||
} | ||
|
||
{ | ||
let _subscriber = tracing_subscriber::registry() | ||
.with(MetricsLayer::new(NoopMeterProvider::new())) | ||
.set_default(); | ||
group.bench_function("metrics_events_1_attr_8", |b| { | ||
b.iter(|| { | ||
tracing::info!( | ||
monotonic_counter.c1 = 1, | ||
key_1 = 1_i64, | ||
key_2 = 1_i64, | ||
key_3 = 1_i64, | ||
key_4 = 1_i64, | ||
key_5 = 1_i64, | ||
key_6 = 1_i64, | ||
key_7 = 1_i64, | ||
key_8 = 1_i64, | ||
"msg" | ||
); | ||
}) | ||
}); | ||
} | ||
group.finish(); | ||
} | ||
|
||
criterion_group! { | ||
name = benches; | ||
config = Criterion::default(); | ||
targets = metrics_events | ||
} | ||
criterion_main!(benches); |
Oops, something went wrong.