Skip to content
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 Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "prometheus-client"
version = "0.24.0"
version = "0.24.1"
authors = ["Max Inden <mail@max-inden.de>"]
edition = "2021"
description = "Open Metrics client library allowing users to natively instrument applications."
Expand Down
27 changes: 25 additions & 2 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ impl<'a> From<protobuf::DescriptorEncoder<'a>> for DescriptorEncoder<'a> {
}
}

/// Select the pattern of metric serialization
#[derive(Debug, PartialEq, Eq, Default, Clone, Copy)]
pub enum OutputFormat {
/// Used for the Prometheus format which does not add the suffix to the metric names.
Prometheus,
/// Used for the OpenMetrics format which adds the suffix for the metric names.
#[default]
OpenMetrics,
}

impl DescriptorEncoder<'_> {
pub(crate) fn with_prefix_and_labels<'s>(
&'s mut self,
Expand All @@ -120,12 +130,16 @@ impl DescriptorEncoder<'_> {
help: &str,
unit: Option<&'s Unit>,
metric_type: MetricType,
output_format: OutputFormat,
) -> Result<MetricEncoder<'s>, std::fmt::Error> {
for_both_mut!(
self,
DescriptorEncoderInner,
e,
Ok(e.encode_descriptor(name, help, unit, metric_type)?.into())
Ok(
e.encode_descriptor(name, help, unit, metric_type, output_format)?
.into()
)
)
}
}
Expand Down Expand Up @@ -203,13 +217,22 @@ impl MetricEncoder<'_> {
&'s mut self,
label_set: &'s S,
) -> Result<MetricEncoder<'s>, std::fmt::Error> {
let output_format = self.output_format();
for_both_mut!(
self,
MetricEncoderInner,
e,
e.encode_family(label_set).map(Into::into)
e.encode_family(label_set, output_format).map(Into::into)
)
}

/// Returns the `OutputFormat` selected for the encoder.
pub fn output_format(&self) -> OutputFormat {
match &self.0 {
MetricEncoderInner::Text(metric_encoder) => metric_encoder.output_format,
MetricEncoderInner::Protobuf(_) => Default::default(),
}
}
}

/// An encodable label set.
Expand Down
5 changes: 4 additions & 1 deletion src/encoding/protobuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub mod openmetrics_data_model {

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

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

Expand Down Expand Up @@ -98,6 +99,7 @@ impl DescriptorEncoder<'_> {
help: &str,
unit: Option<&Unit>,
metric_type: MetricType,
_output_format: OutputFormat,
) -> Result<MetricEncoder<'s>, std::fmt::Error> {
let family = openmetrics_data_model::MetricFamily {
name: {
Expand Down Expand Up @@ -232,6 +234,7 @@ impl MetricEncoder<'_> {
pub fn encode_family<S: EncodeLabelSet>(
&mut self,
label_set: &S,
_output_format: OutputFormat,
) -> Result<MetricEncoder<'_>, std::fmt::Error> {
let mut labels = self.labels.clone();
label_set.encode(
Expand Down
Loading