diff --git a/examples/families-exemplars.rs b/examples/families-exemplars.rs index e2e4e447..eab43f72 100644 --- a/examples/families-exemplars.rs +++ b/examples/families-exemplars.rs @@ -1,5 +1,5 @@ use prometheus_client::{ - encoding::text::SendSyncEncodeMetric, + encoding::text::encode, metrics::{exemplar::HistogramWithExemplars, family::Family, histogram::exponential_buckets}, registry::Registry, }; @@ -16,13 +16,24 @@ fn main() { HistogramWithExemplars::new(exponential_buckets(1.0, 2.0, 10)) }); - // This line doesn't compile: - // - // the trait `TypedMetric` is not implemented for `HistogramWithExemplar` - let b: Box = Box::new(latency); - // Register metrics. - registry.register("latency", "help text goes here", b); + registry.register("latency", "help text goes here", Box::new(latency.clone())); + + latency + .get_or_create(&ResultLabel { + result: "success".to_owned(), + }) + .observe( + 0.001345422, + Some(TraceLabel { + trace_id: "3a2f90c9f80b894f".to_owned(), + }), + ); + + let mut buf = Vec::new(); + encode(&mut buf, ®istry).unwrap(); + let line = std::str::from_utf8(buf.as_slice()).unwrap().to_string(); + println!("{line}"); } #[derive(Clone, Hash, PartialEq, Eq, Encode, Debug, Default)] @@ -32,5 +43,5 @@ pub struct ResultLabel { #[derive(Clone, Hash, PartialEq, Eq, Encode, Debug, Default)] pub struct TraceLabel { - trace_id: String, + pub trace_id: String, } diff --git a/src/metrics/exemplar.rs b/src/metrics/exemplar.rs index 84462003..1015d7f2 100644 --- a/src/metrics/exemplar.rs +++ b/src/metrics/exemplar.rs @@ -4,6 +4,7 @@ use super::counter::{self, Counter}; use super::histogram::Histogram; +use super::{MetricType, TypedMetric}; use parking_lot::{MappedRwLockReadGuard, RwLock, RwLockReadGuard}; use std::collections::HashMap; #[cfg(any(target_arch = "mips", target_arch = "powerpc"))] @@ -37,6 +38,10 @@ pub struct CounterWithExemplar { pub(crate) inner: Arc>>, } +impl TypedMetric for CounterWithExemplar { + const TYPE: MetricType = MetricType::Counter; +} + /// Open Metrics [`Counter`] with an [`Exemplar`] to both measure discrete /// events and track references to data outside of the metric set. #[cfg(any(target_arch = "mips", target_arch = "powerpc"))] @@ -128,6 +133,10 @@ pub struct HistogramWithExemplars { pub(crate) inner: Arc>>, } +impl TypedMetric for HistogramWithExemplars { + const TYPE: MetricType = MetricType::Histogram; +} + impl Clone for HistogramWithExemplars { fn clone(&self) -> Self { Self {