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 | |
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')
41 files changed, 1671 insertions, 1671 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: diff --git a/library/cpp/monlib/metrics/histogram_snapshot.cpp b/library/cpp/monlib/metrics/histogram_snapshot.cpp index 75b5811546..7df9762c1a 100644 --- a/library/cpp/monlib/metrics/histogram_snapshot.cpp +++ b/library/cpp/monlib/metrics/histogram_snapshot.cpp @@ -1,7 +1,7 @@ #include "histogram_snapshot.h" -#include <util/stream/output.h> - +#include <util/stream/output.h> + #include <iostream> @@ -23,16 +23,16 @@ namespace NMonitoring { } } // namespace NMonitoring - + namespace { - + template <typename TStream> auto& Output(TStream& os, const NMonitoring::IHistogramSnapshot& hist) { os << TStringBuf("{"); - + ui32 i = 0; ui32 count = hist.Count(); - + if (count > 0) { for (; i < count - 1; ++i) { os << hist.UpperBound(i) << TStringBuf(": ") << hist.Value(i); @@ -44,12 +44,12 @@ auto& Output(TStream& os, const NMonitoring::IHistogramSnapshot& hist) { } else { os << hist.UpperBound(i) << TStringBuf(": ") << hist.Value(i); } - } - + } + os << TStringBuf("}"); return os; -} +} } // namespace diff --git a/library/cpp/monlib/metrics/labels.h b/library/cpp/monlib/metrics/labels.h index 63dc997c28..80a78efa6c 100644 --- a/library/cpp/monlib/metrics/labels.h +++ b/library/cpp/monlib/metrics/labels.h @@ -292,12 +292,12 @@ namespace NMonitoring { return tmp; } - void SortByName() { + void SortByName() { std::sort(Labels_.begin(), Labels_.end(), [](const auto& lhs, const auto& rhs) { - return lhs.Name() < rhs.Name(); - }); - } - + return lhs.Name() < rhs.Name(); + }); + } + inline size_t Hash() const noexcept override { return TSimpleRangeHash()(Labels_); } diff --git a/library/cpp/monlib/metrics/log_histogram_collector.h b/library/cpp/monlib/metrics/log_histogram_collector.h index b81f84ebf3..9d51c887bc 100644 --- a/library/cpp/monlib/metrics/log_histogram_collector.h +++ b/library/cpp/monlib/metrics/log_histogram_collector.h @@ -1,158 +1,158 @@ -#pragma once - -#include "log_histogram_snapshot.h" - -#include <util/generic/algorithm.h> -#include <util/generic/utility.h> -#include <util/generic/yexception.h> - -#include <mutex> -#include <cmath> - -namespace NMonitoring { - - class TLogHistogramCollector { - public: - static constexpr int DEFAULT_START_POWER = -1; - - explicit TLogHistogramCollector(int startPower = DEFAULT_START_POWER) - : StartPower_(startPower) - , CountZero_(0u) - {} - - void Collect(TLogHistogramSnapshot* logHist) { - std::lock_guard guard(Mutex_); - Merge(logHist); - } - +#pragma once + +#include "log_histogram_snapshot.h" + +#include <util/generic/algorithm.h> +#include <util/generic/utility.h> +#include <util/generic/yexception.h> + +#include <mutex> +#include <cmath> + +namespace NMonitoring { + + class TLogHistogramCollector { + public: + static constexpr int DEFAULT_START_POWER = -1; + + explicit TLogHistogramCollector(int startPower = DEFAULT_START_POWER) + : StartPower_(startPower) + , CountZero_(0u) + {} + + void Collect(TLogHistogramSnapshot* logHist) { + std::lock_guard guard(Mutex_); + Merge(logHist); + } + bool Collect(double value) { - std::lock_guard guard(Mutex_); + std::lock_guard guard(Mutex_); return CollectDouble(value); - } - - TLogHistogramSnapshotPtr Snapshot() const { - std::lock_guard guard(Mutex_); - return MakeIntrusive<TLogHistogramSnapshot>(BASE, CountZero_, StartPower_, Buckets_); - } - - void AddZeros(ui64 zerosCount) noexcept { - std::lock_guard guard(Mutex_); - CountZero_ += zerosCount; - } - - private: - int StartPower_; - ui64 CountZero_; - TVector<double> Buckets_; - mutable std::mutex Mutex_; - - static constexpr size_t MAX_BUCKETS = LOG_HIST_MAX_BUCKETS; - static constexpr double BASE = 1.5; - - private: - int EstimateBucketIndex(double value) const { - return (int) (std::floor(std::log(value) / std::log(BASE)) - StartPower_); - } - - void CollectPositiveDouble(double value) { - ssize_t idx = std::floor(std::log(value) / std::log(BASE)) - StartPower_; - if (idx >= Buckets_.ysize()) { - idx = ExtendUp(idx); - } else if (idx <= 0) { - idx = Max<ssize_t>(0, ExtendDown(idx, 1)); - } - ++Buckets_[idx]; - } - + } + + TLogHistogramSnapshotPtr Snapshot() const { + std::lock_guard guard(Mutex_); + return MakeIntrusive<TLogHistogramSnapshot>(BASE, CountZero_, StartPower_, Buckets_); + } + + void AddZeros(ui64 zerosCount) noexcept { + std::lock_guard guard(Mutex_); + CountZero_ += zerosCount; + } + + private: + int StartPower_; + ui64 CountZero_; + TVector<double> Buckets_; + mutable std::mutex Mutex_; + + static constexpr size_t MAX_BUCKETS = LOG_HIST_MAX_BUCKETS; + static constexpr double BASE = 1.5; + + private: + int EstimateBucketIndex(double value) const { + return (int) (std::floor(std::log(value) / std::log(BASE)) - StartPower_); + } + + void CollectPositiveDouble(double value) { + ssize_t idx = std::floor(std::log(value) / std::log(BASE)) - StartPower_; + if (idx >= Buckets_.ysize()) { + idx = ExtendUp(idx); + } else if (idx <= 0) { + idx = Max<ssize_t>(0, ExtendDown(idx, 1)); + } + ++Buckets_[idx]; + } + bool CollectDouble(double value) { if (Y_UNLIKELY(std::isnan(value) || std::isinf(value))) { return false; } - if (value <= 0.0) { - ++CountZero_; - } else { - CollectPositiveDouble(value); - } + if (value <= 0.0) { + ++CountZero_; + } else { + CollectPositiveDouble(value); + } return true; - } - - void Merge(TLogHistogramSnapshot* logHist) { - CountZero_ += logHist->ZerosCount(); - const i32 firstIdxBeforeExtend = logHist->StartPower() - StartPower_; - const i32 lastIdxBeforeExtend = firstIdxBeforeExtend + logHist->Count() - 1; - if (firstIdxBeforeExtend > Max<i16>() || firstIdxBeforeExtend < Min<i16>()) { - ythrow yexception() << "i16 overflow on first index"; - } - if (lastIdxBeforeExtend > Max<i16>() || lastIdxBeforeExtend < Min<i16>()) { - ythrow yexception() << "i16 overflow on last index"; - } - i64 firstIdx = ExtendBounds(firstIdxBeforeExtend, lastIdxBeforeExtend, 0).first; - size_t toMerge = std::min<ui32>(std::max<i64>(-firstIdx, (i64) 0), logHist->Count()); - if (toMerge) { - for (size_t i = 0; i < toMerge; ++i) { - Buckets_[0] += logHist->Bucket(i); - } - firstIdx = 0; - } - for (size_t i = toMerge; i != logHist->Count(); ++i) { - Buckets_[firstIdx] += logHist->Bucket(i); - ++firstIdx; - } - } - - int ExtendUp(int expectedIndex) { - Y_VERIFY_DEBUG(expectedIndex >= (int) Buckets_.size()); - const size_t toAdd = expectedIndex - Buckets_.size() + 1; - const size_t newSize = Buckets_.size() + toAdd; - if (newSize <= MAX_BUCKETS) { - Buckets_.resize(newSize, 0.0); - return expectedIndex; - } - - const size_t toRemove = newSize - MAX_BUCKETS; - const size_t actualToRemove = std::min<size_t>(toRemove, Buckets_.size()); - if (actualToRemove > 0) { - const double firstWeight = std::accumulate(Buckets_.cbegin(), Buckets_.cbegin() + actualToRemove, 0.0); - Buckets_.erase(Buckets_.cbegin(), Buckets_.cbegin() + actualToRemove); - if (Buckets_.empty()) { - Buckets_.push_back(firstWeight); - } else { - Buckets_[0] = firstWeight; - } - } - Buckets_.resize(MAX_BUCKETS, 0.0); - StartPower_ += toRemove; - return expectedIndex - toRemove; - } - - int ExtendDown(int expectedIndex, int margin) { - Y_VERIFY_DEBUG(expectedIndex <= 0); - int toAdd = std::min<int>(MAX_BUCKETS - Buckets_.size(), margin - expectedIndex); - if (toAdd > 0) { - Buckets_.insert(Buckets_.begin(), toAdd, 0.0); - StartPower_ -= toAdd; - } - return expectedIndex + toAdd; - } - - std::pair<ssize_t, ssize_t> ExtendBounds(ssize_t startIdx, ssize_t endIdx, ui8 margin) { - ssize_t realEndIdx; - ssize_t realStartIdx; - if (endIdx >= Buckets_.ysize()) { - Buckets_.reserve(std::max<size_t>(std::min<ui32>(endIdx - startIdx + 1ul, MAX_BUCKETS), 0ul)); - realEndIdx = ExtendUp(endIdx); - startIdx += realEndIdx - endIdx; - } else { - realEndIdx = endIdx; - } - if (startIdx < 1) { - realStartIdx = ExtendDown(startIdx, margin); - realEndIdx += realStartIdx - startIdx; - } else { - realStartIdx = startIdx; - } - return std::make_pair(realStartIdx, realEndIdx); - } - }; - -} // namespace NMonitoring + } + + void Merge(TLogHistogramSnapshot* logHist) { + CountZero_ += logHist->ZerosCount(); + const i32 firstIdxBeforeExtend = logHist->StartPower() - StartPower_; + const i32 lastIdxBeforeExtend = firstIdxBeforeExtend + logHist->Count() - 1; + if (firstIdxBeforeExtend > Max<i16>() || firstIdxBeforeExtend < Min<i16>()) { + ythrow yexception() << "i16 overflow on first index"; + } + if (lastIdxBeforeExtend > Max<i16>() || lastIdxBeforeExtend < Min<i16>()) { + ythrow yexception() << "i16 overflow on last index"; + } + i64 firstIdx = ExtendBounds(firstIdxBeforeExtend, lastIdxBeforeExtend, 0).first; + size_t toMerge = std::min<ui32>(std::max<i64>(-firstIdx, (i64) 0), logHist->Count()); + if (toMerge) { + for (size_t i = 0; i < toMerge; ++i) { + Buckets_[0] += logHist->Bucket(i); + } + firstIdx = 0; + } + for (size_t i = toMerge; i != logHist->Count(); ++i) { + Buckets_[firstIdx] += logHist->Bucket(i); + ++firstIdx; + } + } + + int ExtendUp(int expectedIndex) { + Y_VERIFY_DEBUG(expectedIndex >= (int) Buckets_.size()); + const size_t toAdd = expectedIndex - Buckets_.size() + 1; + const size_t newSize = Buckets_.size() + toAdd; + if (newSize <= MAX_BUCKETS) { + Buckets_.resize(newSize, 0.0); + return expectedIndex; + } + + const size_t toRemove = newSize - MAX_BUCKETS; + const size_t actualToRemove = std::min<size_t>(toRemove, Buckets_.size()); + if (actualToRemove > 0) { + const double firstWeight = std::accumulate(Buckets_.cbegin(), Buckets_.cbegin() + actualToRemove, 0.0); + Buckets_.erase(Buckets_.cbegin(), Buckets_.cbegin() + actualToRemove); + if (Buckets_.empty()) { + Buckets_.push_back(firstWeight); + } else { + Buckets_[0] = firstWeight; + } + } + Buckets_.resize(MAX_BUCKETS, 0.0); + StartPower_ += toRemove; + return expectedIndex - toRemove; + } + + int ExtendDown(int expectedIndex, int margin) { + Y_VERIFY_DEBUG(expectedIndex <= 0); + int toAdd = std::min<int>(MAX_BUCKETS - Buckets_.size(), margin - expectedIndex); + if (toAdd > 0) { + Buckets_.insert(Buckets_.begin(), toAdd, 0.0); + StartPower_ -= toAdd; + } + return expectedIndex + toAdd; + } + + std::pair<ssize_t, ssize_t> ExtendBounds(ssize_t startIdx, ssize_t endIdx, ui8 margin) { + ssize_t realEndIdx; + ssize_t realStartIdx; + if (endIdx >= Buckets_.ysize()) { + Buckets_.reserve(std::max<size_t>(std::min<ui32>(endIdx - startIdx + 1ul, MAX_BUCKETS), 0ul)); + realEndIdx = ExtendUp(endIdx); + startIdx += realEndIdx - endIdx; + } else { + realEndIdx = endIdx; + } + if (startIdx < 1) { + realStartIdx = ExtendDown(startIdx, margin); + realEndIdx += realStartIdx - startIdx; + } else { + realStartIdx = startIdx; + } + return std::make_pair(realStartIdx, realEndIdx); + } + }; + +} // namespace NMonitoring diff --git a/library/cpp/monlib/metrics/log_histogram_collector_ut.cpp b/library/cpp/monlib/metrics/log_histogram_collector_ut.cpp index ac9a3522ce..560ec83714 100644 --- a/library/cpp/monlib/metrics/log_histogram_collector_ut.cpp +++ b/library/cpp/monlib/metrics/log_histogram_collector_ut.cpp @@ -1,38 +1,38 @@ -#include "log_histogram_collector.h" - -#include <library/cpp/testing/unittest/registar.h> - -Y_UNIT_TEST_SUITE(LogHistogramCollector) { - - Y_UNIT_TEST(ExtendUpEmpty) { - NMonitoring::TLogHistogramCollector collector(-1); - collector.Collect(4.1944122207138854e+17); - auto s = collector.Snapshot(); - UNIT_ASSERT_EQUAL(s->ZerosCount(), 0); - UNIT_ASSERT_EQUAL(s->StartPower(), 1); - UNIT_ASSERT_EQUAL(s->Count(), 100); - UNIT_ASSERT_EQUAL(s->Bucket(s->Count() - 1), 1); - } - - Y_UNIT_TEST(ExtendUpNonEmpty) { - NMonitoring::TLogHistogramCollector collector(-1); - collector.Collect(0.0); - collector.Collect(1/(1.5*1.5*1.5)); - collector.Collect(1/1.5); - auto s = collector.Snapshot(); - - UNIT_ASSERT_EQUAL(s->ZerosCount(), 1); - UNIT_ASSERT_EQUAL(s->StartPower(), -4); - UNIT_ASSERT_EQUAL(s->Count(), 3); - UNIT_ASSERT_EQUAL(s->Bucket(1), 1); - UNIT_ASSERT_EQUAL(s->Bucket(2), 1); - - collector.Collect(4.1944122207138854e+17); - s = collector.Snapshot(); - UNIT_ASSERT_EQUAL(s->ZerosCount(), 1); - UNIT_ASSERT_EQUAL(s->StartPower(), 1); - UNIT_ASSERT_EQUAL(s->Count(), 100); - UNIT_ASSERT_EQUAL(s->Bucket(0), 2); - UNIT_ASSERT_EQUAL(s->Bucket(99), 1); - } -} +#include "log_histogram_collector.h" + +#include <library/cpp/testing/unittest/registar.h> + +Y_UNIT_TEST_SUITE(LogHistogramCollector) { + + Y_UNIT_TEST(ExtendUpEmpty) { + NMonitoring::TLogHistogramCollector collector(-1); + collector.Collect(4.1944122207138854e+17); + auto s = collector.Snapshot(); + UNIT_ASSERT_EQUAL(s->ZerosCount(), 0); + UNIT_ASSERT_EQUAL(s->StartPower(), 1); + UNIT_ASSERT_EQUAL(s->Count(), 100); + UNIT_ASSERT_EQUAL(s->Bucket(s->Count() - 1), 1); + } + + Y_UNIT_TEST(ExtendUpNonEmpty) { + NMonitoring::TLogHistogramCollector collector(-1); + collector.Collect(0.0); + collector.Collect(1/(1.5*1.5*1.5)); + collector.Collect(1/1.5); + auto s = collector.Snapshot(); + + UNIT_ASSERT_EQUAL(s->ZerosCount(), 1); + UNIT_ASSERT_EQUAL(s->StartPower(), -4); + UNIT_ASSERT_EQUAL(s->Count(), 3); + UNIT_ASSERT_EQUAL(s->Bucket(1), 1); + UNIT_ASSERT_EQUAL(s->Bucket(2), 1); + + collector.Collect(4.1944122207138854e+17); + s = collector.Snapshot(); + UNIT_ASSERT_EQUAL(s->ZerosCount(), 1); + UNIT_ASSERT_EQUAL(s->StartPower(), 1); + UNIT_ASSERT_EQUAL(s->Count(), 100); + UNIT_ASSERT_EQUAL(s->Bucket(0), 2); + UNIT_ASSERT_EQUAL(s->Bucket(99), 1); + } +} diff --git a/library/cpp/monlib/metrics/log_histogram_snapshot.h b/library/cpp/monlib/metrics/log_histogram_snapshot.h index 7673b43751..7f7826d49d 100644 --- a/library/cpp/monlib/metrics/log_histogram_snapshot.h +++ b/library/cpp/monlib/metrics/log_histogram_snapshot.h @@ -1,71 +1,71 @@ -#pragma once - -#include <util/generic/ptr.h> -#include <util/generic/vector.h> - -#include <cmath> - -namespace NMonitoring { - - constexpr ui32 LOG_HIST_MAX_BUCKETS = 100; - - class TLogHistogramSnapshot: public TAtomicRefCount<TLogHistogramSnapshot> { - public: - TLogHistogramSnapshot(double base, ui64 zerosCount, int startPower, TVector<double> buckets) - : Base_(base) - , ZerosCount_(zerosCount) - , StartPower_(startPower) - , Buckets_(std::move(buckets)) { - } - - /** - * @return buckets count. - */ - ui32 Count() const noexcept { - return Buckets_.size(); - } - - /** - * @return upper bound for the bucket with particular index. - */ - double UpperBound(int index) const noexcept { - return std::pow(Base_, StartPower_ + index); - } - - /** - * @return value stored in the bucket with particular index. - */ - double Bucket(ui32 index) const noexcept { - return Buckets_[index]; - } - - /** - * @return nonpositive values count - */ - ui64 ZerosCount() const noexcept { - return ZerosCount_; - } - - double Base() const noexcept { - return Base_; - } - - int StartPower() const noexcept { - return StartPower_; - } - - ui64 MemorySizeBytes() const noexcept { - return sizeof(*this) + Buckets_.capacity() * sizeof(double); - } - - private: - double Base_; - ui64 ZerosCount_; - int StartPower_; - TVector<double> Buckets_; - }; - - using TLogHistogramSnapshotPtr = TIntrusivePtr<TLogHistogramSnapshot>; -} +#pragma once + +#include <util/generic/ptr.h> +#include <util/generic/vector.h> + +#include <cmath> + +namespace NMonitoring { + + constexpr ui32 LOG_HIST_MAX_BUCKETS = 100; + + class TLogHistogramSnapshot: public TAtomicRefCount<TLogHistogramSnapshot> { + public: + TLogHistogramSnapshot(double base, ui64 zerosCount, int startPower, TVector<double> buckets) + : Base_(base) + , ZerosCount_(zerosCount) + , StartPower_(startPower) + , Buckets_(std::move(buckets)) { + } + + /** + * @return buckets count. + */ + ui32 Count() const noexcept { + return Buckets_.size(); + } + + /** + * @return upper bound for the bucket with particular index. + */ + double UpperBound(int index) const noexcept { + return std::pow(Base_, StartPower_ + index); + } + + /** + * @return value stored in the bucket with particular index. + */ + double Bucket(ui32 index) const noexcept { + return Buckets_[index]; + } + + /** + * @return nonpositive values count + */ + ui64 ZerosCount() const noexcept { + return ZerosCount_; + } + + double Base() const noexcept { + return Base_; + } + + int StartPower() const noexcept { + return StartPower_; + } + + ui64 MemorySizeBytes() const noexcept { + return sizeof(*this) + Buckets_.capacity() * sizeof(double); + } + + private: + double Base_; + ui64 ZerosCount_; + int StartPower_; + TVector<double> Buckets_; + }; + + using TLogHistogramSnapshotPtr = TIntrusivePtr<TLogHistogramSnapshot>; +} std::ostream& operator<<(std::ostream& os, const NMonitoring::TLogHistogramSnapshot& hist); diff --git a/library/cpp/monlib/metrics/metric_consumer.h b/library/cpp/monlib/metrics/metric_consumer.h index f7a727585a..7049bce68c 100644 --- a/library/cpp/monlib/metrics/metric_consumer.h +++ b/library/cpp/monlib/metrics/metric_consumer.h @@ -2,8 +2,8 @@ #include "metric_type.h" #include "histogram_collector.h" -#include "summary_collector.h" -#include "log_histogram_snapshot.h" +#include "summary_collector.h" +#include "log_histogram_snapshot.h" class TInstant; @@ -31,7 +31,7 @@ namespace NMonitoring { virtual void OnUint64(TInstant time, ui64 value) = 0; virtual void OnHistogram(TInstant time, IHistogramSnapshotPtr snapshot) = 0; - virtual void OnLogHistogram(TInstant time, TLogHistogramSnapshotPtr snapshot) = 0; + virtual void OnLogHistogram(TInstant time, TLogHistogramSnapshotPtr snapshot) = 0; virtual void OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr snapshot) = 0; }; diff --git a/library/cpp/monlib/metrics/metric_registry_ut.cpp b/library/cpp/monlib/metrics/metric_registry_ut.cpp index 86d9a52ec0..b27214e62a 100644 --- a/library/cpp/monlib/metrics/metric_registry_ut.cpp +++ b/library/cpp/monlib/metrics/metric_registry_ut.cpp @@ -25,12 +25,12 @@ void Out<NMonitoring::NProto::TSingleSample::ValueCase>(IOutputStream& os, NMoni case NMonitoring::NProto::TSingleSample::ValueCase::kFloat64: os << "Float64"; break; - case NMonitoring::NProto::TSingleSample::ValueCase::kSummaryDouble: - os << "DSummary"; - break; - case NMonitoring::NProto::TSingleSample::ValueCase::kLogHistogram: - os << "LogHistogram"; - break; + case NMonitoring::NProto::TSingleSample::ValueCase::kSummaryDouble: + os << "DSummary"; + break; + case NMonitoring::NProto::TSingleSample::ValueCase::kLogHistogram: + os << "LogHistogram"; + break; case NMonitoring::NProto::TSingleSample::ValueCase::VALUE_NOT_SET: os << "NOT SET"; break; diff --git a/library/cpp/monlib/metrics/metric_type.h b/library/cpp/monlib/metrics/metric_type.h index 1984c42c1e..63ee9b8d1b 100644 --- a/library/cpp/monlib/metrics/metric_type.h +++ b/library/cpp/monlib/metrics/metric_type.h @@ -14,7 +14,7 @@ namespace NMonitoring { IGAUGE = 4, HIST = 5, HIST_RATE = 6, - DSUMMARY = 7, + DSUMMARY = 7, // ISUMMARY = 8, reserved LOGHIST = 9, }; diff --git a/library/cpp/monlib/metrics/metric_value.cpp b/library/cpp/monlib/metrics/metric_value.cpp index b95d7011c6..9a7d6fe1a1 100644 --- a/library/cpp/monlib/metrics/metric_value.cpp +++ b/library/cpp/monlib/metrics/metric_value.cpp @@ -12,15 +12,15 @@ namespace NMonitoring { SnapshotUnRef<EMetricValueType::HISTOGRAM>(p); } } else if (ValueType_ == EMetricValueType::SUMMARY) { - for (TPoint& p: Points_) { + for (TPoint& p: Points_) { SnapshotUnRef<EMetricValueType::SUMMARY>(p); - } - } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { - for (TPoint& p: Points_) { - SnapshotUnRef<EMetricValueType::LOGHISTOGRAM>(p); - } + } + } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { + for (TPoint& p: Points_) { + SnapshotUnRef<EMetricValueType::LOGHISTOGRAM>(p); + } } - + Points_.clear(); ValueType_ = EMetricValueType::UNKNOWN; } diff --git a/library/cpp/monlib/metrics/metric_value.h b/library/cpp/monlib/metrics/metric_value.h index 607fcc8602..a9d15e75f6 100644 --- a/library/cpp/monlib/metrics/metric_value.h +++ b/library/cpp/monlib/metrics/metric_value.h @@ -2,8 +2,8 @@ #include "histogram_collector.h" #include "metric_value_type.h" -#include "summary_collector.h" -#include "log_histogram_snapshot.h" +#include "summary_collector.h" +#include "log_histogram_snapshot.h" #include <util/datetime/base.h> #include <util/generic/algorithm.h> @@ -43,20 +43,20 @@ namespace NMonitoring { static constexpr auto Type = EMetricValueType::UINT64; }; - template <> - struct TValueType<TLogHistogramSnapshot*> { - static constexpr auto Type = EMetricValueType::LOGHISTOGRAM; - }; - + template <> + struct TValueType<TLogHistogramSnapshot*> { + static constexpr auto Type = EMetricValueType::LOGHISTOGRAM; + }; + template <typename T> struct TValueType<T*, typename std::enable_if_t<std::is_base_of<IHistogramSnapshot, T>::value>> { static constexpr auto Type = EMetricValueType::HISTOGRAM; }; - template <typename T> - struct TValueType<T*, typename std::enable_if_t<std::is_base_of<ISummaryDoubleSnapshot, T>::value>> { + template <typename T> + struct TValueType<T*, typename std::enable_if_t<std::is_base_of<ISummaryDoubleSnapshot, T>::value>> { static constexpr auto Type = EMetricValueType::SUMMARY; - }; + }; /////////////////////////////////////////////////////////////////////////// // TMetricValue @@ -90,13 +90,13 @@ namespace NMonitoring { } explicit TMetricValue(ISummaryDoubleSnapshot* summary) noexcept { - Value_.Summary = summary; - } - - explicit TMetricValue(TLogHistogramSnapshot* logHist) noexcept { - Value_.LogHistogram = logHist; - } - + Value_.Summary = summary; + } + + explicit TMetricValue(TLogHistogramSnapshot* logHist) noexcept { + Value_.LogHistogram = logHist; + } + double AsDouble() const noexcept { return Value_.Double; } @@ -114,9 +114,9 @@ namespace NMonitoring { case EMetricValueType::HISTOGRAM: ythrow yexception() << "histogram cannot be casted to Double"; case EMetricValueType::SUMMARY: - ythrow yexception() << "summary cannot be casted to Double"; - case EMetricValueType::LOGHISTOGRAM: - ythrow yexception() << "loghistogram cannot be casted to Double"; + ythrow yexception() << "summary cannot be casted to Double"; + case EMetricValueType::LOGHISTOGRAM: + ythrow yexception() << "loghistogram cannot be casted to Double"; case EMetricValueType::UNKNOWN: ythrow yexception() << "unknown value type"; } @@ -140,9 +140,9 @@ namespace NMonitoring { case EMetricValueType::HISTOGRAM: ythrow yexception() << "histogram cannot be casted to Uint64"; case EMetricValueType::SUMMARY: - ythrow yexception() << "summary cannot be casted to Uint64"; - case EMetricValueType::LOGHISTOGRAM: - ythrow yexception() << "loghistogram cannot be casted to Uint64"; + ythrow yexception() << "summary cannot be casted to Uint64"; + case EMetricValueType::LOGHISTOGRAM: + ythrow yexception() << "loghistogram cannot be casted to Uint64"; case EMetricValueType::UNKNOWN: ythrow yexception() << "unknown value type"; } @@ -166,9 +166,9 @@ namespace NMonitoring { case EMetricValueType::HISTOGRAM: ythrow yexception() << "histogram cannot be casted to Int64"; case EMetricValueType::SUMMARY: - ythrow yexception() << "summary cannot be casted to Int64"; - case EMetricValueType::LOGHISTOGRAM: - ythrow yexception() << "loghistogram cannot be casted to Int64"; + ythrow yexception() << "summary cannot be casted to Int64"; + case EMetricValueType::LOGHISTOGRAM: + ythrow yexception() << "loghistogram cannot be casted to Int64"; case EMetricValueType::UNKNOWN: ythrow yexception() << "unknown value type"; } @@ -187,10 +187,10 @@ namespace NMonitoring { return Value_.Histogram; } - ISummaryDoubleSnapshot* AsSummaryDouble() const noexcept { - return Value_.Summary; - } - + ISummaryDoubleSnapshot* AsSummaryDouble() const noexcept { + return Value_.Summary; + } + ISummaryDoubleSnapshot* AsSummaryDouble(EMetricValueType type) const { if (type != EMetricValueType::SUMMARY) { ythrow yexception() << type << " cannot be casted to SummaryDouble"; @@ -199,26 +199,26 @@ namespace NMonitoring { return Value_.Summary; } - TLogHistogramSnapshot* AsLogHistogram() const noexcept { - return Value_.LogHistogram; - } - - TLogHistogramSnapshot* AsLogHistogram(EMetricValueType type) const { - if (type != EMetricValueType::LOGHISTOGRAM) { - ythrow yexception() << type << " cannot be casted to LogHistogram"; - } - - return Value_.LogHistogram; - } - + TLogHistogramSnapshot* AsLogHistogram() const noexcept { + return Value_.LogHistogram; + } + + TLogHistogramSnapshot* AsLogHistogram(EMetricValueType type) const { + if (type != EMetricValueType::LOGHISTOGRAM) { + ythrow yexception() << type << " cannot be casted to LogHistogram"; + } + + return Value_.LogHistogram; + } + protected: union { double Double; i64 Int64; ui64 Uint64; IHistogramSnapshot* Histogram; - ISummaryDoubleSnapshot* Summary; - TLogHistogramSnapshot* LogHistogram; + ISummaryDoubleSnapshot* Summary; + TLogHistogramSnapshot* LogHistogram; } Value_; }; @@ -290,18 +290,18 @@ namespace NMonitoring { return TBase::AsSummaryDouble(ValueType_); } - TLogHistogramSnapshot* AsLogHistogram() const { - return TBase::AsLogHistogram(ValueType_); - } - + TLogHistogramSnapshot* AsLogHistogram() const { + return TBase::AsLogHistogram(ValueType_); + } + private: void Ref() { if (ValueType_ == EMetricValueType::SUMMARY) { TBase::AsSummaryDouble()->Ref(); } else if (ValueType_ == EMetricValueType::HISTOGRAM) { TBase::AsHistogram()->Ref(); - } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { - TBase::AsLogHistogram()->Ref(); + } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { + TBase::AsLogHistogram()->Ref(); } } @@ -310,8 +310,8 @@ namespace NMonitoring { TBase::AsSummaryDouble()->UnRef(); } else if (ValueType_ == EMetricValueType::HISTOGRAM) { TBase::AsHistogram()->UnRef(); - } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { - TBase::AsLogHistogram()->UnRef(); + } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { + TBase::AsLogHistogram()->UnRef(); } } @@ -401,7 +401,7 @@ namespace NMonitoring { } else if (ValueType_ == EMetricValueType::HISTOGRAM) { TPoint& p = Points_.back(); p.GetValue().AsHistogram()->Ref(); - } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { + } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { TPoint& p = Points_.back(); p.GetValue().AsLogHistogram()->Ref(); } @@ -424,16 +424,16 @@ namespace NMonitoring { point.GetValue().AsHistogram()->Ref(); } } else if (ValueType_ == EMetricValueType::SUMMARY) { - for (size_t i = prevSize; i < Points_.size(); ++i) { - TPoint& point = Points_[i]; - point.GetValue().AsSummaryDouble()->Ref(); - } - } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { - for (size_t i = prevSize; i < Points_.size(); ++i) { - TPoint& point = Points_[i]; - point.GetValue().AsLogHistogram()->Ref(); - } - } + for (size_t i = prevSize; i < Points_.size(); ++i) { + TPoint& point = Points_[i]; + point.GetValue().AsSummaryDouble()->Ref(); + } + } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { + for (size_t i = prevSize; i < Points_.size(); ++i) { + TPoint& point = Points_[i]; + point.GetValue().AsLogHistogram()->Ref(); + } + } } template <typename TConsumer> @@ -480,50 +480,50 @@ namespace NMonitoring { }; template <EMetricValueType valueType, typename TPoint> - static inline void SnapshotUnRef(TPoint& point) { + static inline void SnapshotUnRef(TPoint& point) { if constexpr (valueType == EMetricValueType::HISTOGRAM) { - if (auto* hist = point.GetValue().AsHistogram()) { - hist->UnRef(); - } + if (auto* hist = point.GetValue().AsHistogram()) { + hist->UnRef(); + } } else if constexpr (valueType == EMetricValueType::SUMMARY) { - if (auto* summary = point.GetValue().AsSummaryDouble()) { - summary->UnRef(); - } - } else if constexpr (valueType == EMetricValueType::LOGHISTOGRAM) { - if (auto* logHist = point.GetValue().AsLogHistogram()) { - logHist->UnRef(); - } - } - } - + if (auto* summary = point.GetValue().AsSummaryDouble()) { + summary->UnRef(); + } + } else if constexpr (valueType == EMetricValueType::LOGHISTOGRAM) { + if (auto* logHist = point.GetValue().AsLogHistogram()) { + logHist->UnRef(); + } + } + } + template <EMetricValueType valueType, typename TPoint> - static void EraseDuplicates(TVector<TPoint>& points) { - // we have to manually clean reference to a snapshot from point - // while removing duplicates - auto result = points.rbegin(); - for (auto it = result + 1; it != points.rend(); ++it) { - if (result->GetTime() != it->GetTime() && ++result != it) { + static void EraseDuplicates(TVector<TPoint>& points) { + // we have to manually clean reference to a snapshot from point + // while removing duplicates + auto result = points.rbegin(); + for (auto it = result + 1; it != points.rend(); ++it) { + if (result->GetTime() != it->GetTime() && ++result != it) { SnapshotUnRef<valueType>(*result); - *result = *it; // (2) copy - it->ClearValue(); // (3) clean pointer in the source - } - } - - // erase tail points - for (auto it = result + 1; it != points.rend(); ++it) { + *result = *it; // (2) copy + it->ClearValue(); // (3) clean pointer in the source + } + } + + // erase tail points + for (auto it = result + 1; it != points.rend(); ++it) { SnapshotUnRef<valueType>(*it); - } - points.erase(points.begin(), (result + 1).base()); - } - - template <typename TPoint> + } + points.erase(points.begin(), (result + 1).base()); + } + + template <typename TPoint> void SortPointsByTs(EMetricValueType valueType, TVector<TPoint>& points) { if (points.size() < 2) { return; } - if (valueType != EMetricValueType::HISTOGRAM && valueType != EMetricValueType::SUMMARY - && valueType != EMetricValueType::LOGHISTOGRAM) { + if (valueType != EMetricValueType::HISTOGRAM && valueType != EMetricValueType::SUMMARY + && valueType != EMetricValueType::LOGHISTOGRAM) { // Stable sort + saving only the last point inside a group of duplicates StableSortBy(points, NPrivate::POINT_KEY_FN); auto it = UniqueBy(points.rbegin(), points.rend(), NPrivate::POINT_KEY_FN); @@ -532,11 +532,11 @@ namespace NMonitoring { StableSortBy(points, NPrivate::POINT_KEY_FN); if (valueType == EMetricValueType::HISTOGRAM) { EraseDuplicates<EMetricValueType::HISTOGRAM>(points); - } else if (valueType == EMetricValueType::LOGHISTOGRAM) { - EraseDuplicates<EMetricValueType::LOGHISTOGRAM>(points); - } else { + } else if (valueType == EMetricValueType::LOGHISTOGRAM) { + EraseDuplicates<EMetricValueType::LOGHISTOGRAM>(points); + } else { EraseDuplicates<EMetricValueType::SUMMARY>(points); - } + } } } } diff --git a/library/cpp/monlib/metrics/metric_value_type.h b/library/cpp/monlib/metrics/metric_value_type.h index ab30a958c2..500e9770b5 100644 --- a/library/cpp/monlib/metrics/metric_value_type.h +++ b/library/cpp/monlib/metrics/metric_value_type.h @@ -10,7 +10,7 @@ enum class EMetricValueType { UINT64, HISTOGRAM, SUMMARY, - LOGHISTOGRAM, + LOGHISTOGRAM, }; } // namespace NMonitoring diff --git a/library/cpp/monlib/metrics/metric_value_ut.cpp b/library/cpp/monlib/metrics/metric_value_ut.cpp index 49b47c4057..261807c904 100644 --- a/library/cpp/monlib/metrics/metric_value_ut.cpp +++ b/library/cpp/monlib/metrics/metric_value_ut.cpp @@ -32,18 +32,18 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { return MakeIntrusive<TTestHistogram>(); } - ISummaryDoubleSnapshotPtr MakeSummarySnapshot(ui64 count = 0u) { - return MakeIntrusive<TSummaryDoubleSnapshot>(0.0, 0.0, 0.0, 0.0, count); - } - - TLogHistogramSnapshotPtr MakeLogHistogram(ui64 count = 0) { - TVector<double> buckets; - for (ui64 i = 0; i < count; ++i) { - buckets.push_back(i); - } - return MakeIntrusive<TLogHistogramSnapshot>(1.5, 0u, 0, buckets); - } - + ISummaryDoubleSnapshotPtr MakeSummarySnapshot(ui64 count = 0u) { + return MakeIntrusive<TSummaryDoubleSnapshot>(0.0, 0.0, 0.0, 0.0, count); + } + + TLogHistogramSnapshotPtr MakeLogHistogram(ui64 count = 0) { + TVector<double> buckets; + for (ui64 i = 0; i < count; ++i) { + buckets.push_back(i); + } + return MakeIntrusive<TLogHistogramSnapshot>(1.5, 0u, 0, buckets); + } + Y_UNIT_TEST(Sorted) { auto ts1 = TInstant::Now(); auto ts2 = ts1 + TDuration::Seconds(1); @@ -78,30 +78,30 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { UNIT_ASSERT_VALUES_EQUAL(1, histogram->RefCount()); } - Y_UNIT_TEST(Summary) { - auto ts = TInstant::Now(); - auto summary = MakeSummarySnapshot(); - UNIT_ASSERT_VALUES_EQUAL(1, summary->RefCount()); - { + Y_UNIT_TEST(Summary) { + auto ts = TInstant::Now(); + auto summary = MakeSummarySnapshot(); + UNIT_ASSERT_VALUES_EQUAL(1, summary->RefCount()); + { TMetricTimeSeries timeSeries; - timeSeries.Add(ts, summary.Get()); - UNIT_ASSERT_VALUES_EQUAL(2, summary->RefCount()); - } - UNIT_ASSERT_VALUES_EQUAL(1, summary->RefCount()); - } - - Y_UNIT_TEST(LogHistogram) { - auto ts = TInstant::Now(); - auto logHist = MakeLogHistogram(); - UNIT_ASSERT_VALUES_EQUAL(1, logHist->RefCount()); - { - TMetricTimeSeries timeSeries; - timeSeries.Add(ts, logHist.Get()); - UNIT_ASSERT_VALUES_EQUAL(2, logHist->RefCount()); - } - UNIT_ASSERT_VALUES_EQUAL(1, logHist->RefCount()); - } - + timeSeries.Add(ts, summary.Get()); + UNIT_ASSERT_VALUES_EQUAL(2, summary->RefCount()); + } + UNIT_ASSERT_VALUES_EQUAL(1, summary->RefCount()); + } + + Y_UNIT_TEST(LogHistogram) { + auto ts = TInstant::Now(); + auto logHist = MakeLogHistogram(); + UNIT_ASSERT_VALUES_EQUAL(1, logHist->RefCount()); + { + TMetricTimeSeries timeSeries; + timeSeries.Add(ts, logHist.Get()); + UNIT_ASSERT_VALUES_EQUAL(2, logHist->RefCount()); + } + UNIT_ASSERT_VALUES_EQUAL(1, logHist->RefCount()); + } + Y_UNIT_TEST(TimeSeriesMovable) { auto ts = TInstant::Now(); auto histogram = MakeIntrusive<TTestHistogram>(); @@ -170,98 +170,98 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { UNIT_ASSERT_VALUES_EQUAL(1, h3->RefCount()); } - Y_UNIT_TEST(LogHistogramsUnique) { - auto ts1 = TInstant::Now(); - auto ts2 = ts1 + TDuration::Seconds(1); - auto ts3 = ts2 + TDuration::Seconds(1); - - auto h1 = MakeLogHistogram(); - auto h2 = MakeLogHistogram(); - auto h3 = MakeLogHistogram(); - - UNIT_ASSERT_VALUES_EQUAL(1, h1->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(1, h2->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(1, h3->RefCount()); - - { + Y_UNIT_TEST(LogHistogramsUnique) { + auto ts1 = TInstant::Now(); + auto ts2 = ts1 + TDuration::Seconds(1); + auto ts3 = ts2 + TDuration::Seconds(1); + + auto h1 = MakeLogHistogram(); + auto h2 = MakeLogHistogram(); + auto h3 = MakeLogHistogram(); + + UNIT_ASSERT_VALUES_EQUAL(1, h1->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(1, h2->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(1, h3->RefCount()); + + { + TMetricTimeSeries timeSeries; + timeSeries.Add(ts1, h1.Get()); // drop at the head + timeSeries.Add(ts1, h1.Get()); + timeSeries.Add(ts1, h1.Get()); + + timeSeries.Add(ts2, h2.Get()); // drop in the middle + timeSeries.Add(ts2, h2.Get()); + timeSeries.Add(ts2, h2.Get()); + + timeSeries.Add(ts3, h3.Get()); // drop at the end + timeSeries.Add(ts3, h3.Get()); + timeSeries.Add(ts3, h3.Get()); + + UNIT_ASSERT_EQUAL(timeSeries.Size(), 9); + + UNIT_ASSERT_VALUES_EQUAL(4, h1->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(4, h2->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(4, h3->RefCount()); + + timeSeries.SortByTs(); + UNIT_ASSERT_EQUAL(timeSeries.Size(), 3); + + UNIT_ASSERT_VALUES_EQUAL(2, h1->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(2, h2->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(2, h3->RefCount()); + } + + UNIT_ASSERT_VALUES_EQUAL(1, h1->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(1, h2->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(1, h3->RefCount()); + } + + Y_UNIT_TEST(SummaryUnique) { + auto ts1 = TInstant::Now(); + auto ts2 = ts1 + TDuration::Seconds(1); + auto ts3 = ts2 + TDuration::Seconds(1); + + auto h1 = MakeSummarySnapshot(); + auto h2 = MakeSummarySnapshot(); + auto h3 = MakeSummarySnapshot(); + + UNIT_ASSERT_VALUES_EQUAL(1, h1->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(1, h2->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(1, h3->RefCount()); + + { TMetricTimeSeries timeSeries; - timeSeries.Add(ts1, h1.Get()); // drop at the head - timeSeries.Add(ts1, h1.Get()); - timeSeries.Add(ts1, h1.Get()); - - timeSeries.Add(ts2, h2.Get()); // drop in the middle - timeSeries.Add(ts2, h2.Get()); - timeSeries.Add(ts2, h2.Get()); - - timeSeries.Add(ts3, h3.Get()); // drop at the end - timeSeries.Add(ts3, h3.Get()); - timeSeries.Add(ts3, h3.Get()); - - UNIT_ASSERT_EQUAL(timeSeries.Size(), 9); - - UNIT_ASSERT_VALUES_EQUAL(4, h1->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(4, h2->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(4, h3->RefCount()); - - timeSeries.SortByTs(); - UNIT_ASSERT_EQUAL(timeSeries.Size(), 3); - - UNIT_ASSERT_VALUES_EQUAL(2, h1->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(2, h2->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(2, h3->RefCount()); - } - - UNIT_ASSERT_VALUES_EQUAL(1, h1->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(1, h2->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(1, h3->RefCount()); - } - - Y_UNIT_TEST(SummaryUnique) { - auto ts1 = TInstant::Now(); - auto ts2 = ts1 + TDuration::Seconds(1); - auto ts3 = ts2 + TDuration::Seconds(1); - - auto h1 = MakeSummarySnapshot(); - auto h2 = MakeSummarySnapshot(); - auto h3 = MakeSummarySnapshot(); - - UNIT_ASSERT_VALUES_EQUAL(1, h1->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(1, h2->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(1, h3->RefCount()); - - { - TMetricTimeSeries timeSeries; - timeSeries.Add(ts1, h1.Get()); // drop at the head - timeSeries.Add(ts1, h1.Get()); - timeSeries.Add(ts1, h1.Get()); - - timeSeries.Add(ts2, h2.Get()); // drop in the middle - timeSeries.Add(ts2, h2.Get()); - timeSeries.Add(ts2, h2.Get()); - - timeSeries.Add(ts3, h3.Get()); // drop at the end - timeSeries.Add(ts3, h3.Get()); - timeSeries.Add(ts3, h3.Get()); - - UNIT_ASSERT_EQUAL(timeSeries.Size(), 9); - - UNIT_ASSERT_VALUES_EQUAL(4, h1->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(4, h2->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(4, h3->RefCount()); - - timeSeries.SortByTs(); - UNIT_ASSERT_EQUAL(timeSeries.Size(), 3); - - UNIT_ASSERT_VALUES_EQUAL(2, h1->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(2, h2->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(2, h3->RefCount()); - } - - UNIT_ASSERT_VALUES_EQUAL(1, h1->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(1, h2->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(1, h3->RefCount()); - } - + timeSeries.Add(ts1, h1.Get()); // drop at the head + timeSeries.Add(ts1, h1.Get()); + timeSeries.Add(ts1, h1.Get()); + + timeSeries.Add(ts2, h2.Get()); // drop in the middle + timeSeries.Add(ts2, h2.Get()); + timeSeries.Add(ts2, h2.Get()); + + timeSeries.Add(ts3, h3.Get()); // drop at the end + timeSeries.Add(ts3, h3.Get()); + timeSeries.Add(ts3, h3.Get()); + + UNIT_ASSERT_EQUAL(timeSeries.Size(), 9); + + UNIT_ASSERT_VALUES_EQUAL(4, h1->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(4, h2->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(4, h3->RefCount()); + + timeSeries.SortByTs(); + UNIT_ASSERT_EQUAL(timeSeries.Size(), 3); + + UNIT_ASSERT_VALUES_EQUAL(2, h1->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(2, h2->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(2, h3->RefCount()); + } + + UNIT_ASSERT_VALUES_EQUAL(1, h1->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(1, h2->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(1, h3->RefCount()); + } + Y_UNIT_TEST(HistogramsUnique2) { auto ts1 = TInstant::Now(); auto ts2 = ts1 + TDuration::Seconds(1); @@ -300,84 +300,84 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { UNIT_ASSERT_EQUAL(timeSeries[4].GetValue().AsHistogram()->Count(), 7); } } - - Y_UNIT_TEST(LogHistogramsUnique2) { - auto ts1 = TInstant::Now(); - auto ts2 = ts1 + TDuration::Seconds(1); - auto ts3 = ts2 + TDuration::Seconds(1); - auto ts4 = ts3 + TDuration::Seconds(1); - auto ts5 = ts4 + TDuration::Seconds(1); - - auto h1 = MakeLogHistogram(1u); - auto h2 = MakeLogHistogram(2u); - auto h3 = MakeLogHistogram(3u); - auto h4 = MakeLogHistogram(4u); - auto h5 = MakeLogHistogram(5u); - auto h6 = MakeLogHistogram(6u); - auto h7 = MakeLogHistogram(7u); - - { + + Y_UNIT_TEST(LogHistogramsUnique2) { + auto ts1 = TInstant::Now(); + auto ts2 = ts1 + TDuration::Seconds(1); + auto ts3 = ts2 + TDuration::Seconds(1); + auto ts4 = ts3 + TDuration::Seconds(1); + auto ts5 = ts4 + TDuration::Seconds(1); + + auto h1 = MakeLogHistogram(1u); + auto h2 = MakeLogHistogram(2u); + auto h3 = MakeLogHistogram(3u); + auto h4 = MakeLogHistogram(4u); + auto h5 = MakeLogHistogram(5u); + auto h6 = MakeLogHistogram(6u); + auto h7 = MakeLogHistogram(7u); + + { + TMetricTimeSeries timeSeries; + timeSeries.Add(ts1, h1.Get()); + timeSeries.Add(ts1, h2.Get()); + + timeSeries.Add(ts2, h3.Get()); + + timeSeries.Add(ts3, h4.Get()); + timeSeries.Add(ts3, h5.Get()); + + timeSeries.Add(ts4, h6.Get()); + timeSeries.Add(ts5, h7.Get()); + + timeSeries.SortByTs(); + + UNIT_ASSERT_EQUAL(timeSeries.Size(), 5); + UNIT_ASSERT_EQUAL(timeSeries[0].GetValue().AsLogHistogram()->Count(), 2); + UNIT_ASSERT_EQUAL(timeSeries[1].GetValue().AsLogHistogram()->Count(), 3); + UNIT_ASSERT_EQUAL(timeSeries[2].GetValue().AsLogHistogram()->Count(), 5); + UNIT_ASSERT_EQUAL(timeSeries[3].GetValue().AsLogHistogram()->Count(), 6); + UNIT_ASSERT_EQUAL(timeSeries[4].GetValue().AsLogHistogram()->Count(), 7); + } + } + + Y_UNIT_TEST(SummaryUnique2) { + auto ts1 = TInstant::Now(); + auto ts2 = ts1 + TDuration::Seconds(1); + auto ts3 = ts2 + TDuration::Seconds(1); + auto ts4 = ts3 + TDuration::Seconds(1); + auto ts5 = ts4 + TDuration::Seconds(1); + + auto h1 = MakeSummarySnapshot(1u); + auto h2 = MakeSummarySnapshot(2u); + auto h3 = MakeSummarySnapshot(3u); + auto h4 = MakeSummarySnapshot(4u); + auto h5 = MakeSummarySnapshot(5u); + auto h6 = MakeSummarySnapshot(6u); + auto h7 = MakeSummarySnapshot(7u); + + { TMetricTimeSeries timeSeries; - timeSeries.Add(ts1, h1.Get()); - timeSeries.Add(ts1, h2.Get()); - - timeSeries.Add(ts2, h3.Get()); - - timeSeries.Add(ts3, h4.Get()); - timeSeries.Add(ts3, h5.Get()); - - timeSeries.Add(ts4, h6.Get()); - timeSeries.Add(ts5, h7.Get()); - - timeSeries.SortByTs(); - - UNIT_ASSERT_EQUAL(timeSeries.Size(), 5); - UNIT_ASSERT_EQUAL(timeSeries[0].GetValue().AsLogHistogram()->Count(), 2); - UNIT_ASSERT_EQUAL(timeSeries[1].GetValue().AsLogHistogram()->Count(), 3); - UNIT_ASSERT_EQUAL(timeSeries[2].GetValue().AsLogHistogram()->Count(), 5); - UNIT_ASSERT_EQUAL(timeSeries[3].GetValue().AsLogHistogram()->Count(), 6); - UNIT_ASSERT_EQUAL(timeSeries[4].GetValue().AsLogHistogram()->Count(), 7); - } - } - - Y_UNIT_TEST(SummaryUnique2) { - auto ts1 = TInstant::Now(); - auto ts2 = ts1 + TDuration::Seconds(1); - auto ts3 = ts2 + TDuration::Seconds(1); - auto ts4 = ts3 + TDuration::Seconds(1); - auto ts5 = ts4 + TDuration::Seconds(1); - - auto h1 = MakeSummarySnapshot(1u); - auto h2 = MakeSummarySnapshot(2u); - auto h3 = MakeSummarySnapshot(3u); - auto h4 = MakeSummarySnapshot(4u); - auto h5 = MakeSummarySnapshot(5u); - auto h6 = MakeSummarySnapshot(6u); - auto h7 = MakeSummarySnapshot(7u); - - { - TMetricTimeSeries timeSeries; - timeSeries.Add(ts1, h1.Get()); - timeSeries.Add(ts1, h2.Get()); - - timeSeries.Add(ts2, h3.Get()); - - timeSeries.Add(ts3, h4.Get()); - timeSeries.Add(ts3, h5.Get()); - - timeSeries.Add(ts4, h6.Get()); - timeSeries.Add(ts5, h7.Get()); - - timeSeries.SortByTs(); - - UNIT_ASSERT_EQUAL(timeSeries.Size(), 5); - UNIT_ASSERT_EQUAL(timeSeries[0].GetValue().AsSummaryDouble()->GetCount(), 2); - UNIT_ASSERT_EQUAL(timeSeries[1].GetValue().AsSummaryDouble()->GetCount(), 3); - UNIT_ASSERT_EQUAL(timeSeries[2].GetValue().AsSummaryDouble()->GetCount(), 5); - UNIT_ASSERT_EQUAL(timeSeries[3].GetValue().AsSummaryDouble()->GetCount(), 6); - UNIT_ASSERT_EQUAL(timeSeries[4].GetValue().AsSummaryDouble()->GetCount(), 7); - } - } + timeSeries.Add(ts1, h1.Get()); + timeSeries.Add(ts1, h2.Get()); + + timeSeries.Add(ts2, h3.Get()); + + timeSeries.Add(ts3, h4.Get()); + timeSeries.Add(ts3, h5.Get()); + + timeSeries.Add(ts4, h6.Get()); + timeSeries.Add(ts5, h7.Get()); + + timeSeries.SortByTs(); + + UNIT_ASSERT_EQUAL(timeSeries.Size(), 5); + UNIT_ASSERT_EQUAL(timeSeries[0].GetValue().AsSummaryDouble()->GetCount(), 2); + UNIT_ASSERT_EQUAL(timeSeries[1].GetValue().AsSummaryDouble()->GetCount(), 3); + UNIT_ASSERT_EQUAL(timeSeries[2].GetValue().AsSummaryDouble()->GetCount(), 5); + UNIT_ASSERT_EQUAL(timeSeries[3].GetValue().AsSummaryDouble()->GetCount(), 6); + UNIT_ASSERT_EQUAL(timeSeries[4].GetValue().AsSummaryDouble()->GetCount(), 7); + } + } Y_UNIT_TEST(TMetricValueWithType) { // correct usage diff --git a/library/cpp/monlib/metrics/summary_collector.cpp b/library/cpp/monlib/metrics/summary_collector.cpp index cae8560891..7496ee17b0 100644 --- a/library/cpp/monlib/metrics/summary_collector.cpp +++ b/library/cpp/monlib/metrics/summary_collector.cpp @@ -1 +1 @@ -#include "summary_collector.h" +#include "summary_collector.h" diff --git a/library/cpp/monlib/metrics/summary_collector.h b/library/cpp/monlib/metrics/summary_collector.h index acba0fddf9..f09f2cfcf7 100644 --- a/library/cpp/monlib/metrics/summary_collector.h +++ b/library/cpp/monlib/metrics/summary_collector.h @@ -1,104 +1,104 @@ -#pragma once - -#include "summary_snapshot.h" - -#include <atomic> -#include <limits> -#include <cmath> - -namespace NMonitoring { - - class ISummaryDoubleCollector { - public: - virtual ~ISummaryDoubleCollector() = default; - - virtual void Collect(double value) = 0; - - virtual ISummaryDoubleSnapshotPtr Snapshot() const = 0; +#pragma once + +#include "summary_snapshot.h" + +#include <atomic> +#include <limits> +#include <cmath> + +namespace NMonitoring { + + class ISummaryDoubleCollector { + public: + virtual ~ISummaryDoubleCollector() = default; + + virtual void Collect(double value) = 0; + + virtual ISummaryDoubleSnapshotPtr Snapshot() const = 0; virtual size_t SizeBytes() const = 0; - }; - - using ISummaryDoubleCollectorPtr = THolder<ISummaryDoubleCollector>; - - class TSummaryDoubleCollector final: public ISummaryDoubleCollector { - public: - TSummaryDoubleCollector() { - Sum_.store(0, std::memory_order_relaxed); - Min_.store(std::numeric_limits<double>::max(), std::memory_order_relaxed); - Max_.store(std::numeric_limits<double>::lowest(), std::memory_order_relaxed); - Count_.store(0, std::memory_order_relaxed); - } - - void Collect(double value) noexcept override { - if (std::isnan(value)) { - return; - } - UpdateSum(value); - UpdateMin(value); - UpdateMax(value); - Last_.store(value, std::memory_order_relaxed); - Count_.fetch_add(1ul, std::memory_order_relaxed); - } - + }; + + using ISummaryDoubleCollectorPtr = THolder<ISummaryDoubleCollector>; + + class TSummaryDoubleCollector final: public ISummaryDoubleCollector { + public: + TSummaryDoubleCollector() { + Sum_.store(0, std::memory_order_relaxed); + Min_.store(std::numeric_limits<double>::max(), std::memory_order_relaxed); + Max_.store(std::numeric_limits<double>::lowest(), std::memory_order_relaxed); + Count_.store(0, std::memory_order_relaxed); + } + + void Collect(double value) noexcept override { + if (std::isnan(value)) { + return; + } + UpdateSum(value); + UpdateMin(value); + UpdateMax(value); + Last_.store(value, std::memory_order_relaxed); + Count_.fetch_add(1ul, std::memory_order_relaxed); + } + ISummaryDoubleSnapshotPtr Snapshot() const override { - return new TSummaryDoubleSnapshot( - Sum_.load(std::memory_order_relaxed), - Min_.load(std::memory_order_relaxed), - Max_.load(std::memory_order_relaxed), - Last_.load(std::memory_order_relaxed), - Count_.load(std::memory_order_relaxed)); - } - + return new TSummaryDoubleSnapshot( + Sum_.load(std::memory_order_relaxed), + Min_.load(std::memory_order_relaxed), + Max_.load(std::memory_order_relaxed), + Last_.load(std::memory_order_relaxed), + Count_.load(std::memory_order_relaxed)); + } + size_t SizeBytes() const override { return sizeof(*this); } - private: - std::atomic<double> Sum_; - std::atomic<double> Min_; - std::atomic<double> Max_; - std::atomic<double> Last_; - std::atomic_uint64_t Count_; - - void UpdateSum(double add) noexcept { - double newValue; - double oldValue = Sum_.load(std::memory_order_relaxed); - do { - newValue = oldValue + add; - } while (!Sum_.compare_exchange_weak( - oldValue, - newValue, - std::memory_order_release, - std::memory_order_consume)); - } - - void UpdateMin(double candidate) noexcept { - double oldValue = Min_.load(std::memory_order_relaxed); - do { - if (oldValue <= candidate) { - break; - } - } while (!Min_.compare_exchange_weak( - oldValue, - candidate, - std::memory_order_release, - std::memory_order_consume)); - } - - void UpdateMax(double candidate) noexcept { - double oldValue = Max_.load(std::memory_order_relaxed); - do { - if (oldValue >= candidate) { - break; - } - } while (!Max_.compare_exchange_weak( - oldValue, - candidate, - std::memory_order_release, - std::memory_order_consume)); - } - - }; - -} + private: + std::atomic<double> Sum_; + std::atomic<double> Min_; + std::atomic<double> Max_; + std::atomic<double> Last_; + std::atomic_uint64_t Count_; + + void UpdateSum(double add) noexcept { + double newValue; + double oldValue = Sum_.load(std::memory_order_relaxed); + do { + newValue = oldValue + add; + } while (!Sum_.compare_exchange_weak( + oldValue, + newValue, + std::memory_order_release, + std::memory_order_consume)); + } + + void UpdateMin(double candidate) noexcept { + double oldValue = Min_.load(std::memory_order_relaxed); + do { + if (oldValue <= candidate) { + break; + } + } while (!Min_.compare_exchange_weak( + oldValue, + candidate, + std::memory_order_release, + std::memory_order_consume)); + } + + void UpdateMax(double candidate) noexcept { + double oldValue = Max_.load(std::memory_order_relaxed); + do { + if (oldValue >= candidate) { + break; + } + } while (!Max_.compare_exchange_weak( + oldValue, + candidate, + std::memory_order_release, + std::memory_order_consume)); + } + + }; + +} diff --git a/library/cpp/monlib/metrics/summary_collector_ut.cpp b/library/cpp/monlib/metrics/summary_collector_ut.cpp index 191929550f..600148f814 100644 --- a/library/cpp/monlib/metrics/summary_collector_ut.cpp +++ b/library/cpp/monlib/metrics/summary_collector_ut.cpp @@ -1,64 +1,64 @@ -#include "summary_collector.h" - +#include "summary_collector.h" + #include <library/cpp/testing/unittest/registar.h> - -#include <util/random/random.h> - -#include <numeric> -#include <algorithm> - -namespace NMonitoring { - -Y_UNIT_TEST_SUITE(SummaryCollectorTest) { - - void CheckSnapshot(ISummaryDoubleSnapshotPtr snapshot, const TVector<double> values) { - const double eps = 1e-9; - - double sum = std::accumulate(values.begin(), values.end(), 0.0); - double min = *std::min_element(values.begin(), values.end()); - double max = *std::max_element(values.begin(), values.end()); - double last = values.back(); - ui64 count = values.size(); - - UNIT_ASSERT_DOUBLES_EQUAL(snapshot->GetSum(), sum, eps); - UNIT_ASSERT_DOUBLES_EQUAL(snapshot->GetMin(), min, eps); - UNIT_ASSERT_DOUBLES_EQUAL(snapshot->GetMax(), max, eps); - UNIT_ASSERT_DOUBLES_EQUAL(snapshot->GetLast(), last, eps); - UNIT_ASSERT_EQUAL(snapshot->GetCount(), count); - } - - Y_UNIT_TEST(Simple) { - { - TVector<double> test{05, -1.5, 0.0, 2.5, 0.25, -1.0}; - TSummaryDoubleCollector summary; - for (auto value : test) { - summary.Collect(value); - } - CheckSnapshot(summary.Snapshot(), test); - } - { - TVector<double> test{-1.0, 1.0, 9.0, -5000.0, 5000.0, 5.0, -5.0}; - TSummaryDoubleCollector summary; - for (auto value : test) { - summary.Collect(value); - } - CheckSnapshot(summary.Snapshot(), test); - } - } - - Y_UNIT_TEST(RandomStressTest) { - const ui32 attemts = 100; - for (ui32 i = 0; i < attemts; ++i) { - const ui32 size = 100; - TVector<double> values(size); - TSummaryDoubleCollector summary; - for (auto& value : values) { - value = RandomNumber<double>() - 0.5; - summary.Collect(value); - } - CheckSnapshot(summary.Snapshot(), values); - } - } -} - -} + +#include <util/random/random.h> + +#include <numeric> +#include <algorithm> + +namespace NMonitoring { + +Y_UNIT_TEST_SUITE(SummaryCollectorTest) { + + void CheckSnapshot(ISummaryDoubleSnapshotPtr snapshot, const TVector<double> values) { + const double eps = 1e-9; + + double sum = std::accumulate(values.begin(), values.end(), 0.0); + double min = *std::min_element(values.begin(), values.end()); + double max = *std::max_element(values.begin(), values.end()); + double last = values.back(); + ui64 count = values.size(); + + UNIT_ASSERT_DOUBLES_EQUAL(snapshot->GetSum(), sum, eps); + UNIT_ASSERT_DOUBLES_EQUAL(snapshot->GetMin(), min, eps); + UNIT_ASSERT_DOUBLES_EQUAL(snapshot->GetMax(), max, eps); + UNIT_ASSERT_DOUBLES_EQUAL(snapshot->GetLast(), last, eps); + UNIT_ASSERT_EQUAL(snapshot->GetCount(), count); + } + + Y_UNIT_TEST(Simple) { + { + TVector<double> test{05, -1.5, 0.0, 2.5, 0.25, -1.0}; + TSummaryDoubleCollector summary; + for (auto value : test) { + summary.Collect(value); + } + CheckSnapshot(summary.Snapshot(), test); + } + { + TVector<double> test{-1.0, 1.0, 9.0, -5000.0, 5000.0, 5.0, -5.0}; + TSummaryDoubleCollector summary; + for (auto value : test) { + summary.Collect(value); + } + CheckSnapshot(summary.Snapshot(), test); + } + } + + Y_UNIT_TEST(RandomStressTest) { + const ui32 attemts = 100; + for (ui32 i = 0; i < attemts; ++i) { + const ui32 size = 100; + TVector<double> values(size); + TSummaryDoubleCollector summary; + for (auto& value : values) { + value = RandomNumber<double>() - 0.5; + summary.Collect(value); + } + CheckSnapshot(summary.Snapshot(), values); + } + } +} + +} diff --git a/library/cpp/monlib/metrics/summary_snapshot.cpp b/library/cpp/monlib/metrics/summary_snapshot.cpp index 0b13263337..39f34902f3 100644 --- a/library/cpp/monlib/metrics/summary_snapshot.cpp +++ b/library/cpp/monlib/metrics/summary_snapshot.cpp @@ -1,9 +1,9 @@ -#include "summary_snapshot.h" - -#include <util/stream/output.h> - +#include "summary_snapshot.h" + +#include <util/stream/output.h> + #include <iostream> - + namespace { @@ -16,11 +16,11 @@ auto& Output(TStream& o, const NMonitoring::ISummaryDoubleSnapshot& s) { o << TStringBuf("max: ") << s.GetMax() << TStringBuf(", "); o << TStringBuf("last: ") << s.GetLast() << TStringBuf(", "); o << TStringBuf("count: ") << s.GetCount(); - + o << TStringBuf("}"); return o; -} +} } // namespace diff --git a/library/cpp/monlib/metrics/summary_snapshot.h b/library/cpp/monlib/metrics/summary_snapshot.h index afcc895fd3..d90b5f1b01 100644 --- a/library/cpp/monlib/metrics/summary_snapshot.h +++ b/library/cpp/monlib/metrics/summary_snapshot.h @@ -1,72 +1,72 @@ -#pragma once - -#include <util/generic/ptr.h> - -namespace NMonitoring { - - class ISummaryDoubleSnapshot: public TAtomicRefCount<ISummaryDoubleSnapshot> { - public: - virtual ~ISummaryDoubleSnapshot() = default; - - // TODO: write documentation - - virtual ui64 GetCount() const = 0; - - virtual double GetSum() const = 0; - - virtual double GetMin() const = 0; - - virtual double GetMax() const = 0; - - virtual double GetLast() const = 0; - - virtual ui64 MemorySizeBytes() const = 0; - }; - - using ISummaryDoubleSnapshotPtr = TIntrusivePtr<ISummaryDoubleSnapshot>; - - class TSummaryDoubleSnapshot final: public ISummaryDoubleSnapshot { - public: - TSummaryDoubleSnapshot(double sum, double min, double max, double last, ui64 count) - : Sum_(sum) - , Min_(min) - , Max_(max) - , Last_(last) - , Count_(count) - {} - - ui64 GetCount() const noexcept override { - return Count_; - } - - double GetSum() const noexcept override { - return Sum_; - } - - double GetMin() const noexcept override { - return Min_; - } - - double GetMax() const noexcept override { - return Max_; - } - - virtual double GetLast() const noexcept override { - return Last_; - } - - ui64 MemorySizeBytes() const noexcept override { - return sizeof(*this); - } - - private: - double Sum_; - double Min_; - double Max_; - double Last_; - ui64 Count_; - }; - -} +#pragma once + +#include <util/generic/ptr.h> + +namespace NMonitoring { + + class ISummaryDoubleSnapshot: public TAtomicRefCount<ISummaryDoubleSnapshot> { + public: + virtual ~ISummaryDoubleSnapshot() = default; + + // TODO: write documentation + + virtual ui64 GetCount() const = 0; + + virtual double GetSum() const = 0; + + virtual double GetMin() const = 0; + + virtual double GetMax() const = 0; + + virtual double GetLast() const = 0; + + virtual ui64 MemorySizeBytes() const = 0; + }; + + using ISummaryDoubleSnapshotPtr = TIntrusivePtr<ISummaryDoubleSnapshot>; + + class TSummaryDoubleSnapshot final: public ISummaryDoubleSnapshot { + public: + TSummaryDoubleSnapshot(double sum, double min, double max, double last, ui64 count) + : Sum_(sum) + , Min_(min) + , Max_(max) + , Last_(last) + , Count_(count) + {} + + ui64 GetCount() const noexcept override { + return Count_; + } + + double GetSum() const noexcept override { + return Sum_; + } + + double GetMin() const noexcept override { + return Min_; + } + + double GetMax() const noexcept override { + return Max_; + } + + virtual double GetLast() const noexcept override { + return Last_; + } + + ui64 MemorySizeBytes() const noexcept override { + return sizeof(*this); + } + + private: + double Sum_; + double Min_; + double Max_; + double Last_; + ui64 Count_; + }; + +} std::ostream& operator<<(std::ostream& os, const NMonitoring::ISummaryDoubleSnapshot& s); diff --git a/library/cpp/monlib/metrics/ut/ya.make b/library/cpp/monlib/metrics/ut/ya.make index aec9974fbd..cb55da7822 100644 --- a/library/cpp/monlib/metrics/ut/ya.make +++ b/library/cpp/monlib/metrics/ut/ya.make @@ -10,7 +10,7 @@ SRCS( fake_ut.cpp histogram_collector_ut.cpp labels_ut.cpp - log_histogram_collector_ut.cpp + log_histogram_collector_ut.cpp metric_registry_ut.cpp metric_sub_registry_ut.cpp metric_value_ut.cpp |