From 6f6b59a0b4d7f08743adc33cce6576f29e4089a3 Mon Sep 17 00:00:00 2001 From: katelyn martin Date: Fri, 1 Nov 2024 00:00:00 +0000 Subject: [PATCH] feat(encoding)!: `EncodeMetric` does not consume `MetricEncoder` see #135. this adjusts the parameter of `encode()` so that it only mutably borrows the encoder. Signed-off-by: katelyn martin --- examples/custom-metric.rs | 2 +- src/encoding.rs | 4 ++-- src/encoding/text.rs | 4 ++-- src/metrics/counter.rs | 4 ++-- src/metrics/exemplar.rs | 4 ++-- src/metrics/family.rs | 6 +++--- src/metrics/gauge.rs | 4 ++-- src/metrics/histogram.rs | 2 +- src/metrics/info.rs | 2 +- src/registry.rs | 4 ++-- 10 files changed, 18 insertions(+), 18 deletions(-) diff --git a/examples/custom-metric.rs b/examples/custom-metric.rs index a61ff8a7..a168a8a3 100644 --- a/examples/custom-metric.rs +++ b/examples/custom-metric.rs @@ -11,7 +11,7 @@ use prometheus_client::registry::Registry; struct MyCustomMetric {} impl EncodeMetric for MyCustomMetric { - fn encode(&self, mut encoder: MetricEncoder) -> Result<(), std::fmt::Error> { + fn encode(&self, encoder: &mut MetricEncoder) -> Result<(), std::fmt::Error> { // This method is called on each Prometheus server scrape. Allowing you // to execute whatever logic is needed to generate and encode your // custom metric. diff --git a/src/encoding.rs b/src/encoding.rs index 663b2c74..41af4578 100644 --- a/src/encoding.rs +++ b/src/encoding.rs @@ -43,7 +43,7 @@ macro_rules! for_both { pub trait EncodeMetric { /// Encode the given instance in the OpenMetrics text encoding. // TODO: Lifetimes on MetricEncoder needed? - fn encode(&self, encoder: MetricEncoder) -> Result<(), std::fmt::Error>; + fn encode(&self, encoder: &mut MetricEncoder) -> Result<(), std::fmt::Error>; /// The OpenMetrics metric type of the instance. // One can not use [`TypedMetric`] directly, as associated constants are not @@ -52,7 +52,7 @@ pub trait EncodeMetric { } impl EncodeMetric for Box { - fn encode(&self, encoder: MetricEncoder) -> Result<(), std::fmt::Error> { + fn encode(&self, encoder: &mut MetricEncoder) -> Result<(), std::fmt::Error> { self.deref().encode(encoder) } diff --git a/src/encoding/text.rs b/src/encoding/text.rs index c18a459a..ca44aa97 100644 --- a/src/encoding/text.rs +++ b/src/encoding/text.rs @@ -1080,13 +1080,13 @@ mod tests { mut encoder: crate::encoding::DescriptorEncoder, ) -> Result<(), std::fmt::Error> { let counter = crate::metrics::counter::ConstCounter::new(42u64); - let metric_encoder = encoder.encode_descriptor( + let mut metric_encoder = encoder.encode_descriptor( &self.name, "some help", None, counter.metric_type(), )?; - counter.encode(metric_encoder)?; + counter.encode(&mut metric_encoder)?; Ok(()) } } diff --git a/src/metrics/counter.rs b/src/metrics/counter.rs index 7016c361..2be9ebdf 100644 --- a/src/metrics/counter.rs +++ b/src/metrics/counter.rs @@ -203,7 +203,7 @@ where N: crate::encoding::EncodeCounterValue, A: Atomic, { - fn encode(&self, mut encoder: MetricEncoder) -> Result<(), std::fmt::Error> { + fn encode(&self, encoder: &mut MetricEncoder) -> Result<(), std::fmt::Error> { encoder.encode_counter::(&self.get(), None) } @@ -235,7 +235,7 @@ impl EncodeMetric for ConstCounter where N: crate::encoding::EncodeCounterValue, { - fn encode(&self, mut encoder: MetricEncoder) -> Result<(), std::fmt::Error> { + fn encode(&self, encoder: &mut MetricEncoder) -> Result<(), std::fmt::Error> { encoder.encode_counter::(&self.value, None) } diff --git a/src/metrics/exemplar.rs b/src/metrics/exemplar.rs index e61f4d5a..77d2f9c5 100644 --- a/src/metrics/exemplar.rs +++ b/src/metrics/exemplar.rs @@ -155,7 +155,7 @@ where N: EncodeCounterValue + EncodeExemplarValue + Clone, A: counter::Atomic, { - fn encode(&self, mut encoder: MetricEncoder) -> Result<(), std::fmt::Error> { + fn encode(&self, encoder: &mut MetricEncoder) -> Result<(), std::fmt::Error> { let (value, exemplar) = self.get(); encoder.encode_counter(&value, exemplar.as_ref()) } @@ -267,7 +267,7 @@ impl HistogramWithExemplars { } impl EncodeMetric for HistogramWithExemplars { - fn encode(&self, mut encoder: MetricEncoder) -> Result<(), std::fmt::Error> { + fn encode(&self, encoder: &mut MetricEncoder) -> Result<(), std::fmt::Error> { let inner = self.inner(); let (sum, count, buckets) = inner.histogram.get(); encoder.encode_histogram(sum, count, &buckets, Some(&inner.exemplars)) diff --git a/src/metrics/family.rs b/src/metrics/family.rs index 2f23b198..05512d7f 100644 --- a/src/metrics/family.rs +++ b/src/metrics/family.rs @@ -312,11 +312,11 @@ where M: EncodeMetric + TypedMetric, C: MetricConstructor, { - fn encode(&self, mut encoder: MetricEncoder) -> Result<(), std::fmt::Error> { + fn encode(&self, encoder: &mut MetricEncoder) -> Result<(), std::fmt::Error> { let guard = self.read(); for (label_set, m) in guard.iter() { - let encoder = encoder.encode_family(label_set)?; - m.encode(encoder)?; + let mut encoder = encoder.encode_family(label_set)?; + m.encode(&mut encoder)?; } Ok(()) } diff --git a/src/metrics/gauge.rs b/src/metrics/gauge.rs index abbc8cee..6a788f42 100644 --- a/src/metrics/gauge.rs +++ b/src/metrics/gauge.rs @@ -346,7 +346,7 @@ where N: EncodeGaugeValue, A: Atomic, { - fn encode(&self, mut encoder: MetricEncoder) -> Result<(), std::fmt::Error> { + fn encode(&self, encoder: &mut MetricEncoder) -> Result<(), std::fmt::Error> { encoder.encode_gauge(&self.get()) } fn metric_type(&self) -> MetricType { @@ -377,7 +377,7 @@ impl EncodeMetric for ConstGauge where N: EncodeGaugeValue, { - fn encode(&self, mut encoder: MetricEncoder) -> Result<(), std::fmt::Error> { + fn encode(&self, encoder: &mut MetricEncoder) -> Result<(), std::fmt::Error> { encoder.encode_gauge(&self.value) } diff --git a/src/metrics/histogram.rs b/src/metrics/histogram.rs index 1e418ad9..4488c61c 100644 --- a/src/metrics/histogram.rs +++ b/src/metrics/histogram.rs @@ -131,7 +131,7 @@ pub fn linear_buckets(start: f64, width: f64, length: u16) -> impl Iterator Result<(), std::fmt::Error> { + fn encode(&self, encoder: &mut MetricEncoder) -> Result<(), std::fmt::Error> { let (sum, count, buckets) = self.get(); encoder.encode_histogram::(sum, count, &buckets, None) } diff --git a/src/metrics/info.rs b/src/metrics/info.rs index 6ab127c4..e9ae28b2 100644 --- a/src/metrics/info.rs +++ b/src/metrics/info.rs @@ -33,7 +33,7 @@ impl EncodeMetric for Info where S: Clone + std::hash::Hash + Eq + EncodeLabelSet, { - fn encode(&self, mut encoder: MetricEncoder) -> Result<(), std::fmt::Error> { + fn encode(&self, encoder: &mut MetricEncoder) -> Result<(), std::fmt::Error> { encoder.encode_info(&self.0) } diff --git a/src/registry.rs b/src/registry.rs index c7dec3ba..77653e22 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -290,13 +290,13 @@ impl Registry { for (descriptor, metric) in self.metrics.iter() { let mut descriptor_encoder = encoder.with_prefix_and_labels(self.prefix.as_ref(), &self.labels); - let metric_encoder = descriptor_encoder.encode_descriptor( + let mut metric_encoder = descriptor_encoder.encode_descriptor( &descriptor.name, &descriptor.help, descriptor.unit.as_ref(), EncodeMetric::metric_type(metric.as_ref()), )?; - metric.encode(metric_encoder)?; + metric.encode(&mut metric_encoder)?; } for collector in self.collectors.iter() {