foyer_common/metrics/registry/
noop.rs

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
//  Copyright 2024 foyer Project Authors
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.

use crate::metrics::{CounterOps, CounterVecOps, GaugeOps, GaugeVecOps, HistogramOps, HistogramVecOps, RegistryOps};

/// Noop metrics placeholder.
#[derive(Debug)]
pub struct NoopMetricsRegistry;

impl CounterOps for NoopMetricsRegistry {
    fn increase(&self, _: u64) {}
}

impl CounterVecOps for NoopMetricsRegistry {
    fn counter(&self, _: &[&'static str]) -> impl CounterOps {
        NoopMetricsRegistry
    }
}

impl GaugeOps for NoopMetricsRegistry {
    fn increase(&self, _: u64) {}

    fn decrease(&self, _: u64) {}

    fn absolute(&self, _: u64) {}
}

impl GaugeVecOps for NoopMetricsRegistry {
    fn gauge(&self, _: &[&'static str]) -> impl GaugeOps {
        NoopMetricsRegistry
    }
}

impl HistogramOps for NoopMetricsRegistry {
    fn record(&self, _: f64) {}
}

impl HistogramVecOps for NoopMetricsRegistry {
    fn histogram(&self, _: &[&'static str]) -> impl HistogramOps {
        NoopMetricsRegistry
    }
}

impl RegistryOps for NoopMetricsRegistry {
    fn register_counter_vec(&self, _: &'static str, _: &'static str, _: &'static [&'static str]) -> impl CounterVecOps {
        NoopMetricsRegistry
    }

    fn register_gauge_vec(&self, _: &'static str, _: &'static str, _: &'static [&'static str]) -> impl GaugeVecOps {
        NoopMetricsRegistry
    }

    fn register_histogram_vec(
        &self,
        _: &'static str,
        _: &'static str,
        _: &'static [&'static str],
    ) -> impl HistogramVecOps {
        NoopMetricsRegistry
    }
}

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

    #[test]
    fn test() {
        let noop = NoopMetricsRegistry;

        let cv = noop.register_counter_vec("test_counter_1", "test counter 1", &["label1", "label2"]);
        let c = cv.counter(&["l1", "l2"]);
        c.increase(42);

        let gv = noop.register_gauge_vec("test_gauge_1", "test gauge 1", &["label1", "label2"]);
        let g = gv.gauge(&["l1", "l2"]);
        g.increase(514);
        g.decrease(114);
        g.absolute(114514);

        let hv = noop.register_histogram_vec("test_histogram_1", "test histogram 1", &["label1", "label2"]);
        let h = hv.histogram(&["l1", "l2"]);
        h.record(114.514);
    }
}