aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/encode/spack/spack_v1_encoder.cpp
blob: 727c772bc2d31cd96df74dabb443a5621d8da4e7 (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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include "spack_v1.h"
#include "compression.h"
#include "varint.h"

#include <library/cpp/monlib/encode/buffered/buffered_encoder_base.h>

#include <util/generic/cast.h>
#include <util/datetime/base.h>
#include <util/string/builder.h>

#ifndef _little_endian_
#error Unsupported platform
#endif

namespace NMonitoring {
    namespace {
        ///////////////////////////////////////////////////////////////////////
        // TEncoderSpackV1
        ///////////////////////////////////////////////////////////////////////
        class TEncoderSpackV1 final: public TBufferedEncoderBase {
        public:
            TEncoderSpackV1(
                IOutputStream* out,
                ETimePrecision timePrecision,
                ECompression compression,
                EMetricsMergingMode mergingMode,
                ESpackV1Version version,
                TStringBuf metricNameLabel
            )
                : Out_(out)
                , TimePrecision_(timePrecision)
                , Compression_(compression)
                , Version_(version)
                , MetricName_(Version_ >= SV1_02 ? LabelNamesPool_.PutIfAbsent(metricNameLabel) : nullptr)
            {
                MetricsMergingMode_ = mergingMode;

                LabelNamesPool_.SetSorted(true);
                LabelValuesPool_.SetSorted(true);
            }

            ~TEncoderSpackV1() override {
                Close();
            }

        private:
            void OnDouble(TInstant time, double value) override {
                TBufferedEncoderBase::OnDouble(time, value);
            }

            void OnInt64(TInstant time, i64 value) override {
                TBufferedEncoderBase::OnInt64(time, value);
            }

            void OnUint64(TInstant time, ui64 value) override {
                TBufferedEncoderBase::OnUint64(time, value);
            }

            void OnHistogram(TInstant time, IHistogramSnapshotPtr snapshot) override {
                TBufferedEncoderBase::OnHistogram(time, snapshot);
            }

            void OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr snapshot) override { 
                TBufferedEncoderBase::OnSummaryDouble(time, snapshot); 
            } 
 
            void OnLogHistogram(TInstant time, TLogHistogramSnapshotPtr snapshot) override { 
                TBufferedEncoderBase::OnLogHistogram(time, snapshot); 
            } 
 
            void Close() override {
                if (Closed_) {
                    return;
                }
                Closed_ = true;

                LabelNamesPool_.Build();
                LabelValuesPool_.Build();

                // Sort all points uniquely by ts -- the size can decrease
                ui64 pointsCount = 0;
                for (TMetric& metric : Metrics_) {
                    if (metric.TimeSeries.Size() > 1) {
                        metric.TimeSeries.SortByTs();
                    }

                    pointsCount += metric.TimeSeries.Size();
                }

                // (1) write header
                TSpackHeader header;
                header.Version = Version_;
                header.TimePrecision = EncodeTimePrecision(TimePrecision_);
                header.Compression = EncodeCompression(Compression_);
                header.LabelNamesSize = static_cast<ui32>(
                    LabelNamesPool_.BytesSize() + LabelNamesPool_.Count());
                header.LabelValuesSize = static_cast<ui32>(
                    LabelValuesPool_.BytesSize() + LabelValuesPool_.Count());
                header.MetricCount = Metrics_.size();
                header.PointsCount = pointsCount;
                Out_->Write(&header, sizeof(header));

                // if compression enabled all below writes must go throught compressor
                auto compressedOut = CompressedOutput(Out_, Compression_);
                if (compressedOut) {
                    Out_ = compressedOut.Get();
                }

                // (2) write string pools
                auto strPoolWrite = [this](TStringBuf str, ui32, ui32) {
                    Out_->Write(str);
                    Out_->Write('\0');
                };

                LabelNamesPool_.ForEach(strPoolWrite);
                LabelValuesPool_.ForEach(strPoolWrite);

                // (3) write common time
                WriteTime(CommonTime_);

                // (4) write common labels' indexes
                WriteLabels(CommonLabels_, nullptr);

                // (5) write metrics
                // metrics count already written in header
                for (TMetric& metric : Metrics_) {
                    // (5.1) types byte
                    ui8 typesByte = PackTypes(metric);
                    Out_->Write(&typesByte, sizeof(typesByte));

                    // TODO: implement
                    ui8 flagsByte = 0x00;
                    Out_->Write(&flagsByte, sizeof(flagsByte));

                    // v1.2 format addition — metric name
                    if (Version_ >= SV1_02) {
                        const auto it = FindIf(metric.Labels, [&](const auto& l) {
                            return l.Key == MetricName_;
                        });
                        Y_ENSURE(it != metric.Labels.end(),
                                 "metric name label '" << LabelNamesPool_.Get(MetricName_->Index) << "' not found, "
                                 << "all metric labels '" << FormatLabels(metric.Labels) << "'");
                        WriteVarUInt32(Out_, it->Value->Index);
                    }

                    // (5.2) labels
                    WriteLabels(metric.Labels, MetricName_);

                    // (5.3) values
                    switch (metric.TimeSeries.Size()) {
                        case 0:
                            break;
                        case 1: {
                            const auto& point = metric.TimeSeries[0];
                            if (point.GetTime() != TInstant::Zero()) {
                                WriteTime(point.GetTime());
                            }
                            EMetricValueType valueType = metric.TimeSeries.GetValueType();
                            WriteValue(metric.MetricType, valueType, point.GetValue());
                            break;
                        }
                        default:
                            WriteVarUInt32(Out_, static_cast<ui32>(metric.TimeSeries.Size()));
                            const TMetricTimeSeries& ts = metric.TimeSeries;
                            EMetricType metricType = metric.MetricType;
                            ts.ForEach([this, metricType](TInstant time, EMetricValueType valueType, TMetricValue value) {
                                // workaround for GCC bug
                                // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61636
                                this->WriteTime(time);
                                this->WriteValue(metricType, valueType, value);
                            });
                            break;
                    }
                }
            }

            // store metric type and values type in one byte
            ui8 PackTypes(const TMetric& metric) {
                EValueType valueType;
                if (metric.TimeSeries.Empty()) {
                    valueType = EValueType::NONE;
                } else if (metric.TimeSeries.Size() == 1) {
                    TInstant time = metric.TimeSeries[0].GetTime();
                    valueType = (time == TInstant::Zero())
                                    ? EValueType::ONE_WITHOUT_TS
                                    : EValueType::ONE_WITH_TS;
                } else {
                    valueType = EValueType::MANY_WITH_TS;
                }
                return (static_cast<ui8>(metric.MetricType) << 2) | static_cast<ui8>(valueType);
            }

            void WriteLabels(const TPooledLabels& labels, const TPooledStr* skipKey) {
                WriteVarUInt32(Out_, static_cast<ui32>(skipKey ? labels.size() - 1 : labels.size()));
                for (auto&& label : labels) {
                    if (label.Key == skipKey) {
                        continue;
                    }
                    WriteVarUInt32(Out_, label.Key->Index);
                    WriteVarUInt32(Out_, label.Value->Index);
                }
            }

            void WriteValue(EMetricType metricType, EMetricValueType valueType, TMetricValue value) {
                switch (metricType) {
                    case EMetricType::GAUGE:
                        WriteFixed(value.AsDouble(valueType));
                        break;

                    case EMetricType::IGAUGE:
                        WriteFixed(value.AsInt64(valueType));
                        break;

                    case EMetricType::COUNTER:
                    case EMetricType::RATE:
                        WriteFixed(value.AsUint64(valueType));
                        break;

                    case EMetricType::HIST:
                    case EMetricType::HIST_RATE:
                        WriteHistogram(*value.AsHistogram());
                        break;

                    case EMetricType::DSUMMARY:
                        WriteSummaryDouble(*value.AsSummaryDouble()); 
                        break; 
 
                    case EMetricType::LOGHIST: 
                        WriteLogHistogram(*value.AsLogHistogram()); 
                        break; 
 
                    default:
                        ythrow yexception() << "unsupported metric type: " << metricType;
                }
            }

            void WriteTime(TInstant instant) {
                switch (TimePrecision_) {
                    case ETimePrecision::SECONDS: {
                        ui32 time = static_cast<ui32>(instant.Seconds());
                        Out_->Write(&time, sizeof(time));
                        break;
                    }
                    case ETimePrecision::MILLIS: {
                        ui64 time = static_cast<ui64>(instant.MilliSeconds());
                        Out_->Write(&time, sizeof(time));
                    }
                }
            }

            template <typename T>
            void WriteFixed(T value) {
                Out_->Write(&value, sizeof(value));
            }

            void WriteHistogram(const IHistogramSnapshot& histogram) {
                ui32 count = histogram.Count();
                WriteVarUInt32(Out_, count);

                for (ui32 i = 0; i < count; i++) {
                    double bound = histogram.UpperBound(i);
                    Out_->Write(&bound, sizeof(bound));
                }
                for (ui32 i = 0; i < count; i++) {
                    ui64 value = histogram.Value(i);
                    Out_->Write(&value, sizeof(value));
                }
            }

            void WriteLogHistogram(const TLogHistogramSnapshot& logHist) { 
                WriteFixed(logHist.Base()); 
                WriteFixed(logHist.ZerosCount()); 
                WriteVarUInt32(Out_, static_cast<ui32>(logHist.StartPower())); 
                WriteVarUInt32(Out_, logHist.Count()); 
                for (ui32 i = 0; i < logHist.Count(); ++i) { 
                    WriteFixed(logHist.Bucket(i)); 
                } 
            } 
 
            void WriteSummaryDouble(const ISummaryDoubleSnapshot& summary) { 
                WriteFixed(summary.GetCount()); 
                WriteFixed(summary.GetSum()); 
                WriteFixed(summary.GetMin()); 
                WriteFixed(summary.GetMax()); 
                WriteFixed(summary.GetLast()); 
            } 
 
        private:
            IOutputStream* Out_;
            ETimePrecision TimePrecision_;
            ECompression Compression_;
            ESpackV1Version Version_;
            const TPooledStr* MetricName_;
            bool Closed_ = false;
        };

    }

    IMetricEncoderPtr EncoderSpackV1(
        IOutputStream* out,
        ETimePrecision timePrecision,
        ECompression compression,
        EMetricsMergingMode mergingMode
    ) {
        return MakeHolder<TEncoderSpackV1>(out, timePrecision, compression, mergingMode, SV1_01, "");
    }

    IMetricEncoderPtr EncoderSpackV12(
        IOutputStream* out,
        ETimePrecision timePrecision,
        ECompression compression,
        EMetricsMergingMode mergingMode,
        TStringBuf metricNameLabel
    ) {
        Y_ENSURE(!metricNameLabel.Empty(), "metricNameLabel can't be empty");
        return MakeHolder<TEncoderSpackV1>(out, timePrecision, compression, mergingMode, SV1_02, metricNameLabel);
    }
}