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 | 498a47e48d41e5ec64ee3aa622a76a80274f35bd (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library/cpp/monlib/encode/json | |
parent | 1906a186042870fd03a12180acd1a6fcee045e42 (diff) | |
download | ydb-498a47e48d41e5ec64ee3aa622a76a80274f35bd.tar.gz |
Restoring authorship annotation for <kbalakirev@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/monlib/encode/json')
-rw-r--r-- | library/cpp/monlib/encode/json/json_decoder.cpp | 86 | ||||
-rw-r--r-- | library/cpp/monlib/encode/json/json_encoder.cpp | 98 | ||||
-rw-r--r-- | library/cpp/monlib/encode/json/json_ut.cpp | 544 | ||||
-rw-r--r-- | library/cpp/monlib/encode/json/typed_point.h | 16 | ||||
-rw-r--r-- | library/cpp/monlib/encode/json/ut/log_histogram_timeseries.json | 92 | ||||
-rw-r--r-- | library/cpp/monlib/encode/json/ut/log_histogram_value.json | 50 | ||||
-rw-r--r-- | library/cpp/monlib/encode/json/ut/summary_inf.json | 40 | ||||
-rw-r--r-- | library/cpp/monlib/encode/json/ut/summary_timeseries.json | 72 | ||||
-rw-r--r-- | library/cpp/monlib/encode/json/ut/summary_value.json | 40 | ||||
-rw-r--r-- | library/cpp/monlib/encode/json/ut/ya.make | 10 |
10 files changed, 524 insertions, 524 deletions
diff --git a/library/cpp/monlib/encode/json/json_decoder.cpp b/library/cpp/monlib/encode/json/json_decoder.cpp index ec4520d7c0..d44ff5fd28 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 7e61d0443a..20d2bb6283 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 5260def99d..09e7909289 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 5edeaab8be..fbaa840c4b 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 6a1e1947d1..e811a2cc57 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 919d709549..002478293b 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 9d335a4d8a..625a6cd8ad 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 82704a7b72..92007af3e6 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 7eaf355d18..366394c5e1 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 70f9e4c917..e50c4f4903 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( |