Skip to content

Commit

Permalink
Alternative implementation of sum encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaseizinger committed Oct 23, 2022
1 parent 50c2e4a commit 120cfb0
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub use prometheus_client_derive_encode::*;
use crate::metrics::exemplar::Exemplar;
use crate::metrics::MetricType;
use std::borrow::Cow;
use std::fmt;
use std::fmt::Write;
use std::ops::Deref;

Expand Down Expand Up @@ -76,6 +77,13 @@ macro_rules! for_both {
}

impl<'a> MetricEncoder<'a> {
pub fn encode_sum(&mut self, value: f64) -> fmt::Result {
match self {
MetricEncoder::Text(inner) => inner.encode_sum(value),
MetricEncoder::Protobuf(inner) => inner.encode_sum(value),
}
}

pub fn encode_suffix(
&mut self,
suffix: &'static str,
Expand Down
23 changes: 23 additions & 0 deletions src/encoding/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,29 @@ impl<'a> MetricEncoder<'a> {
self.no_suffix()
}

pub fn encode_sum(&mut self, value: f64) -> Result<(), std::fmt::Error> {
if self.metric_type != MetricType::Histogram {
panic!("Bad usage")
}

self.family.push(openmetrics_data_model::Metric {
labels: self.labels.clone(),
metric_points: vec![openmetrics_data_model::MetricPoint {
value: Some(openmetrics_data_model::metric_point::Value::HistogramValue(
openmetrics_data_model::HistogramValue {
sum: Some(openmetrics_data_model::histogram_value::Sum::DoubleValue(
value,
)),
..openmetrics_data_model::HistogramValue::default()
},
)),
..Default::default()
}],
});

Ok(())
}

// TODO: This should consume it, no?
pub fn no_suffix(&mut self) -> Result<HistogramEncoder, std::fmt::Error> {
let metric_point = match self.metric_type {
Expand Down
10 changes: 10 additions & 0 deletions src/encoding/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ impl<'a> Encoder<'a> {
self.encode_labels()
}

pub fn encode_sum(&mut self, value: f64) -> Result<(), std::fmt::Error> {
self.write_name_and_unit()?;

self.writer.write_str("_sum")?;

self.encode_labels()?.encode_sum_f64(value)?;

Ok(())
}

/// Signal that the metric has no suffix.
pub fn no_suffix(&mut self) -> Result<HistogramEncoder, std::fmt::Error> {
self.write_name_and_unit()?;
Expand Down
2 changes: 1 addition & 1 deletion src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub trait TypedMetric {
}

/// OpenMetrics metric type.
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[allow(missing_docs)]
pub enum MetricType {
Counter,
Expand Down
2 changes: 1 addition & 1 deletion src/metrics/histogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ fn encode_histogram_with_maybe_exemplars<S: EncodeLabelSet>(
exemplars: Option<&HashMap<usize, Exemplar<S, f64>>>,
mut encoder: MetricEncoder,
) -> Result<(), std::fmt::Error> {
encoder.encode_suffix("sum")?.encode_sum_f64(sum)?;
encoder.encode_sum(sum)?;
encoder.encode_suffix("count")?.encode_count(count)?;

let mut cummulative = 0;
Expand Down

0 comments on commit 120cfb0

Please sign in to comment.