Skip to content

Commit 849ae9c

Browse files
committed
Adding Prometheus serialization format
Signed-off-by: Rodolfo P A Signed-off-by: Rodolfo P A <6721075+rodoufu@users.noreply.github.com>
1 parent 3942d06 commit 849ae9c

5 files changed

Lines changed: 251 additions & 75 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "prometheus-client"
3-
version = "0.24.0"
3+
version = "0.24.1"
44
authors = ["Max Inden <mail@max-inden.de>"]
55
edition = "2021"
66
description = "Open Metrics client library allowing users to natively instrument applications."

src/encoding.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ impl<'a> From<protobuf::DescriptorEncoder<'a>> for DescriptorEncoder<'a> {
9898
}
9999
}
100100

101+
/// Select the pattern of metric serialization
102+
#[derive(Debug, PartialEq, Eq, Default, Clone, Copy)]
103+
pub enum OutputFormat {
104+
/// Used for the Prometheus format which does not add the suffix to the metric names.
105+
Prometheus,
106+
/// Used for the OpenMetrics format which adds the suffix for the metric names.
107+
#[default]
108+
OpenMetrics,
109+
}
110+
101111
impl DescriptorEncoder<'_> {
102112
pub(crate) fn with_prefix_and_labels<'s>(
103113
&'s mut self,
@@ -120,12 +130,16 @@ impl DescriptorEncoder<'_> {
120130
help: &str,
121131
unit: Option<&'s Unit>,
122132
metric_type: MetricType,
133+
output_format: OutputFormat,
123134
) -> Result<MetricEncoder<'s>, std::fmt::Error> {
124135
for_both_mut!(
125136
self,
126137
DescriptorEncoderInner,
127138
e,
128-
Ok(e.encode_descriptor(name, help, unit, metric_type)?.into())
139+
Ok(
140+
e.encode_descriptor(name, help, unit, metric_type, output_format)?
141+
.into()
142+
)
129143
)
130144
}
131145
}
@@ -203,13 +217,22 @@ impl MetricEncoder<'_> {
203217
&'s mut self,
204218
label_set: &'s S,
205219
) -> Result<MetricEncoder<'s>, std::fmt::Error> {
220+
let output_format = self.output_format();
206221
for_both_mut!(
207222
self,
208223
MetricEncoderInner,
209224
e,
210-
e.encode_family(label_set).map(Into::into)
225+
e.encode_family(label_set, output_format).map(Into::into)
211226
)
212227
}
228+
229+
/// Returns the `OutputFormat` selected for the encoder.
230+
pub fn output_format(&self) -> OutputFormat {
231+
match &self.0 {
232+
MetricEncoderInner::Text(metric_encoder) => metric_encoder.output_format,
233+
MetricEncoderInner::Protobuf(_) => Default::default(),
234+
}
235+
}
213236
}
214237

215238
/// An encodable label set.

src/encoding/protobuf.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub mod openmetrics_data_model {
3232

3333
use std::{borrow::Cow, collections::HashMap};
3434

35+
use crate::encoding::OutputFormat;
3536
use crate::metrics::MetricType;
3637
use crate::registry::{Registry, Unit};
3738
use crate::{metrics::exemplar::Exemplar, registry::Prefix};
@@ -43,7 +44,7 @@ use super::{EncodeCounterValue, EncodeExemplarValue, EncodeGaugeValue, EncodeLab
4344
pub fn encode(registry: &Registry) -> Result<openmetrics_data_model::MetricSet, std::fmt::Error> {
4445
let mut metric_set = openmetrics_data_model::MetricSet::default();
4546
let mut descriptor_encoder = DescriptorEncoder::new(&mut metric_set.metric_families).into();
46-
registry.encode(&mut descriptor_encoder)?;
47+
registry.encode(&mut descriptor_encoder, OutputFormat::OpenMetrics)?;
4748
Ok(metric_set)
4849
}
4950

@@ -98,6 +99,7 @@ impl DescriptorEncoder<'_> {
9899
help: &str,
99100
unit: Option<&Unit>,
100101
metric_type: MetricType,
102+
_output_format: OutputFormat,
101103
) -> Result<MetricEncoder<'s>, std::fmt::Error> {
102104
let family = openmetrics_data_model::MetricFamily {
103105
name: {
@@ -232,6 +234,7 @@ impl MetricEncoder<'_> {
232234
pub fn encode_family<S: EncodeLabelSet>(
233235
&mut self,
234236
label_set: &S,
237+
_output_format: OutputFormat,
235238
) -> Result<MetricEncoder<'_>, std::fmt::Error> {
236239
let mut labels = self.labels.clone();
237240
label_set.encode(

0 commit comments

Comments
 (0)