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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//! # OpenTelemetry Prometheus Exporter
//!
//! This exporter currently delegates to the [Prometheus library]
//! library which implements the [Prometheus API].
//!
//! [Prometheus library]: https://github.com/tikv/rust-prometheus
//! [Prometheus API]: https://prometheus.io
use crate::api;
use crate::sdk;
use api::Key;
pub use prometheus::{
    default_registry, gather, Counter, CounterVec, Encoder, Gauge, GaugeVec, Histogram,
    HistogramOpts, HistogramVec, IntCounter, IntCounterVec, IntGauge, IntGaugeVec, Opts, Registry,
    TextEncoder,
};
use std::collections::HashMap;
use std::sync::Arc;

/// Convert from `sdk::LabelSet` to `prometheus`' label format.
fn convert_label_set(label_set: &sdk::LabelSet) -> HashMap<&str, &str> {
    label_set
        .iter()
        .map(|(key, value)| (key.as_ref(), value.as_ref()))
        .collect()
}

/// Convert from list of `Key`s to prometheus' label format.
pub(crate) fn convert_labels(labels: &[Key]) -> Vec<&str> {
    labels
        .iter()
        .map(|k| k.inner())
        .map(|k| k.as_ref())
        .collect()
}

/// Prometheus IntCounterHandle
#[derive(Clone)]
#[allow(missing_debug_implementations)]
pub struct IntCounterHandle(prometheus::IntCounter);

impl api::Counter<i64, sdk::LabelSet> for prometheus::IntCounterVec {
    /// Prometheus' `CounterHandle`
    type Handle = IntCounterHandle;

    /// Creates a `Measurement` object to be used by a `Meter` when batch recording.
    fn measurement(&self, value: i64) -> api::Measurement<sdk::LabelSet> {
        api::Measurement::new(Arc::new(self.clone()), api::MeasurementValue::from(value))
    }

    /// Creates a handle for this instrument.
    fn acquire_handle(&self, labels: &sdk::LabelSet) -> Self::Handle {
        IntCounterHandle(self.with(&convert_label_set(labels)))
    }
}

impl api::Instrument<sdk::LabelSet> for prometheus::IntCounterVec {
    /// Record a single counter measurement value
    fn record_one(&self, value: api::MeasurementValue, label_set: &sdk::LabelSet) {
        self.with(&convert_label_set(label_set))
            .inc_by(value.into_i64())
    }
}

impl api::InstrumentHandle for IntCounterHandle {
    /// Record a single counter measurement value for preset values
    fn record_one(&self, value: api::MeasurementValue) {
        self.0.inc_by(value.into_i64())
    }
}

impl api::CounterHandle<i64> for IntCounterHandle {}

/// Prometheus CounterHandle
#[derive(Clone)]
#[allow(missing_debug_implementations)]
pub struct CounterHandle(prometheus::Counter);

impl api::Counter<f64, sdk::LabelSet> for prometheus::CounterVec {
    type Handle = CounterHandle;

    /// Creates a `Measurement` object to be used by a `Meter` when batch recording.
    fn measurement(&self, value: f64) -> api::Measurement<sdk::LabelSet> {
        api::Measurement::new(Arc::new(self.clone()), api::MeasurementValue::from(value))
    }

    /// Creates a handle for this instrument.
    fn acquire_handle(&self, labels: &sdk::LabelSet) -> Self::Handle {
        CounterHandle(self.with(&convert_label_set(labels)))
    }
}

impl api::Instrument<sdk::LabelSet> for prometheus::CounterVec {
    /// record a single counter measurement value
    fn record_one(&self, value: api::MeasurementValue, label_set: &sdk::LabelSet) {
        self.with(&convert_label_set(label_set))
            .inc_by(value.into_f64())
    }
}

impl api::InstrumentHandle for CounterHandle {
    /// record a single counter measurement value for precomputed labels
    fn record_one(&self, value: api::MeasurementValue) {
        self.0.inc_by(value.into_f64())
    }
}

impl api::CounterHandle<f64> for CounterHandle {}

// GAUGE COMPAT

/// Prometheus IntGaugeHandle
#[derive(Clone)]
#[allow(missing_debug_implementations)]
pub struct IntGaugeHandle(prometheus::IntGauge);

impl api::Gauge<i64, sdk::LabelSet> for prometheus::IntGaugeVec {
    type Handle = IntGaugeHandle;

    /// Creates a `Measurement` object to be used by a `Meter` when batch recording.
    fn measurement(&self, value: i64) -> api::Measurement<sdk::LabelSet> {
        api::Measurement::new(Arc::new(self.clone()), api::MeasurementValue::from(value))
    }

    /// Creates a handle for this instrument.
    fn acquire_handle(&self, labels: &sdk::LabelSet) -> Self::Handle {
        IntGaugeHandle(self.with(&convert_label_set(labels)))
    }
}

impl api::Instrument<sdk::LabelSet> for prometheus::IntGaugeVec {
    /// record a single gauge measurement value
    fn record_one(&self, value: api::MeasurementValue, label_set: &sdk::LabelSet) {
        self.with(&convert_label_set(label_set))
            .set(value.into_i64())
    }
}

impl api::InstrumentHandle for IntGaugeHandle {
    /// record a single gauge measurement value for precomputed labels
    fn record_one(&self, value: api::MeasurementValue) {
        self.0.set(value.into_i64())
    }
}

impl api::GaugeHandle<i64> for IntGaugeHandle {}

/// Prometheus GaugeHandle
#[derive(Clone)]
#[allow(missing_debug_implementations)]
pub struct GaugeHandle(prometheus::Gauge);

impl api::Gauge<f64, sdk::LabelSet> for prometheus::GaugeVec {
    type Handle = GaugeHandle;

    /// Creates a `Measurement` object to be used by a `Meter` when batch recording.
    fn measurement(&self, value: f64) -> api::Measurement<sdk::LabelSet> {
        api::Measurement::new(Arc::new(self.clone()), api::MeasurementValue::from(value))
    }

    /// Creates a handle for this instrument.
    fn acquire_handle(&self, labels: &sdk::LabelSet) -> Self::Handle {
        GaugeHandle(self.with(&convert_label_set(labels)))
    }
}

impl api::Instrument<sdk::LabelSet> for prometheus::GaugeVec {
    /// record a single gauge measurement value
    fn record_one(&self, value: api::MeasurementValue, label_set: &sdk::LabelSet) {
        self.with(&convert_label_set(label_set))
            .set(value.into_f64())
    }
}

impl api::InstrumentHandle for GaugeHandle {
    /// record a single gauge measurement value for precomputed labels
    fn record_one(&self, value: api::MeasurementValue) {
        self.0.set(value.into_f64())
    }
}

impl api::GaugeHandle<f64> for GaugeHandle {}

// MEASURE COMPAT

/// Prometheus IntMeasureHandle
#[derive(Clone)]
#[allow(missing_debug_implementations)]
pub struct IntMeasureHandle(prometheus::Histogram);

/// Prometheus Histograms do not have i64 variant, `IntMeasure` will convert i64 to float when it
/// records values.
#[derive(Clone)]
#[allow(missing_debug_implementations)]
pub struct IntMeasure(prometheus::HistogramVec);

impl IntMeasure {
    pub(crate) fn new(histogram: prometheus::HistogramVec) -> Self {
        IntMeasure(histogram)
    }
}

impl api::Measure<i64, sdk::LabelSet> for IntMeasure {
    type Handle = IntMeasureHandle;

    /// Creates a `Measurement` object to be used by a `Meter` when batch recording.
    fn measurement(&self, value: i64) -> api::Measurement<sdk::LabelSet> {
        api::Measurement::new(Arc::new(self.clone()), api::MeasurementValue::from(value))
    }

    /// Creates a handle for this instrument.
    fn acquire_handle(&self, labels: &sdk::LabelSet) -> Self::Handle {
        IntMeasureHandle(self.0.with(&convert_label_set(labels)))
    }
}

impl api::Instrument<sdk::LabelSet> for IntMeasure {
    /// record a single measure measurement value
    fn record_one(&self, value: api::MeasurementValue, label_set: &sdk::LabelSet) {
        self.0
            .with(&convert_label_set(label_set))
            .observe(value.into_i64() as f64)
    }
}

impl api::InstrumentHandle for IntMeasureHandle {
    /// record a single measure measurement value for precomputed labels
    fn record_one(&self, value: api::MeasurementValue) {
        self.0.observe(value.into_i64() as f64)
    }
}

impl api::MeasureHandle<i64> for IntMeasureHandle {}

/// Prometheus MeasureHandle
#[derive(Clone)]
#[allow(missing_debug_implementations)]
pub struct MeasureHandle(prometheus::Histogram);

impl api::Measure<f64, sdk::LabelSet> for prometheus::HistogramVec {
    type Handle = MeasureHandle;

    /// Creates a `Measurement` object to be used by a `Meter` when batch recording.
    fn measurement(&self, value: f64) -> api::Measurement<sdk::LabelSet> {
        api::Measurement::new(Arc::new(self.clone()), api::MeasurementValue::from(value))
    }

    /// Creates a handle for this instrument.
    fn acquire_handle(&self, labels: &sdk::LabelSet) -> Self::Handle {
        MeasureHandle(self.with(&convert_label_set(labels)))
    }
}

impl api::Instrument<sdk::LabelSet> for prometheus::HistogramVec {
    /// record a single measure measurement value
    fn record_one(&self, value: api::MeasurementValue, label_set: &sdk::LabelSet) {
        self.with(&convert_label_set(label_set))
            .observe(value.into_f64())
    }
}

impl api::InstrumentHandle for MeasureHandle {
    /// record a single measure measurement value for precomputed labels
    fn record_one(&self, value: api::MeasurementValue) {
        self.0.observe(value.into_f64())
    }
}

impl api::MeasureHandle<f64> for MeasureHandle {}