aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/providers/common/metrics/metrics_registry.cpp
blob: 5b796c2d8240dffbf922e962b02f2ef2243d0050 (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
#include "metrics_registry.h"

#include <yql/essentials/providers/common/metrics/protos/metrics_registry.pb.h>

#include <util/generic/hash.h>
#include <util/generic/maybe.h>
#include <util/generic/stack.h>


namespace NYql {
namespace {

//////////////////////////////////////////////////////////////////////////////
// TCountersPhotographer
//////////////////////////////////////////////////////////////////////////////
class TCountersPhotographer: public NMonitoring::ICountableConsumer {
public:
    TCountersPhotographer(NProto::TCounterGroup* groupProto, bool invalidate)
        : HasAnyCounters_(false)
        , Invalidate_(invalidate)
    {
        GroupsProto_.push(groupProto);
    }

    bool HasAnyCounters() const {
        return HasAnyCounters_;
    }

private:
    void OnCounter(
            const TString& labelName, const TString& labelValue,
            const TSensorCounter* counter) override
    {
        HasAnyCounters_ = true;

        auto* counterProto = GroupsProto_.top()->AddCounters();
        counterProto->SetDerivative(counter->ForDerivative());
        auto counterVal = counter->Val();
        counterProto->SetValue(counterVal);
        if (Invalidate_) {
            const_cast<TSensorCounter*>(counter)->Sub(counterVal);
        }

        auto* label = counterProto->MutableLabel();
        label->SetName(labelName);
        label->SetValue(labelValue);
    }

    void OnHistogram(const TString& labelName, const TString& labelValue, NMonitoring::IHistogramSnapshotPtr snapshot, bool) override {
        if (Invalidate_) {
            return;
        }
        auto* counterProto = GroupsProto_.top()->AddCounters();
        auto* label = counterProto->MutableLabel();
        label->SetName(labelName);
        label->SetValue(labelValue);

        auto bucketsCount = snapshot->Count();
        for (ui32 i = 0; i < bucketsCount; i += 1) {
            auto upperBound = snapshot->UpperBound(i);
            auto value = snapshot->Value(i);

            auto* bucket = counterProto->AddBucket();
            bucket->SetUpperBound(upperBound);
            bucket->SetValue(value);
        }
    }

    void OnGroupBegin(
            const TString& labelName, const TString& labelValue,
            const TSensorsGroup*) override
    {
        if (labelName.empty() && labelValue.empty()) {
            // root group is alrady present
            return;
        }

        auto* groupProto = GroupsProto_.top()->AddGroups();
        auto* label = groupProto->MutableLabel();
        label->SetName(labelName);
        label->SetValue(labelValue);

        GroupsProto_.push(groupProto);
    }

    void OnGroupEnd(const TString&, const TString&, const TSensorsGroup*) override {
        GroupsProto_.pop();
    }

private:
    TStack<NProto::TCounterGroup*> GroupsProto_;
    bool HasAnyCounters_;
    bool Invalidate_;
};

//////////////////////////////////////////////////////////////////////////////
// TMetricsRegistryImpl
//////////////////////////////////////////////////////////////////////////////
class TMetricsRegistryImpl final: public IMetricsRegistry {
public:
    TMetricsRegistryImpl(
            TSensorsGroupPtr sensors,
            TMaybe<TString> userName)
        : Sensors_(std::move(sensors))
        , UserName_(std::move(userName))
    {
    }

    void SetCounter(
        const TString& labelName,
        const TString& labelValue,
        i64 value,
        bool derivative) override
    {
        if (UserName_) {
            // per user counter
            auto userCnt = GetCounter(labelName, labelValue, UserName_.Get(),
                derivative);
            if (userCnt) {
                *userCnt = value;
            }

            return;
        }

        auto totalCnt = GetCounter(labelName, labelValue, nullptr, derivative);
        if (totalCnt) {
            *totalCnt = value;
        }
    }

    void IncCounter(
            const TString& labelName,
            const TString& labelValue,
            bool derivative) override
    {
        // total aggregate counter
        auto totalCnt = GetCounter(labelName, labelValue, nullptr, derivative);
        if (totalCnt) {
            totalCnt->Inc();
        }

        if (UserName_) {
            // per user counter
            auto userCnt = GetCounter(labelName, labelValue, UserName_.Get(),
                                      derivative);
            if (userCnt) {
                userCnt->Inc();
            }
        }
    }

    void AddCounter(
            const TString& labelName,
            const TString& labelValue,
            i64 value,
            bool derivative) override
    {
        // total aggregate counter
        auto totalCnt = GetCounter(labelName, labelValue, nullptr, derivative);
        if (totalCnt) {
            totalCnt->Add(value);
        }

        if (UserName_) {
            // per user counter
            auto userCnt = GetCounter(labelName, labelValue, UserName_.Get(),
                                      derivative);
            if (userCnt) {
                userCnt->Add(value);
            }
        }
    }

    bool TakeSnapshot(NProto::TMetricsRegistrySnapshot* snapshot) const override {
        bool hasRootGroupBefore = snapshot->HasRootGroup();
        TCountersPhotographer photographer(snapshot->MutableRootGroup(), snapshot->GetDontIncrement() == false);
        Sensors_->Accept(TString(), TString(), photographer);
        if (!photographer.HasAnyCounters() && !hasRootGroupBefore) {
            // remove prematurely allocated group
            snapshot->ClearRootGroup();
            return false;
        }
        return true;
    }

    void MergeSnapshot(const NProto::TMetricsRegistrySnapshot& snapshot) override {
        MergeFromGroupProto(
            snapshot.HasMergeToRoot()
                ? GetSensorsRootGroup().Get()
                : Sensors_.Get(),
            snapshot.GetRootGroup(),
            snapshot.HasDontIncrement()
                ? snapshot.GetDontIncrement()
                : false);
    }

    IMetricsRegistryPtr Personalized(const TString& userName) const override {
        return new TMetricsRegistryImpl(Sensors_, MakeMaybe(userName));
    }

    void Flush() override {
        // do nothing
    }

    TSensorsGroupPtr GetSensors() override {
        return Sensors_.Get();
    }

private:
    TSensorCounterPtr GetCounter(
            const TString& labelName,
            const TString& labelValue,
            const TString* userName,
            bool derivative)
    {
        static const TString USER("user");
        static const TString USER_ABSOLUTE("user_absolute");
        static const TString TOTAL("total");

        const TString& userGroup = derivative ? USER : USER_ABSOLUTE;
        return Sensors_
                ->GetSubgroup(userGroup, userName ? *userName : TOTAL)
                ->GetNamedCounter(labelName, labelValue, derivative);
    }

    void MergeFromGroupProto(
            TSensorsGroup* group, const NProto::TCounterGroup& groupProto, bool asIs)
    {
        for (const auto& counterProto: groupProto.GetCounters()) {
            const auto& label = counterProto.GetLabel();

            if (!counterProto.GetBucket().empty()) {
                NMonitoring::TBucketBounds bounds;
                auto histSnapshot = NMonitoring::TExplicitHistogramSnapshot::New(counterProto.GetBucket().size());
                bounds.reserve(counterProto.GetBucket().size());
                int i = 0;
                for (const auto& b : counterProto.GetBucket()) {
                    if (i < counterProto.GetBucket().size() - 1) {
                        // skip inf
                        bounds.push_back(b.GetUpperBound());
                    }
                    (*histSnapshot)[i].first = b.GetUpperBound();
                    (*histSnapshot)[i].second = b.GetValue();
                    i += 1;
                }

                auto collector = NMonitoring::ExplicitHistogram(bounds).Release();
                auto histogram = group->GetNamedHistogram(
                    label.GetName(), label.GetValue(),
                    THolder(collector));
                Histograms.insert(std::make_pair(histogram, collector));
                Histograms[histogram]->Collect(*histSnapshot);
            } else {
                auto counter = group->GetNamedCounter(
                            label.GetName(), label.GetValue(),
                            counterProto.GetDerivative());
                if (asIs) {
                    *counter = counterProto.GetValue();
                } else {
                    *counter += counterProto.GetValue();
                }
            }
        }

        for (const auto& subGroupProto: groupProto.GetGroups()) {
            const auto& label = subGroupProto.GetLabel();
            auto subGroup = group->GetSubgroup(
                        label.GetName(), label.GetValue());
            MergeFromGroupProto(subGroup.Get(), subGroupProto, asIs);
        }
    }

private:
    TSensorsGroupPtr Sensors_;
    const TMaybe<TString> UserName_;

    THashMap<NMonitoring::THistogramPtr, NMonitoring::IHistogramCollector*> Histograms;
};

} // namespace


IMetricsRegistryPtr CreateMetricsRegistry(TSensorsGroupPtr sensors) {
    return new TMetricsRegistryImpl(std::move(sensors), Nothing());
}

} // namespace NYql