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

feat(encoding)!: 🧁 encoding traits do not consume encoders #240

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion derive-encode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub fn derive_encode_label_set(input: TokenStream) -> TokenStream {

let gen = quote! {
impl prometheus_client::encoding::EncodeLabelSet for #name {
fn encode(&self, mut encoder: prometheus_client::encoding::LabelSetEncoder) -> std::result::Result<(), std::fmt::Error> {
fn encode(&self, encoder: &mut prometheus_client::encoding::LabelSetEncoder) -> std::result::Result<(), std::fmt::Error> {
use prometheus_client::encoding::EncodeLabel;
use prometheus_client::encoding::EncodeLabelKey;
use prometheus_client::encoding::EncodeLabelValue;
Expand Down
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
8 changes: 4 additions & 4 deletions src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ use crate::encoding::DescriptorEncoder;
/// struct MyCollector {}
///
/// impl Collector for MyCollector {
/// fn encode(&self, mut encoder: DescriptorEncoder) -> Result<(), std::fmt::Error> {
/// fn encode(&self, encoder: &mut DescriptorEncoder) -> Result<(), std::fmt::Error> {
/// let counter = ConstCounter::new(42u64);
/// let metric_encoder = encoder.encode_descriptor(
/// let mut metric_encoder = encoder.encode_descriptor(
/// "my_counter",
/// "some help",
/// None,
/// counter.metric_type(),
/// )?;
/// counter.encode(metric_encoder)?;
/// counter.encode(&mut metric_encoder)?;
/// Ok(())
/// }
/// }
/// ```
pub trait Collector: std::fmt::Debug + Send + Sync + 'static {
/// Once the [`Collector`] is registered, this method is called on each scrape.
fn encode(&self, encoder: DescriptorEncoder) -> Result<(), std::fmt::Error>;
fn encode(&self, encoder: &mut DescriptorEncoder) -> Result<(), std::fmt::Error>;
}
30 changes: 15 additions & 15 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 Expand Up @@ -203,7 +203,7 @@ impl MetricEncoder<'_> {
/// An encodable label set.
pub trait EncodeLabelSet {
/// Encode oneself into the given encoder.
fn encode(&self, encoder: LabelSetEncoder) -> Result<(), std::fmt::Error>;
fn encode(&self, encoder: &mut LabelSetEncoder) -> Result<(), std::fmt::Error>;
}

impl<'a> From<text::LabelSetEncoder<'a>> for LabelSetEncoder<'a> {
Expand Down Expand Up @@ -240,7 +240,7 @@ impl<'a> LabelSetEncoder<'a> {
/// An encodable label.
pub trait EncodeLabel {
/// Encode oneself into the given encoder.
fn encode(&self, encoder: LabelEncoder) -> Result<(), std::fmt::Error>;
fn encode(&self, encoder: &mut LabelEncoder) -> Result<(), std::fmt::Error>;
}

/// Encoder for a label.
Expand Down Expand Up @@ -331,39 +331,39 @@ impl<'a> LabelKeyEncoder<'a> {
}
}
impl<T: EncodeLabel, const N: usize> EncodeLabelSet for [T; N] {
fn encode(&self, encoder: LabelSetEncoder) -> Result<(), std::fmt::Error> {
fn encode(&self, encoder: &mut LabelSetEncoder) -> Result<(), std::fmt::Error> {
self.as_ref().encode(encoder)
}
}

impl<T: EncodeLabel> EncodeLabelSet for &[T] {
fn encode(&self, mut encoder: LabelSetEncoder) -> Result<(), std::fmt::Error> {
fn encode(&self, encoder: &mut LabelSetEncoder) -> Result<(), std::fmt::Error> {
if self.is_empty() {
return Ok(());
}

for label in self.iter() {
label.encode(encoder.encode_label())?
label.encode(&mut encoder.encode_label())?
}

Ok(())
}
}

impl<T: EncodeLabel> EncodeLabelSet for Vec<T> {
fn encode(&self, encoder: LabelSetEncoder) -> Result<(), std::fmt::Error> {
fn encode(&self, encoder: &mut LabelSetEncoder) -> Result<(), std::fmt::Error> {
self.as_slice().encode(encoder)
}
}

impl EncodeLabelSet for NoLabelSet {
fn encode(&self, _encoder: LabelSetEncoder) -> Result<(), std::fmt::Error> {
fn encode(&self, _encoder: &mut LabelSetEncoder) -> Result<(), std::fmt::Error> {
Ok(())
}
}

impl<K: EncodeLabelKey, V: EncodeLabelValue> EncodeLabel for (K, V) {
fn encode(&self, mut encoder: LabelEncoder) -> Result<(), std::fmt::Error> {
fn encode(&self, encoder: &mut LabelEncoder) -> Result<(), std::fmt::Error> {
let (key, value) = self;

let mut label_key_encoder = encoder.encode_label_key()?;
Expand Down Expand Up @@ -691,29 +691,29 @@ impl<'a> CounterValueEncoder<'a> {
/// An encodable exemplar value.
pub trait EncodeExemplarValue {
/// Encode the given instance in the OpenMetrics text encoding.
fn encode(&self, encoder: ExemplarValueEncoder) -> Result<(), std::fmt::Error>;
fn encode(&self, encoder: &mut ExemplarValueEncoder) -> Result<(), std::fmt::Error>;
}

impl EncodeExemplarValue for f64 {
fn encode(&self, mut encoder: ExemplarValueEncoder) -> Result<(), std::fmt::Error> {
fn encode(&self, encoder: &mut ExemplarValueEncoder) -> Result<(), std::fmt::Error> {
encoder.encode(*self)
}
}

impl EncodeExemplarValue for u64 {
fn encode(&self, mut encoder: ExemplarValueEncoder) -> Result<(), std::fmt::Error> {
fn encode(&self, encoder: &mut ExemplarValueEncoder) -> Result<(), std::fmt::Error> {
encoder.encode(*self as f64)
}
}

impl EncodeExemplarValue for f32 {
fn encode(&self, mut encoder: ExemplarValueEncoder) -> Result<(), std::fmt::Error> {
fn encode(&self, encoder: &mut ExemplarValueEncoder) -> Result<(), std::fmt::Error> {
encoder.encode(*self as f64)
}
}

impl EncodeExemplarValue for u32 {
fn encode(&self, mut encoder: ExemplarValueEncoder) -> Result<(), std::fmt::Error> {
fn encode(&self, encoder: &mut ExemplarValueEncoder) -> Result<(), std::fmt::Error> {
encoder.encode(*self as f64)
}
}
Expand Down
49 changes: 22 additions & 27 deletions src/encoding/protobuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,11 @@ impl DescriptorEncoder<'_> {
..Default::default()
};
let mut labels = vec![];
self.labels.encode(
LabelSetEncoder {
labels: &mut labels,
}
.into(),
)?;
let mut encoder = LabelSetEncoder {
labels: &mut labels,
}
.into();
self.labels.encode(&mut encoder)?;
self.metric_families.push(family);

Ok(MetricEncoder {
Expand Down Expand Up @@ -209,12 +208,11 @@ impl MetricEncoder<'_> {
label_set: &impl super::EncodeLabelSet,
) -> Result<(), std::fmt::Error> {
let mut info_labels = vec![];
label_set.encode(
LabelSetEncoder {
labels: &mut info_labels,
}
.into(),
)?;
let mut encoder = LabelSetEncoder {
labels: &mut info_labels,
}
.into();
label_set.encode(&mut encoder)?;

self.family.push(openmetrics_data_model::Metric {
labels: self.labels.clone(),
Expand All @@ -234,12 +232,11 @@ impl MetricEncoder<'_> {
label_set: &S,
) -> Result<MetricEncoder, std::fmt::Error> {
let mut labels = self.labels.clone();
label_set.encode(
LabelSetEncoder {
labels: &mut labels,
}
.into(),
)?;
let mut encoder = LabelSetEncoder {
labels: &mut labels,
}
.into();
label_set.encode(&mut encoder)?;

Ok(MetricEncoder {
metric_type: self.metric_type,
Expand Down Expand Up @@ -297,17 +294,15 @@ impl<S: EncodeLabelSet, V: EncodeExemplarValue> TryFrom<&Exemplar<S, V>>

fn try_from(exemplar: &Exemplar<S, V>) -> Result<Self, Self::Error> {
let mut value = f64::default();
exemplar
.value
.encode(ExemplarValueEncoder { value: &mut value }.into())?;
let mut encoder = ExemplarValueEncoder { value: &mut value }.into();
exemplar.value.encode(&mut encoder)?;

let mut labels = vec![];
exemplar.label_set.encode(
LabelSetEncoder {
labels: &mut labels,
}
.into(),
)?;
let mut encoder = LabelSetEncoder {
labels: &mut labels,
}
.into();
exemplar.label_set.encode(&mut encoder)?;

Ok(openmetrics_data_model::Exemplar {
value,
Expand Down
26 changes: 15 additions & 11 deletions src/encoding/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ impl<'a> std::fmt::Debug for MetricEncoder<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut labels = String::new();
if let Some(l) = self.family_labels {
l.encode(LabelSetEncoder::new(&mut labels).into())?;
let mut encoder = LabelSetEncoder::new(&mut labels).into();
l.encode(&mut encoder)?;
}

f.debug_struct("Encoder")
Expand Down Expand Up @@ -451,10 +452,10 @@ impl<'a> MetricEncoder<'a> {
self.writer.write_str(" # {")?;
exemplar
.label_set
.encode(LabelSetEncoder::new(self.writer).into())?;
.encode(&mut LabelSetEncoder::new(self.writer).into())?;
self.writer.write_str("} ")?;
exemplar.value.encode(
ExemplarValueEncoder {
&mut ExemplarValueEncoder {
writer: self.writer,
}
.into(),
Expand Down Expand Up @@ -502,14 +503,15 @@ impl<'a> MetricEncoder<'a> {
self.writer.write_str("{")?;

self.const_labels
.encode(LabelSetEncoder::new(self.writer).into())?;
.encode(&mut LabelSetEncoder::new(self.writer).into())?;

if let Some(additional_labels) = additional_labels {
if !self.const_labels.is_empty() {
self.writer.write_str(",")?;
}

additional_labels.encode(LabelSetEncoder::new(self.writer).into())?;
let mut encoder = LabelSetEncoder::new(self.writer).into();
additional_labels.encode(&mut encoder)?;
}

/// Writer impl which prepends a comma on the first call to write output to the wrapped writer
Expand Down Expand Up @@ -539,9 +541,11 @@ impl<'a> MetricEncoder<'a> {
writer: self.writer,
should_prepend: true,
};
labels.encode(LabelSetEncoder::new(&mut writer).into())?;
let mut encoder = LabelSetEncoder::new(&mut writer).into();
labels.encode(&mut encoder)?;
} else {
labels.encode(LabelSetEncoder::new(self.writer).into())?;
let mut encoder = LabelSetEncoder::new(self.writer).into();
labels.encode(&mut encoder)?;
};
}

Expand Down Expand Up @@ -936,7 +940,7 @@ mod tests {
struct EmptyLabels {}

impl EncodeLabelSet for EmptyLabels {
fn encode(&self, _encoder: crate::encoding::LabelSetEncoder) -> Result<(), Error> {
fn encode(&self, _encoder: &mut crate::encoding::LabelSetEncoder) -> Result<(), Error> {
Ok(())
}
}
Expand Down Expand Up @@ -1073,16 +1077,16 @@ mod tests {
impl crate::collector::Collector for Collector {
fn encode(
&self,
mut encoder: crate::encoding::DescriptorEncoder,
encoder: &mut 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
Loading