Skip to content

Commit

Permalink
feat(encoding)!: EncodeLabelSet does not consume LabelSetEncoder
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 05da98f commit 3a88e8c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 37 deletions.
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
10 changes: 5 additions & 5 deletions src/encoding.rs
Original file line number Diff line number Diff line change
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 @@ -331,13 +331,13 @@ 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(());
}
Expand All @@ -351,13 +351,13 @@ impl<T: EncodeLabel> EncodeLabelSet for &[T] {
}

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(())
}
}
Expand Down
44 changes: 20 additions & 24 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 @@ -302,12 +299,11 @@ impl<S: EncodeLabelSet, V: EncodeExemplarValue> TryFrom<&Exemplar<S, V>>
.encode(ExemplarValueEncoder { value: &mut value }.into())?;

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
18 changes: 11 additions & 7 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,7 +452,7 @@ 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 {
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

0 comments on commit 3a88e8c

Please sign in to comment.