diff options
author | kbalakirev <kbalakirev@yandex-team.ru> | 2022-02-10 16:48:58 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:48:58 +0300 |
commit | 1906a186042870fd03a12180acd1a6fcee045e42 (patch) | |
tree | 6cb2ad21897c14f8beca06c9341cb3054952892b /library/cpp/monlib/encode | |
parent | 8a7e5c149f1efbd31f0dbbf8f62f368debccb8a9 (diff) | |
download | ydb-1906a186042870fd03a12180acd1a6fcee045e42.tar.gz |
Restoring authorship annotation for <kbalakirev@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/monlib/encode')
23 files changed, 838 insertions, 838 deletions
diff --git a/library/cpp/monlib/encode/buffered/buffered_encoder_base.cpp b/library/cpp/monlib/encode/buffered/buffered_encoder_base.cpp index 87c832d642..b58d9ff475 100644 --- a/library/cpp/monlib/encode/buffered/buffered_encoder_base.cpp +++ b/library/cpp/monlib/encode/buffered/buffered_encoder_base.cpp @@ -132,18 +132,18 @@ void TBufferedEncoderBase::OnHistogram(TInstant time, IHistogramSnapshotPtr s) { metric.TimeSeries.Add(time, s.Get()); } -void TBufferedEncoderBase::OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr s) { +void TBufferedEncoderBase::OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr s) { State_.Expect(TEncoderState::EState::METRIC); TMetric& metric = Metrics_.back(); metric.TimeSeries.Add(time, s.Get()); -} - -void TBufferedEncoderBase::OnLogHistogram(TInstant time, TLogHistogramSnapshotPtr s) { +} + +void TBufferedEncoderBase::OnLogHistogram(TInstant time, TLogHistogramSnapshotPtr s) { State_.Expect(TEncoderState::EState::METRIC); TMetric& metric = Metrics_.back(); metric.TimeSeries.Add(time, s.Get()); -} - +} + TString TBufferedEncoderBase::FormatLabels(const TPooledLabels& labels) const { auto formattedLabels = TVector<TString>(Reserve(labels.size() + CommonLabels_.size())); auto addLabel = [&](const TPooledLabel& l) { diff --git a/library/cpp/monlib/encode/buffered/buffered_encoder_base.h b/library/cpp/monlib/encode/buffered/buffered_encoder_base.h index fe3714e58f..1c717fb299 100644 --- a/library/cpp/monlib/encode/buffered/buffered_encoder_base.h +++ b/library/cpp/monlib/encode/buffered/buffered_encoder_base.h @@ -34,8 +34,8 @@ public: void OnUint64(TInstant time, ui64 value) override; void OnHistogram(TInstant time, IHistogramSnapshotPtr snapshot) override; - void OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr snapshot) override; - void OnLogHistogram(TInstant, TLogHistogramSnapshotPtr) override; + void OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr snapshot) override; + void OnLogHistogram(TInstant, TLogHistogramSnapshotPtr) override; protected: using TPooledStr = TStringPoolBuilder::TValue; diff --git a/library/cpp/monlib/encode/fake/fake.cpp b/library/cpp/monlib/encode/fake/fake.cpp index 69d691361a..e8b35c6fa7 100644 --- a/library/cpp/monlib/encode/fake/fake.cpp +++ b/library/cpp/monlib/encode/fake/fake.cpp @@ -35,12 +35,12 @@ namespace NMonitoring { void OnHistogram(TInstant, IHistogramSnapshotPtr) override { } - void OnSummaryDouble(TInstant, ISummaryDoubleSnapshotPtr) override { - } - - void OnLogHistogram(TInstant, TLogHistogramSnapshotPtr) override { - } - + void OnSummaryDouble(TInstant, ISummaryDoubleSnapshotPtr) override { + } + + void OnLogHistogram(TInstant, TLogHistogramSnapshotPtr) override { + } + void Close() override { } }; diff --git a/library/cpp/monlib/encode/json/json_decoder.cpp b/library/cpp/monlib/encode/json/json_decoder.cpp index d44ff5fd28..ec4520d7c0 100644 --- a/library/cpp/monlib/encode/json/json_decoder.cpp +++ b/library/cpp/monlib/encode/json/json_decoder.cpp @@ -73,38 +73,38 @@ private: bool InfPresented_ = false; TBucketValue InfValue_; }; - + class TSummaryDoubleBuilder { public: ISummaryDoubleSnapshotPtr Build() const { return MakeIntrusive<TSummaryDoubleSnapshot>(Sum_, Min_, Max_, Last_, Count_); } - + void SetSum(double sum) { Empty_ = false; Sum_ = sum; } - + void SetMin(double min) { Empty_ = false; Min_ = min; } - + void SetMax(double max) { Empty_ = false; Max_ = max; } - + void SetLast(double last) { Empty_ = false; Last_ = last; } - + void SetCount(ui64 count) { Empty_ = false; Count_ = count; } - + void Clear() { Empty_ = true; Sum_ = 0; @@ -113,11 +113,11 @@ public: Last_ = 0; Count_ = 0; } - + bool Empty() const { return Empty_; } - + private: double Sum_ = 0; double Min_ = 0; @@ -126,51 +126,51 @@ private: ui64 Count_ = 0; bool Empty_ = true; }; - + class TLogHistogramBuilder { public: void SetBase(double base) { DECODE_ENSURE(base > 0, "base must be positive"); Base_ = base; } - + void SetZerosCount(ui64 zerosCount) { DECODE_ENSURE(zerosCount >= 0, "zeros count must be positive"); ZerosCount_ = zerosCount; } - + void SetStartPower(int startPower) { StartPower_ = startPower; } - + void AddBucketValue(double value) { DECODE_ENSURE(value > 0.0, "bucket values must be positive"); DECODE_ENSURE(value < std::numeric_limits<double>::max(), "bucket values must be finite"); Buckets_.push_back(value); } - + void Clear() { Buckets_.clear(); Base_ = 1.5; ZerosCount_ = 0; StartPower_ = 0; } - + bool Empty() const { return Buckets_.empty() && ZerosCount_ == 0; } - + TLogHistogramSnapshotPtr Build() { return MakeIntrusive<TLogHistogramSnapshot>(Base_, ZerosCount_, StartPower_, std::move(Buckets_)); } - + private: double Base_ = 1.5; ui64 ZerosCount_ = 0; int StartPower_ = 0; TVector<double> Buckets_; }; - + std::pair<double, bool> ParseSpecDouble(TStringBuf string) { if (string == TStringBuf("nan") || string == TStringBuf("NaN")) { return {std::numeric_limits<double>::quiet_NaN(), true}; @@ -542,7 +542,7 @@ if (Y_UNLIKELY(!(CONDITION))) { \ LastMetric_.SummaryBuilder.SetCount(value); State_.ToPrev(); break; - + case TState::METRIC_DSUMMARY_SUM: LastMetric_.SummaryBuilder.SetSum(value); State_.ToPrev(); @@ -559,26 +559,26 @@ if (Y_UNLIKELY(!(CONDITION))) { \ LastMetric_.SummaryBuilder.SetLast(value); State_.ToPrev(); break; - + case TState::METRIC_LOG_HIST_BASE: LastMetric_.LogHistBuilder.SetBase(value); State_.ToPrev(); break; - + case TState::METRIC_LOG_HIST_ZEROS: LastMetric_.LogHistBuilder.SetZerosCount(value); State_.ToPrev(); break; - + case TState::METRIC_LOG_HIST_START_POWER: LastMetric_.LogHistBuilder.SetStartPower(value); State_.ToPrev(); break; - + case TState::METRIC_LOG_HIST_BUCKETS: LastMetric_.LogHistBuilder.AddBucketValue(value); break; - + default: return false; } @@ -627,7 +627,7 @@ if (Y_UNLIKELY(!(CONDITION))) { \ LastMetric_.SummaryBuilder.SetCount(value); State_.ToPrev(); break; - + case TState::METRIC_DSUMMARY_SUM: LastMetric_.SummaryBuilder.SetSum(value); State_.ToPrev(); @@ -644,26 +644,26 @@ if (Y_UNLIKELY(!(CONDITION))) { \ LastMetric_.SummaryBuilder.SetLast(value); State_.ToPrev(); break; - + case TState::METRIC_LOG_HIST_BASE: LastMetric_.LogHistBuilder.SetBase(value); State_.ToPrev(); break; - + case TState::METRIC_LOG_HIST_ZEROS: LastMetric_.LogHistBuilder.SetZerosCount(value); State_.ToPrev(); break; - + case TState::METRIC_LOG_HIST_START_POWER: LastMetric_.LogHistBuilder.SetStartPower(value); State_.ToPrev(); break; - + case TState::METRIC_LOG_HIST_BUCKETS: LastMetric_.LogHistBuilder.AddBucketValue(value); break; - + default: return false; } @@ -697,16 +697,16 @@ if (Y_UNLIKELY(!(CONDITION))) { \ LastMetric_.SummaryBuilder.SetLast(value); State_.ToPrev(); break; - + case TState::METRIC_LOG_HIST_BASE: LastMetric_.LogHistBuilder.SetBase(value); State_.ToPrev(); break; - + case TState::METRIC_LOG_HIST_BUCKETS: LastMetric_.LogHistBuilder.AddBucketValue(value); break; - + default: return false; } @@ -810,7 +810,7 @@ if (Y_UNLIKELY(!(CONDITION))) { \ return true; } - + bool OnMapKey(const TStringBuf& key) override { switch (State_.Current()) { case TState::ROOT_OBJECT: @@ -822,12 +822,12 @@ if (Y_UNLIKELY(!(CONDITION))) { \ State_.ToNext(TState::METRICS_ARRAY); } break; - + case TState::COMMON_LABELS: case TState::METRIC_LABELS: LastLabelName_ = key; break; - + case TState::METRIC_OBJECT: if (key == TStringBuf("labels")) { State_.ToNext(TState::METRIC_LABELS); @@ -865,7 +865,7 @@ if (Y_UNLIKELY(!(CONDITION))) { \ return false; } break; - + case TState::METRIC_TIMESERIES: if (key == TStringBuf("ts")) { State_.ToNext(TState::METRIC_TS); @@ -924,22 +924,22 @@ if (Y_UNLIKELY(!(CONDITION))) { \ return true; } - + bool OnOpenMap() override { switch (State_.Current()) { case TState::ROOT_OBJECT: MetricConsumer_->OnStreamBegin(); break; - + case TState::COMMON_LABELS: MetricConsumer_->OnLabelsBegin(); break; - + case TState::METRICS_ARRAY: State_.ToNext(TState::METRIC_OBJECT); LastMetric_.Clear(); break; - + default: break; } @@ -1147,7 +1147,7 @@ void DecodeJson(TStringBuf data, IMetricConsumer* c, TStringBuf metricNameLabel) // no need to check a return value. If there is an error, a TJsonDecodeError is thrown NJson::ReadJson(&memIn, &decoder); } - + TCommonPartsProxy commonPartsProxy(std::move(commonPartsCollector.CommonParts()), c); { TMemoryInput memIn(data); @@ -1156,7 +1156,7 @@ void DecodeJson(TStringBuf data, IMetricConsumer* c, TStringBuf metricNameLabel) NJson::ReadJson(&memIn, &decoder); } } - + #undef DECODE_ENSURE } diff --git a/library/cpp/monlib/encode/json/json_encoder.cpp b/library/cpp/monlib/encode/json/json_encoder.cpp index 20d2bb6283..7e61d0443a 100644 --- a/library/cpp/monlib/encode/json/json_encoder.cpp +++ b/library/cpp/monlib/encode/json/json_encoder.cpp @@ -93,55 +93,55 @@ namespace NMonitoring { Buf_.EndObject(); } - void WriteValue(ISummaryDoubleSnapshot* s) { + void WriteValue(ISummaryDoubleSnapshot* s) { Y_ENSURE(Style_ == EJsonStyle::Solomon); Buf_.WriteKey(TStringBuf("summary")); - Buf_.BeginObject(); - + Buf_.BeginObject(); + Buf_.WriteKey(TStringBuf("sum")); - Buf_.WriteDouble(s->GetSum()); - + Buf_.WriteDouble(s->GetSum()); + Buf_.WriteKey(TStringBuf("min")); - Buf_.WriteDouble(s->GetMin()); - + Buf_.WriteDouble(s->GetMin()); + Buf_.WriteKey(TStringBuf("max")); - Buf_.WriteDouble(s->GetMax()); - + Buf_.WriteDouble(s->GetMax()); + Buf_.WriteKey(TStringBuf("last")); - Buf_.WriteDouble(s->GetLast()); - + Buf_.WriteDouble(s->GetLast()); + Buf_.WriteKey(TStringBuf("count")); - Buf_.WriteULongLong(s->GetCount()); - - Buf_.EndObject(); - } - - void WriteValue(TLogHistogramSnapshot* s) { + Buf_.WriteULongLong(s->GetCount()); + + Buf_.EndObject(); + } + + void WriteValue(TLogHistogramSnapshot* s) { Y_ENSURE(Style_ == EJsonStyle::Solomon); Buf_.WriteKey(TStringBuf("log_hist")); - Buf_.BeginObject(); - + Buf_.BeginObject(); + Buf_.WriteKey(TStringBuf("base")); - Buf_.WriteDouble(s->Base()); - + Buf_.WriteDouble(s->Base()); + Buf_.WriteKey(TStringBuf("zeros_count")); - Buf_.WriteULongLong(s->ZerosCount()); - + Buf_.WriteULongLong(s->ZerosCount()); + Buf_.WriteKey(TStringBuf("start_power")); - Buf_.WriteInt(s->StartPower()); - + Buf_.WriteInt(s->StartPower()); + Buf_.WriteKey(TStringBuf("buckets")); - Buf_.BeginList(); - for (size_t i = 0; i < s->Count(); ++i) { - Buf_.WriteDouble(s->Bucket(i)); - } - Buf_.EndList(); - - Buf_.EndObject(); - } - + Buf_.BeginList(); + for (size_t i = 0; i < s->Count(); ++i) { + Buf_.WriteDouble(s->Bucket(i)); + } + Buf_.EndList(); + + Buf_.EndObject(); + } + void WriteValue(EMetricValueType type, TMetricValue value) { switch (type) { case EMetricValueType::DOUBLE: @@ -161,13 +161,13 @@ namespace NMonitoring { break; case EMetricValueType::SUMMARY: - WriteValue(value.AsSummaryDouble()); - break; - - case EMetricValueType::LOGHISTOGRAM: - WriteValue(value.AsLogHistogram()); - break; - + WriteValue(value.AsSummaryDouble()); + break; + + case EMetricValueType::LOGHISTOGRAM: + WriteValue(value.AsLogHistogram()); + break; + case EMetricValueType::UNKNOWN: ythrow yexception() << "unknown metric value type"; } @@ -358,16 +358,16 @@ namespace NMonitoring { Write<IHistogramSnapshot*>(time, snapshot.Get()); } - void OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr snapshot) override { + void OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr snapshot) override { State_.Expect(TEncoderState::EState::METRIC); - Write<ISummaryDoubleSnapshot*>(time, snapshot.Get()); - } - - void OnLogHistogram(TInstant time, TLogHistogramSnapshotPtr snapshot) override { + Write<ISummaryDoubleSnapshot*>(time, snapshot.Get()); + } + + void OnLogHistogram(TInstant time, TLogHistogramSnapshotPtr snapshot) override { State_.Expect(TEncoderState::EState::METRIC); - Write<TLogHistogramSnapshot*>(time, snapshot.Get()); - } - + Write<TLogHistogramSnapshot*>(time, snapshot.Get()); + } + template <typename T> void Write(TInstant time, T value) { State_.Expect(TEncoderState::EState::METRIC); diff --git a/library/cpp/monlib/encode/json/json_ut.cpp b/library/cpp/monlib/encode/json/json_ut.cpp index 09e7909289..5260def99d 100644 --- a/library/cpp/monlib/encode/json/json_ut.cpp +++ b/library/cpp/monlib/encode/json/json_ut.cpp @@ -101,34 +101,34 @@ namespace { } } - void AssertPointEqual(const NProto::TPoint& p, TInstant time, const TLogHistogramSnapshot& expected) { - UNIT_ASSERT_VALUES_EQUAL(p.GetTime(), time.MilliSeconds()); - UNIT_ASSERT_EQUAL(p.GetValueCase(), NProto::TPoint::kLogHistogram); - - const double eps = 1e-10; - const NProto::TLogHistogram& h = p.GetLogHistogram(); - - UNIT_ASSERT_DOUBLES_EQUAL(h.GetBase(), expected.Base(), eps); - UNIT_ASSERT_VALUES_EQUAL(h.GetZerosCount(), expected.ZerosCount()); - UNIT_ASSERT_VALUES_EQUAL(h.GetStartPower(), expected.StartPower()); - UNIT_ASSERT_VALUES_EQUAL(h.BucketsSize(), expected.Count()); - for (size_t i = 0; i < expected.Count(); ++i) { - UNIT_ASSERT_DOUBLES_EQUAL(h.GetBuckets(i), expected.Bucket(i), eps); - } - } - - void AssertPointEqual(const NProto::TPoint& p, TInstant time, const ISummaryDoubleSnapshot& expected) { - UNIT_ASSERT_VALUES_EQUAL(p.GetTime(), time.MilliSeconds()); - UNIT_ASSERT_EQUAL(p.GetValueCase(), NProto::TPoint::kSummaryDouble); - auto actual = p.GetSummaryDouble(); - const double eps = 1e-10; - UNIT_ASSERT_DOUBLES_EQUAL(actual.GetSum(), expected.GetSum(), eps); - UNIT_ASSERT_DOUBLES_EQUAL(actual.GetMin(), expected.GetMin(), eps); - UNIT_ASSERT_DOUBLES_EQUAL(actual.GetMax(), expected.GetMax(), eps); - UNIT_ASSERT_DOUBLES_EQUAL(actual.GetLast(), expected.GetLast(), eps); - UNIT_ASSERT_VALUES_EQUAL(actual.GetCount(), expected.GetCount()); - } - + void AssertPointEqual(const NProto::TPoint& p, TInstant time, const TLogHistogramSnapshot& expected) { + UNIT_ASSERT_VALUES_EQUAL(p.GetTime(), time.MilliSeconds()); + UNIT_ASSERT_EQUAL(p.GetValueCase(), NProto::TPoint::kLogHistogram); + + const double eps = 1e-10; + const NProto::TLogHistogram& h = p.GetLogHistogram(); + + UNIT_ASSERT_DOUBLES_EQUAL(h.GetBase(), expected.Base(), eps); + UNIT_ASSERT_VALUES_EQUAL(h.GetZerosCount(), expected.ZerosCount()); + UNIT_ASSERT_VALUES_EQUAL(h.GetStartPower(), expected.StartPower()); + UNIT_ASSERT_VALUES_EQUAL(h.BucketsSize(), expected.Count()); + for (size_t i = 0; i < expected.Count(); ++i) { + UNIT_ASSERT_DOUBLES_EQUAL(h.GetBuckets(i), expected.Bucket(i), eps); + } + } + + void AssertPointEqual(const NProto::TPoint& p, TInstant time, const ISummaryDoubleSnapshot& expected) { + UNIT_ASSERT_VALUES_EQUAL(p.GetTime(), time.MilliSeconds()); + UNIT_ASSERT_EQUAL(p.GetValueCase(), NProto::TPoint::kSummaryDouble); + auto actual = p.GetSummaryDouble(); + const double eps = 1e-10; + UNIT_ASSERT_DOUBLES_EQUAL(actual.GetSum(), expected.GetSum(), eps); + UNIT_ASSERT_DOUBLES_EQUAL(actual.GetMin(), expected.GetMin(), eps); + UNIT_ASSERT_DOUBLES_EQUAL(actual.GetMax(), expected.GetMax(), eps); + UNIT_ASSERT_DOUBLES_EQUAL(actual.GetLast(), expected.GetLast(), eps); + UNIT_ASSERT_VALUES_EQUAL(actual.GetCount(), expected.GetCount()); + } + } // namespace @@ -734,223 +734,223 @@ Y_UNIT_TEST_SUITE(TJsonTest) { return out.Str(); } - Y_UNIT_TEST(SummaryValueEncode) { + Y_UNIT_TEST(SummaryValueEncode) { auto writeDocument = [](IMetricEncoder* e) { - e->OnStreamBegin(); - { + e->OnStreamBegin(); + { e->OnMetricBegin(EMetricType::DSUMMARY); - { - e->OnLabelsBegin(); + { + e->OnLabelsBegin(); e->OnLabel("metric", "temperature"); - e->OnLabelsEnd(); - } - - e->OnSummaryDouble(now, MakeIntrusive<TSummaryDoubleSnapshot>(10., -0.5, 0.5, 0.3, 30u)); + e->OnLabelsEnd(); + } + + e->OnSummaryDouble(now, MakeIntrusive<TSummaryDoubleSnapshot>(10., -0.5, 0.5, 0.3, 30u)); e->OnMetricEnd(); - } - e->OnStreamEnd(); - }; - - TString result1 = EncodeToString(EncoderJson, writeDocument); - UNIT_ASSERT_NO_DIFF(result1, NResource::Find("/summary_value.json")); - - TString result2 = EncodeToString(BufferedEncoderJson, writeDocument); - UNIT_ASSERT_NO_DIFF(result2, NResource::Find("/summary_value.json")); - } - - ISummaryDoubleSnapshotPtr TestInfSummary() { - return MakeIntrusive<TSummaryDoubleSnapshot>( - std::numeric_limits<double>::quiet_NaN(), - -std::numeric_limits<double>::infinity(), - std::numeric_limits<double>::infinity(), - 0.3, - 30u); - } - - Y_UNIT_TEST(SummaryInfEncode) { + } + e->OnStreamEnd(); + }; + + TString result1 = EncodeToString(EncoderJson, writeDocument); + UNIT_ASSERT_NO_DIFF(result1, NResource::Find("/summary_value.json")); + + TString result2 = EncodeToString(BufferedEncoderJson, writeDocument); + UNIT_ASSERT_NO_DIFF(result2, NResource::Find("/summary_value.json")); + } + + ISummaryDoubleSnapshotPtr TestInfSummary() { + return MakeIntrusive<TSummaryDoubleSnapshot>( + std::numeric_limits<double>::quiet_NaN(), + -std::numeric_limits<double>::infinity(), + std::numeric_limits<double>::infinity(), + 0.3, + 30u); + } + + Y_UNIT_TEST(SummaryInfEncode) { auto writeDocument = [](IMetricEncoder* e) { - e->OnStreamBegin(); - { + e->OnStreamBegin(); + { e->OnMetricBegin(EMetricType::DSUMMARY); - { - e->OnLabelsBegin(); + { + e->OnLabelsBegin(); e->OnLabel("metric", "temperature"); - e->OnLabelsEnd(); - } - - e->OnSummaryDouble(now, TestInfSummary()); + e->OnLabelsEnd(); + } + + e->OnSummaryDouble(now, TestInfSummary()); e->OnMetricEnd(); - } - e->OnStreamEnd(); - }; - - TString result1 = EncodeToString(EncoderJson, writeDocument); - UNIT_ASSERT_NO_DIFF(result1, NResource::Find("/summary_inf.json")); - - TString result2 = EncodeToString(BufferedEncoderJson, writeDocument); - UNIT_ASSERT_NO_DIFF(result2, NResource::Find("/summary_inf.json")); - } - - Y_UNIT_TEST(SummaryInfDecode) { - NProto::TMultiSamplesList samples; - { + } + e->OnStreamEnd(); + }; + + TString result1 = EncodeToString(EncoderJson, writeDocument); + UNIT_ASSERT_NO_DIFF(result1, NResource::Find("/summary_inf.json")); + + TString result2 = EncodeToString(BufferedEncoderJson, writeDocument); + UNIT_ASSERT_NO_DIFF(result2, NResource::Find("/summary_inf.json")); + } + + Y_UNIT_TEST(SummaryInfDecode) { + NProto::TMultiSamplesList samples; + { IMetricEncoderPtr e = EncoderProtobuf(&samples); - - TString testJson = NResource::Find("/summary_inf.json"); - DecodeJson(testJson, e.Get()); - } - - UNIT_ASSERT_VALUES_EQUAL(1, samples.SamplesSize()); - const NProto::TMultiSample& s = samples.GetSamples(0); - + + TString testJson = NResource::Find("/summary_inf.json"); + DecodeJson(testJson, e.Get()); + } + + UNIT_ASSERT_VALUES_EQUAL(1, samples.SamplesSize()); + const NProto::TMultiSample& s = samples.GetSamples(0); + UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::DSUMMARY); - UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); + UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); AssertLabelEqual(s.GetLabels(0), "metric", "temperature"); - - UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); - - auto actual = s.GetPoints(0).GetSummaryDouble(); - UNIT_ASSERT(std::isnan(actual.GetSum())); - UNIT_ASSERT(actual.GetMin() < 0); - UNIT_ASSERT(std::isinf(actual.GetMin())); - UNIT_ASSERT(actual.GetMax() > 0); - UNIT_ASSERT(std::isinf(actual.GetMax())); - } - - Y_UNIT_TEST(SummaryValueDecode) { - NProto::TMultiSamplesList samples; - { + + UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); + + auto actual = s.GetPoints(0).GetSummaryDouble(); + UNIT_ASSERT(std::isnan(actual.GetSum())); + UNIT_ASSERT(actual.GetMin() < 0); + UNIT_ASSERT(std::isinf(actual.GetMin())); + UNIT_ASSERT(actual.GetMax() > 0); + UNIT_ASSERT(std::isinf(actual.GetMax())); + } + + Y_UNIT_TEST(SummaryValueDecode) { + NProto::TMultiSamplesList samples; + { IMetricEncoderPtr e = EncoderProtobuf(&samples); - - TString testJson = NResource::Find("/summary_value.json"); - DecodeJson(testJson, e.Get()); - } - - UNIT_ASSERT_VALUES_EQUAL(1, samples.SamplesSize()); - const NProto::TMultiSample& s = samples.GetSamples(0); - + + TString testJson = NResource::Find("/summary_value.json"); + DecodeJson(testJson, e.Get()); + } + + UNIT_ASSERT_VALUES_EQUAL(1, samples.SamplesSize()); + const NProto::TMultiSample& s = samples.GetSamples(0); + UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::DSUMMARY); - UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); + UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); AssertLabelEqual(s.GetLabels(0), "metric", "temperature"); - - UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); - - auto snapshot = TSummaryDoubleSnapshot(10., -0.5, 0.5, 0.3, 30u); - AssertPointEqual(s.GetPoints(0), now, snapshot); - } - - Y_UNIT_TEST(SummaryTimeSeriesEncode) { + + UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); + + auto snapshot = TSummaryDoubleSnapshot(10., -0.5, 0.5, 0.3, 30u); + AssertPointEqual(s.GetPoints(0), now, snapshot); + } + + Y_UNIT_TEST(SummaryTimeSeriesEncode) { auto writeDocument = [](IMetricEncoder* e) { - e->OnStreamBegin(); - { + e->OnStreamBegin(); + { e->OnMetricBegin(EMetricType::DSUMMARY); - { - e->OnLabelsBegin(); + { + e->OnLabelsBegin(); e->OnLabel("metric", "temperature"); - e->OnLabelsEnd(); - } - - TSummaryDoubleCollector summary; - summary.Collect(0.3); - summary.Collect(-0.5); - summary.Collect(1.); - - e->OnSummaryDouble(now, summary.Snapshot()); - - summary.Collect(-1.5); - summary.Collect(0.01); - - e->OnSummaryDouble(now + TDuration::Seconds(15), summary.Snapshot()); - + e->OnLabelsEnd(); + } + + TSummaryDoubleCollector summary; + summary.Collect(0.3); + summary.Collect(-0.5); + summary.Collect(1.); + + e->OnSummaryDouble(now, summary.Snapshot()); + + summary.Collect(-1.5); + summary.Collect(0.01); + + e->OnSummaryDouble(now + TDuration::Seconds(15), summary.Snapshot()); + e->OnMetricEnd(); - } - e->OnStreamEnd(); - }; - - TString result1 = EncodeToString(EncoderJson, writeDocument); - UNIT_ASSERT_NO_DIFF(result1, NResource::Find("/summary_timeseries.json")); - - TString result2 = EncodeToString(BufferedEncoderJson, writeDocument); - UNIT_ASSERT_NO_DIFF(result2, NResource::Find("/summary_timeseries.json")); - } - - Y_UNIT_TEST(SummaryTimeSeriesDecode) { - NProto::TMultiSamplesList samples; - { + } + e->OnStreamEnd(); + }; + + TString result1 = EncodeToString(EncoderJson, writeDocument); + UNIT_ASSERT_NO_DIFF(result1, NResource::Find("/summary_timeseries.json")); + + TString result2 = EncodeToString(BufferedEncoderJson, writeDocument); + UNIT_ASSERT_NO_DIFF(result2, NResource::Find("/summary_timeseries.json")); + } + + Y_UNIT_TEST(SummaryTimeSeriesDecode) { + NProto::TMultiSamplesList samples; + { IMetricEncoderPtr e = EncoderProtobuf(&samples); - - TString testJson = NResource::Find("/summary_timeseries.json"); - DecodeJson(testJson, e.Get()); - } - - UNIT_ASSERT_VALUES_EQUAL(1, samples.SamplesSize()); - const NProto::TMultiSample& s = samples.GetSamples(0); - + + TString testJson = NResource::Find("/summary_timeseries.json"); + DecodeJson(testJson, e.Get()); + } + + UNIT_ASSERT_VALUES_EQUAL(1, samples.SamplesSize()); + const NProto::TMultiSample& s = samples.GetSamples(0); + UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::DSUMMARY); - UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); + UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); AssertLabelEqual(s.GetLabels(0), "metric", "temperature"); - - UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 2); - - TSummaryDoubleCollector summary; - summary.Collect(0.3); - summary.Collect(-0.5); - summary.Collect(1.); - - AssertPointEqual(s.GetPoints(0), now, *summary.Snapshot()); - - summary.Collect(-1.5); - summary.Collect(0.01); - - AssertPointEqual(s.GetPoints(1), now + TDuration::Seconds(15), *summary.Snapshot()); - } - - Y_UNIT_TEST(LogHistogramValueEncode) { - auto writeDocument = [](IMetricEncoder* e) { - e->OnStreamBegin(); - { - e->OnMetricBegin(EMetricType::LOGHIST); - { - e->OnLabelsBegin(); + + UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 2); + + TSummaryDoubleCollector summary; + summary.Collect(0.3); + summary.Collect(-0.5); + summary.Collect(1.); + + AssertPointEqual(s.GetPoints(0), now, *summary.Snapshot()); + + summary.Collect(-1.5); + summary.Collect(0.01); + + AssertPointEqual(s.GetPoints(1), now + TDuration::Seconds(15), *summary.Snapshot()); + } + + Y_UNIT_TEST(LogHistogramValueEncode) { + auto writeDocument = [](IMetricEncoder* e) { + e->OnStreamBegin(); + { + e->OnMetricBegin(EMetricType::LOGHIST); + { + e->OnLabelsBegin(); e->OnLabel("metric", "ms"); - e->OnLabelsEnd(); - } - - e->OnLogHistogram(now, TestLogHistogram()); - e->OnMetricEnd(); - } - e->OnStreamEnd(); - }; - - TString result1 = EncodeToString(EncoderJson, writeDocument); - UNIT_ASSERT_NO_DIFF(result1, NResource::Find("/log_histogram_value.json")); - - TString result2 = EncodeToString(BufferedEncoderJson, writeDocument); - UNIT_ASSERT_NO_DIFF(result2, NResource::Find("/log_histogram_value.json")); - } - - Y_UNIT_TEST(LogHistogramValueDecode) { - NProto::TMultiSamplesList samples; - { - IMetricEncoderPtr e = EncoderProtobuf(&samples); - - TString testJson = NResource::Find("/log_histogram_value.json"); - DecodeJson(testJson, e.Get()); - } - - UNIT_ASSERT_VALUES_EQUAL(1, samples.SamplesSize()); - const NProto::TMultiSample& s = samples.GetSamples(0); - - UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::LOGHISTOGRAM); - UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); + e->OnLabelsEnd(); + } + + e->OnLogHistogram(now, TestLogHistogram()); + e->OnMetricEnd(); + } + e->OnStreamEnd(); + }; + + TString result1 = EncodeToString(EncoderJson, writeDocument); + UNIT_ASSERT_NO_DIFF(result1, NResource::Find("/log_histogram_value.json")); + + TString result2 = EncodeToString(BufferedEncoderJson, writeDocument); + UNIT_ASSERT_NO_DIFF(result2, NResource::Find("/log_histogram_value.json")); + } + + Y_UNIT_TEST(LogHistogramValueDecode) { + NProto::TMultiSamplesList samples; + { + IMetricEncoderPtr e = EncoderProtobuf(&samples); + + TString testJson = NResource::Find("/log_histogram_value.json"); + DecodeJson(testJson, e.Get()); + } + + UNIT_ASSERT_VALUES_EQUAL(1, samples.SamplesSize()); + const NProto::TMultiSample& s = samples.GetSamples(0); + + UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::LOGHISTOGRAM); + UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); AssertLabelEqual(s.GetLabels(0), "metric", "ms"); - - UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); - - auto snapshot = TestLogHistogram(); - AssertPointEqual(s.GetPoints(0), now, *snapshot); - } - + + UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); + + auto snapshot = TestLogHistogram(); + AssertPointEqual(s.GetPoints(0), now, *snapshot); + } + Y_UNIT_TEST(HistogramValueEncode) { auto writeDocument = [](IMetricEncoder* e) { e->OnStreamBegin(); @@ -981,58 +981,58 @@ Y_UNIT_TEST_SUITE(TJsonTest) { UNIT_ASSERT_NO_DIFF(result2, NResource::Find("/histogram_value.json")); } - Y_UNIT_TEST(LogHistogramTimeSeriesEncode) { - auto writeDocument = [](IMetricEncoder* e) { - e->OnStreamBegin(); - { - e->OnMetricBegin(EMetricType::LOGHIST); - { - e->OnLabelsBegin(); + Y_UNIT_TEST(LogHistogramTimeSeriesEncode) { + auto writeDocument = [](IMetricEncoder* e) { + e->OnStreamBegin(); + { + e->OnMetricBegin(EMetricType::LOGHIST); + { + e->OnLabelsBegin(); e->OnLabel("metric", "ms"); - e->OnLabelsEnd(); - } - - e->OnLogHistogram(now, TestLogHistogram(1));; - - e->OnLogHistogram(now + TDuration::Seconds(15), TestLogHistogram(2)); - - e->OnMetricEnd(); - } - e->OnStreamEnd(); - }; - - TString result1 = EncodeToString(EncoderJson, writeDocument); - UNIT_ASSERT_NO_DIFF(result1, NResource::Find("/log_histogram_timeseries.json")); - - TString result2 = EncodeToString(BufferedEncoderJson, writeDocument); - UNIT_ASSERT_NO_DIFF(result2, NResource::Find("/log_histogram_timeseries.json")); - } - - Y_UNIT_TEST(LogHistogramTimeSeriesDecode) { - NProto::TMultiSamplesList samples; - { - IMetricEncoderPtr e = EncoderProtobuf(&samples); - - TString testJson = NResource::Find("/log_histogram_timeseries.json"); - DecodeJson(testJson, e.Get()); - } - - UNIT_ASSERT_VALUES_EQUAL(1, samples.SamplesSize()); - const NProto::TMultiSample& s = samples.GetSamples(0); - - UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::LOGHISTOGRAM); - UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); + e->OnLabelsEnd(); + } + + e->OnLogHistogram(now, TestLogHistogram(1));; + + e->OnLogHistogram(now + TDuration::Seconds(15), TestLogHistogram(2)); + + e->OnMetricEnd(); + } + e->OnStreamEnd(); + }; + + TString result1 = EncodeToString(EncoderJson, writeDocument); + UNIT_ASSERT_NO_DIFF(result1, NResource::Find("/log_histogram_timeseries.json")); + + TString result2 = EncodeToString(BufferedEncoderJson, writeDocument); + UNIT_ASSERT_NO_DIFF(result2, NResource::Find("/log_histogram_timeseries.json")); + } + + Y_UNIT_TEST(LogHistogramTimeSeriesDecode) { + NProto::TMultiSamplesList samples; + { + IMetricEncoderPtr e = EncoderProtobuf(&samples); + + TString testJson = NResource::Find("/log_histogram_timeseries.json"); + DecodeJson(testJson, e.Get()); + } + + UNIT_ASSERT_VALUES_EQUAL(1, samples.SamplesSize()); + const NProto::TMultiSample& s = samples.GetSamples(0); + + UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::LOGHISTOGRAM); + UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); AssertLabelEqual(s.GetLabels(0), "metric", "ms"); - - UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 2); - - auto logHist = TestLogHistogram(1); - AssertPointEqual(s.GetPoints(0), now, *logHist); - - logHist = TestLogHistogram(2); - AssertPointEqual(s.GetPoints(1), now + TDuration::Seconds(15), *logHist); - } - + + UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 2); + + auto logHist = TestLogHistogram(1); + AssertPointEqual(s.GetPoints(0), now, *logHist); + + logHist = TestLogHistogram(2); + AssertPointEqual(s.GetPoints(1), now + TDuration::Seconds(15), *logHist); + } + void HistogramValueDecode(const TString& filePath) { NProto::TMultiSamplesList samples; { diff --git a/library/cpp/monlib/encode/json/typed_point.h b/library/cpp/monlib/encode/json/typed_point.h index fbaa840c4b..5edeaab8be 100644 --- a/library/cpp/monlib/encode/json/typed_point.h +++ b/library/cpp/monlib/encode/json/typed_point.h @@ -98,20 +98,20 @@ namespace NMonitoring { if (ValueType_ == EMetricValueType::HISTOGRAM) { Value_.AsHistogram()->Ref(); } else if (ValueType_ == EMetricValueType::SUMMARY) { - Value_.AsSummaryDouble()->Ref(); - } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { - Value_.AsLogHistogram()->Ref(); - } + Value_.AsSummaryDouble()->Ref(); + } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { + Value_.AsLogHistogram()->Ref(); + } } void UnRef() { if (ValueType_ == EMetricValueType::HISTOGRAM) { Value_.AsHistogram()->UnRef(); } else if (ValueType_ == EMetricValueType::SUMMARY) { - Value_.AsSummaryDouble()->UnRef(); - } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { - Value_.AsLogHistogram()->UnRef(); - } + Value_.AsSummaryDouble()->UnRef(); + } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { + Value_.AsLogHistogram()->UnRef(); + } } private: diff --git a/library/cpp/monlib/encode/json/ut/log_histogram_timeseries.json b/library/cpp/monlib/encode/json/ut/log_histogram_timeseries.json index e811a2cc57..6a1e1947d1 100644 --- a/library/cpp/monlib/encode/json/ut/log_histogram_timeseries.json +++ b/library/cpp/monlib/encode/json/ut/log_histogram_timeseries.json @@ -1,47 +1,47 @@ -{ - "sensors": - [ - { - "kind":"LOGHIST", - "labels": - { +{ + "sensors": + [ + { + "kind":"LOGHIST", + "labels": + { "metric":"ms" - }, - "timeseries": - [ - { - "ts":1509843723, - "log_hist": - { - "base":1.5, - "zeros_count":1, - "start_power":0, - "buckets": - [ - 0.5, - 0.25, - 0.25, - 0.5 - ] - } - }, - { - "ts":1509843738, - "log_hist": - { - "base":1.5, - "zeros_count":1, - "start_power":0, - "buckets": - [ - 1, - 0.5, - 0.5, - 1 - ] - } - } - ] - } - ] -} + }, + "timeseries": + [ + { + "ts":1509843723, + "log_hist": + { + "base":1.5, + "zeros_count":1, + "start_power":0, + "buckets": + [ + 0.5, + 0.25, + 0.25, + 0.5 + ] + } + }, + { + "ts":1509843738, + "log_hist": + { + "base":1.5, + "zeros_count":1, + "start_power":0, + "buckets": + [ + 1, + 0.5, + 0.5, + 1 + ] + } + } + ] + } + ] +} diff --git a/library/cpp/monlib/encode/json/ut/log_histogram_value.json b/library/cpp/monlib/encode/json/ut/log_histogram_value.json index 002478293b..919d709549 100644 --- a/library/cpp/monlib/encode/json/ut/log_histogram_value.json +++ b/library/cpp/monlib/encode/json/ut/log_histogram_value.json @@ -1,26 +1,26 @@ -{ - "sensors": - [ - { - "kind":"LOGHIST", - "labels": - { +{ + "sensors": + [ + { + "kind":"LOGHIST", + "labels": + { "metric":"ms" - }, - "ts":1509843723, - "log_hist": - { - "base":1.5, - "zeros_count":1, - "start_power":0, - "buckets": - [ - 0.5, - 0.25, - 0.25, - 0.5 - ] - } - } - ] -} + }, + "ts":1509843723, + "log_hist": + { + "base":1.5, + "zeros_count":1, + "start_power":0, + "buckets": + [ + 0.5, + 0.25, + 0.25, + 0.5 + ] + } + } + ] +} diff --git a/library/cpp/monlib/encode/json/ut/summary_inf.json b/library/cpp/monlib/encode/json/ut/summary_inf.json index 625a6cd8ad..9d335a4d8a 100644 --- a/library/cpp/monlib/encode/json/ut/summary_inf.json +++ b/library/cpp/monlib/encode/json/ut/summary_inf.json @@ -1,21 +1,21 @@ -{ - "sensors": - [ - { - "kind":"DSUMMARY", - "labels": - { +{ + "sensors": + [ + { + "kind":"DSUMMARY", + "labels": + { "metric":"temperature" - }, - "ts":1509843723, - "summary": - { - "sum":"nan", - "min":"-inf", - "max":"inf", - "last":0.3, - "count":30 - } - } - ] -} + }, + "ts":1509843723, + "summary": + { + "sum":"nan", + "min":"-inf", + "max":"inf", + "last":0.3, + "count":30 + } + } + ] +} diff --git a/library/cpp/monlib/encode/json/ut/summary_timeseries.json b/library/cpp/monlib/encode/json/ut/summary_timeseries.json index 92007af3e6..82704a7b72 100644 --- a/library/cpp/monlib/encode/json/ut/summary_timeseries.json +++ b/library/cpp/monlib/encode/json/ut/summary_timeseries.json @@ -1,37 +1,37 @@ -{ - "sensors": - [ - { - "kind":"DSUMMARY", - "labels": - { +{ + "sensors": + [ + { + "kind":"DSUMMARY", + "labels": + { "metric":"temperature" - }, - "timeseries": - [ - { - "ts":1509843723, - "summary": - { - "sum":0.8, - "min":-0.5, - "max":1, - "last":1, - "count":3 - } - }, - { - "ts":1509843738, - "summary": - { - "sum":-0.69, - "min":-1.5, - "max":1, - "last":0.01, - "count":5 - } - } - ] - } - ] -} + }, + "timeseries": + [ + { + "ts":1509843723, + "summary": + { + "sum":0.8, + "min":-0.5, + "max":1, + "last":1, + "count":3 + } + }, + { + "ts":1509843738, + "summary": + { + "sum":-0.69, + "min":-1.5, + "max":1, + "last":0.01, + "count":5 + } + } + ] + } + ] +} diff --git a/library/cpp/monlib/encode/json/ut/summary_value.json b/library/cpp/monlib/encode/json/ut/summary_value.json index 366394c5e1..7eaf355d18 100644 --- a/library/cpp/monlib/encode/json/ut/summary_value.json +++ b/library/cpp/monlib/encode/json/ut/summary_value.json @@ -1,21 +1,21 @@ -{ - "sensors": - [ - { - "kind":"DSUMMARY", - "labels": - { +{ + "sensors": + [ + { + "kind":"DSUMMARY", + "labels": + { "metric":"temperature" - }, - "ts":1509843723, - "summary": - { - "sum":10, - "min":-0.5, - "max":0.5, - "last":0.3, - "count":30 - } - } - ] -} + }, + "ts":1509843723, + "summary": + { + "sum":10, + "min":-0.5, + "max":0.5, + "last":0.3, + "count":30 + } + } + ] +} diff --git a/library/cpp/monlib/encode/json/ut/ya.make b/library/cpp/monlib/encode/json/ut/ya.make index e50c4f4903..70f9e4c917 100644 --- a/library/cpp/monlib/encode/json/ut/ya.make +++ b/library/cpp/monlib/encode/json/ut/ya.make @@ -29,11 +29,11 @@ RESOURCE( test_decode_to_encode.json /test_decode_to_encode.json crash.json /crash.json hist_crash.json /hist_crash.json - summary_value.json /summary_value.json - summary_inf.json /summary_inf.json - summary_timeseries.json /summary_timeseries.json - log_histogram_value.json /log_histogram_value.json - log_histogram_timeseries.json /log_histogram_timeseries.json + summary_value.json /summary_value.json + summary_inf.json /summary_inf.json + summary_timeseries.json /summary_timeseries.json + log_histogram_value.json /log_histogram_value.json + log_histogram_timeseries.json /log_histogram_timeseries.json ) PEERDIR( diff --git a/library/cpp/monlib/encode/prometheus/prometheus_encoder.cpp b/library/cpp/monlib/encode/prometheus/prometheus_encoder.cpp index 15efeb8c03..f77c4a3137 100644 --- a/library/cpp/monlib/encode/prometheus/prometheus_encoder.cpp +++ b/library/cpp/monlib/encode/prometheus/prometheus_encoder.cpp @@ -45,11 +45,11 @@ namespace NMonitoring { case EMetricType::HIST_RATE: Out_->Write("histogram"); break; - case EMetricType::LOGHIST: - // TODO(@kbalakirev): implement this case - break; + case EMetricType::LOGHIST: + // TODO(@kbalakirev): implement this case + break; case EMetricType::DSUMMARY: - ythrow yexception() << "writing summary type is forbiden"; + ythrow yexception() << "writing summary type is forbiden"; case EMetricType::UNKNOWN: ythrow yexception() << "unknown metric type: " << MetricTypeToStr(type) << ", name: " << name; @@ -90,14 +90,14 @@ namespace NMonitoring { WriteValue(name, NPrometheus::COUNT_SUFFIX, labels, "", "", time, totalCount); } - void WriteSummaryDouble(TStringBuf name, const TLabels& labels, TInstant time, ISummaryDoubleSnapshot* s) { - WriteValue(name, NPrometheus::SUM_SUFFIX, labels, "", "", time, s->GetSum()); - WriteValue(name, NPrometheus::MIN_SUFFIX, labels, "", "", time, s->GetMin()); - WriteValue(name, NPrometheus::MAX_SUFFIX, labels, "", "", time, s->GetMax()); - WriteValue(name, NPrometheus::LAST_SUFFIX, labels, "", "", time, s->GetLast()); - WriteValue(name, NPrometheus::COUNT_SUFFIX, labels, "", "", time, s->GetCount()); - } - + void WriteSummaryDouble(TStringBuf name, const TLabels& labels, TInstant time, ISummaryDoubleSnapshot* s) { + WriteValue(name, NPrometheus::SUM_SUFFIX, labels, "", "", time, s->GetSum()); + WriteValue(name, NPrometheus::MIN_SUFFIX, labels, "", "", time, s->GetMin()); + WriteValue(name, NPrometheus::MAX_SUFFIX, labels, "", "", time, s->GetMax()); + WriteValue(name, NPrometheus::LAST_SUFFIX, labels, "", "", time, s->GetLast()); + WriteValue(name, NPrometheus::COUNT_SUFFIX, labels, "", "", time, s->GetCount()); + } + void WriteLn() { Out_->Write('\n'); } @@ -220,7 +220,7 @@ namespace NMonitoring { if (ValueType == EMetricValueType::HISTOGRAM) { Value.AsHistogram()->UnRef(); } else if (ValueType == EMetricValueType::SUMMARY) { - Value.AsSummaryDouble()->UnRef(); + Value.AsSummaryDouble()->UnRef(); } ValueType = EMetricValueType::UNKNOWN; Value = {}; @@ -232,14 +232,14 @@ namespace NMonitoring { if (ValueType == EMetricValueType::HISTOGRAM) { Value.AsHistogram()->UnRef(); } else if (ValueType == EMetricValueType::SUMMARY) { - Value.AsSummaryDouble()->UnRef(); + Value.AsSummaryDouble()->UnRef(); } ValueType = TValueType<T>::Type; Value = TMetricValue(value); if (ValueType == EMetricValueType::HISTOGRAM) { Value.AsHistogram()->Ref(); } else if (ValueType == EMetricValueType::SUMMARY) { - Value.AsSummaryDouble()->Ref(); + Value.AsSummaryDouble()->Ref(); } } }; @@ -335,16 +335,16 @@ namespace NMonitoring { MetricState_.SetValue(snapshot.Get()); } - void OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr snapshot) override { + void OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr snapshot) override { State_.Expect(TEncoderState::EState::METRIC); MetricState_.Time = time; MetricState_.SetValue(snapshot.Get()); - } - - void OnLogHistogram(TInstant, TLogHistogramSnapshotPtr) override { - // TODO(@kbalakirev): implement this function - } - + } + + void OnLogHistogram(TInstant, TLogHistogramSnapshotPtr) override { + // TODO(@kbalakirev): implement this function + } + void Close() override { } @@ -366,7 +366,7 @@ namespace NMonitoring { const TString& metricName = ToString(nameLabel->Value()); if (MetricState_.Type != EMetricType::DSUMMARY) { Writer_.WriteType(MetricState_.Type, metricName); - } + } if (MetricState_.Time == TInstant::Zero()) { MetricState_.Time = CommonTime_; @@ -382,8 +382,8 @@ namespace NMonitoring { MetricState_.Time, MetricState_.Value.AsHistogram()); } else if (type == EMetricType::DSUMMARY) { - Writer_.WriteSummaryDouble( - metricName, + Writer_.WriteSummaryDouble( + metricName, MetricState_.Labels, MetricState_.Time, MetricState_.Value.AsSummaryDouble()); diff --git a/library/cpp/monlib/encode/prometheus/prometheus_encoder_ut.cpp b/library/cpp/monlib/encode/prometheus/prometheus_encoder_ut.cpp index fd9debb060..562dec43cd 100644 --- a/library/cpp/monlib/encode/prometheus/prometheus_encoder_ut.cpp +++ b/library/cpp/monlib/encode/prometheus/prometheus_encoder_ut.cpp @@ -20,10 +20,10 @@ Y_UNIT_TEST_SUITE(TPrometheusEncoderTest) { return ss.Str(); } - ISummaryDoubleSnapshotPtr TestSummaryDouble() { - return MakeIntrusive<TSummaryDoubleSnapshot>(10.1, -0.45, 0.478, 0.3, 30u); - } - + ISummaryDoubleSnapshotPtr TestSummaryDouble() { + return MakeIntrusive<TSummaryDoubleSnapshot>(10.1, -0.45, 0.478, 0.3, 30u); + } + Y_UNIT_TEST(Empty) { auto result = EncodeToString([](IMetricEncoder* e) { e->OnStreamBegin(); @@ -112,17 +112,17 @@ Y_UNIT_TEST_SUITE(TPrometheusEncoderTest) { e->OnDouble(TInstant::Zero(), INFINITY); e->OnMetricEnd(); } - { + { e->OnMetricBegin(EMetricType::DSUMMARY); - { - e->OnLabelsBegin(); - e->OnLabel("sensor", "seconds"); - e->OnLabel("disk", "sdb1"); - e->OnLabelsEnd(); - } - e->OnSummaryDouble(TInstant::Zero(), TestSummaryDouble()); + { + e->OnLabelsBegin(); + e->OnLabel("sensor", "seconds"); + e->OnLabel("disk", "sdb1"); + e->OnLabelsEnd(); + } + e->OnSummaryDouble(TInstant::Zero(), TestSummaryDouble()); e->OnMetricEnd(); - } + } e->OnStreamEnd(); }); @@ -138,11 +138,11 @@ Y_UNIT_TEST_SUITE(TPrometheusEncoderTest) { "nanValue nan\n" "# TYPE infValue gauge\n" "infValue inf\n" - "seconds_sum{disk=\"sdb1\", } 10.1\n" - "seconds_min{disk=\"sdb1\", } -0.45\n" - "seconds_max{disk=\"sdb1\", } 0.478\n" - "seconds_last{disk=\"sdb1\", } 0.3\n" - "seconds_count{disk=\"sdb1\", } 30\n" + "seconds_sum{disk=\"sdb1\", } 10.1\n" + "seconds_min{disk=\"sdb1\", } -0.45\n" + "seconds_max{disk=\"sdb1\", } 0.478\n" + "seconds_last{disk=\"sdb1\", } 0.3\n" + "seconds_count{disk=\"sdb1\", } 30\n" "\n"); } diff --git a/library/cpp/monlib/encode/protobuf/protobuf_encoder.cpp b/library/cpp/monlib/encode/protobuf/protobuf_encoder.cpp index 2d11b9d5ba..b7e2fca629 100644 --- a/library/cpp/monlib/encode/protobuf/protobuf_encoder.cpp +++ b/library/cpp/monlib/encode/protobuf/protobuf_encoder.cpp @@ -19,9 +19,9 @@ namespace NMonitoring { case EMetricType::HIST_RATE: return NProto::HIST_RATE; case EMetricType::DSUMMARY: - return NProto::DSUMMARY; - case EMetricType::LOGHIST: - return NProto::LOGHISTOGRAM; + return NProto::DSUMMARY; + case EMetricType::LOGHIST: + return NProto::LOGHISTOGRAM; case EMetricType::UNKNOWN: return NProto::UNKNOWN; } @@ -37,23 +37,23 @@ namespace NMonitoring { } } - void FillSummaryDouble(const ISummaryDoubleSnapshot& snapshot, NProto::TSummaryDouble* summary) { - summary->SetSum(snapshot.GetSum()); - summary->SetMin(snapshot.GetMin()); - summary->SetMax(snapshot.GetMax()); - summary->SetLast(snapshot.GetLast()); - summary->SetCount(snapshot.GetCount()); - } - - void FillLogHistogram(const TLogHistogramSnapshot& snapshot, NProto::TLogHistogram* logHist) { - logHist->SetBase(snapshot.Base()); - logHist->SetZerosCount(snapshot.ZerosCount()); - logHist->SetStartPower(snapshot.StartPower()); - for (ui32 i = 0; i < snapshot.Count(); ++i) { - logHist->AddBuckets(snapshot.Bucket(i)); - } - } - + void FillSummaryDouble(const ISummaryDoubleSnapshot& snapshot, NProto::TSummaryDouble* summary) { + summary->SetSum(snapshot.GetSum()); + summary->SetMin(snapshot.GetMin()); + summary->SetMax(snapshot.GetMax()); + summary->SetLast(snapshot.GetLast()); + summary->SetCount(snapshot.GetCount()); + } + + void FillLogHistogram(const TLogHistogramSnapshot& snapshot, NProto::TLogHistogram* logHist) { + logHist->SetBase(snapshot.Base()); + logHist->SetZerosCount(snapshot.ZerosCount()); + logHist->SetStartPower(snapshot.StartPower()); + for (ui32 i = 0; i < snapshot.Count(); ++i) { + logHist->AddBuckets(snapshot.Bucket(i)); + } + } + /////////////////////////////////////////////////////////////////////////////// // TSingleamplesEncoder /////////////////////////////////////////////////////////////////////////////// @@ -121,18 +121,18 @@ namespace NMonitoring { FillHistogram(*snapshot, Sample_->MutableHistogram()); } - void OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr snapshot) override { + void OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr snapshot) override { Y_ENSURE(Sample_, "metric not started"); - Sample_->SetTime(time.MilliSeconds()); - FillSummaryDouble(*snapshot, Sample_->MutableSummaryDouble()); - } - - void OnLogHistogram(TInstant time, TLogHistogramSnapshotPtr snapshot) override { + Sample_->SetTime(time.MilliSeconds()); + FillSummaryDouble(*snapshot, Sample_->MutableSummaryDouble()); + } + + void OnLogHistogram(TInstant time, TLogHistogramSnapshotPtr snapshot) override { Y_ENSURE(Sample_, "metric not started"); - Sample_->SetTime(time.MilliSeconds()); - FillLogHistogram(*snapshot, Sample_->MutableLogHistogram()); - } - + Sample_->SetTime(time.MilliSeconds()); + FillLogHistogram(*snapshot, Sample_->MutableLogHistogram()); + } + void Close() override { } @@ -215,18 +215,18 @@ namespace NMonitoring { void OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr snapshot) override { Y_ENSURE(Sample_, "metric not started"); - NProto::TPoint* point = Sample_->AddPoints(); - point->SetTime(time.MilliSeconds()); - FillSummaryDouble(*snapshot, point->MutableSummaryDouble()); - } - - void OnLogHistogram(TInstant time, TLogHistogramSnapshotPtr snapshot) override { + NProto::TPoint* point = Sample_->AddPoints(); + point->SetTime(time.MilliSeconds()); + FillSummaryDouble(*snapshot, point->MutableSummaryDouble()); + } + + void OnLogHistogram(TInstant time, TLogHistogramSnapshotPtr snapshot) override { Y_ENSURE(Sample_, "metric not started"); - NProto::TPoint* point = Sample_->AddPoints(); - point->SetTime(time.MilliSeconds()); - FillLogHistogram(*snapshot, point->MutableLogHistogram()); - } - + NProto::TPoint* point = Sample_->AddPoints(); + point->SetTime(time.MilliSeconds()); + FillLogHistogram(*snapshot, point->MutableLogHistogram()); + } + void Close() override { } diff --git a/library/cpp/monlib/encode/protobuf/protos/samples.proto b/library/cpp/monlib/encode/protobuf/protos/samples.proto index 371f4181d2..6e6646303a 100644 --- a/library/cpp/monlib/encode/protobuf/protos/samples.proto +++ b/library/cpp/monlib/encode/protobuf/protos/samples.proto @@ -19,8 +19,8 @@ enum EMetricType { RATE = 4; HISTOGRAM = 5; HIST_RATE = 6; - DSUMMARY = 7; - LOGHISTOGRAM = 8; + DSUMMARY = 7; + LOGHISTOGRAM = 8; } message THistogram { @@ -28,21 +28,21 @@ message THistogram { repeated uint64 Values = 2; // values stored in each bucket } -message TLogHistogram { - double Base = 1; - uint64 ZerosCount = 2; - int32 StartPower = 3; - repeated double Buckets = 4; -} - -message TSummaryDouble { - double Sum = 1; - double Min = 2; - double Max = 3; - double Last = 4; - uint64 Count = 5; -} - +message TLogHistogram { + double Base = 1; + uint64 ZerosCount = 2; + int32 StartPower = 3; + repeated double Buckets = 4; +} + +message TSummaryDouble { + double Sum = 1; + double Min = 2; + double Max = 3; + double Last = 4; + uint64 Count = 5; +} + // see TSingleSample message TPoint { uint64 Time = 1; @@ -51,8 +51,8 @@ message TPoint { fixed64 Uint64 = 3; double Float64 = 4; THistogram Histogram = 5; - TSummaryDouble SummaryDouble = 6; - TLogHistogram LogHistogram = 7; + TSummaryDouble SummaryDouble = 6; + TLogHistogram LogHistogram = 7; } } @@ -67,8 +67,8 @@ message TSingleSample { fixed64 Uint64 = 5; double Float64 = 6; THistogram Histogram = 7; - TSummaryDouble SummaryDouble = 8; - TLogHistogram LogHistogram = 9; + TSummaryDouble SummaryDouble = 8; + TLogHistogram LogHistogram = 9; } } diff --git a/library/cpp/monlib/encode/spack/spack_v1_decoder.cpp b/library/cpp/monlib/encode/spack/spack_v1_decoder.cpp index 1f445fc80d..cffcf94df1 100644 --- a/library/cpp/monlib/encode/spack/spack_v1_decoder.cpp +++ b/library/cpp/monlib/encode/spack/spack_v1_decoder.cpp @@ -167,49 +167,49 @@ namespace NMonitoring { break; case EMetricType::DSUMMARY: - c->OnSummaryDouble(time, ReadSummaryDouble()); - break; - + c->OnSummaryDouble(time, ReadSummaryDouble()); + break; + case EMetricType::HIST: case EMetricType::HIST_RATE: c->OnHistogram(time, ReadHistogram()); break; - case EMetricType::LOGHIST: - c->OnLogHistogram(time, ReadLogHistogram()); - break; - + case EMetricType::LOGHIST: + c->OnLogHistogram(time, ReadLogHistogram()); + break; + default: throw TSpackDecodeError() << "Unsupported metric type: " << metricType; } } - ISummaryDoubleSnapshotPtr ReadSummaryDouble() { - ui64 count = ReadFixed<ui64>(); - double sum = ReadFixed<double>(); - double min = ReadFixed<double>(); - double max = ReadFixed<double>(); - double last = ReadFixed<double>(); - return MakeIntrusive<TSummaryDoubleSnapshot>(sum, min, max, last, count); - } - - TLogHistogramSnapshotPtr ReadLogHistogram() { - double base = ReadFixed<double>(); - ui64 zerosCount = ReadFixed<ui64>(); - int startPower = static_cast<int>(ReadVarint()); - ui32 count = ReadVarint(); + ISummaryDoubleSnapshotPtr ReadSummaryDouble() { + ui64 count = ReadFixed<ui64>(); + double sum = ReadFixed<double>(); + double min = ReadFixed<double>(); + double max = ReadFixed<double>(); + double last = ReadFixed<double>(); + return MakeIntrusive<TSummaryDoubleSnapshot>(sum, min, max, last, count); + } + + TLogHistogramSnapshotPtr ReadLogHistogram() { + double base = ReadFixed<double>(); + ui64 zerosCount = ReadFixed<ui64>(); + int startPower = static_cast<int>(ReadVarint()); + ui32 count = ReadVarint(); // see https://a.yandex-team.ru/arc/trunk/arcadia/infra/yasm/stockpile_client/points.cpp?rev=r8593154#L31 // and https://a.yandex-team.ru/arc/trunk/arcadia/infra/yasm/common/points/hgram/normal/normal.h?rev=r8268697#L9 // TODO: share this constant value Y_ENSURE(count <= 100u, "more than 100 buckets in log histogram: " << count); - TVector<double> buckets; - buckets.reserve(count); - for (ui32 i = 0; i < count; ++i) { - buckets.emplace_back(ReadFixed<double>()); - } - return MakeIntrusive<TLogHistogramSnapshot>(base, zerosCount, startPower, std::move(buckets)); - } - + TVector<double> buckets; + buckets.reserve(count); + for (ui32 i = 0; i < count; ++i) { + buckets.emplace_back(ReadFixed<double>()); + } + return MakeIntrusive<TLogHistogramSnapshot>(base, zerosCount, startPower, std::move(buckets)); + } + IHistogramSnapshotPtr ReadHistogram() { ui32 bucketsCount = ReadVarint(); auto s = TExplicitHistogramSnapshot::New(bucketsCount); @@ -245,9 +245,9 @@ namespace NMonitoring { IMetricConsumer* c) { for (ui32 i = 0; i < count; i++) { - auto nameIdx = ReadVarint(); - auto valueIdx = ReadVarint(); - c->OnLabel(labelNames.Get(nameIdx), labelValues.Get(valueIdx)); + auto nameIdx = ReadVarint(); + auto valueIdx = ReadVarint(); + c->OnLabel(labelNames.Get(nameIdx), labelValues.Get(valueIdx)); } } @@ -385,7 +385,7 @@ namespace NMonitoring { *result = EMetricType::DSUMMARY; } return true; - } else if (byte == EncodeMetricType(EMetricType::LOGHIST)) { + } else if (byte == EncodeMetricType(EMetricType::LOGHIST)) { if (result) { *result = EMetricType::LOGHIST; } diff --git a/library/cpp/monlib/encode/spack/spack_v1_encoder.cpp b/library/cpp/monlib/encode/spack/spack_v1_encoder.cpp index a2b0bb5f50..727c772bc2 100644 --- a/library/cpp/monlib/encode/spack/spack_v1_encoder.cpp +++ b/library/cpp/monlib/encode/spack/spack_v1_encoder.cpp @@ -60,14 +60,14 @@ namespace NMonitoring { 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 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; @@ -222,13 +222,13 @@ namespace NMonitoring { break; case EMetricType::DSUMMARY: - WriteSummaryDouble(*value.AsSummaryDouble()); - break; - - case EMetricType::LOGHIST: - WriteLogHistogram(*value.AsLogHistogram()); - break; - + WriteSummaryDouble(*value.AsSummaryDouble()); + break; + + case EMetricType::LOGHIST: + WriteLogHistogram(*value.AsLogHistogram()); + break; + default: ythrow yexception() << "unsupported metric type: " << metricType; } @@ -267,24 +267,24 @@ namespace NMonitoring { } } - 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()); - } - + 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_; diff --git a/library/cpp/monlib/encode/spack/spack_v1_ut.cpp b/library/cpp/monlib/encode/spack/spack_v1_ut.cpp index fe778eb7e0..2d8a8fbb51 100644 --- a/library/cpp/monlib/encode/spack/spack_v1_ut.cpp +++ b/library/cpp/monlib/encode/spack/spack_v1_ut.cpp @@ -57,9 +57,9 @@ Y_UNIT_TEST_SUITE(TSpackTest) { 0x00, // time precision (fixed ui8) 0x00, // compression algorithm (fixed ui8) 0x0d, 0x00, 0x00, 0x00, // label names size (fixed ui32) - 0x40, 0x00, 0x00, 0x00, // labels values size (fixed ui32) + 0x40, 0x00, 0x00, 0x00, // labels values size (fixed ui32) 0x08, 0x00, 0x00, 0x00, // metric count (fixed ui32) - 0x08, 0x00, 0x00, 0x00, // points count (fixed ui32) + 0x08, 0x00, 0x00, 0x00, // points count (fixed ui32) }; ui8 expectedHeader[] = { @@ -70,9 +70,9 @@ Y_UNIT_TEST_SUITE(TSpackTest) { 0x00, // time precision (fixed ui8) 0x00, // compression algorithm (fixed ui8) 0x0d, 0x00, 0x00, 0x00, // label names size (fixed ui32) - 0x40, 0x00, 0x00, 0x00, // labels values size (fixed ui32) + 0x40, 0x00, 0x00, 0x00, // labels values size (fixed ui32) 0x08, 0x00, 0x00, 0x00, // metric count (fixed ui32) - 0x08, 0x00, 0x00, 0x00, // points count (fixed ui32) + 0x08, 0x00, 0x00, 0x00, // points count (fixed ui32) }; ui8 expectedStringPools[] = { @@ -86,7 +86,7 @@ Y_UNIT_TEST_SUITE(TSpackTest) { 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, // "responseTimeMillis\0" 0x54, 0x69, 0x6d, 0x65, 0x4d, 0x69, 0x6c, 0x6c, 0x69, 0x73, 0x00, - 0x62, 0x79, 0x74, 0x65, 0x73, 0x00, // "bytes\0" + 0x62, 0x79, 0x74, 0x65, 0x73, 0x00, // "bytes\0" 0x74, 0x65, 0x6D, 0x70, 0x65, 0x72, 0x61, 0x74, // "temperature\0" 0x75, 0x72, 0x65, 0x00, 0x6d, 0x73, 0x00, // "ms\0" @@ -191,42 +191,42 @@ Y_UNIT_TEST_SUITE(TSpackTest) { 0x00, // flags (fixed ui8) 0x01, // metric labels count (varint) 0x00, // label name index (varint) - 0x06, // label value index (varint) + 0x06, // label value index (varint) 0x0b, 0x63, 0xfe, 0x59, // time in seconds (fixed ui32) 0x39, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // value (fixed i64) }; ui8 expectedMetric7[] = { - 0x1e, // types (DSUMMARY | ONE_WITH_TS) (fixed ui8) - 0x00, // flags (fixed ui8) + 0x1e, // types (DSUMMARY | ONE_WITH_TS) (fixed ui8) + 0x00, // flags (fixed ui8) 0x01, // metric labels count (varint) - 0x00, // label name index (varint) + 0x00, // label name index (varint) 0x07, // label value index (varint) - 0x0b, 0x63, 0xfe, 0x59, // time in seconds (fixed ui32) - 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // count (fixed ui64) - 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x24, 0x40, // sum (fixed double) - 0xcd, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xdc, 0xbf, // min (fixed double) - 0x64, 0x3b, 0xdf, 0x4f, 0x8d, 0x97, 0xde, 0x3f, // max (fixed double) - 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0xd3, 0x3f, // last (fixed double) - }; - + 0x0b, 0x63, 0xfe, 0x59, // time in seconds (fixed ui32) + 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // count (fixed ui64) + 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x24, 0x40, // sum (fixed double) + 0xcd, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xdc, 0xbf, // min (fixed double) + 0x64, 0x3b, 0xdf, 0x4f, 0x8d, 0x97, 0xde, 0x3f, // max (fixed double) + 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0xd3, 0x3f, // last (fixed double) + }; + ui8 expectedMetric8[] = { 0x26, // types (LOGHIST | ONE_WITH_TS) (fixed ui8) - 0x00, // flags (fixed ui8) + 0x00, // flags (fixed ui8) 0x01, // metric labels count (variant) - 0x00, // label name index (variant) - 0x08, // label value index (variant) - 0x0b, 0x63, 0xfe, 0x59, // time in seconds (fixed ui32) - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0x3F, // base (fixed double) - 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // zerosCount (fixed ui64) - 0x00, // startPower (variant) - 0x04, // buckets count (variant) - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x3F, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xD0, 0x3F, // bucket values - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xD0, 0x3F, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x3F, - }; - + 0x00, // label name index (variant) + 0x08, // label value index (variant) + 0x0b, 0x63, 0xfe, 0x59, // time in seconds (fixed ui32) + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0x3F, // base (fixed double) + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // zerosCount (fixed ui64) + 0x00, // startPower (variant) + 0x04, // buckets count (variant) + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x3F, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xD0, 0x3F, // bucket values + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xD0, 0x3F, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x3F, + }; + const size_t expectedSize = Y_ARRAY_SIZE(expectedHeader) + Y_ARRAY_SIZE(expectedStringPools) + @@ -252,15 +252,15 @@ Y_UNIT_TEST_SUITE(TSpackTest) { return h->Snapshot(); } - TLogHistogramSnapshotPtr TestLogHistogram() { - TVector buckets{0.5, 0.25, 0.25, 0.5}; - return MakeIntrusive<TLogHistogramSnapshot>(1.5, 1u, 0, std::move(buckets)); - } - - ISummaryDoubleSnapshotPtr TestSummaryDouble() { - return MakeIntrusive<TSummaryDoubleSnapshot>(10.1, -0.45, 0.478, 0.3, 30u); - } - + TLogHistogramSnapshotPtr TestLogHistogram() { + TVector buckets{0.5, 0.25, 0.25, 0.5}; + return MakeIntrusive<TLogHistogramSnapshot>(1.5, 1u, 0, std::move(buckets)); + } + + ISummaryDoubleSnapshotPtr TestSummaryDouble() { + return MakeIntrusive<TSummaryDoubleSnapshot>(10.1, -0.45, 0.478, 0.3, 30u); + } + Y_UNIT_TEST(Encode) { TBuffer buffer; TBufferOutput out(buffer); @@ -345,29 +345,29 @@ Y_UNIT_TEST_SUITE(TSpackTest) { } { // metric 7 e->OnMetricBegin(EMetricType::DSUMMARY); - { - e->OnLabelsBegin(); + { + e->OnLabelsBegin(); e->OnLabel("name", "temperature"); - e->OnLabelsEnd(); - } - e->OnSummaryDouble(now, TestSummaryDouble()); + e->OnLabelsEnd(); + } + e->OnSummaryDouble(now, TestSummaryDouble()); e->OnMetricEnd(); - } + } { // metric 8 - e->OnMetricBegin(EMetricType::LOGHIST); - { - e->OnLabelsBegin(); + e->OnMetricBegin(EMetricType::LOGHIST); + { + e->OnLabelsBegin(); e->OnLabel("name", "ms"); - e->OnLabelsEnd(); - } - e->OnLogHistogram(now, TestLogHistogram()); - e->OnMetricEnd(); - } + e->OnLabelsEnd(); + } + e->OnLogHistogram(now, TestLogHistogram()); + e->OnMetricEnd(); + } e->OnStreamEnd(); e->Close(); - // Cout << "encoded: " << HexEncode(buffer.Data(), buffer.Size()) << Endl; - // Cout << "size: " << buffer.Size() << Endl; + // Cout << "encoded: " << HexEncode(buffer.Data(), buffer.Size()) << Endl; + // Cout << "size: " << buffer.Size() << Endl; UNIT_ASSERT_VALUES_EQUAL(buffer.Size(), expectedSize); @@ -401,10 +401,10 @@ Y_UNIT_TEST_SUITE(TSpackTest) { UNIT_ASSERT_BINARY_EQUALS(p, expectedMetric6); p += Y_ARRAY_SIZE(expectedMetric6); - + UNIT_ASSERT_BINARY_EQUALS(p, expectedMetric7); p += Y_ARRAY_SIZE(expectedMetric7); - + UNIT_ASSERT_BINARY_EQUALS(p, expectedMetric8); p += Y_ARRAY_SIZE(expectedMetric8); } @@ -509,7 +509,7 @@ Y_UNIT_TEST_SUITE(TSpackTest) { UNIT_ASSERT_VALUES_EQUAL(samples.CommonLabelsSize(), 1); AssertLabelEqual(samples.GetCommonLabels(0), "project", "solomon"); - UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 8); + UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 8); { const NProto::TMultiSample& s = samples.GetSamples(0); UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::RATE); @@ -576,51 +576,51 @@ Y_UNIT_TEST_SUITE(TSpackTest) { UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); AssertPointEqual(s.GetPoints(0), now, i64(1337)); } - { - const NProto::TMultiSample& s = samples.GetSamples(6); + { + const NProto::TMultiSample& s = samples.GetSamples(6); UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::DSUMMARY); - UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); + UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); AssertLabelEqual(s.GetLabels(0), "name", "temperature"); - - UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); - - const NProto::TPoint& p = s.GetPoints(0); - UNIT_ASSERT_VALUES_EQUAL(p.GetTime(), now.MilliSeconds()); - UNIT_ASSERT_EQUAL(p.GetValueCase(), NProto::TPoint::kSummaryDouble); - - auto expected = TestSummaryDouble(); - - auto actual = p.GetSummaryDouble(); - - UNIT_ASSERT_VALUES_EQUAL(expected->GetSum(), actual.GetSum()); - UNIT_ASSERT_VALUES_EQUAL(expected->GetMin(), actual.GetMin()); - UNIT_ASSERT_VALUES_EQUAL(expected->GetMax(), actual.GetMax()); - UNIT_ASSERT_VALUES_EQUAL(expected->GetLast(), actual.GetLast()); - UNIT_ASSERT_VALUES_EQUAL(expected->GetCount(), actual.GetCount()); - } - { - const NProto::TMultiSample& s = samples.GetSamples(7); - UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::LOGHISTOGRAM); - UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); + + UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); + + const NProto::TPoint& p = s.GetPoints(0); + UNIT_ASSERT_VALUES_EQUAL(p.GetTime(), now.MilliSeconds()); + UNIT_ASSERT_EQUAL(p.GetValueCase(), NProto::TPoint::kSummaryDouble); + + auto expected = TestSummaryDouble(); + + auto actual = p.GetSummaryDouble(); + + UNIT_ASSERT_VALUES_EQUAL(expected->GetSum(), actual.GetSum()); + UNIT_ASSERT_VALUES_EQUAL(expected->GetMin(), actual.GetMin()); + UNIT_ASSERT_VALUES_EQUAL(expected->GetMax(), actual.GetMax()); + UNIT_ASSERT_VALUES_EQUAL(expected->GetLast(), actual.GetLast()); + UNIT_ASSERT_VALUES_EQUAL(expected->GetCount(), actual.GetCount()); + } + { + const NProto::TMultiSample& s = samples.GetSamples(7); + UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::LOGHISTOGRAM); + UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); AssertLabelEqual(s.GetLabels(0), "name", "ms"); - - UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); - - const NProto::TPoint& p = s.GetPoints(0); - UNIT_ASSERT_VALUES_EQUAL(p.GetTime(), now.MilliSeconds()); - UNIT_ASSERT_EQUAL(p.GetValueCase(), NProto::TPoint::kLogHistogram); - - auto expected = TestLogHistogram(); - auto actual = p.GetLogHistogram(); - - UNIT_ASSERT_VALUES_EQUAL(expected->ZerosCount(), actual.GetZerosCount()); - UNIT_ASSERT_VALUES_EQUAL(expected->Base(), actual.GetBase()); - UNIT_ASSERT_VALUES_EQUAL(expected->StartPower(), actual.GetStartPower()); - UNIT_ASSERT_VALUES_EQUAL(expected->Count(), actual.BucketsSize()); - for (size_t i = 0; i < expected->Count(); ++i) { - UNIT_ASSERT_VALUES_EQUAL(expected->Bucket(i), actual.GetBuckets(i)); - } - } + + UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); + + const NProto::TPoint& p = s.GetPoints(0); + UNIT_ASSERT_VALUES_EQUAL(p.GetTime(), now.MilliSeconds()); + UNIT_ASSERT_EQUAL(p.GetValueCase(), NProto::TPoint::kLogHistogram); + + auto expected = TestLogHistogram(); + auto actual = p.GetLogHistogram(); + + UNIT_ASSERT_VALUES_EQUAL(expected->ZerosCount(), actual.GetZerosCount()); + UNIT_ASSERT_VALUES_EQUAL(expected->Base(), actual.GetBase()); + UNIT_ASSERT_VALUES_EQUAL(expected->StartPower(), actual.GetStartPower()); + UNIT_ASSERT_VALUES_EQUAL(expected->Count(), actual.BucketsSize()); + for (size_t i = 0; i < expected->Count(); ++i) { + UNIT_ASSERT_VALUES_EQUAL(expected->Bucket(i), actual.GetBuckets(i)); + } + } } void TestCompression(ECompression alg) { diff --git a/library/cpp/monlib/encode/text/text_encoder.cpp b/library/cpp/monlib/encode/text/text_encoder.cpp index 10336261f0..8aca133542 100644 --- a/library/cpp/monlib/encode/text/text_encoder.cpp +++ b/library/cpp/monlib/encode/text/text_encoder.cpp @@ -94,16 +94,16 @@ namespace NMonitoring { TimeSeries_.Add(time, snapshot.Get()); } - void OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr snapshot) override { + void OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr snapshot) override { State_.Expect(TEncoderState::EState::METRIC); - TimeSeries_.Add(time, snapshot.Get()); - } - + TimeSeries_.Add(time, snapshot.Get()); + } + void OnLogHistogram(TInstant ts, TLogHistogramSnapshotPtr snapshot) override { State_.Expect(TEncoderState::EState::METRIC); TimeSeries_.Add(ts, snapshot.Get()); - } - + } + void Close() override { } @@ -132,11 +132,11 @@ namespace NMonitoring { (*Out_) << *value.AsHistogram(); break; case EMetricValueType::SUMMARY: - (*Out_) << *value.AsSummaryDouble(); - break; - case EMetricValueType::LOGHISTOGRAM: + (*Out_) << *value.AsSummaryDouble(); + break; + case EMetricValueType::LOGHISTOGRAM: (*Out_) << *value.AsLogHistogram(); - break; + break; case EMetricValueType::UNKNOWN: ythrow yexception() << "unknown metric value type"; } diff --git a/library/cpp/monlib/encode/text/text_encoder_ut.cpp b/library/cpp/monlib/encode/text/text_encoder_ut.cpp index 554b6f5fa9..09418cff96 100644 --- a/library/cpp/monlib/encode/text/text_encoder_ut.cpp +++ b/library/cpp/monlib/encode/text/text_encoder_ut.cpp @@ -260,24 +260,24 @@ Y_UNIT_TEST_SUITE(TTextText) { " HIST readTimeMillis{} [{1: 0, 2: 0, 3: 1, 4: 0, 5: 7, inf: 1}]\n" "HIST_RATE writeTimeMillis{} [{1: 0, 2: 0, 3: 1, 4: 0, 5: 7, inf: 1}]\n"); } - - Y_UNIT_TEST(Summary) { - auto s = MakeIntrusive<TSummaryDoubleSnapshot>(10.1, -0.45, 0.478, 0.3, 30u); + + Y_UNIT_TEST(Summary) { + auto s = MakeIntrusive<TSummaryDoubleSnapshot>(10.1, -0.45, 0.478, 0.3, 30u); TString result = EncodeToString(true, [s](IMetricEncoder* e) { - e->OnStreamBegin(); - { + e->OnStreamBegin(); + { e->OnMetricBegin(EMetricType::DSUMMARY); - { - e->OnLabelsBegin(); - e->OnLabel("sensor", "temperature"); - e->OnLabelsEnd(); - } - e->OnSummaryDouble(TInstant::Zero(), s); + { + e->OnLabelsBegin(); + e->OnLabel("sensor", "temperature"); + e->OnLabelsEnd(); + } + e->OnSummaryDouble(TInstant::Zero(), s); e->OnMetricEnd(); - } - e->OnStreamEnd(); - }); - UNIT_ASSERT_STRINGS_EQUAL(result, - " DSUMMARY temperature{} [{sum: 10.1, min: -0.45, max: 0.478, last: 0.3, count: 30}]\n"); - } + } + e->OnStreamEnd(); + }); + UNIT_ASSERT_STRINGS_EQUAL(result, + " DSUMMARY temperature{} [{sum: 10.1, min: -0.45, max: 0.478, last: 0.3, count: 30}]\n"); + } } diff --git a/library/cpp/monlib/encode/unistat/unistat_decoder.cpp b/library/cpp/monlib/encode/unistat/unistat_decoder.cpp index b2344b0905..d8ba7d80af 100644 --- a/library/cpp/monlib/encode/unistat/unistat_decoder.cpp +++ b/library/cpp/monlib/encode/unistat/unistat_decoder.cpp @@ -211,7 +211,7 @@ namespace NMonitoring { case EMetricType::HIST_RATE: Consumer_->OnHistogram(Timestamp_, MetricContext_.Value.AsHistogram()); break; - case EMetricType::LOGHIST: + case EMetricType::LOGHIST: case EMetricType::DSUMMARY: case EMetricType::IGAUGE: case EMetricType::COUNTER: |