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
use crate::name::MetricName;
use crate::Flush;

use crate::attributes::MetricId;
use crate::{Input, InputDyn, InputKind, InputMetric, InputScope};
use std::io;
use std::sync::Arc;

lazy_static! {
    /// The reference instance identifying an uninitialized metric config.
    pub static ref VOID_INPUT: Arc<dyn InputDyn + Send + Sync> = Arc::new(Void::new());

    /// The reference instance identifying an uninitialized metric scope.
    pub static ref NO_METRIC_SCOPE: Arc<dyn InputScope + Send + Sync> = VOID_INPUT.input_dyn();
}

/// Discard metrics Input.
#[derive(Clone, Default)]
pub struct Void {}

/// Discard metrics Input.
#[derive(Clone)]
pub struct VoidInput {}

impl Void {
    /// Void metrics builder.
    #[deprecated(since = "0.7.2", note = "Use new()")]
    pub fn metrics() -> Self {
        Self::new()
    }

    /// Void metrics builder.
    pub fn new() -> Self {
        Void {}
    }
}

impl Input for Void {
    type SCOPE = VoidInput;

    fn metrics(&self) -> Self::SCOPE {
        VoidInput {}
    }
}

impl InputScope for VoidInput {
    fn new_metric(&self, name: MetricName, _kind: InputKind) -> InputMetric {
        InputMetric::new(MetricId::forge("void", name), |_value, _labels| {})
    }
}

impl Flush for VoidInput {
    fn flush(&self) -> io::Result<()> {
        Ok(())
    }
}

#[cfg(test)]
pub mod test {
    use super::*;

    #[test]
    fn test_to_void() {
        let c = Void::new().metrics();
        let m = c.new_metric("test".into(), InputKind::Marker);
        m.write(33, labels![]);
    }
}