Skip to content

Commit

Permalink
src/encoding: Take reference for value (#115)
Browse files Browse the repository at this point in the history
No need to take ownership of the value. Relevant when using with non-copy types.

Signed-off-by: Max Inden <mail@max-inden.de>
  • Loading branch information
mxinden committed Dec 10, 2022
1 parent b87f6ff commit 1f20c3c
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion examples/custom-metric.rs
Expand Up @@ -20,7 +20,7 @@ impl EncodeMetric for MyCustomMetric {
// E.g. every CPU cycle spend in this method delays the response send to
// the Prometheus server.

encoder.encode_counter::<(), _, u64>(rand::random::<u64>(), None)
encoder.encode_counter::<(), _, u64>(&rand::random::<u64>(), None)
}

fn metric_type(&self) -> prometheus_client::metrics::MetricType {
Expand Down
7 changes: 5 additions & 2 deletions src/encoding.rs
Expand Up @@ -96,14 +96,17 @@ impl<'a, 'b> MetricEncoder<'a, 'b> {
ExemplarValue: EncodeExemplarValue,
>(
&mut self,
v: CounterValue,
v: &CounterValue,
exemplar: Option<&Exemplar<S, ExemplarValue>>,
) -> Result<(), std::fmt::Error> {
for_both_mut!(self, MetricEncoderInner, e, e.encode_counter(v, exemplar))
}

/// Encode a gauge.
pub fn encode_gauge(&mut self, v: impl EncodeGaugeValue) -> Result<(), std::fmt::Error> {
pub fn encode_gauge<GaugeValue: EncodeGaugeValue>(
&mut self,
v: &GaugeValue,
) -> Result<(), std::fmt::Error> {
for_both_mut!(self, MetricEncoderInner, e, e.encode_gauge(v))
}

Expand Down
17 changes: 12 additions & 5 deletions src/encoding/protobuf.rs
Expand Up @@ -36,7 +36,7 @@ use crate::metrics::exemplar::Exemplar;
use crate::metrics::MetricType;
use crate::registry::Registry;

use super::{EncodeExemplarValue, EncodeLabelSet};
use super::{EncodeCounterValue, EncodeExemplarValue, EncodeGaugeValue, EncodeLabelSet};

/// Encode the metrics registered with the provided [`Registry`] into MetricSet
/// using the OpenMetrics protobuf format.
Expand Down Expand Up @@ -102,10 +102,14 @@ pub(crate) struct MetricEncoder<'a> {
}

impl<'a> MetricEncoder<'a> {
pub fn encode_counter<S: EncodeLabelSet, V: EncodeExemplarValue>(
pub fn encode_counter<
S: EncodeLabelSet,
CounterValue: EncodeCounterValue,
ExemplarValue: EncodeExemplarValue,
>(
&mut self,
v: impl super::EncodeCounterValue,
exemplar: Option<&Exemplar<S, V>>,
v: &CounterValue,
exemplar: Option<&Exemplar<S, ExemplarValue>>,
) -> Result<(), std::fmt::Error> {
let mut value = openmetrics_data_model::counter_value::Total::IntValue(0);
let mut e = CounterValueEncoder { value: &mut value }.into();
Expand All @@ -128,7 +132,10 @@ impl<'a> MetricEncoder<'a> {
Ok(())
}

pub fn encode_gauge(&mut self, v: impl super::EncodeGaugeValue) -> Result<(), std::fmt::Error> {
pub fn encode_gauge<GaugeValue: EncodeGaugeValue>(
&mut self,
v: &GaugeValue,
) -> Result<(), std::fmt::Error> {
let mut value = openmetrics_data_model::gauge_value::Value::IntValue(0);
let mut e = GaugeValueEncoder { value: &mut value }.into();
v.encode(&mut e)?;
Expand Down
15 changes: 11 additions & 4 deletions src/encoding/text.rs
Expand Up @@ -112,10 +112,14 @@ impl<'a, 'b> std::fmt::Debug for MetricEncoder<'a, 'b> {
}

impl<'a, 'b> MetricEncoder<'a, 'b> {
pub fn encode_counter<S: EncodeLabelSet, V: EncodeExemplarValue>(
pub fn encode_counter<
S: EncodeLabelSet,
CounterValue: super::EncodeCounterValue,
ExemplarValue: EncodeExemplarValue,
>(
&mut self,
v: impl super::EncodeCounterValue,
exemplar: Option<&Exemplar<S, V>>,
v: &CounterValue,
exemplar: Option<&Exemplar<S, ExemplarValue>>,
) -> Result<(), std::fmt::Error> {
self.write_name_and_unit()?;

Expand All @@ -139,7 +143,10 @@ impl<'a, 'b> MetricEncoder<'a, 'b> {
Ok(())
}

pub fn encode_gauge(&mut self, v: impl super::EncodeGaugeValue) -> Result<(), std::fmt::Error> {
pub fn encode_gauge<GaugeValue: super::EncodeGaugeValue>(
&mut self,
v: &GaugeValue,
) -> Result<(), std::fmt::Error> {
self.write_name_and_unit()?;

self.encode_labels::<()>(None)?;
Expand Down
2 changes: 1 addition & 1 deletion src/metrics/counter.rs
Expand Up @@ -179,7 +179,7 @@ where
A: Atomic<N>,
{
fn encode(&self, mut encoder: MetricEncoder) -> Result<(), std::fmt::Error> {
encoder.encode_counter::<(), _, u64>(self.get(), None)
encoder.encode_counter::<(), _, u64>(&self.get(), None)
}

fn metric_type(&self) -> MetricType {
Expand Down
2 changes: 1 addition & 1 deletion src/metrics/exemplar.rs
Expand Up @@ -157,7 +157,7 @@ where
{
fn encode(&self, mut encoder: MetricEncoder) -> Result<(), std::fmt::Error> {
let (value, exemplar) = self.get();
encoder.encode_counter(value, exemplar.as_ref())
encoder.encode_counter(&value, exemplar.as_ref())
}

fn metric_type(&self) -> MetricType {
Expand Down
2 changes: 1 addition & 1 deletion src/metrics/gauge.rs
Expand Up @@ -246,7 +246,7 @@ where
A: Atomic<N>,
{
fn encode(&self, mut encoder: MetricEncoder) -> Result<(), std::fmt::Error> {
encoder.encode_gauge(self.get())
encoder.encode_gauge(&self.get())
}
fn metric_type(&self) -> MetricType {
Self::TYPE
Expand Down

0 comments on commit 1f20c3c

Please sign in to comment.