Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src/encoding: Take reference for value #115

Merged
merged 3 commits into from Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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