diff --git a/metrics-tracing-context/Cargo.toml b/metrics-tracing-context/Cargo.toml index e0b90a5..a1c12c0 100644 --- a/metrics-tracing-context/Cargo.toml +++ b/metrics-tracing-context/Cargo.toml @@ -22,6 +22,10 @@ bench = false name = "visit" harness = false +[[bench]] +name = "layer" +harness = false + [dependencies] metrics = { version = "0.13.0-alpha.1", path = "../metrics", features = ["std"] } metrics-util = { version = "0.4.0-alpha.1", path = "../metrics-util" } diff --git a/metrics-tracing-context/benches/layer.rs b/metrics-tracing-context/benches/layer.rs new file mode 100644 index 0000000..92f1904 --- /dev/null +++ b/metrics-tracing-context/benches/layer.rs @@ -0,0 +1,46 @@ +use criterion::{criterion_group, criterion_main, Benchmark, Criterion}; +use metrics::{Key, KeyData, Label, NoopRecorder, Recorder}; +use metrics_tracing_context::{MetricsLayer, TracingContextLayer}; +use metrics_util::layers::Layer; +use tracing::{ + dispatcher::{with_default, Dispatch}, + span, Level, +}; +use tracing_subscriber::{layer::SubscriberExt, Registry}; + +fn layer_benchmark(c: &mut Criterion) { + c.bench( + "layer", + Benchmark::new("all/enhance_key", |b| { + let subscriber = Registry::default().with(MetricsLayer::new()); + let dispatcher = Dispatch::new(subscriber); + with_default(&dispatcher, || { + let user = "ferris"; + let email = "ferris@rust-lang.org"; + let span = span!(Level::TRACE, "login", user, user.email = email); + let _guard = span.enter(); + + let tracing_layer = TracingContextLayer::all(); + let recorder = tracing_layer.layer(NoopRecorder); + static LABELS: [Label; 1] = [Label::from_static_parts("foo", "bar")]; + static KEY_DATA: KeyData = KeyData::from_static_parts("key", &LABELS); + + b.iter(|| { + recorder.increment_counter(Key::Borrowed(&KEY_DATA), 1); + }) + }) + }) + .with_function("noop recorder overhead (increment_counter)", |b| { + let recorder = NoopRecorder; + static LABELS: [Label; 1] = [Label::from_static_parts("foo", "bar")]; + static KEY_DATA: KeyData = KeyData::from_static_parts("key", &LABELS); + + b.iter(|| { + recorder.increment_counter(Key::Borrowed(&KEY_DATA), 1); + }) + }), + ); +} + +criterion_group!(benches, layer_benchmark); +criterion_main!(benches); diff --git a/metrics-tracing-context/src/tracing_integration.rs b/metrics-tracing-context/src/tracing_integration.rs index 9d07340..f55b8a4 100644 --- a/metrics-tracing-context/src/tracing_integration.rs +++ b/metrics-tracing-context/src/tracing_integration.rs @@ -20,7 +20,7 @@ impl Visit for Labels { } fn record_bool(&mut self, field: &Field, value: bool) { - let label = Label::new(field.name(), if value { "true" } else { "false " }); + let label = Label::new(field.name(), if value { "true" } else { "false" }); self.0.push(label); } diff --git a/metrics-util/src/debugging.rs b/metrics-util/src/debugging.rs index f26e948..4c6ab66 100644 --- a/metrics-util/src/debugging.rs +++ b/metrics-util/src/debugging.rs @@ -75,7 +75,7 @@ pub struct Snapshotter { registry: Arc>, metrics: Option>>>, units: UnitMap, - descriptions: DescriptionMap, + descs: DescriptionMap, } impl Snapshotter { @@ -115,27 +115,15 @@ impl Snapshotter { metrics.clone() }; - for (dkey, _) in metrics.into_iter() { - if let Some(handle) = handles.get(&dkey) { - collect_metric( - dkey, - handle, - &self.units, - &self.descriptions, - &mut snapshot, - ); + for (dk, _) in metrics.into_iter() { + if let Some(h) = handles.get(&dk) { + collect_metric(dk, h, &self.units, &self.descs, &mut snapshot); } } } None => { - for (dkey, handle) in handles.into_iter() { - collect_metric( - dkey, - &handle, - &self.units, - &self.descriptions, - &mut snapshot, - ); + for (dk, h) in handles.into_iter() { + collect_metric(dk, &h, &self.units, &self.descs, &mut snapshot); } } } @@ -152,7 +140,7 @@ pub struct DebuggingRecorder { registry: Arc>, metrics: Option>>>, units: Arc>>, - descriptions: Arc>>, + descs: Arc>>, } impl DebuggingRecorder { @@ -177,7 +165,7 @@ impl DebuggingRecorder { registry: Arc::new(Registry::new()), metrics, units: Arc::new(Mutex::new(HashMap::new())), - descriptions: Arc::new(Mutex::new(HashMap::new())), + descs: Arc::new(Mutex::new(HashMap::new())), } } @@ -187,7 +175,7 @@ impl DebuggingRecorder { registry: self.registry.clone(), metrics: self.metrics.clone(), units: self.units.clone(), - descriptions: self.descriptions.clone(), + descs: self.descs.clone(), } } @@ -202,17 +190,17 @@ impl DebuggingRecorder { &self, rkey: DifferentiatedKey, unit: Option, - description: Option<&'static str>, + desc: Option<&'static str>, ) { if let Some(unit) = unit { let mut units = self.units.lock().expect("units lock poisoned"); let uentry = units.entry(rkey.clone()).or_insert_with(|| unit.clone()); *uentry = unit; } - if let Some(description) = description { - let mut descriptions = self.descriptions.lock().expect("description lock poisoned"); - let dentry = descriptions.entry(rkey).or_insert_with(|| description); - *dentry = description; + if let Some(desc) = desc { + let mut descs = self.descs.lock().expect("description lock poisoned"); + let dentry = descs.entry(rkey).or_insert_with(|| desc); + *dentry = desc; } } diff --git a/metrics/src/recorder.rs b/metrics/src/recorder.rs index dd27356..9e73649 100644 --- a/metrics/src/recorder.rs +++ b/metrics/src/recorder.rs @@ -51,7 +51,11 @@ pub trait Recorder { fn record_histogram(&self, key: Key, value: u64); } -struct NoopRecorder; +/// A no-op recorder. +/// +/// Used as the default recorder when one has not been installed yet. Useful for acting as the root +/// recorder when testing layers. +pub struct NoopRecorder; impl Recorder for NoopRecorder { fn register_counter(&self, _key: Key, _unit: Option, _description: Option<&'static str>) {