1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use crate::api::metrics;

mod prometheus_metrics;

pub struct Meter {
    registry: &'static prometheus::Registry,
    component: &'static str,
}

impl Meter {
    pub fn new(component: &'static str) -> Self {
        Meter {
            registry: prometheus::default_registry(),
            component,
        }
    }

    fn build_opts(&self, name: String, description: String) -> prometheus::Opts {
        // Prometheus cannot have empty help strings
        let help = if !description.is_empty() {
            description
        } else {
            format!("{} metric", name)
        };
        prometheus::Opts::new(name, help).namespace(format!("{}_", self.component))
    }
}

impl metrics::Meter for Meter {
    type LabelSet = prometheus_metrics::LabelSet;
    type I64Counter = prometheus::IntCounterVec;
    type F64Counter = prometheus::CounterVec;
    type I64Gauge = prometheus::IntGaugeVec;
    type F64Gauge = prometheus::GaugeVec;
    type I64Measure = prometheus_metrics::IntMeasure;
    type F64Measure = prometheus::HistogramVec;

    fn labels(&self, key_values: Vec<crate::KeyValue>) -> Self::LabelSet {
        let mut label_set: Self::LabelSet = Default::default();

        for crate::KeyValue { key, value } in key_values.into_iter() {
            label_set.insert(key.into(), value.into());
        }

        label_set
    }

    fn new_i64_counter<S: Into<String>>(
        &self,
        name: S,
        opts: metrics::Options,
    ) -> Self::I64Counter {
        let metrics::Options {
            description,
            unit: _unit,
            keys,
            alternate: _alternative,
        } = opts;
        let counter_opts = self.build_opts(name.into(), description);
        let labels = prometheus_metrics::convert_labels(&keys);
        let counter = prometheus::IntCounterVec::new(counter_opts, &labels).unwrap();
        self.registry.register(Box::new(counter.clone())).unwrap();

        counter
    }

    fn new_f64_counter<S: Into<String>>(
        &self,
        name: S,
        opts: metrics::Options,
    ) -> Self::F64Counter {
        let metrics::Options {
            description,
            unit: _unit,
            keys,
            alternate: _alternative,
        } = opts;
        let counter_opts = self.build_opts(name.into(), description);
        let labels = prometheus_metrics::convert_labels(&keys);
        let counter = prometheus::CounterVec::new(counter_opts, &labels).unwrap();
        self.registry.register(Box::new(counter.clone())).unwrap();

        counter
    }

    fn new_i64_gauge<S: Into<String>>(&self, name: S, opts: metrics::Options) -> Self::I64Gauge {
        let metrics::Options {
            description,
            unit: _unit,
            keys,
            alternate: _alternative,
        } = opts;
        let gauge_opts = self.build_opts(name.into(), description);
        let labels = prometheus_metrics::convert_labels(&keys);
        let gauge = prometheus::IntGaugeVec::new(gauge_opts, &labels).unwrap();
        self.registry.register(Box::new(gauge.clone())).unwrap();

        gauge
    }

    fn new_f64_gauge<S: Into<String>>(&self, name: S, opts: metrics::Options) -> Self::F64Gauge {
        let metrics::Options {
            description,
            unit: _unit,
            keys,
            alternate: _alternative,
        } = opts;
        let gauge_opts = self.build_opts(name.into(), description);
        let labels = prometheus_metrics::convert_labels(&keys);
        let gauge = prometheus::GaugeVec::new(gauge_opts, &labels).unwrap();
        self.registry.register(Box::new(gauge.clone())).unwrap();

        gauge
    }

    fn new_i64_measure<S: Into<String>>(
        &self,
        name: S,
        opts: metrics::Options,
    ) -> Self::I64Measure {
        let metrics::Options {
            description,
            unit: _unit,
            keys,
            alternate: _alternative,
        } = opts;
        let common_opts = self.build_opts(name.into(), description);
        let labels = prometheus_metrics::convert_labels(&keys);
        let histogram_opts = prometheus::HistogramOpts::from(common_opts);
        let histogram = prometheus::HistogramVec::new(histogram_opts, &labels).unwrap();
        self.registry.register(Box::new(histogram.clone())).unwrap();

        prometheus_metrics::IntMeasure::new(histogram)
    }

    fn new_f64_measure<S: Into<String>>(
        &self,
        name: S,
        opts: metrics::Options,
    ) -> Self::F64Measure {
        let metrics::Options {
            description,
            unit: _unit,
            keys,
            alternate: _alternative,
        } = opts;
        let common_opts = self.build_opts(name.into(), description);
        let labels = prometheus_metrics::convert_labels(&keys);
        let histogram_opts = prometheus::HistogramOpts::from(common_opts);
        let histogram = prometheus::HistogramVec::new(histogram_opts, &labels).unwrap();
        self.registry.register(Box::new(histogram.clone())).unwrap();

        histogram
    }

    fn record_batch<M: IntoIterator<Item = metrics::Measurement<Self::LabelSet>>>(
        &self,
        label_set: &Self::LabelSet,
        measurements: M,
    ) {
        for measure in measurements.into_iter() {
            let instrument = measure.instrument();
            instrument.record_one(measure.into_value(), &label_set);
        }
    }
}