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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
//! Wrappers for metric types defined in `prometheus-client`.

use elsa::sync::FrozenMap;
use once_cell::sync::OnceCell;
use prometheus_client::{
    encoding::{
        EncodeLabelKey, EncodeLabelSet, EncodeLabelValue, EncodeMetric, LabelKeyEncoder,
        LabelValueEncoder, MetricEncoder,
    },
    metrics::{
        gauge::Gauge as GaugeInner, histogram::Histogram as HistogramInner, MetricType, TypedMetric,
    },
    registry::Unit,
};

use std::{
    collections::HashMap,
    fmt,
    hash::Hash,
    marker::PhantomData,
    ops,
    sync::Arc,
    time::{Duration, Instant},
};

use crate::{
    buckets::Buckets,
    builder::BuildMetric,
    traits::{EncodedGaugeValue, GaugeValue, HistogramValue, MapLabels},
};

/// Label with a unit suffix implementing [`EncodeLabelKey`].
#[doc(hidden)] // used in proc macros only
#[derive(Debug)]
pub struct LabelWithUnit {
    name: &'static str,
    unit: Unit,
}

impl LabelWithUnit {
    pub const fn new(name: &'static str, unit: Unit) -> Self {
        Self { name, unit }
    }
}

impl EncodeLabelKey for LabelWithUnit {
    fn encode(&self, encoder: &mut LabelKeyEncoder<'_>) -> fmt::Result {
        use std::fmt::Write as _;

        write!(encoder, "{}_{}", self.name, self.unit.as_str())
    }
}

/// Wraps a [`Duration`] so that it can be used as a label value, which will be set to the fractional
/// number of seconds in the duration, i.e. [`Duration::as_secs_f64()`]. Mostly useful for [`Info`] metrics.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DurationAsSecs(pub Duration);

impl From<Duration> for DurationAsSecs {
    fn from(duration: Duration) -> Self {
        Self(duration)
    }
}

impl EncodeLabelValue for DurationAsSecs {
    fn encode(&self, encoder: &mut LabelValueEncoder) -> fmt::Result {
        EncodeLabelValue::encode(&self.0.as_secs_f64(), encoder)
    }
}

/// Gauge metric.
///
/// Gauges are integer or floating-point values that can go up or down. Logically, a reported gauge value
/// can be treated as valid until the next value is reported.
///
/// Gauge values must implement the [`GaugeValue`] trait.
pub struct Gauge<V: GaugeValue = i64>(GaugeInner<V, V::Atomic>);

impl<V: GaugeValue> fmt::Debug for Gauge<V> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&self.0, formatter)
    }
}

impl<V: GaugeValue> Clone for Gauge<V> {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl<V: GaugeValue> Default for Gauge<V> {
    fn default() -> Self {
        Self(GaugeInner::default())
    }
}

impl<V: GaugeValue> Gauge<V> {
    /// Increases this [`Gauge`] by `v`, returning the previous value.
    pub fn inc_by(&self, v: V) -> V {
        self.0.inc_by(v)
    }

    /// Increases this [`Gauge`] by `v` and returns a guard that will decrement this value back
    /// when dropped. This can be useful for gauges that measure consumption of a certain resource.
    pub fn inc_guard(&self, v: V) -> GaugeGuard<V> {
        let guard = GaugeGuard {
            gauge: self.clone(),
            increment: v,
        };
        self.0.inc_by(v);
        guard
    }

    /// Decreases this [`Gauge`] by `v`, returning the previous value.
    ///
    /// # Panics
    ///
    /// Depending on the value type, this method may panic on underflow; use with care.
    pub fn dec_by(&self, v: V) -> V {
        self.0.dec_by(v)
    }

    /// Sets the value of this [`Gauge`] returning the previous value.
    pub fn set(&self, value: V) -> V {
        self.0.set(value)
    }

    /// Gets the current value of the gauge.
    pub fn get(&self) -> V {
        self.0.get()
    }
}

impl<V: GaugeValue> EncodeMetric for Gauge<V> {
    fn encode(&self, mut encoder: MetricEncoder<'_>) -> fmt::Result {
        match self.get().encode() {
            EncodedGaugeValue::I64(value) => encoder.encode_gauge(&value),
            EncodedGaugeValue::F64(value) => encoder.encode_gauge(&value),
        }
    }

    fn metric_type(&self) -> MetricType {
        <Self as TypedMetric>::TYPE
    }
}

impl<V: GaugeValue> TypedMetric for Gauge<V> {
    const TYPE: MetricType = MetricType::Gauge;
}

/// Guard for a [`Gauge`] returned by [`Gauge::inc_guard()`]. When dropped, a guard decrements
/// the gauge by the same value that it was increased by when creating the guard.
#[derive(Debug)]
pub struct GaugeGuard<V: GaugeValue = i64> {
    gauge: Gauge<V>,
    increment: V,
}

impl<V: GaugeValue> Drop for GaugeGuard<V> {
    fn drop(&mut self) {
        self.gauge.dec_by(self.increment);
    }
}

/// Histogram metric.
///
/// Histograms are floating-point values counted in configurable buckets. Logically, a histogram observes
/// a certain probability distribution, and observations are transient (unlike gauge values).
///
/// Histogram values must implement the [`HistogramValue`] trait.
#[derive(Debug)]
pub struct Histogram<V: HistogramValue = f64> {
    inner: HistogramInner,
    _value: PhantomData<V>,
}

impl<V: HistogramValue> Clone for Histogram<V> {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
            _value: PhantomData,
        }
    }
}

impl<V: HistogramValue> Histogram<V> {
    pub(crate) fn new(buckets: Buckets) -> Self {
        Self {
            inner: HistogramInner::new(buckets.iter()),
            _value: PhantomData,
        }
    }

    /// Observes the specified `value` of the metric.
    pub fn observe(&self, value: V) {
        self.inner.observe(value.encode());
    }
}

impl Histogram<Duration> {
    /// Starts latency observation for the metric. When the observation is finished,
    /// call [`LatencyObserver::observe()`].
    pub fn start(&self) -> LatencyObserver<'_> {
        LatencyObserver {
            start: Instant::now(),
            histogram: self,
        }
    }
}

impl<V: HistogramValue> EncodeMetric for Histogram<V> {
    fn encode(&self, encoder: MetricEncoder<'_>) -> fmt::Result {
        self.inner.encode(encoder)
    }

    fn metric_type(&self) -> MetricType {
        <Self as TypedMetric>::TYPE
    }
}

impl<V: HistogramValue> TypedMetric for Histogram<V> {
    const TYPE: MetricType = MetricType::Histogram;
}

/// Observer of latency for a [`Histogram`].
#[must_use = "`LatencyObserver` should be `observe()`d"]
#[derive(Debug)]
pub struct LatencyObserver<'a> {
    start: Instant,
    histogram: &'a Histogram<Duration>,
}

impl LatencyObserver<'_> {
    /// Observes and returns the latency passed since this observer was created.
    pub fn observe(self) -> Duration {
        let elapsed = self.start.elapsed();
        self.histogram.observe(elapsed);
        elapsed
    }
}

/// Information metric.
///
/// Information metrics represent pieces of information that are not changed during program lifetime
/// (e.g., config parameters of a certain component).
#[derive(Debug)]
pub struct Info<S>(Arc<OnceCell<S>>);

impl<S> Default for Info<S> {
    fn default() -> Self {
        Self(Arc::default())
    }
}

impl<S> Clone for Info<S> {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl<S: EncodeLabelSet> Info<S> {
    /// Gets the current value of the metric.
    pub fn get(&self) -> Option<&S> {
        self.0.get()
    }

    /// Sets the value of this metric.
    ///
    /// # Errors
    ///
    /// Returns an error if the value is already set.
    pub fn set(&self, value: S) -> Result<(), SetInfoError<S>> {
        self.0.set(value).map_err(SetInfoError)
    }
}

impl<S: EncodeLabelSet> EncodeMetric for Info<S> {
    fn encode(&self, mut encoder: MetricEncoder<'_>) -> fmt::Result {
        if let Some(value) = self.0.get() {
            encoder.encode_info(value)
        } else {
            Ok(())
        }
    }

    fn metric_type(&self) -> MetricType {
        MetricType::Info
    }
}

impl<S: EncodeLabelSet> TypedMetric for Info<S> {
    const TYPE: MetricType = MetricType::Info;
}

/// Error returned from [`Info::set()`].
#[derive(Debug)]
pub struct SetInfoError<S>(S);

impl<S> SetInfoError<S> {
    /// Converts the error into the unsuccessfully set value.
    pub fn into_inner(self) -> S {
        self.0
    }
}

impl<S> fmt::Display for SetInfoError<S> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("cannot set info metric value; it is already set")
    }
}

struct FamilyInner<S, M: BuildMetric> {
    map: FrozenMap<S, Box<M>>,
    builder: M::Builder,
}

impl<S, M> fmt::Debug for FamilyInner<S, M>
where
    S: fmt::Debug + Clone + Eq + Hash,
    M: BuildMetric + fmt::Debug,
    M::Builder: fmt::Debug,
{
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        let map_keys = self.map.keys_cloned();
        let map_snapshot: HashMap<_, _> = map_keys
            .iter()
            .map(|key| (key, self.map.get(key).unwrap()))
            .collect();

        formatter
            .debug_struct("Family")
            .field("map", &map_snapshot)
            .field("constructor", &self.builder)
            .finish()
    }
}

impl<S, M> FamilyInner<S, M>
where
    S: Clone + Eq + Hash,
    M: BuildMetric,
{
    fn get_or_create(&self, labels: &S) -> &M {
        if let Some(metric) = self.map.get(labels) {
            return metric;
        }
        self.map
            .insert_with(labels.clone(), || Box::new(M::build(self.builder)))
    }
}

/// Family of metrics labelled by one or more labels.
///
/// Family members can be accessed by indexing.
pub struct Family<S, M: BuildMetric, L = ()> {
    inner: Arc<FamilyInner<S, M>>,
    labels: L,
}

/// [`Family`] with separately specified label names.
///
/// Separately specifying labels allows to not define newtype wrappers for labels. Instead, labels
/// (the first type param of `LabeledFamily`) can be specified as values (e.g., `&'static str`
/// or `u8`), and the label names are provided separately using the `labels = [..]` attribute
/// with the [`Metrics`](macro@crate::Metrics) derive macro.
///
/// - If there's a single label, its value type must be specified directly: `&'static str`.
/// - If there are several labels, they must be specified as a tuple: `(&'static str, u16)`.
/// - The number of labels must match the number of label names and the constant param of `LabeledFamily`
///   (which is set to 1 by default). E.g., for two labels you should use `LabeledFamily<_, _, 2>`.
///
/// # Examples
///
/// ## Family with single label
///
/// ```
/// use vise::{Counter, LabeledFamily, Metrics};
/// # use vise::{Format, Registry};
///
/// #[derive(Debug, Metrics)]
/// struct TestMetrics {
///     #[metrics(labels = ["method"])]
///     counters: LabeledFamily<&'static str, Counter>,
/// }
///
/// // `counters` are keyed by a `&str`:
/// let metrics = TestMetrics::default();
/// metrics.counters[&"test"].inc();
/// metrics.counters[&"another_test"].inc_by(3);
/// // In the encoded metrics, these entries will be mentioned as follows:
/// let entries = [
///     r#"counters_total{method="test"} 1"#,
///     r#"counters_total{method="another_test"} 3"#,
/// ];
/// # let mut registry = Registry::empty();
/// # registry.register_metrics(&metrics);
/// # let mut buffer = String::new();
/// # registry.encode(&mut buffer, Format::OpenMetrics).unwrap();
/// # for entry in entries {
/// #     assert!(buffer.contains(&entry), "{buffer}");
/// # }
/// ```
///
/// ## Family with multiple labels
///
/// ```
/// # use vise::{Buckets, Format, Histogram, LabeledFamily, Metrics, Registry};
/// # use std::time::Duration;
/// const LABELS: [&str; 2] = ["method", "code"];
/// type Labels = (&'static str, u16);
///
/// #[derive(Debug, Metrics)]
/// struct TestMetrics {
///     #[metrics(labels = LABELS, buckets = Buckets::LATENCIES)]
///     latencies: LabeledFamily<Labels, Histogram<Duration>, 2>,
///     // ^ note that label names and type can be extracted elsewhere
/// }
///
/// let metrics = TestMetrics::default();
/// metrics.latencies[&("call", 200)].observe(Duration::from_millis(25));
/// metrics.latencies[&("send", 502)].observe(Duration::from_secs(1));
/// // In the encoded metrics, these entries will be mentioned as follows:
/// let entries = [
///     r#"latencies_sum{method="call",code="200"} 0.025"#,
///     r#"latencies_sum{method="send",code="502"} 1.0"#,
/// ];
/// # let mut registry = Registry::empty();
/// # registry.register_metrics(&metrics);
/// # let mut buffer = String::new();
/// # registry.encode(&mut buffer, Format::OpenMetrics).unwrap();
/// # for entry in entries {
/// #     assert!(buffer.contains(&entry), "{buffer}");
/// # }
/// ```
pub type LabeledFamily<S, M, const N: usize = 1> = Family<S, M, [&'static str; N]>;

impl<S, M, L> fmt::Debug for Family<S, M, L>
where
    S: fmt::Debug + Clone + Eq + Hash,
    M: BuildMetric + fmt::Debug,
    M::Builder: fmt::Debug,
{
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&self.inner, formatter)
    }
}

impl<S, M: BuildMetric, L: Clone> Clone for Family<S, M, L> {
    fn clone(&self) -> Self {
        Self {
            inner: Arc::clone(&self.inner),
            labels: self.labels.clone(),
        }
    }
}

impl<S, M, L> Family<S, M, L>
where
    S: Clone + Eq + Hash,
    M: BuildMetric,
{
    pub(crate) fn new(builder: M::Builder, labels: L) -> Self {
        let inner = Arc::new(FamilyInner {
            map: FrozenMap::new(),
            builder,
        });
        Self { inner, labels }
    }

    /// Checks whether this family contains a metric with the specified labels. This is mostly useful
    /// for testing.
    pub fn contains(&self, labels: &S) -> bool {
        self.inner.map.get(labels).is_some()
    }

    /// Gets a metric with the specified labels if it was reported previously. This is mostly useful
    /// for testing; use indexing for reporting.
    pub fn get(&self, labels: &S) -> Option<&M> {
        self.inner.map.get(labels)
    }

    /// Returns all metrics currently present in this family together with the corresponding labels.
    /// This is inefficient and mostly useful for testing purposes.
    #[allow(clippy::missing_panics_doc)] // false positive
    pub fn to_entries(&self) -> HashMap<S, &M> {
        let labels = self.inner.map.keys_cloned();
        labels
            .into_iter()
            .map(|key| {
                let metric = self.inner.map.get(&key).unwrap();
                (key, metric)
            })
            .collect()
    }
}

/// Will create a new metric with the specified labels if it's missing in the family.
impl<S, M, L> ops::Index<&S> for Family<S, M, L>
where
    S: Clone + Eq + Hash,
    M: BuildMetric,
{
    type Output = M;

    fn index(&self, labels: &S) -> &Self::Output {
        self.inner.get_or_create(labels)
    }
}

impl<S, M, L> EncodeMetric for Family<S, M, L>
where
    M: BuildMetric,
    S: Clone + Eq + Hash,
    L: MapLabels<S>,
{
    fn encode(&self, mut encoder: MetricEncoder<'_>) -> fmt::Result {
        for labels in &self.inner.map.keys_cloned() {
            let metric = self.inner.map.get(labels).unwrap();
            let mapped_labels = self.labels.map_labels(labels);
            let encoder = encoder.encode_family(&mapped_labels)?;
            metric.encode(encoder)?;
        }
        Ok(())
    }

    fn metric_type(&self) -> MetricType {
        <Self as TypedMetric>::TYPE
    }
}

impl<S, M: BuildMetric, L> TypedMetric for Family<S, M, L> {
    const TYPE: MetricType = <M as TypedMetric>::TYPE;
}

#[cfg(test)]
mod tests {
    use prometheus_client::metrics::family::Family as StandardFamily;

    use crate::MetricBuilder;
    use std::{sync::mpsc, thread};

    use super::*;

    type Label = (&'static str, &'static str);

    #[test]
    fn standard_family_is_easy_to_deadlock() {
        let (stop_sender, stop_receiver) = mpsc::channel();
        thread::spawn(move || {
            let family = StandardFamily::<Label, Gauge>::default();
            let first_metric = family.get_or_create(&("method", "test"));
            let second_metric = family.get_or_create(&("method", "other"));
            // ^ The second call will deadlock because of how `Family` is organized internally; its
            // `get_or_create()` provides a read guard for the internal map, and creating a new metric
            // requires a write lock on the same map.

            first_metric.set(10);
            second_metric.set(20);
            stop_sender.send(()).ok();
        });

        let err = stop_receiver
            .recv_timeout(Duration::from_millis(200))
            .unwrap_err();
        assert!(matches!(err, mpsc::RecvTimeoutError::Timeout));
    }

    #[test]
    fn family_accesses_are_not_deadlocked() {
        let family = Family::<Label, Gauge>::new(MetricBuilder::new(), ());
        let first_metric = &family[&("method", "test")];
        let second_metric = &family[&("method", "other")];
        first_metric.set(10);
        second_metric.set(20);

        // We circumvent deadlocking problems by using a *frozen map* (one that can be updated via a shared ref).
        // See its docs for more details. As an added bonus, we can use indexing notation instead of
        // clunky methods!
    }
}