Skip to content

Commit

Permalink
feat(encoding)!: EncodeMetric does not consume MetricEncoder
Browse files Browse the repository at this point in the history
see prometheus#135.

this adjusts the parameter of `encode()` so that it only mutably borrows
the encoder.

Signed-off-by: katelyn martin <[email protected]>
  • Loading branch information
cratelyn committed Nov 18, 2024
1 parent 3a88e8c commit 6f6b59a
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion examples/custom-metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -52,7 +52,7 @@ pub trait EncodeMetric {
}

impl EncodeMetric for Box<dyn EncodeMetric> {
fn encode(&self, encoder: MetricEncoder) -> Result<(), std::fmt::Error> {
fn encode(&self, encoder: &mut MetricEncoder) -> Result<(), std::fmt::Error> {
self.deref().encode(encoder)
}

Expand Down
4 changes: 2 additions & 2 deletions src/encoding/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/metrics/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ where
N: crate::encoding::EncodeCounterValue,
A: Atomic<N>,
{
fn encode(&self, mut encoder: MetricEncoder) -> Result<(), std::fmt::Error> {
fn encode(&self, encoder: &mut MetricEncoder) -> Result<(), std::fmt::Error> {
encoder.encode_counter::<NoLabelSet, _, u64>(&self.get(), None)
}

Expand Down Expand Up @@ -235,7 +235,7 @@ impl<N> EncodeMetric for ConstCounter<N>
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::<NoLabelSet, _, u64>(&self.value, None)
}

Expand Down
4 changes: 2 additions & 2 deletions src/metrics/exemplar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ where
N: EncodeCounterValue + EncodeExemplarValue + Clone,
A: counter::Atomic<N>,
{
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())
}
Expand Down Expand Up @@ -267,7 +267,7 @@ impl<S> HistogramWithExemplars<S> {
}

impl<S: EncodeLabelSet> EncodeMetric for HistogramWithExemplars<S> {
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))
Expand Down
6 changes: 3 additions & 3 deletions src/metrics/family.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,11 @@ where
M: EncodeMetric + TypedMetric,
C: MetricConstructor<M>,
{
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(())
}
Expand Down
4 changes: 2 additions & 2 deletions src/metrics/gauge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ where
N: EncodeGaugeValue,
A: Atomic<N>,
{
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 {
Expand Down Expand Up @@ -377,7 +377,7 @@ impl<N> EncodeMetric for ConstGauge<N>
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)
}

Expand Down
2 changes: 1 addition & 1 deletion src/metrics/histogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub fn linear_buckets(start: f64, width: f64, length: u16) -> impl Iterator<Item
}

impl EncodeMetric for Histogram {
fn encode(&self, mut encoder: MetricEncoder) -> Result<(), std::fmt::Error> {
fn encode(&self, encoder: &mut MetricEncoder) -> Result<(), std::fmt::Error> {
let (sum, count, buckets) = self.get();
encoder.encode_histogram::<NoLabelSet>(sum, count, &buckets, None)
}
Expand Down
2 changes: 1 addition & 1 deletion src/metrics/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl<S> EncodeMetric for Info<S>
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)
}

Expand Down
4 changes: 2 additions & 2 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 6f6b59a

Please sign in to comment.