Skip to content
Merged
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 src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::encoding::DescriptorEncoder;
/// struct MyCollector {}
///
/// impl Collector for MyCollector {
/// fn encode<'a>(&'a self, mut encoder: DescriptorEncoder) -> Result<(), std::fmt::Error> {
/// fn encode(&self, mut encoder: DescriptorEncoder) -> Result<(), std::fmt::Error> {
/// let counter = ConstCounter::new(42);
/// let metric_encoder = encoder.encode_descriptor(
/// "my_counter",
Expand Down
66 changes: 33 additions & 33 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,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: 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 @@ -60,87 +60,87 @@ impl EncodeMetric for Box<dyn EncodeMetric> {

/// Encoder for a Metric Descriptor.
#[derive(Debug)]
pub struct DescriptorEncoder<'a, 'b>(DescriptorEncoderInner<'a, 'b>);
pub struct DescriptorEncoder<'a>(DescriptorEncoderInner<'a>);

#[derive(Debug)]
enum DescriptorEncoderInner<'a, 'b> {
Text(text::DescriptorEncoder<'a, 'b>),
enum DescriptorEncoderInner<'a> {
Text(text::DescriptorEncoder<'a>),

#[cfg(feature = "protobuf")]
Protobuf(protobuf::DescriptorEncoder<'a, 'b>),
Protobuf(protobuf::DescriptorEncoder<'a>),
}

impl<'a, 'b> From<text::DescriptorEncoder<'a, 'b>> for DescriptorEncoder<'a, 'b> {
fn from(e: text::DescriptorEncoder<'a, 'b>) -> Self {
impl<'a> From<text::DescriptorEncoder<'a>> for DescriptorEncoder<'a> {
fn from(e: text::DescriptorEncoder<'a>) -> Self {
Self(DescriptorEncoderInner::Text(e))
}
}

#[cfg(feature = "protobuf")]
impl<'a, 'b> From<protobuf::DescriptorEncoder<'a, 'b>> for DescriptorEncoder<'a, 'b> {
fn from(e: protobuf::DescriptorEncoder<'a, 'b>) -> Self {
impl<'a> From<protobuf::DescriptorEncoder<'a>> for DescriptorEncoder<'a> {
fn from(e: protobuf::DescriptorEncoder<'a>) -> Self {
Self(DescriptorEncoderInner::Protobuf(e))
}
}

impl<'a, 'b> DescriptorEncoder<'a, 'b> {
pub(crate) fn with_prefix_and_labels<'c, 'd>(
&'c mut self,
prefix: Option<&'d Prefix>,
labels: &'d [(Cow<'static, str>, Cow<'static, str>)],
impl DescriptorEncoder<'_> {
pub(crate) fn with_prefix_and_labels<'s>(
&'s mut self,
prefix: Option<&'s Prefix>,
labels: &'s [(Cow<'static, str>, Cow<'static, str>)],
// TODO: result needed?
) -> DescriptorEncoder<'c, 'd> {
) -> DescriptorEncoder<'s> {
for_both_mut!(
self,
DescriptorEncoderInner,
e,
e.with_prefix_and_labels(prefix, labels)
e.with_prefix_and_labels(prefix, labels).into()
)
}

/// Encode a descriptor.
pub fn encode_descriptor<'c, 'd, 'e>(
&'c mut self,
name: &'d str,
pub fn encode_descriptor<'s>(
&'s mut self,
name: &'s str,
help: &str,
unit: Option<&'d Unit>,
unit: Option<&'s Unit>,
metric_type: MetricType,
) -> Result<MetricEncoder<'c, 'd, 'c, 'e>, std::fmt::Error> {
) -> Result<MetricEncoder<'s>, std::fmt::Error> {
for_both_mut!(
self,
DescriptorEncoderInner,
e,
e.encode_descriptor(name, help, unit, metric_type)
Ok(e.encode_descriptor(name, help, unit, metric_type)?.into())
)
}
}

/// Encoder for a metric.
#[derive(Debug)]
pub struct MetricEncoder<'a, 'b, 'c, 'd>(MetricEncoderInner<'a, 'b, 'c, 'd>);
pub struct MetricEncoder<'a>(MetricEncoderInner<'a>);

#[derive(Debug)]
enum MetricEncoderInner<'a, 'b, 'c, 'd> {
Text(text::MetricEncoder<'a, 'b, 'c, 'd>),
enum MetricEncoderInner<'a> {
Text(text::MetricEncoder<'a>),

#[cfg(feature = "protobuf")]
Protobuf(protobuf::MetricEncoder<'a>),
}

impl<'a, 'b, 'c, 'd> From<text::MetricEncoder<'a, 'b, 'c, 'd>> for MetricEncoder<'a, 'b, 'c, 'd> {
fn from(e: text::MetricEncoder<'a, 'b, 'c, 'd>) -> Self {
impl<'a> From<text::MetricEncoder<'a>> for MetricEncoder<'a> {
fn from(e: text::MetricEncoder<'a>) -> Self {
Self(MetricEncoderInner::Text(e))
}
}

#[cfg(feature = "protobuf")]
impl<'a, 'b, 'c, 'd> From<protobuf::MetricEncoder<'a>> for MetricEncoder<'a, 'b, 'c, 'd> {
impl<'a> From<protobuf::MetricEncoder<'a>> for MetricEncoder<'a> {
fn from(e: protobuf::MetricEncoder<'a>) -> Self {
Self(MetricEncoderInner::Protobuf(e))
}
}

impl<'a, 'b, 'c, 'd> MetricEncoder<'a, 'b, 'c, 'd> {
impl MetricEncoder<'_> {
/// Encode a counter.
pub fn encode_counter<
S: EncodeLabelSet,
Expand Down Expand Up @@ -184,10 +184,10 @@ impl<'a, 'b, 'c, 'd> MetricEncoder<'a, 'b, 'c, 'd> {
}

/// Encode a metric family.
pub fn encode_family<'e, 'f, S: EncodeLabelSet>(
&'e mut self,
label_set: &'f S,
) -> Result<MetricEncoder<'e, 'e, 'e, 'f>, std::fmt::Error> {
pub fn encode_family<'s, S: EncodeLabelSet>(
&'s mut self,
label_set: &'s S,
) -> Result<MetricEncoder<'s>, std::fmt::Error> {
for_both_mut!(
self,
MetricEncoderInner,
Expand Down
54 changes: 26 additions & 28 deletions src/encoding/protobuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,43 +63,42 @@ impl From<MetricType> for openmetrics_data_model::MetricType {
///
/// This is an inner type for [`super::DescriptorEncoder`].
#[derive(Debug)]
pub(crate) struct DescriptorEncoder<'a, 'b> {
pub(crate) struct DescriptorEncoder<'a> {
metric_families: &'a mut Vec<openmetrics_data_model::MetricFamily>,
prefix: Option<&'b Prefix>,
labels: &'b [(Cow<'static, str>, Cow<'static, str>)],
prefix: Option<&'a Prefix>,
labels: &'a [(Cow<'static, str>, Cow<'static, str>)],
}

impl<'a, 'b> DescriptorEncoder<'a, 'b> {
pub(crate) fn new<'c, 'd>(
metric_families: &'c mut Vec<openmetrics_data_model::MetricFamily>,
) -> DescriptorEncoder<'c, 'd> {
impl DescriptorEncoder<'_> {
pub(crate) fn new(
metric_families: &mut Vec<openmetrics_data_model::MetricFamily>,
) -> DescriptorEncoder {
DescriptorEncoder {
metric_families,
prefix: Default::default(),
labels: Default::default(),
}
}

pub(crate) fn with_prefix_and_labels<'c, 'd>(
&'c mut self,
prefix: Option<&'d Prefix>,
labels: &'d [(Cow<'static, str>, Cow<'static, str>)],
) -> super::DescriptorEncoder<'c, 'd> {
pub(crate) fn with_prefix_and_labels<'s>(
&'s mut self,
prefix: Option<&'s Prefix>,
labels: &'s [(Cow<'static, str>, Cow<'static, str>)],
) -> DescriptorEncoder<'s> {
DescriptorEncoder {
prefix,
labels,
metric_families: self.metric_families,
}
.into()
}

pub fn encode_descriptor<'c, 'd, 'e>(
&'c mut self,
name: &'d str,
pub fn encode_descriptor<'s>(
&'s mut self,
name: &str,
help: &str,
unit: Option<&'d Unit>,
unit: Option<&Unit>,
metric_type: MetricType,
) -> Result<super::MetricEncoder<'c, 'd, 'c, 'e>, std::fmt::Error> {
) -> Result<MetricEncoder<'s>, std::fmt::Error> {
let family = openmetrics_data_model::MetricFamily {
name: {
match self.prefix {
Expand Down Expand Up @@ -127,34 +126,33 @@ impl<'a, 'b> DescriptorEncoder<'a, 'b> {
.into(),
)?;
self.metric_families.push(family);
let encoder = MetricEncoder {

Ok(MetricEncoder {
family: &mut self
.metric_families
.last_mut()
.expect("previous push")
.metrics,
metric_type,
labels,
};

Ok(encoder.into())
})
}
}

/// Encoder for protobuf encoding.
///
/// This is an inner type for [`super::MetricEncoder`].
#[derive(Debug)]
pub(crate) struct MetricEncoder<'a> {
pub(crate) struct MetricEncoder<'f> {
/// OpenMetrics metric type of the metric.
metric_type: MetricType,
/// Vector of OpenMetrics metrics to which encoded metrics are added.
family: &'a mut Vec<openmetrics_data_model::Metric>,
family: &'f mut Vec<openmetrics_data_model::Metric>,
/// Labels to be added to each metric.
labels: Vec<openmetrics_data_model::Label>,
}

impl<'a> MetricEncoder<'a> {
impl MetricEncoder<'_> {
pub fn encode_counter<
S: EncodeLabelSet,
CounterValue: EncodeCounterValue,
Expand Down Expand Up @@ -231,10 +229,10 @@ impl<'a> MetricEncoder<'a> {
Ok(())
}

pub fn encode_family<'b, S: EncodeLabelSet>(
&'b mut self,
pub fn encode_family<S: EncodeLabelSet>(
&mut self,
label_set: &S,
) -> Result<MetricEncoder<'b>, std::fmt::Error> {
) -> Result<MetricEncoder, std::fmt::Error> {
let mut labels = self.labels.clone();
label_set.encode(
LabelSetEncoder {
Expand Down
Loading