Skip to content

Commit 832b788

Browse files
authored
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.
1 parent b2013d0 commit 832b788

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

src/metrics.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ pub(crate) struct MetricVisitor<'a> {
145145
}
146146

147147
impl<'a> Visit for MetricVisitor<'a> {
148-
fn record_debug(&mut self, _field: &Field, _value: &dyn fmt::Debug) {
149-
// Do nothing
148+
fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) {
149+
self.attributes
150+
.push(KeyValue::new(field.name(), format!("{value:?}")));
150151
}
151152

152153
fn record_u64(&mut self, field: &Field, value: u64) {

tests/metrics_publishing.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,68 @@ async fn f64_histogram_with_attributes_is_exported() {
388388
exporter.export().unwrap();
389389
}
390390

391+
#[tokio::test]
392+
async fn display_attribute_is_exported() {
393+
let (subscriber, exporter) = init_subscriber(
394+
"hello_world".to_string(),
395+
InstrumentKind::Counter,
396+
1_u64,
397+
Some(AttributeSet::from(
398+
[KeyValue::new("display_key_1", "display: foo")].as_slice(),
399+
)),
400+
);
401+
402+
struct DisplayAttribute(String);
403+
404+
impl std::fmt::Display for DisplayAttribute {
405+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
406+
write!(f, "display: {}", self.0)
407+
}
408+
}
409+
410+
let display_attribute = DisplayAttribute("foo".to_string());
411+
412+
tracing::subscriber::with_default(subscriber, || {
413+
tracing::info!(
414+
monotonic_counter.hello_world = 1_u64,
415+
display_key_1 = %display_attribute,
416+
);
417+
});
418+
419+
exporter.export().unwrap();
420+
}
421+
422+
#[tokio::test]
423+
async fn debug_attribute_is_exported() {
424+
let (subscriber, exporter) = init_subscriber(
425+
"hello_world".to_string(),
426+
InstrumentKind::Counter,
427+
1_u64,
428+
Some(AttributeSet::from(
429+
[KeyValue::new("debug_key_1", "debug: foo")].as_slice(),
430+
)),
431+
);
432+
433+
struct DebugAttribute(String);
434+
435+
impl std::fmt::Debug for DebugAttribute {
436+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
437+
write!(f, "debug: {}", self.0)
438+
}
439+
}
440+
441+
let debug_attribute = DebugAttribute("foo".to_string());
442+
443+
tracing::subscriber::with_default(subscriber, || {
444+
tracing::info!(
445+
monotonic_counter.hello_world = 1_u64,
446+
debug_key_1 = ?debug_attribute,
447+
);
448+
});
449+
450+
exporter.export().unwrap();
451+
}
452+
391453
fn init_subscriber<T>(
392454
expected_metric_name: String,
393455
expected_instrument_kind: InstrumentKind,

0 commit comments

Comments
 (0)