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

#include "metric_registry.h"

namespace NMonitoring {

/**
 * This registry is wrapping given delegate registry to add common labels
 * to all created metrics through this sub registry.
 */
class TMetricSubRegistry final: public IMetricRegistry {
public:
    /**
     * Do not keep ownership of the given delegate.
     */
    TMetricSubRegistry(TLabels commonLabels, IMetricRegistry* delegate) noexcept
        : CommonLabels_{std::move(commonLabels)}
        , DelegatePtr_{delegate}
    {
    }

    /**
     * Keeps ownership of the given delegate.
     */
    TMetricSubRegistry(TLabels commonLabels, std::shared_ptr<IMetricRegistry> delegate) noexcept
        : CommonLabels_{std::move(commonLabels)}
        , Delegate_{std::move(delegate)}
        , DelegatePtr_{Delegate_.get()}
    {
    }

    IGauge* Gauge(ILabelsPtr labels) override {
        AddCommonLabels(labels.Get());
        return DelegatePtr_->Gauge(std::move(labels));
    }
    IGauge* GaugeWithOpts(ILabelsPtr labels, TMetricOpts opts = {}) override {
        AddCommonLabels(labels.Get());
        return DelegatePtr_->GaugeWithOpts(std::move(labels), std::move(opts));
    }

    ILazyGauge* LazyGauge(ILabelsPtr labels, std::function<double()> supplier) override {
        AddCommonLabels(labels.Get());
        return DelegatePtr_->LazyGauge(std::move(labels), std::move(supplier));
    }
    ILazyGauge* LazyGaugeWithOpts(ILabelsPtr labels, std::function<double()> supplier, TMetricOpts opts = {}) override {
        AddCommonLabels(labels.Get());
        return DelegatePtr_->LazyGaugeWithOpts(std::move(labels), std::move(supplier), std::move(opts));
    }

    IIntGauge* IntGauge(ILabelsPtr labels) override {
        AddCommonLabels(labels.Get());
        return DelegatePtr_->IntGauge(std::move(labels));
    }
    IIntGauge* IntGaugeWithOpts(ILabelsPtr labels, TMetricOpts opts = {}) override {
        AddCommonLabels(labels.Get());
        return DelegatePtr_->IntGaugeWithOpts(std::move(labels), std::move(opts));
    }

    ILazyIntGauge* LazyIntGauge(ILabelsPtr labels, std::function<i64()> supplier) override {
        AddCommonLabels(labels.Get());
        return DelegatePtr_->LazyIntGauge(std::move(labels), std::move(supplier));
    }
    ILazyIntGauge* LazyIntGaugeWithOpts(ILabelsPtr labels, std::function<i64()> supplier, TMetricOpts opts = {}) override {
        AddCommonLabels(labels.Get());
        return DelegatePtr_->LazyIntGaugeWithOpts(std::move(labels), std::move(supplier), std::move(opts));
    }

    ICounter* Counter(ILabelsPtr labels) override {
        AddCommonLabels(labels.Get());
        return DelegatePtr_->Counter(std::move(labels));
    }
    ICounter* CounterWithOpts(ILabelsPtr labels, TMetricOpts opts = {}) override {
        AddCommonLabels(labels.Get());
        return DelegatePtr_->CounterWithOpts(std::move(labels), std::move(opts));
    }

    ILazyCounter* LazyCounter(ILabelsPtr labels, std::function<ui64()> supplier) override {
        AddCommonLabels(labels.Get());
        return DelegatePtr_->LazyCounter(std::move(labels), std::move(supplier));
    }
    ILazyCounter* LazyCounterWithOpts(ILabelsPtr labels, std::function<ui64()> supplier, TMetricOpts opts = {}) override {
        AddCommonLabels(labels.Get());
        return DelegatePtr_->LazyCounterWithOpts(std::move(labels), std::move(supplier), std::move(opts));
    }

    IRate* Rate(ILabelsPtr labels) override {
        AddCommonLabels(labels.Get());
        return DelegatePtr_->Rate(std::move(labels));
    }

    IRate* RateWithOpts(ILabelsPtr labels, TMetricOpts opts = {}) override {
        AddCommonLabels(labels.Get());
        return DelegatePtr_->RateWithOpts(std::move(labels), std::move(opts));
    }

    ILazyRate* LazyRate(ILabelsPtr labels, std::function<ui64()> supplier) override {
        AddCommonLabels(labels.Get());
        return DelegatePtr_->LazyRate(std::move(labels), std::move(supplier));
    }
    ILazyRate* LazyRateWithOpts(ILabelsPtr labels, std::function<ui64()> supplier, TMetricOpts opts = {}) override {
        AddCommonLabels(labels.Get());
        return DelegatePtr_->LazyRateWithOpts(std::move(labels), std::move(supplier), std::move(opts));
    }

    IHistogram* HistogramCounter(ILabelsPtr labels, IHistogramCollectorPtr collector) override {
        AddCommonLabels(labels.Get());
        return DelegatePtr_->HistogramCounter(std::move(labels), std::move(collector));
    }
    IHistogram* HistogramCounterWithOpts(ILabelsPtr labels, IHistogramCollectorPtr collector, TMetricOpts opts = {}) override {
        AddCommonLabels(labels.Get());
        return DelegatePtr_->HistogramCounterWithOpts(std::move(labels), std::move(collector), std::move(opts));
    }

    IHistogram* HistogramRate(ILabelsPtr labels, IHistogramCollectorPtr collector) override {
        AddCommonLabels(labels.Get());
        return DelegatePtr_->HistogramRate(std::move(labels), std::move(collector));
    }
    IHistogram* HistogramRateWithOpts(ILabelsPtr labels, IHistogramCollectorPtr collector, TMetricOpts opts = {}) override {
        AddCommonLabels(labels.Get());
        return DelegatePtr_->HistogramRateWithOpts(std::move(labels), std::move(collector), std::move(opts));
    }

    IHistogram* HistogramCounter(ILabelsPtr labels, std::function<IHistogramCollectorPtr()> collector) override {
        AddCommonLabels(labels.Get());
        return DelegatePtr_->HistogramCounter(std::move(labels), std::move(collector));
    }
    IHistogram* HistogramCounterWithOpts(ILabelsPtr labels, std::function<IHistogramCollectorPtr()> collector, TMetricOpts opts = {}) override {
        AddCommonLabels(labels.Get());
        return DelegatePtr_->HistogramCounterWithOpts(std::move(labels), std::move(collector), std::move(opts));
    }

    IHistogram* HistogramRate(ILabelsPtr labels, std::function<IHistogramCollectorPtr()> collector) override {
        AddCommonLabels(labels.Get());
        return DelegatePtr_->HistogramRate(std::move(labels), std::move(collector));
    }
    IHistogram* HistogramRateWithOpts(ILabelsPtr labels, std::function<IHistogramCollectorPtr()> collector, TMetricOpts opts) override {
        AddCommonLabels(labels.Get());
        return DelegatePtr_->HistogramRateWithOpts(std::move(labels), std::move(collector), std::move(opts));
    }

    void Accept(TInstant time, IMetricConsumer* consumer) const override {
        DelegatePtr_->Accept(time, consumer);
    }

    void Append(TInstant time, IMetricConsumer* consumer) const override {
        DelegatePtr_->Append(time, consumer);
    }

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

    void RemoveMetric(const ILabels& labels) noexcept override {
        TLabelsImpl<TStringBuf> toRemove;
        for (auto& l: labels) {
            toRemove.Add(l);
        }
        AddCommonLabels(&toRemove);
        DelegatePtr_->RemoveMetric(toRemove);
    }

private:
    void AddCommonLabels(ILabels* labels) const {
        for (auto& label: CommonLabels_) {
            labels->Add(label);
        }
    }

private:
    const TLabels CommonLabels_;
    std::shared_ptr<IMetricRegistry> Delegate_;
    IMetricRegistry* DelegatePtr_;
};

} // namespace NMonitoring