diff options
author | Alexander Smirnov <alex@ydb.tech> | 2025-03-07 00:51:42 +0000 |
---|---|---|
committer | Alexander Smirnov <alex@ydb.tech> | 2025-03-07 00:51:42 +0000 |
commit | 26f81abb45fa0b914da68e22b79c4b8ba8ac105b (patch) | |
tree | 2dd0455697e85b89272f2d4197703cb92f33958e /library/cpp/monlib/encode | |
parent | 98be31c26bacbdc98081620b680a520f0bd0246f (diff) | |
parent | 9b0a13bddce49bfb5ea59d8c4a668d32d7dada3b (diff) | |
download | ydb-26f81abb45fa0b914da68e22b79c4b8ba8ac105b.tar.gz |
Merge branch 'rightlib' into merge-libs-250307-0050
Diffstat (limited to 'library/cpp/monlib/encode')
-rw-r--r-- | library/cpp/monlib/encode/json/json_decoder.cpp | 32 | ||||
-rw-r--r-- | library/cpp/monlib/encode/json/json_encoder.cpp | 16 | ||||
-rw-r--r-- | library/cpp/monlib/encode/json/json_ut.cpp | 18 | ||||
-rw-r--r-- | library/cpp/monlib/encode/json/ut/expected_buffered_memOnly.json | 95 | ||||
-rw-r--r-- | library/cpp/monlib/encode/json/ut/expected_memOnly.json | 95 | ||||
-rw-r--r-- | library/cpp/monlib/encode/json/ut/metrics.json | 1 | ||||
-rw-r--r-- | library/cpp/monlib/encode/json/ut/ya.make | 2 |
7 files changed, 250 insertions, 9 deletions
diff --git a/library/cpp/monlib/encode/json/json_decoder.cpp b/library/cpp/monlib/encode/json/json_decoder.cpp index 37189900759..801ff0833d2 100644 --- a/library/cpp/monlib/encode/json/json_decoder.cpp +++ b/library/cpp/monlib/encode/json/json_decoder.cpp @@ -193,6 +193,7 @@ struct TMetricCollector { TLogHistogramBuilder LogHistBuilder; TTypedPoint LastPoint; TVector<TTypedPoint> TimeSeries; + bool IsMemOnly = false; bool SeenTsOrValue = false; bool SeenTimeseries = false; @@ -207,6 +208,7 @@ struct TMetricCollector { HistogramBuilder.Clear(); SummaryBuilder.Clear(); LogHistBuilder.Clear(); + IsMemOnly = false; } void AddLabel(const TLabel& label) { @@ -222,6 +224,10 @@ struct TMetricCollector { LastPoint.SetValue(value); } + void SetMemOnly(bool isMemOnly) { + IsMemOnly = isMemOnly; + } + void SaveLastPoint() { DECODE_ENSURE(LastPoint.GetTime() != TInstant::Zero(), "cannot add point without or zero timestamp"); @@ -419,6 +425,10 @@ private: Consumer_->OnSummaryDouble(time, std::move(snapshot)); } + void OnMemOnly(bool isMemOnly) override{ + Consumer_->OnMemOnly(isMemOnly); + } + private: const TCommonParts CommonParts_; IMetricConsumer* Consumer_; @@ -442,6 +452,7 @@ class TDecoderJson final: public NJson::TJsonCallbacks { METRIC_LABELS, METRIC_TYPE, METRIC_MODE, // TODO: must be deleted + METRIC_MEMONLY, METRIC_TIMESERIES, METRIC_TS, METRIC_VALUE, @@ -858,7 +869,7 @@ if (Y_UNLIKELY(!(CONDITION))) { \ } else if (key == TStringBuf("log_hist")) { State_.ToNext(TState::METRIC_LOG_HIST); } else if (key == TStringBuf("memOnly")) { - // deprecated. Skip it without errors for backward compatibility + State_.ToNext(TState::METRIC_MEMONLY); } else { ErrorMsg_ = TStringBuilder() << "unexpected key \"" << key << "\" in a metric schema"; return false; @@ -1033,6 +1044,18 @@ if (Y_UNLIKELY(!(CONDITION))) { \ return true; } + bool OnBoolean(bool value) override { + switch (State_.Current()) { + case TState::METRIC_MEMONLY: + LastMetric_.IsMemOnly = value; + State_.ToPrev(); + break; + default: + return false; + } + return true; + } + void ConsumeMetric() { // for backwad compatibility all unknown metrics treated as gauges if (LastMetric_.Type == EMetricType::UNKNOWN) { @@ -1055,7 +1078,12 @@ if (Y_UNLIKELY(!(CONDITION))) { \ MetricConsumer_->OnLabelsEnd(); } - // (3) values + // (3) flags + if (LastMetric_.IsMemOnly) { + MetricConsumer_->OnMemOnly(true); + } + + // (4) values switch (LastMetric_.Type) { case EMetricType::GAUGE: LastMetric_.Consume([this](TInstant time, EMetricValueType valueType, TMetricValue value) { diff --git a/library/cpp/monlib/encode/json/json_encoder.cpp b/library/cpp/monlib/encode/json/json_encoder.cpp index 69da5170f0d..2af058155a8 100644 --- a/library/cpp/monlib/encode/json/json_encoder.cpp +++ b/library/cpp/monlib/encode/json/json_encoder.cpp @@ -274,6 +274,14 @@ namespace NMonitoring { WriteMetricType(type); } + void OnMemOnly(bool isMemOnly) override { + State_.Expect(TEncoderState::EState::METRIC); + if (isMemOnly) { + Buf_.WriteKey("memOnly"); + Buf_.WriteBool(isMemOnly); + } + } + void OnMetricEnd() override { State_.Switch(TEncoderState::EState::METRIC, TEncoderState::EState::ROOT); if (!Buf_.KeyExpected()) { @@ -493,6 +501,7 @@ namespace NMonitoring { Buf_.WriteKey(TStringBuf("labels")); WriteLabels(metric.Labels, false); + WriteFlags(metric); metric.TimeSeries.SortByTs(); if (metric.TimeSeries.Size() == 1) { const auto& point = metric.TimeSeries[0]; @@ -532,6 +541,13 @@ namespace NMonitoring { } } + void WriteFlags(const TMetric& metric) { + if (metric.IsMemOnly) { + Buf_.WriteKey("memOnly"); + Buf_.WriteBool(true); + } + } + private: bool Closed_{false}; bool EmptyLabels_ = false; diff --git a/library/cpp/monlib/encode/json/json_ut.cpp b/library/cpp/monlib/encode/json/json_ut.cpp index 49de352062b..78103a42b5c 100644 --- a/library/cpp/monlib/encode/json/json_ut.cpp +++ b/library/cpp/monlib/encode/json/json_ut.cpp @@ -136,7 +136,7 @@ Y_UNIT_TEST_SUITE(TJsonTest) { const TInstant now = TInstant::ParseIso8601Deprecated("2017-11-05T01:02:03Z"); Y_UNIT_TEST(Encode) { - auto check = [](bool cloud, bool buffered, TStringBuf expectedResourceKey) { + auto check = [](bool cloud, bool buffered, bool memOnly, TStringBuf expectedResourceKey) { TString json; TStringOutput out(json); auto e = cloud @@ -158,6 +158,7 @@ Y_UNIT_TEST_SUITE(TJsonTest) { e->OnLabel("metric", "single"); e->OnLabel("labels", "l1"); e->OnLabelsEnd(); + e->OnMemOnly(memOnly); } e->OnUint64(now, 17); e->OnMetricEnd(); @@ -219,6 +220,7 @@ Y_UNIT_TEST_SUITE(TJsonTest) { e->OnLabel("labels", "l6"); e->OnLabelsEnd(); } + e->OnMemOnly(memOnly); e->OnMetricEnd(); } e->OnStreamEnd(); @@ -235,10 +237,12 @@ Y_UNIT_TEST_SUITE(TJsonTest) { UNIT_ASSERT_EQUAL(parseJson(json), parseJson(expectedJson)); }; - check(false, false, "/expected.json"); - check(false, true, "/expected_buffered.json"); - check(true, false, "/expected_cloud.json"); - check(true, true, "/expected_cloud_buffered.json"); + check(false, false, false, "/expected.json"); + check(true, false, false, "/expected_cloud.json"); + check(false, true, false, "/expected_buffered.json"); + check(true, true, false, "/expected_cloud_buffered.json"); + check(false, false, true, "/expected_memOnly.json"); + check(false, true, true, "/expected_buffered_memOnly.json"); } TLogHistogramSnapshotPtr TestLogHistogram(ui32 v = 1) { @@ -478,7 +482,7 @@ Y_UNIT_TEST_SUITE(TJsonTest) { UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 2); AssertLabelEqual(s.GetLabels(0), "export", "Oxygen"); AssertLabelEqual(s.GetLabels(1), "metric", "QueueSize"); - + UNIT_ASSERT_EQUAL(s.GetIsMemOnly(), true); UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); auto ts = TInstant::ParseIso8601Deprecated("2017-11-05T12:34:56.000Z"); AssertPointEqual(s.GetPoints(0), ts, 3.14159); @@ -487,6 +491,7 @@ Y_UNIT_TEST_SUITE(TJsonTest) { const NProto::TMultiSample& s = samples.GetSamples(3); UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE); UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); + UNIT_ASSERT_EQUAL(s.GetIsMemOnly(), true); AssertLabelEqual(s.GetLabels(0), "metric", "Writes"); UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 2); @@ -1286,5 +1291,4 @@ Y_UNIT_TEST_SUITE(TJsonTest) { AssertLabelEqual(s.GetLabels(1), "export", "Oxygen"); } } - } diff --git a/library/cpp/monlib/encode/json/ut/expected_buffered_memOnly.json b/library/cpp/monlib/encode/json/ut/expected_buffered_memOnly.json new file mode 100644 index 00000000000..7273c2e4779 --- /dev/null +++ b/library/cpp/monlib/encode/json/ut/expected_buffered_memOnly.json @@ -0,0 +1,95 @@ +{ + "ts":1500000000, + "commonLabels": + { + "project":"solomon" + }, + "sensors": + [ + { + "kind":"COUNTER", + "labels": + { + "labels":"l1", + "metric":"single" + }, + "memOnly":true, + "ts":1509843723, + "value":17 + }, + { + "kind":"RATE", + "labels": + { + "labels":"l2", + "metric":"single" + }, + "ts":1509843723, + "value":17 + }, + { + "kind":"GAUGE", + "labels": + { + "labels":"l3", + "metric":"single" + }, + "ts":1509843723, + "value":3.14 + }, + { + "kind":"IGAUGE", + "labels": + { + "labels":"l4", + "metric":"single_igauge" + }, + "ts":1509843723, + "value":42 + }, + { + "kind":"GAUGE", + "labels": + { + "labels":"l5", + "metric":"multiple" + }, + "timeseries": + [ + { + "ts":1509843723, + "value":"nan" + }, + { + "ts":1509843738, + "value":"inf" + }, + { + "ts":1509843753, + "value":"-inf" + } + ] + }, + { + "kind":"COUNTER", + "labels": + { + "labels":"l6", + "metric":"multiple" + }, + "memOnly":true, + "timeseries": + [ + { + "ts":1509843723, + "value":1337 + }, + { + "ts":1509843738, + "value":1338 + } + ] + } + ] + } +
\ No newline at end of file diff --git a/library/cpp/monlib/encode/json/ut/expected_memOnly.json b/library/cpp/monlib/encode/json/ut/expected_memOnly.json new file mode 100644 index 00000000000..ee3398427b9 --- /dev/null +++ b/library/cpp/monlib/encode/json/ut/expected_memOnly.json @@ -0,0 +1,95 @@ +{ + "ts":1500000000, + "commonLabels": + { + "project":"solomon" + }, + "sensors": + [ + { + "kind":"COUNTER", + "labels": + { + "metric":"single", + "labels":"l1" + }, + "memOnly":true, + "ts":1509843723, + "value":17 + }, + { + "kind":"RATE", + "labels": + { + "metric":"single", + "labels":"l2" + }, + "ts":1509843723, + "value":17 + }, + { + "kind":"GAUGE", + "labels": + { + "metric":"single", + "labels":"l3" + }, + "ts":1509843723, + "value":3.14 + }, + { + "kind":"IGAUGE", + "labels": + { + "metric":"single_igauge", + "labels":"l4" + }, + "ts":1509843723, + "value":42 + }, + { + "kind":"GAUGE", + "labels": + { + "metric":"multiple", + "labels":"l5" + }, + "timeseries": + [ + { + "ts":1509843723, + "value":"nan" + }, + { + "ts":1509843738, + "value":"inf" + }, + { + "ts":1509843753, + "value":"-inf" + } + ] + }, + { + "kind":"COUNTER", + "memOnly":true, + "timeseries": + [ + { + "ts":1509843723, + "value":1337 + }, + { + "ts":1509843738, + "value":1338 + } + ], + "labels": + { + "metric":"multiple", + "labels":"l6" + } + } + ] + } +
\ No newline at end of file diff --git a/library/cpp/monlib/encode/json/ut/metrics.json b/library/cpp/monlib/encode/json/ut/metrics.json index 2be4617d515..80547792aea 100644 --- a/library/cpp/monlib/encode/json/ut/metrics.json +++ b/library/cpp/monlib/encode/json/ut/metrics.json @@ -27,6 +27,7 @@ { "type": "GAUGE", "labels": { "metric": "Writes" }, + "memOnly": true, "timeseries": [ { "ts": "2017-08-28T12:32:11Z", diff --git a/library/cpp/monlib/encode/json/ut/ya.make b/library/cpp/monlib/encode/json/ut/ya.make index 9be38d2fd47..fbc26536de7 100644 --- a/library/cpp/monlib/encode/json/ut/ya.make +++ b/library/cpp/monlib/encode/json/ut/ya.make @@ -10,7 +10,9 @@ RESOURCE( buffered_ts_merge.json /buffered_ts_merge.json empty_series.json /empty_series.json expected.json /expected.json + expected_memOnly.json /expected_memOnly.json expected_buffered.json /expected_buffered.json + expected_buffered_memOnly.json /expected_buffered_memOnly.json expected_cloud.json /expected_cloud.json expected_cloud_buffered.json /expected_cloud_buffered.json merged.json /merged.json |