Skip to content

Commit

Permalink
refactor(encoding): replace EncodeLabelSet impl for () with priva…
Browse files Browse the repository at this point in the history
…te uninhabited type
  • Loading branch information
tylerlevine committed Jan 8, 2024
1 parent 21116bd commit cf8671e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 32 deletions.
5 changes: 4 additions & 1 deletion src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ pub trait EncodeLabel {
#[derive(Debug)]
pub struct LabelEncoder<'a>(LabelEncoderInner<'a>);

/// Uninhabited type to represent the lack of a label set for a metric
pub(crate) enum NoLabelSet {}

#[derive(Debug)]
enum LabelEncoderInner<'a> {
Text(text::LabelEncoder<'a>),
Expand Down Expand Up @@ -349,7 +352,7 @@ impl<T: EncodeLabel> EncodeLabelSet for Vec<T> {
}
}

impl EncodeLabelSet for () {
impl EncodeLabelSet for NoLabelSet {
fn encode(&self, _encoder: LabelSetEncoder) -> Result<(), std::fmt::Error> {
Ok(())
}
Expand Down
32 changes: 6 additions & 26 deletions src/encoding/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
//! assert_eq!(expected, buffer);
//! ```

use crate::encoding::{EncodeExemplarValue, EncodeLabelSet};
use crate::encoding::{EncodeExemplarValue, EncodeLabelSet, NoLabelSet};
use crate::metrics::exemplar::Exemplar;
use crate::metrics::MetricType;
use crate::registry::{Prefix, Registry, Unit};
Expand Down Expand Up @@ -186,7 +186,7 @@ impl<'a> MetricEncoder<'a> {

self.write_suffix("total")?;

self.encode_labels::<()>(None)?;
self.encode_labels::<NoLabelSet>(None)?;

v.encode(
&mut CounterValueEncoder {
Expand All @@ -210,7 +210,7 @@ impl<'a> MetricEncoder<'a> {
) -> Result<(), std::fmt::Error> {
self.write_prefix_name_unit()?;

self.encode_labels::<()>(None)?;
self.encode_labels::<NoLabelSet>(None)?;

v.encode(
&mut GaugeValueEncoder {
Expand Down Expand Up @@ -266,14 +266,14 @@ impl<'a> MetricEncoder<'a> {
) -> Result<(), std::fmt::Error> {
self.write_prefix_name_unit()?;
self.write_suffix("sum")?;
self.encode_labels::<()>(None)?;
self.encode_labels::<NoLabelSet>(None)?;
self.writer.write_str(" ")?;
self.writer.write_str(dtoa::Buffer::new().format(sum))?;
self.newline()?;

self.write_prefix_name_unit()?;
self.write_suffix("count")?;
self.encode_labels::<()>(None)?;
self.encode_labels::<NoLabelSet>(None)?;
self.writer.write_str(" ")?;
self.writer.write_str(itoa::Buffer::new().format(count))?;
self.newline()?;
Expand Down Expand Up @@ -747,24 +747,6 @@ mod tests {
parse_with_python_client(encoded);
}

#[test]
fn encode_histogram_family_with_empty_family_labels() {
let mut registry = Registry::default();
let family =
Family::new_with_constructor(|| Histogram::new(exponential_buckets(1.0, 2.0, 10)));
registry.register("my_histogram", "My histogram", family.clone());

family
.get_or_create(&())
.observe(1.0);

let mut encoded = String::new();

encode(&mut encoded, &registry).unwrap();

parse_with_python_client(encoded);
}

#[test]
fn encode_histogram_family_with_empty_struct_family_labels() {
let mut registry = Registry::default();
Expand All @@ -781,9 +763,7 @@ mod tests {
}
}

family
.get_or_create(&EmptyLabels {})
.observe(1.0);
family.get_or_create(&EmptyLabels {}).observe(1.0);

let mut encoded = String::new();

Expand Down
6 changes: 3 additions & 3 deletions src/metrics/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! See [`Counter`] for details.

use crate::encoding::{EncodeMetric, MetricEncoder};
use crate::encoding::{EncodeMetric, MetricEncoder, NoLabelSet};

use super::{MetricType, TypedMetric};
use std::marker::PhantomData;
Expand Down Expand Up @@ -179,7 +179,7 @@ where
A: Atomic<N>,
{
fn encode(&self, mut encoder: MetricEncoder) -> Result<(), std::fmt::Error> {
encoder.encode_counter::<(), _, u64>(&self.get(), None)
encoder.encode_counter::<NoLabelSet, _, u64>(&self.get(), None)
}

fn metric_type(&self) -> MetricType {
Expand Down Expand Up @@ -211,7 +211,7 @@ where
N: crate::encoding::EncodeCounterValue,
{
fn encode(&self, mut encoder: MetricEncoder) -> Result<(), std::fmt::Error> {
encoder.encode_counter::<(), _, u64>(&self.value, None)
encoder.encode_counter::<NoLabelSet, _, u64>(&self.value, None)
}

fn metric_type(&self) -> MetricType {
Expand Down
4 changes: 2 additions & 2 deletions src/metrics/histogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! See [`Histogram`] for details.

use crate::encoding::{EncodeMetric, MetricEncoder};
use crate::encoding::{EncodeMetric, MetricEncoder, NoLabelSet};

use super::{MetricType, TypedMetric};
use parking_lot::{MappedRwLockReadGuard, RwLock, RwLockReadGuard};
Expand Down Expand Up @@ -133,7 +133,7 @@ pub fn linear_buckets(start: f64, width: f64, length: u16) -> impl Iterator<Item
impl EncodeMetric for Histogram {
fn encode(&self, mut encoder: MetricEncoder) -> Result<(), std::fmt::Error> {
let (sum, count, buckets) = self.get();
encoder.encode_histogram::<()>(sum, count, &buckets, None)
encoder.encode_histogram::<NoLabelSet>(sum, count, &buckets, None)
}

fn metric_type(&self) -> MetricType {
Expand Down

0 comments on commit cf8671e

Please sign in to comment.