aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/metrics/metric_registry.h
blob: f60467cf91225e20f7955f32a3ee04a82167e67d (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
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
#pragma once

#include "labels.h"
#include "metric.h"

#include <util/system/rwlock.h>

#include <library/cpp/threading/light_rw_lock/lightrwlock.h>


namespace NMonitoring {

    struct TMetricOpts {
        bool MemOnly = false;
    };

    class IMetricFactory {
    public:
        virtual ~IMetricFactory() = default;

        virtual IGauge* Gauge(ILabelsPtr labels) = 0;
        virtual IGauge* GaugeWithOpts(ILabelsPtr labels, TMetricOpts opts = {}) {
                Y_UNUSED(opts);
                return Gauge(std::move(labels));
        }

        virtual ILazyGauge* LazyGauge(ILabelsPtr labels, std::function<double()> supplier) = 0;
        virtual ILazyGauge* LazyGaugeWithOpts(ILabelsPtr labels, std::function<double()> supplier, TMetricOpts opts = {}) {
                Y_UNUSED(opts);
                return LazyGauge(std::move(labels), std::move(supplier));
        }

        virtual IIntGauge* IntGauge(ILabelsPtr labels) = 0;
        virtual IIntGauge* IntGaugeWithOpts(ILabelsPtr labels, TMetricOpts opts = {}) {
            Y_UNUSED(opts);
            return IntGauge(std::move(labels));
        }
        virtual ILazyIntGauge* LazyIntGauge(ILabelsPtr labels, std::function<i64()> supplier) = 0;
        virtual ILazyIntGauge* LazyIntGaugeWithOpts(ILabelsPtr labels, std::function<i64()> supplier, TMetricOpts opts = {}) {
            Y_UNUSED(opts);
            return LazyIntGauge(std::move(labels), std::move(supplier));
        }

        virtual ICounter* Counter(ILabelsPtr labels) = 0;
        virtual ICounter* CounterWithOpts(ILabelsPtr labels, TMetricOpts opts = {}) {
            Y_UNUSED(opts);
            return Counter(std::move(labels));
        }

        virtual ILazyCounter* LazyCounter(ILabelsPtr labels, std::function<ui64()> supplier) = 0;
        virtual ILazyCounter* LazyCounterWithOpts(ILabelsPtr labels, std::function<ui64()> supplier, TMetricOpts opts = {}) {
            Y_UNUSED(opts);
            return LazyCounter(std::move(labels), std::move(supplier));
        }

        virtual IRate* Rate(ILabelsPtr labels) = 0;
        virtual IRate* RateWithOpts(ILabelsPtr labels, TMetricOpts opts = {}) {
            Y_UNUSED(opts);
            return Rate(std::move(labels));
        }

        virtual ILazyRate* LazyRate(ILabelsPtr labels, std::function<ui64()> supplier) = 0;
        virtual ILazyRate* LazyRateWithOpts(ILabelsPtr labels, std::function<ui64()> supplier, TMetricOpts opts = {}) {
            Y_UNUSED(opts);
            return LazyRate(std::move(labels), std::move(supplier));
        }

        virtual IHistogram* HistogramCounter(
                ILabelsPtr labels,
                IHistogramCollectorPtr collector) = 0;
        virtual IHistogram* HistogramCounterWithOpts(
                ILabelsPtr labels,
                IHistogramCollectorPtr collector,
                TMetricOpts opts = {})
        {
            Y_UNUSED(opts);
            return HistogramCounter(std::move(labels), std::move(collector));
        }

        virtual IHistogram* HistogramRate(
                ILabelsPtr labels,
                IHistogramCollectorPtr collector) = 0;
        virtual IHistogram* HistogramRateWithOpts(
            ILabelsPtr labels,
            IHistogramCollectorPtr collector,
            TMetricOpts opts = {})
        {
                Y_UNUSED(opts);
                return HistogramRate(std::move(labels), std::move(collector));
        }

        virtual IHistogram* HistogramCounter(
                ILabelsPtr labels,
                std::function<IHistogramCollectorPtr()> makeHistogramCollector) = 0;
        virtual IHistogram* HistogramCounterWithOpts(
                ILabelsPtr labels,
                std::function<IHistogramCollectorPtr()> makeHistogramCollector,
                TMetricOpts opts = {})
        {
                Y_UNUSED(opts);
                return HistogramCounter(std::move(labels), std::move(makeHistogramCollector));
        }

        virtual IHistogram* HistogramRate(
                ILabelsPtr labels,
                std::function<IHistogramCollectorPtr()> makeHistogramCollector) = 0;
        virtual IHistogram* HistogramRateWithOpts(
                ILabelsPtr labels,
                std::function<IHistogramCollectorPtr()> makeHistogramCollector,
                TMetricOpts opts = {})
        {
                Y_UNUSED(opts);
                return HistogramRate(std::move(labels), std::move(makeHistogramCollector));
        }
    };

    class IMetricSupplier {
    public:
        virtual ~IMetricSupplier() = default;

        virtual void Accept(TInstant time, IMetricConsumer* consumer) const = 0;
        virtual void Append(TInstant time, IMetricConsumer* consumer) const = 0;
    };

    class IMetricRegistry: public IMetricSupplier, public IMetricFactory {
    public:
        virtual const TLabels& CommonLabels() const noexcept = 0;
        virtual void RemoveMetric(const ILabels& labels) noexcept = 0;
    };


    ///////////////////////////////////////////////////////////////////////////////
    // TMetricRegistry
    ///////////////////////////////////////////////////////////////////////////////
    class TMetricRegistry: public IMetricRegistry {
    public:
        TMetricRegistry();
        TMetricRegistry(TMetricRegistry&& other);

        ~TMetricRegistry();

        explicit TMetricRegistry(const TLabels& commonLabels);

        /**
         * Not thread-safe. There should be no concurrent operations in the registry.
         */
        TMetricRegistry& operator=(TMetricRegistry&& other);

        /**
         * Get a global metrics registry instance.
         */
        static TMetricRegistry* Instance();
        static std::shared_ptr<TMetricRegistry> SharedInstance();

        TGauge* Gauge(TLabels labels);
        TGauge* GaugeWithOpts(TLabels labels, TMetricOpts opts = {});
        TLazyGauge* LazyGauge(TLabels labels, std::function<double()> supplier);
        TLazyGauge* LazyGaugeWithOpts(TLabels labels, std::function<double()> supplier, TMetricOpts opts = {});
        TIntGauge* IntGauge(TLabels labels);
        TIntGauge* IntGaugeWithOpts(TLabels labels, TMetricOpts opts = {});
        TLazyIntGauge* LazyIntGauge(TLabels labels, std::function<i64()> supplier);
        TLazyIntGauge* LazyIntGaugeWithOpts(TLabels labels, std::function<i64()> supplier, TMetricOpts opts = {});
        TCounter* Counter(TLabels labels);
        TCounter* CounterWithOpts(TLabels labels, TMetricOpts opts = {});
        TLazyCounter* LazyCounter(TLabels labels, std::function<ui64()> supplier);
        TLazyCounter* LazyCounterWithOpts(TLabels labels, std::function<ui64()> supplier, TMetricOpts opts = {});
        TRate* Rate(TLabels labels);
        TRate* RateWithOpts(TLabels labels, TMetricOpts opts = {});
        TLazyRate* LazyRate(TLabels labels, std::function<ui64()> supplier);
        TLazyRate* LazyRateWithOpts(TLabels labels, std::function<ui64()> supplier, TMetricOpts opts = {});

        THistogram* HistogramCounter(
                TLabels labels,
                IHistogramCollectorPtr collector);
        THistogram* HistogramCounterWithOpts(
                TLabels labels,
                IHistogramCollectorPtr collector,
                TMetricOpts opts = {});

        THistogram* HistogramRate(
                TLabels labels,
                IHistogramCollectorPtr collector);
        THistogram* HistogramRateWithOpts(
                TLabels labels,
                IHistogramCollectorPtr collector,
                TMetricOpts opts = {});

        THistogram* HistogramCounter(
                TLabels labels,
                std::function<IHistogramCollectorPtr()> makeHistogramCollector);
        THistogram* HistogramCounterWithOpts(
                TLabels labels,
                std::function<IHistogramCollectorPtr()> makeHistogramCollector,
                TMetricOpts opts = {});

        THistogram* HistogramRate(
                TLabels labels,
                std::function<IHistogramCollectorPtr()> makeHistogramCollector);
        THistogram* HistogramRateWithOpts(
                TLabels labels,
                std::function<IHistogramCollectorPtr()> makeHistogramCollector,
                TMetricOpts opts = {});

        /**
         * Set all registered metrics to zero
         */
        void Reset();
        /**
         * Remove all registered metrics from registry
         */
        void Clear();

        void Accept(TInstant time, IMetricConsumer* consumer) const override;
        void Append(TInstant time, IMetricConsumer* consumer) const override;

        const TLabels& CommonLabels() const noexcept override {
            return CommonLabels_;
        }

        void RemoveMetric(const ILabels& labels) noexcept override;

    private:
        TGauge* Gauge(ILabelsPtr labels) override;
        TGauge* GaugeWithOpts(ILabelsPtr labels, TMetricOpts opts = {}) override;
        TLazyGauge* LazyGauge(ILabelsPtr labels, std::function<double()> supplier) override;
        TLazyGauge* LazyGaugeWithOpts(ILabelsPtr labels, std::function<double()> supplier, TMetricOpts opts = {}) override;
        TIntGauge* IntGauge(ILabelsPtr labels) override;
        TIntGauge* IntGaugeWithOpts(ILabelsPtr labels, TMetricOpts opts = {}) override;
        TLazyIntGauge* LazyIntGauge(ILabelsPtr labels, std::function<i64()> supplier) override;
        TLazyIntGauge* LazyIntGaugeWithOpts(ILabelsPtr labels, std::function<i64()> supplier, TMetricOpts opts = {}) override;
        TCounter* Counter(ILabelsPtr labels) override;
        TCounter* CounterWithOpts(ILabelsPtr labels, TMetricOpts opts = {}) override;
        TLazyCounter* LazyCounter(ILabelsPtr labels, std::function<ui64()> supplier) override;
        TLazyCounter* LazyCounterWithOpts(ILabelsPtr labels, std::function<ui64()> supplier, TMetricOpts opts = {}) override;
        TRate* Rate(ILabelsPtr labels) override;
        TRate* RateWithOpts(ILabelsPtr labels, TMetricOpts opts = {}) override;
        TLazyRate* LazyRate(ILabelsPtr labels, std::function<ui64()> supplier) override;
        TLazyRate* LazyRateWithOpts(ILabelsPtr labels, std::function<ui64()> supplier, TMetricOpts opts = {}) override;

        THistogram* HistogramCounter(
                ILabelsPtr labels,
                IHistogramCollectorPtr collector) override;
        THistogram* HistogramCounterWithOpts(
                ILabelsPtr labels,
                IHistogramCollectorPtr collector,
                TMetricOpts opts = {}) override;

        THistogram* HistogramRate(
                ILabelsPtr labels,
                IHistogramCollectorPtr collector) override;
        THistogram* HistogramRateWithOpts(
                ILabelsPtr labels,
                IHistogramCollectorPtr collector,
                TMetricOpts opts = {}) override;

        THistogram* HistogramCounter(
                ILabelsPtr labels,
                std::function<IHistogramCollectorPtr()> makeHistogramCollector) override;
        THistogram* HistogramCounterWithOpts(
                ILabelsPtr labels,
                std::function<IHistogramCollectorPtr()> makeHistogramCollector,
                TMetricOpts opts = {}) override;

        THistogram* HistogramRate(
                ILabelsPtr labels,
                std::function<IHistogramCollectorPtr()> makeHistogramCollector) override;
        THistogram* HistogramRateWithOpts(
                ILabelsPtr labels,
                std::function<IHistogramCollectorPtr()> makeHistogramCollector,
                TMetricOpts opts = {}) override;

        struct TMetricValue {
            IMetricPtr Metric;
            TMetricOpts Opts;
        };

    private:
        THolder<TRWMutex> Lock_ = MakeHolder<TRWMutex>();
        THashMap<ILabelsPtr, TMetricValue> Metrics_;

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

        TLabels CommonLabels_;
    };

    void WriteLabels(IMetricConsumer* consumer, const ILabels& labels);
}