aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/metrics/fake.h
blob: b01ff2505ad96a75bc92a59bde4e1e4bbeebf7d3 (plain) (blame)
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
#pragma once

#include "metric.h"
#include "metric_registry.h"

namespace NMonitoring {
    class TFakeMetricRegistry: public IMetricRegistry {
    public:
        TFakeMetricRegistry() noexcept
            : CommonLabels_{0}
        {
        }

        explicit TFakeMetricRegistry(TLabels commonLabels) noexcept
            : CommonLabels_{std::move(commonLabels)}
        {
        }

        IGauge* Gauge(ILabelsPtr labels) override;
        ILazyGauge* LazyGauge(ILabelsPtr labels, std::function<double()> supplier) override;
        IIntGauge* IntGauge(ILabelsPtr labels) override;
        ILazyIntGauge* LazyIntGauge(ILabelsPtr labels, std::function<i64()> supplier) override;
        ICounter* Counter(ILabelsPtr labels) override;
        ILazyCounter* LazyCounter(ILabelsPtr labels, std::function<ui64()> supplier) override;
        IRate* Rate(ILabelsPtr labels) override;
        ILazyRate* LazyRate(ILabelsPtr labels, std::function<ui64()> supplier) override;

        IHistogram* HistogramCounter(
                ILabelsPtr labels,
                IHistogramCollectorPtr collector) override;

        IHistogram* HistogramRate(
                ILabelsPtr labels,
                IHistogramCollectorPtr collector) override;

        IHistogram* HistogramCounter(
                ILabelsPtr labels,
                std::function<IHistogramCollectorPtr()> collector) override;

        IHistogram* HistogramRate(
                ILabelsPtr labels,
                std::function<IHistogramCollectorPtr()> collector) override;
        void Accept(TInstant time, IMetricConsumer* consumer) const override;
        void Append(TInstant time, IMetricConsumer* consumer) const override;

        const TLabels& CommonLabels() const noexcept override;
        void RemoveMetric(const ILabels& labels) noexcept override;

    private:
        TRWMutex Lock_;
        THashMap<ILabelsPtr, IMetricPtr> Metrics_;

        template <typename TMetric, EMetricType type, typename TLabelsType, typename... Args>
        TMetric* Metric(TLabelsType&& labels, Args&&... args);

        const TLabels CommonLabels_;
    };

    template <typename TBase>
    struct TFakeAcceptor: TBase {
        void Accept(TInstant time, IMetricConsumer* consumer) const override {
            Y_UNUSED(time, consumer);
        }
    };

    struct TFakeIntGauge final: public TFakeAcceptor<IIntGauge> {
        i64 Add(i64 n) noexcept override {
            Y_UNUSED(n);
            return 0;
        }

        void Set(i64 n) noexcept override {
            Y_UNUSED(n);
        }

        i64 Get() const noexcept override {
            return 0;
        }
    };

    struct TFakeLazyIntGauge final: public TFakeAcceptor<ILazyIntGauge> {
        i64 Get() const noexcept override {
            return 0;
        }
    };

    struct TFakeRate final: public TFakeAcceptor<IRate> {
        ui64 Add(ui64 n) noexcept override {
            Y_UNUSED(n);
            return 0;
        }

        ui64 Get() const noexcept override {
            return 0;
        }

        void Reset() noexcept override {
        }
    };

    struct TFakeLazyRate final: public TFakeAcceptor<ILazyRate> {
        ui64 Get() const noexcept override {
            return 0;
        }
    };

    struct TFakeGauge final: public TFakeAcceptor<IGauge> {
        double Add(double n) noexcept override {
            Y_UNUSED(n);
            return 0;
        }

        void Set(double n) noexcept override {
            Y_UNUSED(n);
        }

        double Get() const noexcept override {
            return 0;
        }
    };

    struct TFakeLazyGauge final: public TFakeAcceptor<ILazyGauge> {
        double Get() const noexcept override {
            return 0;
        }
    };

    struct TFakeHistogram final: public IHistogram {
        TFakeHistogram(bool isRate = false)
            : IHistogram{isRate}
        {
        }

        void Record(double value) noexcept override {
            Y_UNUSED(value);
        }

        void Record(double value, ui32 count) noexcept override {
            Y_UNUSED(value, count);
        }

        IHistogramSnapshotPtr TakeSnapshot() const override {
            return nullptr;
        }

        void Accept(TInstant time, IMetricConsumer* consumer) const override {
            Y_UNUSED(time, consumer);
        }

        void Reset() noexcept override {
        }
    };

    struct TFakeCounter final: public TFakeAcceptor<ICounter> {
        ui64 Add(ui64 n) noexcept override {
            Y_UNUSED(n);
            return 0;
        }

        ui64 Get() const noexcept override {
            return 0;
        }

        void Reset() noexcept override {
        }
    };

    struct TFakeLazyCounter final: public TFakeAcceptor<ILazyCounter> {
        ui64 Get() const noexcept override {
            return 0;
        }
    };
} // namespace NMonitoring