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
|
#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));
}
ILazyGauge* LazyGauge(ILabelsPtr labels, std::function<double()> supplier) override {
AddCommonLabels(labels.Get());
return DelegatePtr_->LazyGauge(std::move(labels), std::move(supplier));
}
IIntGauge* IntGauge(ILabelsPtr labels) override {
AddCommonLabels(labels.Get());
return DelegatePtr_->IntGauge(std::move(labels));
}
ILazyIntGauge* LazyIntGauge(ILabelsPtr labels, std::function<i64()> supplier) override {
AddCommonLabels(labels.Get());
return DelegatePtr_->LazyIntGauge(std::move(labels), std::move(supplier));
}
ICounter* Counter(ILabelsPtr labels) override {
AddCommonLabels(labels.Get());
return DelegatePtr_->Counter(std::move(labels));
}
ILazyCounter* LazyCounter(ILabelsPtr labels, std::function<ui64()> supplier) override {
AddCommonLabels(labels.Get());
return DelegatePtr_->LazyCounter(std::move(labels), std::move(supplier));
}
IRate* Rate(ILabelsPtr labels) override {
AddCommonLabels(labels.Get());
return DelegatePtr_->Rate(std::move(labels));
}
ILazyRate* LazyRate(ILabelsPtr labels, std::function<ui64()> supplier) override {
AddCommonLabels(labels.Get());
return DelegatePtr_->LazyRate(std::move(labels), std::move(supplier));
}
IHistogram* HistogramCounter(ILabelsPtr labels, IHistogramCollectorPtr collector) override {
AddCommonLabels(labels.Get());
return DelegatePtr_->HistogramCounter(std::move(labels), std::move(collector));
}
IHistogram* HistogramRate(ILabelsPtr labels, IHistogramCollectorPtr collector) override {
AddCommonLabels(labels.Get());
return DelegatePtr_->HistogramRate(std::move(labels), std::move(collector));
}
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);
}
bool HasMetric(const ILabels &labels) noexcept override {
TLabelsImpl<TStringBuf> toCheck;
for (auto& l: labels) {
toCheck.Add(l);
}
AddCommonLabels(&toCheck);
return DelegatePtr_->HasMetric(toCheck);
}
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
|