From 832b7883d458b8e102585e7c29997735bdc86c14 Mon Sep 17 00:00:00 2001 From: Kenta Kudo Date: Tue, 23 Jan 2024 08:51:37 +0700 Subject: [PATCH] Display attribute in metrics (#85) ## Motivation As described in #81, currently the metrics layer doesn't do anything when Debug / Display values are given. ## Solution Record `Debug` attributes as otel metric attributes. --- src/metrics.rs | 5 +-- tests/metrics_publishing.rs | 62 +++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/src/metrics.rs b/src/metrics.rs index 0025537..8a6eae2 100644 --- a/src/metrics.rs +++ b/src/metrics.rs @@ -145,8 +145,9 @@ pub(crate) struct MetricVisitor<'a> { } impl<'a> Visit for MetricVisitor<'a> { - fn record_debug(&mut self, _field: &Field, _value: &dyn fmt::Debug) { - // Do nothing + fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) { + self.attributes + .push(KeyValue::new(field.name(), format!("{value:?}"))); } fn record_u64(&mut self, field: &Field, value: u64) { diff --git a/tests/metrics_publishing.rs b/tests/metrics_publishing.rs index d1dbac9..3a5d32e 100644 --- a/tests/metrics_publishing.rs +++ b/tests/metrics_publishing.rs @@ -388,6 +388,68 @@ async fn f64_histogram_with_attributes_is_exported() { exporter.export().unwrap(); } +#[tokio::test] +async fn display_attribute_is_exported() { + let (subscriber, exporter) = init_subscriber( + "hello_world".to_string(), + InstrumentKind::Counter, + 1_u64, + Some(AttributeSet::from( + [KeyValue::new("display_key_1", "display: foo")].as_slice(), + )), + ); + + struct DisplayAttribute(String); + + impl std::fmt::Display for DisplayAttribute { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "display: {}", self.0) + } + } + + let display_attribute = DisplayAttribute("foo".to_string()); + + tracing::subscriber::with_default(subscriber, || { + tracing::info!( + monotonic_counter.hello_world = 1_u64, + display_key_1 = %display_attribute, + ); + }); + + exporter.export().unwrap(); +} + +#[tokio::test] +async fn debug_attribute_is_exported() { + let (subscriber, exporter) = init_subscriber( + "hello_world".to_string(), + InstrumentKind::Counter, + 1_u64, + Some(AttributeSet::from( + [KeyValue::new("debug_key_1", "debug: foo")].as_slice(), + )), + ); + + struct DebugAttribute(String); + + impl std::fmt::Debug for DebugAttribute { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "debug: {}", self.0) + } + } + + let debug_attribute = DebugAttribute("foo".to_string()); + + tracing::subscriber::with_default(subscriber, || { + tracing::info!( + monotonic_counter.hello_world = 1_u64, + debug_key_1 = ?debug_attribute, + ); + }); + + exporter.export().unwrap(); +} + fn init_subscriber( expected_metric_name: String, expected_instrument_kind: InstrumentKind,