Skip to content

Commit d2ad9df

Browse files
Introduce encoding::Number type
The different types of numbers in the prometheus ecosystem is limited to signed & unsigned integers and double values. We can thus represent those as an enum and delete a fair bit of code.
1 parent 00e9508 commit d2ad9df

File tree

7 files changed

+169
-507
lines changed

7 files changed

+169
-507
lines changed

examples/custom-metric.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use prometheus_client::encoding::{text::encode, EncodeMetric, MetricEncoder};
1+
use prometheus_client::encoding::{text::encode, EncodeMetric, MetricEncoder, Number};
22
use prometheus_client::metrics::MetricType;
33
use prometheus_client::registry::Registry;
44

@@ -21,7 +21,7 @@ impl EncodeMetric for MyCustomMetric {
2121
// the Prometheus server.
2222

2323
// TODO: Ideally the type hints would not be needed.
24-
encoder.encode_counter::<(), u64>(rand::random::<u64>(), None)
24+
encoder.encode_counter::<()>(Number::UnsignedInteger(rand::random::<u64>()), None)
2525
}
2626

2727
fn metric_type(&self) -> prometheus_client::metrics::MetricType {

src/encoding.rs

+15-141
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ macro_rules! for_both {
7777
}
7878

7979
impl<'a> MetricEncoder<'a> {
80-
pub fn encode_counter<S: EncodeLabelSet, V: EncodeExemplarValue>(
80+
pub fn encode_counter<S: EncodeLabelSet>(
8181
&mut self,
82-
v: impl EncodeCounterValue,
83-
exemplar: Option<&Exemplar<S, V>>,
82+
v: Number,
83+
exemplar: Option<&Exemplar<S>>,
8484
) -> Result<(), std::fmt::Error> {
8585
for_both!(self, e, e.encode_counter(v, exemplar))
8686
}
8787

88-
pub fn encode_gauge(&mut self, v: impl EncodeGaugeValue) -> Result<(), std::fmt::Error> {
88+
pub fn encode_gauge(&mut self, v: Number) -> Result<(), std::fmt::Error> {
8989
for_both!(self, e, e.encode_gauge(v))
9090
}
9191

@@ -98,7 +98,7 @@ impl<'a> MetricEncoder<'a> {
9898
sum: f64,
9999
count: u64,
100100
buckets: &[(f64, u64)],
101-
exemplars: Option<&HashMap<usize, Exemplar<S, f64>>>,
101+
exemplars: Option<&HashMap<usize, Exemplar<S>>>,
102102
) -> Result<(), std::fmt::Error> {
103103
for_both!(self, e, e.encode_histogram(sum, count, buckets, exemplars))
104104
}
@@ -320,145 +320,19 @@ impl<'a> EncodeLabelValue for u64 {
320320
}
321321
}
322322

323-
pub trait EncodeGaugeValue {
324-
/// Encode the given instance in the OpenMetrics text encoding.
325-
fn encode(&self, encoder: &mut GaugeValueEncoder) -> Result<(), std::fmt::Error>;
326-
}
327-
328-
impl EncodeGaugeValue for i64 {
329-
fn encode(&self, encoder: &mut GaugeValueEncoder) -> Result<(), std::fmt::Error> {
330-
encoder.encode_i64(*self)
331-
}
332-
}
333-
334-
impl EncodeGaugeValue for f64 {
335-
fn encode(&self, encoder: &mut GaugeValueEncoder) -> Result<(), std::fmt::Error> {
336-
encoder.encode_f64(*self)
337-
}
338-
}
339-
340-
pub enum GaugeValueEncoder<'a> {
341-
Text(text::GaugeValueEncoder<'a>),
342-
Protobuf(proto::GaugeValueEncoder),
323+
#[derive(Debug, Clone, Copy)]
324+
pub enum Number {
325+
SignedInteger(i64),
326+
UnsignedInteger(u64),
327+
Double(f64),
343328
}
344329

345-
impl<'a> GaugeValueEncoder<'a> {
346-
fn encode_f64(&mut self, v: f64) -> Result<(), std::fmt::Error> {
330+
impl Number {
331+
pub fn as_f64(self) -> f64 {
347332
match self {
348-
GaugeValueEncoder::Text(e) => e.encode_f64(v),
349-
GaugeValueEncoder::Protobuf(e) => e.encode_f64(v),
333+
Number::SignedInteger(i) => i as f64,
334+
Number::UnsignedInteger(i) => i as f64,
335+
Number::Double(d) => d,
350336
}
351337
}
352-
353-
fn encode_i64(&mut self, v: i64) -> Result<(), std::fmt::Error> {
354-
match self {
355-
GaugeValueEncoder::Text(e) => e.encode_i64(v),
356-
GaugeValueEncoder::Protobuf(e) => e.encode_i64(v),
357-
}
358-
}
359-
}
360-
361-
impl<'a> From<text::GaugeValueEncoder<'a>> for GaugeValueEncoder<'a> {
362-
fn from(e: text::GaugeValueEncoder<'a>) -> Self {
363-
GaugeValueEncoder::Text(e)
364-
}
365-
}
366-
367-
impl<'a> From<proto::GaugeValueEncoder> for GaugeValueEncoder<'a> {
368-
fn from(e: proto::GaugeValueEncoder) -> Self {
369-
GaugeValueEncoder::Protobuf(e)
370-
}
371-
}
372-
373-
pub trait EncodeCounterValue {
374-
/// Encode the given instance in the OpenMetrics text encoding.
375-
fn encode(&self, encoder: &mut CounterValueEncoder) -> Result<(), std::fmt::Error>;
376-
}
377-
378-
impl EncodeCounterValue for u64 {
379-
fn encode(&self, encoder: &mut CounterValueEncoder) -> Result<(), std::fmt::Error> {
380-
encoder.encode_u64(*self)
381-
}
382-
}
383-
384-
impl EncodeCounterValue for f64 {
385-
fn encode(&self, encoder: &mut CounterValueEncoder) -> Result<(), std::fmt::Error> {
386-
encoder.encode_f64(*self)
387-
}
388-
}
389-
390-
pub enum CounterValueEncoder<'a> {
391-
Text(text::CounterValueEncoder<'a>),
392-
Protobuf(proto::CounterValueEncoder),
393-
}
394-
395-
impl<'a> CounterValueEncoder<'a> {
396-
fn encode_f64(&mut self, v: f64) -> Result<(), std::fmt::Error> {
397-
match self {
398-
CounterValueEncoder::Text(e) => e.encode_f64(v),
399-
CounterValueEncoder::Protobuf(e) => e.encode_f64(v),
400-
}
401-
}
402-
403-
fn encode_u64(&mut self, v: u64) -> Result<(), std::fmt::Error> {
404-
match self {
405-
CounterValueEncoder::Text(e) => e.encode_u64(v),
406-
CounterValueEncoder::Protobuf(e) => e.encode_u64(v),
407-
}
408-
}
409-
}
410-
411-
pub trait EncodeExemplarValue {
412-
/// Encode the given instance in the OpenMetrics text encoding.
413-
fn encode(&self, encoder: ExemplarValueEncoder) -> Result<(), std::fmt::Error>;
414-
}
415-
416-
impl EncodeExemplarValue for f64 {
417-
fn encode(&self, mut encoder: ExemplarValueEncoder) -> Result<(), std::fmt::Error> {
418-
encoder.encode(*self)
419-
}
420-
}
421-
422-
impl EncodeExemplarValue for u64 {
423-
fn encode(&self, mut encoder: ExemplarValueEncoder) -> Result<(), std::fmt::Error> {
424-
encoder.encode(*self as f64)
425-
}
426-
}
427-
428-
impl<'a> From<text::CounterValueEncoder<'a>> for CounterValueEncoder<'a> {
429-
fn from(e: text::CounterValueEncoder<'a>) -> Self {
430-
CounterValueEncoder::Text(e)
431-
}
432-
}
433-
434-
impl<'a> From<proto::CounterValueEncoder> for CounterValueEncoder<'a> {
435-
fn from(e: proto::CounterValueEncoder) -> Self {
436-
CounterValueEncoder::Protobuf(e)
437-
}
438-
}
439-
440-
pub enum ExemplarValueEncoder<'a> {
441-
Text(text::ExemplarValueEncoder<'a>),
442-
Protobuf(proto::ExemplarValueEncoder<'a>),
443-
}
444-
445-
impl<'a> ExemplarValueEncoder<'a> {
446-
fn encode(&mut self, v: f64) -> Result<(), std::fmt::Error> {
447-
match self {
448-
ExemplarValueEncoder::Text(e) => e.encode(v),
449-
ExemplarValueEncoder::Protobuf(e) => e.encode(v),
450-
}
451-
}
452-
}
453-
454-
impl<'a> From<text::ExemplarValueEncoder<'a>> for ExemplarValueEncoder<'a> {
455-
fn from(e: text::ExemplarValueEncoder<'a>) -> Self {
456-
ExemplarValueEncoder::Text(e)
457-
}
458-
}
459-
460-
impl<'a> From<proto::ExemplarValueEncoder<'a>> for ExemplarValueEncoder<'a> {
461-
fn from(e: proto::ExemplarValueEncoder<'a>) -> Self {
462-
ExemplarValueEncoder::Protobuf(e)
463-
}
464338
}

0 commit comments

Comments
 (0)