diff options
author | Sergey Polovko <sergey@polovko.me> | 2022-02-10 16:47:03 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:47:03 +0300 |
commit | 2e714b5ebd40a1f4cc31c27f1ad6e49ca6d895f5 (patch) | |
tree | b83306b6e37edeea782e9eed673d89286c4fef35 /library/cpp/monlib/encode/json | |
parent | 3e0b762a82514bac89c1dd6ea7211e381d8aa248 (diff) | |
download | ydb-2e714b5ebd40a1f4cc31c27f1ad6e49ca6d895f5.tar.gz |
Restoring authorship annotation for Sergey Polovko <sergey@polovko.me>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/monlib/encode/json')
25 files changed, 1376 insertions, 1376 deletions
diff --git a/library/cpp/monlib/encode/json/fuzz/main.cpp b/library/cpp/monlib/encode/json/fuzz/main.cpp index 65f98d8a8b..4f40310e06 100644 --- a/library/cpp/monlib/encode/json/fuzz/main.cpp +++ b/library/cpp/monlib/encode/json/fuzz/main.cpp @@ -1,5 +1,5 @@ -#include <library/cpp/monlib/encode/json/json.h> -#include <library/cpp/monlib/encode/fake/fake.h> +#include <library/cpp/monlib/encode/json/json.h> +#include <library/cpp/monlib/encode/fake/fake.h> #include <util/generic/strbuf.h> diff --git a/library/cpp/monlib/encode/json/fuzz/ya.make b/library/cpp/monlib/encode/json/fuzz/ya.make index 8ff5c6f9f9..75baa77716 100644 --- a/library/cpp/monlib/encode/json/fuzz/ya.make +++ b/library/cpp/monlib/encode/json/fuzz/ya.make @@ -6,8 +6,8 @@ OWNER( ) PEERDIR( - library/cpp/monlib/encode/json - library/cpp/monlib/encode/fake + library/cpp/monlib/encode/json + library/cpp/monlib/encode/fake ) SIZE(MEDIUM) diff --git a/library/cpp/monlib/encode/json/json.h b/library/cpp/monlib/encode/json/json.h index 2f0c8b0e1d..21530f20c3 100644 --- a/library/cpp/monlib/encode/json/json.h +++ b/library/cpp/monlib/encode/json/json.h @@ -1,20 +1,20 @@ -#pragma once - -#include <library/cpp/monlib/encode/encoder.h> -#include <library/cpp/monlib/encode/format.h> - +#pragma once -class IOutputStream; - -namespace NMonitoring { +#include <library/cpp/monlib/encode/encoder.h> +#include <library/cpp/monlib/encode/format.h> + + +class IOutputStream; + +namespace NMonitoring { class TJsonDecodeError: public yexception { }; - IMetricEncoderPtr EncoderJson(IOutputStream* out, int indentation = 0); - + IMetricEncoderPtr EncoderJson(IOutputStream* out, int indentation = 0); + /// Buffered encoder will merge series with same labels into one. - IMetricEncoderPtr BufferedEncoderJson(IOutputStream* out, int indentation = 0); + IMetricEncoderPtr BufferedEncoderJson(IOutputStream* out, int indentation = 0); IMetricEncoderPtr EncoderCloudJson(IOutputStream* out, int indentation = 0, @@ -25,5 +25,5 @@ namespace NMonitoring { TStringBuf metricNameLabel = "name"); void DecodeJson(TStringBuf data, IMetricConsumer* c, TStringBuf metricNameLabel = "name"); - -} + +} diff --git a/library/cpp/monlib/encode/json/json_decoder.cpp b/library/cpp/monlib/encode/json/json_decoder.cpp index c8824e1ccc..d44ff5fd28 100644 --- a/library/cpp/monlib/encode/json/json_decoder.cpp +++ b/library/cpp/monlib/encode/json/json_decoder.cpp @@ -1,24 +1,24 @@ -#include "json.h" -#include "typed_point.h" - +#include "json.h" +#include "typed_point.h" + #include <library/cpp/monlib/exception/exception.h> -#include <library/cpp/monlib/metrics/labels.h> -#include <library/cpp/monlib/metrics/metric_value.h> - +#include <library/cpp/monlib/metrics/labels.h> +#include <library/cpp/monlib/metrics/metric_value.h> + #include <library/cpp/json/json_reader.h> - -#include <util/datetime/base.h> -#include <util/string/cast.h> - -#include <limits> - -namespace NMonitoring { + +#include <util/datetime/base.h> +#include <util/string/cast.h> + +#include <limits> + +namespace NMonitoring { #define DECODE_ENSURE(COND, ...) MONLIB_ENSURE_EX(COND, TJsonDecodeError() << __VA_ARGS__) namespace { - + /////////////////////////////////////////////////////////////////////// // THistogramBuilder /////////////////////////////////////////////////////////////////////// @@ -32,11 +32,11 @@ public: } Bounds_.push_back(bound); } - + void AddValue(TBucketValue value) { Values_.push_back(value); } - + void AddInf(TBucketValue value) { InfPresented_ = true; InfValue_ = value; @@ -53,14 +53,14 @@ public: Bounds_.clear(); Values_.clear(); InfPresented_ = false; - + return snapshot; } - + bool Empty() const noexcept { return Bounds_.empty() && Values_.empty(); } - + void Clear() { Bounds_.clear(); Values_.clear(); @@ -69,7 +69,7 @@ public: private: TBucketBounds Bounds_; TBucketValues Values_; - + bool InfPresented_ = false; TBucketValue InfValue_; }; @@ -182,7 +182,7 @@ std::pair<double, bool> ParseSpecDouble(TStringBuf string) { return {0, false}; } } - + /////////////////////////////////////////////////////////////////////// // TMetricCollector /////////////////////////////////////////////////////////////////////// @@ -194,10 +194,10 @@ struct TMetricCollector { TLogHistogramBuilder LogHistBuilder; TTypedPoint LastPoint; TVector<TTypedPoint> TimeSeries; - + bool SeenTsOrValue = false; bool SeenTimeseries = false; - + void Clear() { Type = EMetricType::UNKNOWN; Labels.Clear(); @@ -209,20 +209,20 @@ struct TMetricCollector { SummaryBuilder.Clear(); LogHistBuilder.Clear(); } - + void AddLabel(const TLabel& label) { Labels.Add(label.Name(), label.Value()); } - + void SetLastTime(TInstant time) { LastPoint.SetTime(time); } - + template <typename T> void SetLastValue(T value) { LastPoint.SetValue(value); } - + void SaveLastPoint() { DECODE_ENSURE(LastPoint.GetTime() != TInstant::Zero(), "cannot add point without or zero timestamp"); @@ -248,21 +248,21 @@ struct TMetricCollector { } else { for (const auto& p: TimeSeries) { consumer(p.GetTime(), p.GetValueType(), p.GetValue()); - } + } } } }; - + struct TCommonParts { TInstant CommonTime; TLabels CommonLabels; }; - + class IHaltableMetricConsumer: public IMetricConsumer { public: virtual bool NeedToStop() const = 0; }; - + // TODO(ivanzhukov@): check all states for cases when a json document is invalid // e.g. "metrics" or "commonLabels" keys are specified multiple times class TCommonPartsCollector: public IHaltableMetricConsumer { @@ -433,11 +433,11 @@ class TDecoderJson final: public NJson::TJsonCallbacks { struct TState { enum EState { ROOT_OBJECT = 0x01, - + COMMON_LABELS, COMMON_TS, METRICS_ARRAY, - + METRIC_OBJECT, METRIC_NAME, METRIC_LABELS, @@ -462,21 +462,21 @@ class TDecoderJson final: public NJson::TJsonCallbacks { METRIC_LOG_HIST_START_POWER, METRIC_LOG_HIST_BUCKETS, }; - + constexpr EState Current() const noexcept { return static_cast<EState>(State_ & 0xFF); } - + void ToNext(EState state) noexcept { constexpr auto bitSize = 8 * sizeof(ui8); State_ = (State_ << bitSize) | static_cast<ui8>(state); } - + void ToPrev() noexcept { constexpr auto bitSize = 8 * sizeof(ui8); State_ = State_ >> bitSize; } - + private: ui64 State_ = static_cast<ui64>(ROOT_OBJECT); }; @@ -522,16 +522,16 @@ if (Y_UNLIKELY(!(CONDITION))) { \ LastMetric_.SetLastValue(static_cast<i64>(value)); State_.ToPrev(); break; - + case TState::METRIC_HIST_BOUNDS: LastMetric_.HistogramBuilder.AddBound(static_cast<double>(value)); break; - + case TState::METRIC_HIST_BUCKETS: PARSE_ENSURE(value >= 0 && static_cast<ui64>(value) <= Max<TBucketValues::value_type>(), "value is out of bounds " << value); LastMetric_.HistogramBuilder.AddValue(value); break; - + case TState::METRIC_HIST_INF: PARSE_ENSURE(value >= 0, "unexpected negative number in histogram inf: " << value); LastMetric_.HistogramBuilder.AddInf(value); @@ -584,7 +584,7 @@ if (Y_UNLIKELY(!(CONDITION))) { \ } return true; } - + bool OnUInteger(unsigned long long value) override { switch (State_.Current()) { case TState::COMMON_TS: @@ -597,32 +597,32 @@ if (Y_UNLIKELY(!(CONDITION))) { \ } break; - + case TState::METRIC_TS: LastMetric_.SetLastTime(TInstant::Seconds(value)); State_.ToPrev(); break; - + case TState::METRIC_VALUE: PARSE_ENSURE(value <= Max<ui64>(), "Metric value is out of bounds: " << value); LastMetric_.SetLastValue(static_cast<ui64>(value)); State_.ToPrev(); break; - + case TState::METRIC_HIST_BOUNDS: LastMetric_.HistogramBuilder.AddBound(static_cast<double>(value)); break; - + case TState::METRIC_HIST_BUCKETS: PARSE_ENSURE(value <= Max<TBucketValues::value_type>(), "Histogram bucket value is out of bounds: " << value); LastMetric_.HistogramBuilder.AddValue(value); break; - + case TState::METRIC_HIST_INF: LastMetric_.HistogramBuilder.AddInf(value); State_.ToPrev(); break; - + case TState::METRIC_DSUMMARY_COUNT: LastMetric_.SummaryBuilder.SetCount(value); State_.ToPrev(); @@ -669,18 +669,18 @@ if (Y_UNLIKELY(!(CONDITION))) { \ } return true; } - + bool OnDouble(double value) override { switch (State_.Current()) { case TState::METRIC_VALUE: LastMetric_.SetLastValue(value); State_.ToPrev(); break; - + case TState::METRIC_HIST_BOUNDS: LastMetric_.HistogramBuilder.AddBound(value); break; - + case TState::METRIC_DSUMMARY_SUM: LastMetric_.SummaryBuilder.SetSum(value); State_.ToPrev(); @@ -752,22 +752,22 @@ if (Y_UNLIKELY(!(CONDITION))) { \ LastMetric_.SetLastValue(doubleValue); } else { return false; - } + } State_.ToPrev(); break; - + case TState::METRIC_TYPE: LastMetric_.Type = MetricTypeFromStr(value); State_.ToPrev(); break; - + case TState::METRIC_MODE: if (value == TStringBuf("deriv")) { LastMetric_.Type = EMetricType::RATE; } State_.ToPrev(); break; - + case TState::METRIC_DSUMMARY_SUM: if (auto [doubleValue, ok] = ParseSpecDouble(value); ok) { LastMetric_.SummaryBuilder.SetSum(doubleValue); @@ -776,7 +776,7 @@ if (Y_UNLIKELY(!(CONDITION))) { \ } State_.ToPrev(); break; - + case TState::METRIC_DSUMMARY_MIN: if (auto [doubleValue, ok] = ParseSpecDouble(value); ok) { LastMetric_.SummaryBuilder.SetMin(doubleValue); @@ -785,7 +785,7 @@ if (Y_UNLIKELY(!(CONDITION))) { \ } State_.ToPrev(); break; - + case TState::METRIC_DSUMMARY_MAX: if (auto [doubleValue, ok] = ParseSpecDouble(value); ok) { LastMetric_.SummaryBuilder.SetMax(doubleValue); @@ -794,7 +794,7 @@ if (Y_UNLIKELY(!(CONDITION))) { \ } State_.ToPrev(); break; - + case TState::METRIC_DSUMMARY_LAST: if (auto [doubleValue, ok] = ParseSpecDouble(value); ok) { LastMetric_.SummaryBuilder.SetLast(doubleValue); @@ -803,11 +803,11 @@ if (Y_UNLIKELY(!(CONDITION))) { \ } State_.ToPrev(); break; - + default: return false; } - + return true; } @@ -877,7 +877,7 @@ if (Y_UNLIKELY(!(CONDITION))) { \ State_.ToNext(TState::METRIC_DSUMMARY); } else if (key == TStringBuf("log_hist")) { State_.ToNext(TState::METRIC_LOG_HIST); - } + } break; case TState::METRIC_HIST: @@ -889,7 +889,7 @@ if (Y_UNLIKELY(!(CONDITION))) { \ State_.ToNext(TState::METRIC_HIST_INF); } break; - + case TState::METRIC_LOG_HIST: if (key == TStringBuf("base")) { State_.ToNext(TState::METRIC_LOG_HIST_BASE); @@ -901,7 +901,7 @@ if (Y_UNLIKELY(!(CONDITION))) { \ State_.ToNext(TState::METRIC_LOG_HIST_BUCKETS); } break; - + case TState::METRIC_DSUMMARY: if (key == TStringBuf("sum")) { State_.ToNext(TState::METRIC_DSUMMARY_SUM); @@ -914,14 +914,14 @@ if (Y_UNLIKELY(!(CONDITION))) { \ } else if (key == TStringBuf("count")) { State_.ToNext(TState::METRIC_DSUMMARY_COUNT); } - + break; - - + + default: return false; } - + return true; } @@ -945,17 +945,17 @@ if (Y_UNLIKELY(!(CONDITION))) { \ } return true; } - + bool OnCloseMap() override { switch (State_.Current()) { case TState::ROOT_OBJECT: MetricConsumer_->OnStreamEnd(); break; - + case TState::METRIC_LABELS: State_.ToPrev(); break; - + case TState::COMMON_LABELS: MetricConsumer_->OnLabelsEnd(); State_.ToPrev(); @@ -966,22 +966,22 @@ if (Y_UNLIKELY(!(CONDITION))) { \ } break; - + case TState::METRIC_OBJECT: ConsumeMetric(); State_.ToPrev(); break; - + case TState::METRIC_TIMESERIES: LastMetric_.SaveLastPoint(); break; - + case TState::METRIC_HIST: case TState::METRIC_DSUMMARY: case TState::METRIC_LOG_HIST: State_.ToPrev(); break; - + default: break; } @@ -999,7 +999,7 @@ if (Y_UNLIKELY(!(CONDITION))) { \ "unexpected array begin"); return true; } - + bool OnCloseArray() override { switch (State_.Current()) { case TState::METRICS_ARRAY: @@ -1009,13 +1009,13 @@ if (Y_UNLIKELY(!(CONDITION))) { \ case TState::METRIC_LOG_HIST_BUCKETS: State_.ToPrev(); break; - + default: return false; } return true; } - + void OnError(size_t off, TStringBuf reason) override { if (IsIntentionallyHalted_) { return; @@ -1023,13 +1023,13 @@ if (Y_UNLIKELY(!(CONDITION))) { \ size_t snippetBeg = (off < 20) ? 0 : (off - 20); TStringBuf snippet = Data_.SubStr(snippetBeg, 40); - + throw TJsonDecodeError() << "cannot parse JSON, error at: " << off << ", reason: " << (ErrorMsg_.empty() ? reason : TStringBuf{ErrorMsg_}) << "\nsnippet: ..." << snippet << "..."; } - + bool OnEnd() override { return true; } @@ -1041,21 +1041,21 @@ if (Y_UNLIKELY(!(CONDITION))) { \ LastMetric_.Type = EMetricType::GAUGE; } else { LastMetric_.Type = EMetricType::HIST; - } + } } - + // (1) begin metric MetricConsumer_->OnMetricBegin(LastMetric_.Type); - + // (2) labels if (!LastMetric_.Labels.empty()) { MetricConsumer_->OnLabelsBegin(); for (auto&& label : LastMetric_.Labels) { MetricConsumer_->OnLabel(label.Name(), label.Value()); - } + } MetricConsumer_->OnLabelsEnd(); } - + // (3) values switch (LastMetric_.Type) { case EMetricType::GAUGE: @@ -1069,14 +1069,14 @@ if (Y_UNLIKELY(!(CONDITION))) { \ MetricConsumer_->OnInt64(time, value.AsInt64(valueType)); }); break; - + case EMetricType::COUNTER: case EMetricType::RATE: LastMetric_.Consume([this](TInstant time, EMetricValueType valueType, TMetricValue value) { MetricConsumer_->OnUint64(time, value.AsUint64(valueType)); }); break; - + case EMetricType::HIST: case EMetricType::HIST_RATE: if (LastMetric_.TimeSeries.empty()) { @@ -1087,10 +1087,10 @@ if (Y_UNLIKELY(!(CONDITION))) { \ for (const auto& p : LastMetric_.TimeSeries) { DECODE_ENSURE(p.GetValueType() == EMetricValueType::HISTOGRAM, "Value is not a histogram"); MetricConsumer_->OnHistogram(p.GetTime(), p.GetValue().AsHistogram()); - } - } + } + } break; - + case EMetricType::DSUMMARY: if (LastMetric_.TimeSeries.empty()) { auto time = LastMetric_.LastPoint.GetTime(); @@ -1103,7 +1103,7 @@ if (Y_UNLIKELY(!(CONDITION))) { \ } } break; - + case EMetricType::LOGHIST: if (LastMetric_.TimeSeries.empty()) { auto time = LastMetric_.LastPoint.GetTime(); @@ -1113,19 +1113,19 @@ if (Y_UNLIKELY(!(CONDITION))) { \ for (const auto& p : LastMetric_.TimeSeries) { DECODE_ENSURE(p.GetValueType() == EMetricValueType::LOGHISTOGRAM, "Value is not a log_histogram"); MetricConsumer_->OnLogHistogram(p.GetTime(), p.GetValue().AsLogHistogram()); - } - } + } + } break; - + case EMetricType::UNKNOWN: // TODO: output metric labels ythrow yexception() << "unknown metric type"; } - + // (4) end metric MetricConsumer_->OnMetricEnd(); } - + private: TStringBuf Data_; IHaltableMetricConsumer* MetricConsumer_; @@ -1136,9 +1136,9 @@ private: TString ErrorMsg_; bool IsIntentionallyHalted_{false}; }; - + } // namespace - + void DecodeJson(TStringBuf data, IMetricConsumer* c, TStringBuf metricNameLabel) { TCommonPartsCollector commonPartsCollector; { @@ -1159,4 +1159,4 @@ void DecodeJson(TStringBuf data, IMetricConsumer* c, TStringBuf metricNameLabel) #undef DECODE_ENSURE -} +} diff --git a/library/cpp/monlib/encode/json/json_encoder.cpp b/library/cpp/monlib/encode/json/json_encoder.cpp index 639177b5d1..20d2bb6283 100644 --- a/library/cpp/monlib/encode/json/json_encoder.cpp +++ b/library/cpp/monlib/encode/json/json_encoder.cpp @@ -1,39 +1,39 @@ -#include "json.h" -#include "typed_point.h" - -#include <library/cpp/monlib/encode/buffered/buffered_encoder_base.h> -#include <library/cpp/monlib/encode/encoder_state.h> -#include <library/cpp/monlib/metrics/metric.h> -#include <library/cpp/monlib/metrics/metric_value.h> -#include <library/cpp/monlib/metrics/labels.h> - +#include "json.h" +#include "typed_point.h" + +#include <library/cpp/monlib/encode/buffered/buffered_encoder_base.h> +#include <library/cpp/monlib/encode/encoder_state.h> +#include <library/cpp/monlib/metrics/metric.h> +#include <library/cpp/monlib/metrics/metric_value.h> +#include <library/cpp/monlib/metrics/labels.h> + #include <library/cpp/json/writer/json.h> - + #include <util/charset/utf8.h> #include <util/generic/algorithm.h> -namespace NMonitoring { - namespace { +namespace NMonitoring { + namespace { enum class EJsonStyle { Solomon, Cloud }; - /////////////////////////////////////////////////////////////////////// - // TJsonWriter - /////////////////////////////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////// + // TJsonWriter + /////////////////////////////////////////////////////////////////////// class TJsonWriter { - public: + public: TJsonWriter(IOutputStream* out, int indentation, EJsonStyle style, TStringBuf metricNameLabel) - : Buf_(NJsonWriter::HEM_UNSAFE, out) + : Buf_(NJsonWriter::HEM_UNSAFE, out) , Style_(style) , MetricNameLabel_(metricNameLabel) , CurrentMetricName_() - { - Buf_.SetIndentSpaces(indentation); - Buf_.SetWriteNanAsString(); - } - + { + Buf_.SetIndentSpaces(indentation); + Buf_.SetWriteNanAsString(); + } + void WriteTime(TInstant time) { if (time != TInstant::Zero()) { Buf_.WriteKey(TStringBuf("ts")); @@ -50,49 +50,49 @@ namespace NMonitoring { Buf_.WriteDouble(value); } - void WriteValue(i64 value) { + void WriteValue(i64 value) { Buf_.WriteKey(TStringBuf("value")); - Buf_.WriteLongLong(value); - } - + Buf_.WriteLongLong(value); + } + void WriteValue(ui64 value) { Buf_.WriteKey(TStringBuf("value")); Buf_.WriteULongLong(value); } - void WriteValue(IHistogramSnapshot* s) { + void WriteValue(IHistogramSnapshot* s) { Y_ENSURE(Style_ == EJsonStyle::Solomon); Buf_.WriteKey(TStringBuf("hist")); - Buf_.BeginObject(); - if (ui32 count = s->Count()) { - bool hasInf = (s->UpperBound(count - 1) == Max<double>()); - if (hasInf) { - count--; - } - + Buf_.BeginObject(); + if (ui32 count = s->Count()) { + bool hasInf = (s->UpperBound(count - 1) == Max<double>()); + if (hasInf) { + count--; + } + Buf_.WriteKey(TStringBuf("bounds")); - Buf_.BeginList(); - for (ui32 i = 0; i < count; i++) { - Buf_.WriteDouble(s->UpperBound(i)); - } - Buf_.EndList(); - + Buf_.BeginList(); + for (ui32 i = 0; i < count; i++) { + Buf_.WriteDouble(s->UpperBound(i)); + } + Buf_.EndList(); + Buf_.WriteKey(TStringBuf("buckets")); - Buf_.BeginList(); - for (ui32 i = 0; i < count; i++) { - Buf_.WriteULongLong(s->Value(i)); - } - Buf_.EndList(); - - if (hasInf) { + Buf_.BeginList(); + for (ui32 i = 0; i < count; i++) { + Buf_.WriteULongLong(s->Value(i)); + } + Buf_.EndList(); + + if (hasInf) { Buf_.WriteKey(TStringBuf("inf")); - Buf_.WriteULongLong(s->Value(count)); - } - } - Buf_.EndObject(); - } - + Buf_.WriteULongLong(s->Value(count)); + } + } + Buf_.EndObject(); + } + void WriteValue(ISummaryDoubleSnapshot* s) { Y_ENSURE(Style_ == EJsonStyle::Solomon); @@ -142,25 +142,25 @@ namespace NMonitoring { Buf_.EndObject(); } - void WriteValue(EMetricValueType type, TMetricValue value) { + void WriteValue(EMetricValueType type, TMetricValue value) { switch (type) { - case EMetricValueType::DOUBLE: - WriteValue(value.AsDouble()); + case EMetricValueType::DOUBLE: + WriteValue(value.AsDouble()); + break; + + case EMetricValueType::INT64: + WriteValue(value.AsInt64()); + break; + + case EMetricValueType::UINT64: + WriteValue(value.AsUint64()); break; - case EMetricValueType::INT64: - WriteValue(value.AsInt64()); - break; - - case EMetricValueType::UINT64: - WriteValue(value.AsUint64()); + case EMetricValueType::HISTOGRAM: + WriteValue(value.AsHistogram()); break; - case EMetricValueType::HISTOGRAM: - WriteValue(value.AsHistogram()); - break; - - case EMetricValueType::SUMMARY: + case EMetricValueType::SUMMARY: WriteValue(value.AsSummaryDouble()); break; @@ -168,7 +168,7 @@ namespace NMonitoring { WriteValue(value.AsLogHistogram()); break; - case EMetricValueType::UNKNOWN: + case EMetricValueType::UNKNOWN: ythrow yexception() << "unknown metric value type"; } } @@ -229,10 +229,10 @@ namespace NMonitoring { TString CurrentMetricName_; }; - /////////////////////////////////////////////////////////////////////// - // TEncoderJson - /////////////////////////////////////////////////////////////////////// - class TEncoderJson final: public IMetricEncoder, public TJsonWriter { + /////////////////////////////////////////////////////////////////////// + // TEncoderJson + /////////////////////////////////////////////////////////////////////// + class TEncoderJson final: public IMetricEncoder, public TJsonWriter { public: TEncoderJson(IOutputStream* out, int indentation, EJsonStyle style, TStringBuf metricNameLabel) : TJsonWriter{out, indentation, style, metricNameLabel} @@ -240,202 +240,202 @@ namespace NMonitoring { } ~TEncoderJson() override { - Close(); - } - - private: - void OnStreamBegin() override { + Close(); + } + + private: + void OnStreamBegin() override { State_.Expect(TEncoderState::EState::ROOT); - Buf_.BeginObject(); - } - - void OnStreamEnd() override { + Buf_.BeginObject(); + } + + void OnStreamEnd() override { State_.Expect(TEncoderState::EState::ROOT); - if (!Buf_.KeyExpected()) { - // not closed metrics array - Buf_.EndList(); - } - Buf_.EndObject(); - } - - void OnCommonTime(TInstant time) override { + if (!Buf_.KeyExpected()) { + // not closed metrics array + Buf_.EndList(); + } + Buf_.EndObject(); + } + + void OnCommonTime(TInstant time) override { State_.Expect(TEncoderState::EState::ROOT); - WriteTime(time); - } - - void OnMetricBegin(EMetricType type) override { - State_.Switch(TEncoderState::EState::ROOT, TEncoderState::EState::METRIC); - if (Buf_.KeyExpected()) { - // first metric, so open metrics array + WriteTime(time); + } + + void OnMetricBegin(EMetricType type) override { + State_.Switch(TEncoderState::EState::ROOT, TEncoderState::EState::METRIC); + if (Buf_.KeyExpected()) { + // first metric, so open metrics array Buf_.WriteKey(TStringBuf(Style_ == EJsonStyle::Solomon ? "sensors" : "metrics")); - Buf_.BeginList(); - } - Buf_.BeginObject(); + Buf_.BeginList(); + } + Buf_.BeginObject(); WriteMetricType(type); - } - - void OnMetricEnd() override { - State_.Switch(TEncoderState::EState::METRIC, TEncoderState::EState::ROOT); - if (!Buf_.KeyExpected()) { - // not closed timeseries array - Buf_.EndList(); - } - - if (!TimeSeries_ && LastPoint_.HasValue()) { - // we have seen only one point between OnMetricBegin() and - // OnMetricEnd() calls - WriteTime(LastPoint_.GetTime()); - WriteValue(LastPoint_.GetValueType(), LastPoint_.GetValue()); - } - Buf_.EndObject(); - - LastPoint_ = {}; - TimeSeries_ = false; - } - - void OnLabelsBegin() override { - if (!Buf_.KeyExpected()) { - // not closed metrics or timeseries array if labels go after values - Buf_.EndList(); - } + } + + void OnMetricEnd() override { + State_.Switch(TEncoderState::EState::METRIC, TEncoderState::EState::ROOT); + if (!Buf_.KeyExpected()) { + // not closed timeseries array + Buf_.EndList(); + } + + if (!TimeSeries_ && LastPoint_.HasValue()) { + // we have seen only one point between OnMetricBegin() and + // OnMetricEnd() calls + WriteTime(LastPoint_.GetTime()); + WriteValue(LastPoint_.GetValueType(), LastPoint_.GetValue()); + } + Buf_.EndObject(); + + LastPoint_ = {}; + TimeSeries_ = false; + } + + void OnLabelsBegin() override { + if (!Buf_.KeyExpected()) { + // not closed metrics or timeseries array if labels go after values + Buf_.EndList(); + } if (State_ == TEncoderState::EState::ROOT) { State_ = TEncoderState::EState::COMMON_LABELS; Buf_.WriteKey(TStringBuf(Style_ == EJsonStyle::Solomon ? "commonLabels" : "labels")); - } else if (State_ == TEncoderState::EState::METRIC) { - State_ = TEncoderState::EState::METRIC_LABELS; + } else if (State_ == TEncoderState::EState::METRIC) { + State_ = TEncoderState::EState::METRIC_LABELS; Buf_.WriteKey(TStringBuf("labels")); - } else { - State_.ThrowInvalid("expected METRIC or ROOT"); - } - Buf_.BeginObject(); + } else { + State_.ThrowInvalid("expected METRIC or ROOT"); + } + Buf_.BeginObject(); EmptyLabels_ = true; - } - - void OnLabelsEnd() override { - if (State_ == TEncoderState::EState::METRIC_LABELS) { - State_ = TEncoderState::EState::METRIC; + } + + void OnLabelsEnd() override { + if (State_ == TEncoderState::EState::METRIC_LABELS) { + State_ = TEncoderState::EState::METRIC; } else if (State_ == TEncoderState::EState::COMMON_LABELS) { State_ = TEncoderState::EState::ROOT; - } else { - State_.ThrowInvalid("expected LABELS or COMMON_LABELS"); - } + } else { + State_.ThrowInvalid("expected LABELS or COMMON_LABELS"); + } Y_ENSURE(!EmptyLabels_, "Labels cannot be empty"); - Buf_.EndObject(); + Buf_.EndObject(); if (State_ == TEncoderState::EState::METRIC) { WriteName(); } - } - - void OnLabel(TStringBuf name, TStringBuf value) override { - if (State_ == TEncoderState::EState::METRIC_LABELS || State_ == TEncoderState::EState::COMMON_LABELS) { + } + + void OnLabel(TStringBuf name, TStringBuf value) override { + if (State_ == TEncoderState::EState::METRIC_LABELS || State_ == TEncoderState::EState::COMMON_LABELS) { WriteLabel(name, value); - } else { - State_.ThrowInvalid("expected LABELS or COMMON_LABELS"); - } + } else { + State_.ThrowInvalid("expected LABELS or COMMON_LABELS"); + } EmptyLabels_ = false; - } - - void OnDouble(TInstant time, double value) override { - State_.Expect(TEncoderState::EState::METRIC); - Write<double>(time, value); - } - - void OnInt64(TInstant time, i64 value) override { - State_.Expect(TEncoderState::EState::METRIC); - Write<i64>(time, value); - } - - void OnUint64(TInstant time, ui64 value) override { - State_.Expect(TEncoderState::EState::METRIC); - Write<ui64>(time, value); - } - - void OnHistogram(TInstant time, IHistogramSnapshotPtr snapshot) override { - State_.Expect(TEncoderState::EState::METRIC); - Write<IHistogramSnapshot*>(time, snapshot.Get()); - } - + } + + void OnDouble(TInstant time, double value) override { + State_.Expect(TEncoderState::EState::METRIC); + Write<double>(time, value); + } + + void OnInt64(TInstant time, i64 value) override { + State_.Expect(TEncoderState::EState::METRIC); + Write<i64>(time, value); + } + + void OnUint64(TInstant time, ui64 value) override { + State_.Expect(TEncoderState::EState::METRIC); + Write<ui64>(time, value); + } + + void OnHistogram(TInstant time, IHistogramSnapshotPtr snapshot) override { + State_.Expect(TEncoderState::EState::METRIC); + Write<IHistogramSnapshot*>(time, snapshot.Get()); + } + void OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr snapshot) override { - State_.Expect(TEncoderState::EState::METRIC); + State_.Expect(TEncoderState::EState::METRIC); Write<ISummaryDoubleSnapshot*>(time, snapshot.Get()); } void OnLogHistogram(TInstant time, TLogHistogramSnapshotPtr snapshot) override { - State_.Expect(TEncoderState::EState::METRIC); + State_.Expect(TEncoderState::EState::METRIC); Write<TLogHistogramSnapshot*>(time, snapshot.Get()); } - template <typename T> - void Write(TInstant time, T value) { - State_.Expect(TEncoderState::EState::METRIC); - - if (!LastPoint_.HasValue()) { - LastPoint_ = {time, value}; - } else { - // second point - // TODO: output types - Y_ENSURE(LastPoint_.GetValueType() == TValueType<T>::Type, - "mixed metric value types in one metric"); - - if (!TimeSeries_) { + template <typename T> + void Write(TInstant time, T value) { + State_.Expect(TEncoderState::EState::METRIC); + + if (!LastPoint_.HasValue()) { + LastPoint_ = {time, value}; + } else { + // second point + // TODO: output types + Y_ENSURE(LastPoint_.GetValueType() == TValueType<T>::Type, + "mixed metric value types in one metric"); + + if (!TimeSeries_) { Buf_.WriteKey(TStringBuf("timeseries")); - Buf_.BeginList(); - Buf_.BeginObject(); - Y_ENSURE(LastPoint_.GetTime() != TInstant::Zero(), + Buf_.BeginList(); + Buf_.BeginObject(); + Y_ENSURE(LastPoint_.GetTime() != TInstant::Zero(), "time cannot be empty or zero in a timeseries point"); - WriteTime(LastPoint_.GetTime()); - WriteValue(LastPoint_.GetValueType(), LastPoint_.GetValue()); - Buf_.EndObject(); - TimeSeries_ = true; - } - - if (TimeSeries_) { - Buf_.BeginObject(); + WriteTime(LastPoint_.GetTime()); + WriteValue(LastPoint_.GetValueType(), LastPoint_.GetValue()); + Buf_.EndObject(); + TimeSeries_ = true; + } + + if (TimeSeries_) { + Buf_.BeginObject(); Y_ENSURE(time != TInstant::Zero(), "time cannot be empty or zero in a timeseries point"); - - WriteTime(time); - WriteValue(value); - Buf_.EndObject(); - } - } - } - - void Close() override { - LastPoint_ = {}; - } - + + WriteTime(time); + WriteValue(value); + Buf_.EndObject(); + } + } + } + + void Close() override { + LastPoint_ = {}; + } + private: TEncoderState State_; - TTypedPoint LastPoint_; + TTypedPoint LastPoint_; bool TimeSeries_ = false; bool EmptyLabels_ = false; }; - /////////////////////////////////////////////////////////////////////// - // TBufferedJsonEncoder - /////////////////////////////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////// + // TBufferedJsonEncoder + /////////////////////////////////////////////////////////////////////// class TBufferedJsonEncoder : public TBufferedEncoderBase, public TJsonWriter { public: TBufferedJsonEncoder(IOutputStream* out, int indentation, EJsonStyle style, TStringBuf metricNameLabel) : TJsonWriter{out, indentation, style, metricNameLabel} { - MetricsMergingMode_ = EMetricsMergingMode::MERGE_METRICS; - } - + MetricsMergingMode_ = EMetricsMergingMode::MERGE_METRICS; + } + ~TBufferedJsonEncoder() override { Close(); - } - + } + void OnLabelsBegin() override { TBufferedEncoderBase::OnLabelsBegin(); EmptyLabels_ = true; } - void OnLabel(TStringBuf name, TStringBuf value) override { + void OnLabel(TStringBuf name, TStringBuf value) override { TBufferedEncoderBase::OnLabel(name, value); EmptyLabels_ = false; } @@ -456,36 +456,36 @@ namespace NMonitoring { } Closed_ = true; - + LabelValuesPool_.Build(); LabelNamesPool_.Build(); - + Buf_.BeginObject(); - + WriteTime(CommonTime_); if (CommonLabels_.size() > 0) { Buf_.WriteKey(TStringBuf(Style_ == EJsonStyle::Solomon ? "commonLabels": "labels")); WriteLabels(CommonLabels_, true); - } + } - if (Metrics_.size() > 0) { + if (Metrics_.size() > 0) { Buf_.WriteKey(TStringBuf(Style_ == EJsonStyle::Solomon ? "sensors" : "metrics")); - WriteMetrics(); + WriteMetrics(); } Buf_.EndObject(); - } - - private: - void WriteMetrics() { + } + + private: + void WriteMetrics() { Buf_.BeginList(); - for (auto&& metric : Metrics_) { - WriteMetric(metric); + for (auto&& metric : Metrics_) { + WriteMetric(metric); } Buf_.EndList(); } - void WriteMetric(TMetric& metric) { + void WriteMetric(TMetric& metric) { Buf_.BeginObject(); WriteMetricType(metric.MetricType); @@ -493,19 +493,19 @@ namespace NMonitoring { Buf_.WriteKey(TStringBuf("labels")); WriteLabels(metric.Labels, false); - metric.TimeSeries.SortByTs(); - if (metric.TimeSeries.Size() == 1) { - const auto& point = metric.TimeSeries[0]; - WriteTime(point.GetTime()); - WriteValue(metric.TimeSeries.GetValueType(), point.GetValue()); - } else if (metric.TimeSeries.Size() > 1) { + metric.TimeSeries.SortByTs(); + if (metric.TimeSeries.Size() == 1) { + const auto& point = metric.TimeSeries[0]; + WriteTime(point.GetTime()); + WriteValue(metric.TimeSeries.GetValueType(), point.GetValue()); + } else if (metric.TimeSeries.Size() > 1) { Buf_.WriteKey(TStringBuf("timeseries")); Buf_.BeginList(); - metric.TimeSeries.ForEach([this](TInstant time, EMetricValueType type, TMetricValue value) { + metric.TimeSeries.ForEach([this](TInstant time, EMetricValueType type, TMetricValue value) { Buf_.BeginObject(); // make gcc 6.1 happy https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61636 - this->WriteTime(time); - this->WriteValue(type, value); + this->WriteTime(time); + this->WriteValue(type, value); Buf_.EndObject(); }); @@ -535,14 +535,14 @@ namespace NMonitoring { private: bool Closed_{false}; bool EmptyLabels_ = false; - }; - } - - IMetricEncoderPtr EncoderJson(IOutputStream* out, int indentation) { + }; + } + + IMetricEncoderPtr EncoderJson(IOutputStream* out, int indentation) { return MakeHolder<TEncoderJson>(out, indentation, EJsonStyle::Solomon, ""); - } - - IMetricEncoderPtr BufferedEncoderJson(IOutputStream* out, int indentation) { + } + + IMetricEncoderPtr BufferedEncoderJson(IOutputStream* out, int indentation) { return MakeHolder<TBufferedJsonEncoder>(out, indentation, EJsonStyle::Solomon, ""); } @@ -553,4 +553,4 @@ namespace NMonitoring { IMetricEncoderPtr BufferedEncoderCloudJson(IOutputStream* out, int indentation, TStringBuf metricNameLabel) { return MakeHolder<TBufferedJsonEncoder>(out, indentation, EJsonStyle::Cloud, metricNameLabel); } -} +} diff --git a/library/cpp/monlib/encode/json/json_ut.cpp b/library/cpp/monlib/encode/json/json_ut.cpp index f80105795b..09e7909289 100644 --- a/library/cpp/monlib/encode/json/json_ut.cpp +++ b/library/cpp/monlib/encode/json/json_ut.cpp @@ -1,19 +1,19 @@ -#include "json.h" - -#include <library/cpp/monlib/encode/protobuf/protobuf.h> -#include <library/cpp/monlib/metrics/labels.h> - +#include "json.h" + +#include <library/cpp/monlib/encode/protobuf/protobuf.h> +#include <library/cpp/monlib/metrics/labels.h> + #include <library/cpp/json/json_reader.h> #include <library/cpp/resource/resource.h> #include <library/cpp/testing/unittest/registar.h> - -#include <util/stream/str.h> + +#include <util/stream/str.h> #include <util/string/builder.h> - -#include <limits> - -using namespace NMonitoring; - + +#include <limits> + +using namespace NMonitoring; + namespace NMonitoring { bool operator<(const TLabel& lhs, const TLabel& rhs) { return lhs.Name() < rhs.Name() || @@ -134,7 +134,7 @@ namespace { 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) { TString json; @@ -145,12 +145,12 @@ Y_UNIT_TEST_SUITE(TJsonTest) { e->OnStreamBegin(); { // common time e->OnCommonTime(TInstant::Seconds(1500000000)); - } + } { // common labels - e->OnLabelsBegin(); + e->OnLabelsBegin(); e->OnLabel("project", "solomon"); - e->OnLabelsEnd(); - } + e->OnLabelsEnd(); + } { // metric #1 e->OnMetricBegin(EMetricType::COUNTER); { @@ -161,7 +161,7 @@ Y_UNIT_TEST_SUITE(TJsonTest) { } e->OnUint64(now, 17); e->OnMetricEnd(); - } + } { // metric #2 e->OnMetricBegin(EMetricType::RATE); { @@ -172,7 +172,7 @@ Y_UNIT_TEST_SUITE(TJsonTest) { } e->OnUint64(now, 17); e->OnMetricEnd(); - } + } { // metric #3 e->OnMetricBegin(EMetricType::GAUGE); e->OnDouble(now, 3.14); @@ -251,7 +251,7 @@ Y_UNIT_TEST_SUITE(TJsonTest) { auto emit = [&](IMetricEncoder* encoder, EMetricType metricType) { encoder->OnStreamBegin(); - { + { encoder->OnMetricBegin(metricType); { encoder->OnLabelsBegin(); @@ -280,11 +280,11 @@ Y_UNIT_TEST_SUITE(TJsonTest) { } encoder->OnMetricEnd(); - } + } encoder->OnStreamEnd(); encoder->Close(); }; - + auto doTest = [&](bool buffered, EMetricType metricType) { TString json; TStringOutput out(json); @@ -301,107 +301,107 @@ Y_UNIT_TEST_SUITE(TJsonTest) { doTest(true, EMetricType::HIST); doTest(true, EMetricType::LOGHIST); doTest(true, EMetricType::DSUMMARY); - } - - Y_UNIT_TEST(MetricsWithDifferentLabelOrderGetMerged) { + } + + Y_UNIT_TEST(MetricsWithDifferentLabelOrderGetMerged) { TString json; TStringOutput out(json); auto e = BufferedEncoderJson(&out, 2); - + e->OnStreamBegin(); { - e->OnMetricBegin(EMetricType::RATE); + e->OnMetricBegin(EMetricType::RATE); { e->OnLabelsBegin(); - e->OnLabel("metric", "hello"); + e->OnLabel("metric", "hello"); e->OnLabel("label", "world"); e->OnLabelsEnd(); } e->OnUint64(TInstant::Zero(), 0); - e->OnMetricEnd(); - } + e->OnMetricEnd(); + } { - e->OnMetricBegin(EMetricType::RATE); + e->OnMetricBegin(EMetricType::RATE); { e->OnLabelsBegin(); e->OnLabel("label", "world"); - e->OnLabel("metric", "hello"); + e->OnLabel("metric", "hello"); e->OnLabelsEnd(); } e->OnUint64(TInstant::Zero(), 1); - e->OnMetricEnd(); + e->OnMetricEnd(); } e->OnStreamEnd(); e->Close(); json += "\n"; - + TString expectedJson = NResource::Find("/merged.json"); // we cannot be sure regarding the label order in the result, // so we'll have to parse the expected value and then compare it with actual - + NProto::TMultiSamplesList samples; - IMetricEncoderPtr d = EncoderProtobuf(&samples); + IMetricEncoderPtr d = EncoderProtobuf(&samples); DecodeJson(expectedJson, d.Get()); - + UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 1); { const NProto::TMultiSample& s = samples.GetSamples(0); - UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::RATE); - AssertLabels(s, TLabels{{"metric", "hello"}, {"label", "world"}}); - + UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::RATE); + AssertLabels(s, TLabels{{"metric", "hello"}, {"label", "world"}}); + UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); AssertPointEqual(s.GetPoints(0), TInstant::Zero(), ui64(1)); - } - } + } + } Y_UNIT_TEST(Decode1) { - NProto::TMultiSamplesList samples; - { - IMetricEncoderPtr e = EncoderProtobuf(&samples); - - TString testJson = NResource::Find("/expected.json"); - DecodeJson(testJson, e.Get()); - } - - UNIT_ASSERT_VALUES_EQUAL( - TInstant::MilliSeconds(samples.GetCommonTime()), - TInstant::Seconds(1500000000)); - - UNIT_ASSERT_VALUES_EQUAL(samples.CommonLabelsSize(), 1); - AssertLabelEqual(samples.GetCommonLabels(0), "project", "solomon"); - + NProto::TMultiSamplesList samples; + { + IMetricEncoderPtr e = EncoderProtobuf(&samples); + + TString testJson = NResource::Find("/expected.json"); + DecodeJson(testJson, e.Get()); + } + + UNIT_ASSERT_VALUES_EQUAL( + TInstant::MilliSeconds(samples.GetCommonTime()), + TInstant::Seconds(1500000000)); + + UNIT_ASSERT_VALUES_EQUAL(samples.CommonLabelsSize(), 1); + AssertLabelEqual(samples.GetCommonLabels(0), "project", "solomon"); + UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 6); - { - const NProto::TMultiSample& s = samples.GetSamples(0); - UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::COUNTER); - UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 2); - AssertLabelEqual(s.GetLabels(0), "metric", "single"); + { + const NProto::TMultiSample& s = samples.GetSamples(0); + UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::COUNTER); + UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 2); + AssertLabelEqual(s.GetLabels(0), "metric", "single"); AssertLabelEqual(s.GetLabels(1), "labels", "l1"); - - UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); - AssertPointEqual(s.GetPoints(0), now, ui64(17)); - } - { - const NProto::TMultiSample& s = samples.GetSamples(1); - UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::RATE); - UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 2); - AssertLabelEqual(s.GetLabels(0), "metric", "single"); + + UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); + AssertPointEqual(s.GetPoints(0), now, ui64(17)); + } + { + const NProto::TMultiSample& s = samples.GetSamples(1); + UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::RATE); + UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 2); + AssertLabelEqual(s.GetLabels(0), "metric", "single"); AssertLabelEqual(s.GetLabels(1), "labels", "l2"); - - UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); - AssertPointEqual(s.GetPoints(0), now, ui64(17)); - } - { - const NProto::TMultiSample& s = samples.GetSamples(2); - UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE); - UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 2); - AssertLabelEqual(s.GetLabels(0), "metric", "single"); + + UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); + AssertPointEqual(s.GetPoints(0), now, ui64(17)); + } + { + const NProto::TMultiSample& s = samples.GetSamples(2); + UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE); + UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 2); + AssertLabelEqual(s.GetLabels(0), "metric", "single"); AssertLabelEqual(s.GetLabels(1), "labels", "l3"); - - UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); - AssertPointEqual(s.GetPoints(0), now, 3.14); - } - { - const NProto::TMultiSample& s = samples.GetSamples(3); + + UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); + AssertPointEqual(s.GetPoints(0), now, 3.14); + } + { + const NProto::TMultiSample& s = samples.GetSamples(3); UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::IGAUGE); UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 2); AssertLabelEqual(s.GetLabels(0), "metric", "single_igauge"); @@ -412,152 +412,152 @@ Y_UNIT_TEST_SUITE(TJsonTest) { } { const NProto::TMultiSample& s = samples.GetSamples(4); - UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE); - UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 2); - AssertLabelEqual(s.GetLabels(0), "metric", "multiple"); + UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE); + UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 2); + AssertLabelEqual(s.GetLabels(0), "metric", "multiple"); AssertLabelEqual(s.GetLabels(1), "labels", "l5"); - - UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 3); - AssertPointEqualNan(s.GetPoints(0), now); - AssertPointEqualInf(s.GetPoints(1), now + TDuration::Seconds(15), 1); - AssertPointEqualInf(s.GetPoints(2), now + TDuration::Seconds(30), -11); - } - { + + UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 3); + AssertPointEqualNan(s.GetPoints(0), now); + AssertPointEqualInf(s.GetPoints(1), now + TDuration::Seconds(15), 1); + AssertPointEqualInf(s.GetPoints(2), now + TDuration::Seconds(30), -11); + } + { const NProto::TMultiSample& s = samples.GetSamples(5); - UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::COUNTER); - UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 2); - AssertLabelEqual(s.GetLabels(0), "metric", "multiple"); + UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::COUNTER); + UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 2); + AssertLabelEqual(s.GetLabels(0), "metric", "multiple"); AssertLabelEqual(s.GetLabels(1), "labels", "l6"); - - UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 2); - AssertPointEqual(s.GetPoints(0), now, ui64(1337)); - AssertPointEqual(s.GetPoints(1), now + TDuration::Seconds(15), ui64(1338)); - } - } - - Y_UNIT_TEST(DecodeMetrics) { - NProto::TMultiSamplesList samples; - { - IMetricEncoderPtr e = EncoderProtobuf(&samples); - - TString metricsJson = NResource::Find("/metrics.json"); - DecodeJson(metricsJson, e.Get()); - } - - UNIT_ASSERT_VALUES_EQUAL( - TInstant::MilliSeconds(samples.GetCommonTime()), + + UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 2); + AssertPointEqual(s.GetPoints(0), now, ui64(1337)); + AssertPointEqual(s.GetPoints(1), now + TDuration::Seconds(15), ui64(1338)); + } + } + + Y_UNIT_TEST(DecodeMetrics) { + NProto::TMultiSamplesList samples; + { + IMetricEncoderPtr e = EncoderProtobuf(&samples); + + TString metricsJson = NResource::Find("/metrics.json"); + DecodeJson(metricsJson, e.Get()); + } + + UNIT_ASSERT_VALUES_EQUAL( + TInstant::MilliSeconds(samples.GetCommonTime()), TInstant::ParseIso8601Deprecated("2017-08-27T12:34:56Z")); - - UNIT_ASSERT_VALUES_EQUAL(samples.CommonLabelsSize(), 3); - AssertLabelEqual(samples.GetCommonLabels(0), "project", "solomon"); - AssertLabelEqual(samples.GetCommonLabels(1), "cluster", "man"); - AssertLabelEqual(samples.GetCommonLabels(2), "service", "stockpile"); - + + UNIT_ASSERT_VALUES_EQUAL(samples.CommonLabelsSize(), 3); + AssertLabelEqual(samples.GetCommonLabels(0), "project", "solomon"); + AssertLabelEqual(samples.GetCommonLabels(1), "cluster", "man"); + AssertLabelEqual(samples.GetCommonLabels(2), "service", "stockpile"); + UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 4); - { - const NProto::TMultiSample& s = samples.GetSamples(0); - UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE); - UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); - AssertLabelEqual(s.GetLabels(0), "metric", "Memory"); - - UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); - AssertPointEqual(s.GetPoints(0), TInstant::Zero(), 10.0); - } - { - const NProto::TMultiSample& s = samples.GetSamples(1); - UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::RATE); - UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); - AssertLabelEqual(s.GetLabels(0), "metric", "UserTime"); - - UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); - AssertPointEqual(s.GetPoints(0), TInstant::Zero(), ui64(1)); - } - { - const NProto::TMultiSample& s = samples.GetSamples(2); - UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE); - UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 2); - AssertLabelEqual(s.GetLabels(0), "export", "Oxygen"); - AssertLabelEqual(s.GetLabels(1), "metric", "QueueSize"); - - UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); + { + const NProto::TMultiSample& s = samples.GetSamples(0); + UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE); + UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); + AssertLabelEqual(s.GetLabels(0), "metric", "Memory"); + + UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); + AssertPointEqual(s.GetPoints(0), TInstant::Zero(), 10.0); + } + { + const NProto::TMultiSample& s = samples.GetSamples(1); + UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::RATE); + UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); + AssertLabelEqual(s.GetLabels(0), "metric", "UserTime"); + + UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); + AssertPointEqual(s.GetPoints(0), TInstant::Zero(), ui64(1)); + } + { + const NProto::TMultiSample& s = samples.GetSamples(2); + UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE); + UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 2); + AssertLabelEqual(s.GetLabels(0), "export", "Oxygen"); + AssertLabelEqual(s.GetLabels(1), "metric", "QueueSize"); + + UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); auto ts = TInstant::ParseIso8601Deprecated("2017-11-05T12:34:56.000Z"); - AssertPointEqual(s.GetPoints(0), ts, 3.14159); - } - { + AssertPointEqual(s.GetPoints(0), ts, 3.14159); + } + { const NProto::TMultiSample& s = samples.GetSamples(3); - UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE); - UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); - AssertLabelEqual(s.GetLabels(0), "metric", "Writes"); - - UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 2); + UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE); + UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); + AssertLabelEqual(s.GetLabels(0), "metric", "Writes"); + + UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 2); auto ts1 = TInstant::ParseIso8601Deprecated("2017-08-28T12:32:11Z"); - AssertPointEqual(s.GetPoints(0), ts1, -10.0); - auto ts2 = TInstant::Seconds(1503923187); - AssertPointEqual(s.GetPoints(1), ts2, 20.0); - } - } - - Y_UNIT_TEST(DecodeSensors) { - NProto::TMultiSamplesList samples; - { - IMetricEncoderPtr e = EncoderProtobuf(&samples); - - TString sensorsJson = NResource::Find("/sensors.json"); - DecodeJson(sensorsJson, e.Get()); - } - - UNIT_ASSERT_VALUES_EQUAL( - TInstant::MilliSeconds(samples.GetCommonTime()), - TInstant::ParseIso8601Deprecated("2017-08-27T12:34:56Z")); - - UNIT_ASSERT_VALUES_EQUAL(samples.CommonLabelsSize(), 3); - AssertLabelEqual(samples.GetCommonLabels(0), "project", "solomon"); - AssertLabelEqual(samples.GetCommonLabels(1), "cluster", "man"); - AssertLabelEqual(samples.GetCommonLabels(2), "service", "stockpile"); - - UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 4); - { - const NProto::TMultiSample& s = samples.GetSamples(0); - UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE); - UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); - AssertLabelEqual(s.GetLabels(0), "metric", "Memory"); - - UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); - AssertPointEqual(s.GetPoints(0), TInstant::Zero(), 10.0); - } - { - const NProto::TMultiSample& s = samples.GetSamples(1); - UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::RATE); - UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); - AssertLabelEqual(s.GetLabels(0), "metric", "UserTime"); - - UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); - AssertPointEqual(s.GetPoints(0), TInstant::Zero(), ui64(1)); - } - { - const NProto::TMultiSample& s = samples.GetSamples(2); - UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE); - UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 2); - AssertLabelEqual(s.GetLabels(0), "export", "Oxygen"); - AssertLabelEqual(s.GetLabels(1), "metric", "QueueSize"); - - UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); - auto ts = TInstant::ParseIso8601Deprecated("2017-11-05T12:34:56.000Z"); - AssertPointEqual(s.GetPoints(0), ts, 3.14159); - } - { - const NProto::TMultiSample& s = samples.GetSamples(3); - UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE); - UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); - AssertLabelEqual(s.GetLabels(0), "metric", "Writes"); - - UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 2); - auto ts1 = TInstant::ParseIso8601Deprecated("2017-08-28T12:32:11Z"); - AssertPointEqual(s.GetPoints(0), ts1, -10.0); - auto ts2 = TInstant::Seconds(1503923187); - AssertPointEqual(s.GetPoints(1), ts2, 20.0); - } - } + AssertPointEqual(s.GetPoints(0), ts1, -10.0); + auto ts2 = TInstant::Seconds(1503923187); + AssertPointEqual(s.GetPoints(1), ts2, 20.0); + } + } + + Y_UNIT_TEST(DecodeSensors) { + NProto::TMultiSamplesList samples; + { + IMetricEncoderPtr e = EncoderProtobuf(&samples); + + TString sensorsJson = NResource::Find("/sensors.json"); + DecodeJson(sensorsJson, e.Get()); + } + + UNIT_ASSERT_VALUES_EQUAL( + TInstant::MilliSeconds(samples.GetCommonTime()), + TInstant::ParseIso8601Deprecated("2017-08-27T12:34:56Z")); + + UNIT_ASSERT_VALUES_EQUAL(samples.CommonLabelsSize(), 3); + AssertLabelEqual(samples.GetCommonLabels(0), "project", "solomon"); + AssertLabelEqual(samples.GetCommonLabels(1), "cluster", "man"); + AssertLabelEqual(samples.GetCommonLabels(2), "service", "stockpile"); + + UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 4); + { + const NProto::TMultiSample& s = samples.GetSamples(0); + UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE); + UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); + AssertLabelEqual(s.GetLabels(0), "metric", "Memory"); + + UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); + AssertPointEqual(s.GetPoints(0), TInstant::Zero(), 10.0); + } + { + const NProto::TMultiSample& s = samples.GetSamples(1); + UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::RATE); + UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); + AssertLabelEqual(s.GetLabels(0), "metric", "UserTime"); + + UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); + AssertPointEqual(s.GetPoints(0), TInstant::Zero(), ui64(1)); + } + { + const NProto::TMultiSample& s = samples.GetSamples(2); + UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE); + UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 2); + AssertLabelEqual(s.GetLabels(0), "export", "Oxygen"); + AssertLabelEqual(s.GetLabels(1), "metric", "QueueSize"); + + UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); + auto ts = TInstant::ParseIso8601Deprecated("2017-11-05T12:34:56.000Z"); + AssertPointEqual(s.GetPoints(0), ts, 3.14159); + } + { + const NProto::TMultiSample& s = samples.GetSamples(3); + UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE); + UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); + AssertLabelEqual(s.GetLabels(0), "metric", "Writes"); + + UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 2); + auto ts1 = TInstant::ParseIso8601Deprecated("2017-08-28T12:32:11Z"); + AssertPointEqual(s.GetPoints(0), ts1, -10.0); + auto ts2 = TInstant::Seconds(1503923187); + AssertPointEqual(s.GetPoints(1), ts2, 20.0); + } + } Y_UNIT_TEST(DecodeToEncoder) { auto testJson = NResource::Find("/test_decode_to_encode.json"); @@ -574,16 +574,16 @@ Y_UNIT_TEST_SUITE(TJsonTest) { UNIT_ASSERT_VALUES_EQUAL(val1, val2); } - void WriteEmptySeries(const IMetricEncoderPtr& e) { + void WriteEmptySeries(const IMetricEncoderPtr& e) { e->OnStreamBegin(); { - e->OnMetricBegin(EMetricType::COUNTER); + e->OnMetricBegin(EMetricType::COUNTER); { e->OnLabelsBegin(); e->OnLabel("foo", "bar"); e->OnLabelsEnd(); } - e->OnMetricEnd(); + e->OnMetricEnd(); } e->OnStreamEnd(); @@ -602,9 +602,9 @@ Y_UNIT_TEST_SUITE(TJsonTest) { UNIT_ASSERT_NO_DIFF(json, expectedJson); } - void WriteEmptyLabels(IMetricEncoderPtr& e) { + void WriteEmptyLabels(IMetricEncoderPtr& e) { e->OnStreamBegin(); - e->OnMetricBegin(EMetricType::COUNTER); + e->OnMetricBegin(EMetricType::COUNTER); e->OnLabelsBegin(); UNIT_ASSERT_EXCEPTION(e->OnLabelsEnd(), yexception); @@ -638,29 +638,29 @@ Y_UNIT_TEST_SUITE(TJsonTest) { UNIT_ASSERT_NO_DIFF(json, expectedJson); } - Y_UNIT_TEST(BufferedEncoderMergesMetrics) { + Y_UNIT_TEST(BufferedEncoderMergesMetrics) { TString json; TStringOutput out(json); auto e = BufferedEncoderJson(&out, 2); auto ts = 1; - auto writeMetric = [&] (const TString& val) { - e->OnMetricBegin(EMetricType::COUNTER); + auto writeMetric = [&] (const TString& val) { + e->OnMetricBegin(EMetricType::COUNTER); e->OnLabelsBegin(); e->OnLabel("foo", val); e->OnLabelsEnd(); e->OnUint64(TInstant::Seconds(ts++), 42); - e->OnMetricEnd(); + e->OnMetricEnd(); }; e->OnStreamBegin(); - writeMetric("bar"); - writeMetric("bar"); - writeMetric("baz"); - writeMetric("bar"); + writeMetric("bar"); + writeMetric("bar"); + writeMetric("baz"); + writeMetric("bar"); e->OnStreamEnd(); e->Close(); @@ -676,13 +676,13 @@ Y_UNIT_TEST_SUITE(TJsonTest) { auto e = EncoderJson(&out); auto writePreamble = [&] { e->OnStreamBegin(); - e->OnMetricBegin(EMetricType::COUNTER); + e->OnMetricBegin(EMetricType::COUNTER); e->OnLabelsBegin(); e->OnLabel("foo", "bar"); e->OnLabelsEnd(); }; - // writing two values for a metric in a row will trigger + // writing two values for a metric in a row will trigger // timeseries object construction writePreamble(); e->OnUint64(TInstant::Zero(), 42); @@ -705,7 +705,7 @@ Y_UNIT_TEST_SUITE(TJsonTest) { { auto e = BufferedEncoderJson(&out, 2); e->OnStreamBegin(); - e->OnMetricBegin(EMetricType::COUNTER); + e->OnMetricBegin(EMetricType::COUNTER); e->OnLabelsBegin(); e->OnLabel("foo", "bar"); e->OnLabelsEnd(); @@ -715,38 +715,38 @@ Y_UNIT_TEST_SUITE(TJsonTest) { e->OnUint64(TInstant::Zero(), 43); e->OnUint64(TInstant::Zero(), 44); e->OnUint64(TInstant::Zero(), 45); - e->OnMetricEnd(); + e->OnMetricEnd(); e->OnStreamEnd(); } out << "\n"; UNIT_ASSERT_NO_DIFF(out.Str(), NResource::Find("/buffered_ts_merge.json")); - } - - template <typename TFactory, typename TConsumer> - TString EncodeToString(TFactory factory, TConsumer consumer) { - TStringStream out; - { - IMetricEncoderPtr e = factory(&out, 2); - consumer(e.Get()); - } - out << '\n'; - return out.Str(); } - + + template <typename TFactory, typename TConsumer> + TString EncodeToString(TFactory factory, TConsumer consumer) { + TStringStream out; + { + IMetricEncoderPtr e = factory(&out, 2); + consumer(e.Get()); + } + out << '\n'; + return out.Str(); + } + Y_UNIT_TEST(SummaryValueEncode) { - auto writeDocument = [](IMetricEncoder* e) { + auto writeDocument = [](IMetricEncoder* e) { e->OnStreamBegin(); { - e->OnMetricBegin(EMetricType::DSUMMARY); + e->OnMetricBegin(EMetricType::DSUMMARY); { e->OnLabelsBegin(); - e->OnLabel("metric", "temperature"); + e->OnLabel("metric", "temperature"); e->OnLabelsEnd(); } e->OnSummaryDouble(now, MakeIntrusive<TSummaryDoubleSnapshot>(10., -0.5, 0.5, 0.3, 30u)); - e->OnMetricEnd(); + e->OnMetricEnd(); } e->OnStreamEnd(); }; @@ -768,18 +768,18 @@ Y_UNIT_TEST_SUITE(TJsonTest) { } Y_UNIT_TEST(SummaryInfEncode) { - auto writeDocument = [](IMetricEncoder* e) { + auto writeDocument = [](IMetricEncoder* e) { e->OnStreamBegin(); { - e->OnMetricBegin(EMetricType::DSUMMARY); + e->OnMetricBegin(EMetricType::DSUMMARY); { e->OnLabelsBegin(); - e->OnLabel("metric", "temperature"); + e->OnLabel("metric", "temperature"); e->OnLabelsEnd(); } e->OnSummaryDouble(now, TestInfSummary()); - e->OnMetricEnd(); + e->OnMetricEnd(); } e->OnStreamEnd(); }; @@ -794,7 +794,7 @@ Y_UNIT_TEST_SUITE(TJsonTest) { Y_UNIT_TEST(SummaryInfDecode) { NProto::TMultiSamplesList samples; { - IMetricEncoderPtr e = EncoderProtobuf(&samples); + IMetricEncoderPtr e = EncoderProtobuf(&samples); TString testJson = NResource::Find("/summary_inf.json"); DecodeJson(testJson, e.Get()); @@ -803,9 +803,9 @@ Y_UNIT_TEST_SUITE(TJsonTest) { UNIT_ASSERT_VALUES_EQUAL(1, samples.SamplesSize()); const NProto::TMultiSample& s = samples.GetSamples(0); - UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::DSUMMARY); + UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::DSUMMARY); UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); - AssertLabelEqual(s.GetLabels(0), "metric", "temperature"); + AssertLabelEqual(s.GetLabels(0), "metric", "temperature"); UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); @@ -820,7 +820,7 @@ Y_UNIT_TEST_SUITE(TJsonTest) { Y_UNIT_TEST(SummaryValueDecode) { NProto::TMultiSamplesList samples; { - IMetricEncoderPtr e = EncoderProtobuf(&samples); + IMetricEncoderPtr e = EncoderProtobuf(&samples); TString testJson = NResource::Find("/summary_value.json"); DecodeJson(testJson, e.Get()); @@ -829,9 +829,9 @@ Y_UNIT_TEST_SUITE(TJsonTest) { UNIT_ASSERT_VALUES_EQUAL(1, samples.SamplesSize()); const NProto::TMultiSample& s = samples.GetSamples(0); - UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::DSUMMARY); + UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::DSUMMARY); UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); - AssertLabelEqual(s.GetLabels(0), "metric", "temperature"); + AssertLabelEqual(s.GetLabels(0), "metric", "temperature"); UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); @@ -840,13 +840,13 @@ Y_UNIT_TEST_SUITE(TJsonTest) { } Y_UNIT_TEST(SummaryTimeSeriesEncode) { - auto writeDocument = [](IMetricEncoder* e) { + auto writeDocument = [](IMetricEncoder* e) { e->OnStreamBegin(); { - e->OnMetricBegin(EMetricType::DSUMMARY); + e->OnMetricBegin(EMetricType::DSUMMARY); { e->OnLabelsBegin(); - e->OnLabel("metric", "temperature"); + e->OnLabel("metric", "temperature"); e->OnLabelsEnd(); } @@ -862,7 +862,7 @@ Y_UNIT_TEST_SUITE(TJsonTest) { e->OnSummaryDouble(now + TDuration::Seconds(15), summary.Snapshot()); - e->OnMetricEnd(); + e->OnMetricEnd(); } e->OnStreamEnd(); }; @@ -877,7 +877,7 @@ Y_UNIT_TEST_SUITE(TJsonTest) { Y_UNIT_TEST(SummaryTimeSeriesDecode) { NProto::TMultiSamplesList samples; { - IMetricEncoderPtr e = EncoderProtobuf(&samples); + IMetricEncoderPtr e = EncoderProtobuf(&samples); TString testJson = NResource::Find("/summary_timeseries.json"); DecodeJson(testJson, e.Get()); @@ -886,9 +886,9 @@ Y_UNIT_TEST_SUITE(TJsonTest) { UNIT_ASSERT_VALUES_EQUAL(1, samples.SamplesSize()); const NProto::TMultiSample& s = samples.GetSamples(0); - UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::DSUMMARY); + UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::DSUMMARY); UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); - AssertLabelEqual(s.GetLabels(0), "metric", "temperature"); + AssertLabelEqual(s.GetLabels(0), "metric", "temperature"); UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 2); @@ -912,7 +912,7 @@ Y_UNIT_TEST_SUITE(TJsonTest) { e->OnMetricBegin(EMetricType::LOGHIST); { e->OnLabelsBegin(); - e->OnLabel("metric", "ms"); + e->OnLabel("metric", "ms"); e->OnLabelsEnd(); } @@ -943,7 +943,7 @@ Y_UNIT_TEST_SUITE(TJsonTest) { UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::LOGHISTOGRAM); UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); - AssertLabelEqual(s.GetLabels(0), "metric", "ms"); + AssertLabelEqual(s.GetLabels(0), "metric", "ms"); UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); @@ -951,36 +951,36 @@ Y_UNIT_TEST_SUITE(TJsonTest) { AssertPointEqual(s.GetPoints(0), now, *snapshot); } - Y_UNIT_TEST(HistogramValueEncode) { - auto writeDocument = [](IMetricEncoder* e) { - e->OnStreamBegin(); - { - e->OnMetricBegin(EMetricType::HIST); - { - e->OnLabelsBegin(); - e->OnLabel("metric", "responseTimeMillis"); - e->OnLabelsEnd(); - } - - // {1: 1, 2: 1, 4: 2, 8: 4, 16: 8, inf: 83} - auto h = ExponentialHistogram(6, 2); - for (i64 i = 1; i < 100; i++) { - h->Collect(i); - } - - e->OnHistogram(now, h->Snapshot()); - e->OnMetricEnd(); - } - e->OnStreamEnd(); - }; - - TString result1 = EncodeToString(EncoderJson, writeDocument); - UNIT_ASSERT_NO_DIFF(result1, NResource::Find("/histogram_value.json")); - - TString result2 = EncodeToString(BufferedEncoderJson, writeDocument); - UNIT_ASSERT_NO_DIFF(result2, NResource::Find("/histogram_value.json")); - } - + Y_UNIT_TEST(HistogramValueEncode) { + auto writeDocument = [](IMetricEncoder* e) { + e->OnStreamBegin(); + { + e->OnMetricBegin(EMetricType::HIST); + { + e->OnLabelsBegin(); + e->OnLabel("metric", "responseTimeMillis"); + e->OnLabelsEnd(); + } + + // {1: 1, 2: 1, 4: 2, 8: 4, 16: 8, inf: 83} + auto h = ExponentialHistogram(6, 2); + for (i64 i = 1; i < 100; i++) { + h->Collect(i); + } + + e->OnHistogram(now, h->Snapshot()); + e->OnMetricEnd(); + } + e->OnStreamEnd(); + }; + + TString result1 = EncodeToString(EncoderJson, writeDocument); + UNIT_ASSERT_NO_DIFF(result1, NResource::Find("/histogram_value.json")); + + TString result2 = EncodeToString(BufferedEncoderJson, writeDocument); + UNIT_ASSERT_NO_DIFF(result2, NResource::Find("/histogram_value.json")); + } + Y_UNIT_TEST(LogHistogramTimeSeriesEncode) { auto writeDocument = [](IMetricEncoder* e) { e->OnStreamBegin(); @@ -988,7 +988,7 @@ Y_UNIT_TEST_SUITE(TJsonTest) { e->OnMetricBegin(EMetricType::LOGHIST); { e->OnLabelsBegin(); - e->OnLabel("metric", "ms"); + e->OnLabel("metric", "ms"); e->OnLabelsEnd(); } @@ -1022,7 +1022,7 @@ Y_UNIT_TEST_SUITE(TJsonTest) { UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::LOGHISTOGRAM); UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); - AssertLabelEqual(s.GetLabels(0), "metric", "ms"); + AssertLabelEqual(s.GetLabels(0), "metric", "ms"); UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 2); @@ -1034,130 +1034,130 @@ Y_UNIT_TEST_SUITE(TJsonTest) { } void HistogramValueDecode(const TString& filePath) { - NProto::TMultiSamplesList samples; - { - IMetricEncoderPtr e = EncoderProtobuf(&samples); - + NProto::TMultiSamplesList samples; + { + IMetricEncoderPtr e = EncoderProtobuf(&samples); + TString testJson = NResource::Find(filePath); - DecodeJson(testJson, e.Get()); - } - - UNIT_ASSERT_VALUES_EQUAL(1, samples.SamplesSize()); - const NProto::TMultiSample& s = samples.GetSamples(0); - - UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::HISTOGRAM); - UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); - AssertLabelEqual(s.GetLabels(0), "metric", "responseTimeMillis"); - - UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); - - auto h = ExponentialHistogram(6, 2); - for (i64 i = 1; i < 100; i++) { - h->Collect(i); - } - - AssertPointEqual(s.GetPoints(0), now, *h->Snapshot()); - } - + DecodeJson(testJson, e.Get()); + } + + UNIT_ASSERT_VALUES_EQUAL(1, samples.SamplesSize()); + const NProto::TMultiSample& s = samples.GetSamples(0); + + UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::HISTOGRAM); + UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); + AssertLabelEqual(s.GetLabels(0), "metric", "responseTimeMillis"); + + UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); + + auto h = ExponentialHistogram(6, 2); + for (i64 i = 1; i < 100; i++) { + h->Collect(i); + } + + AssertPointEqual(s.GetPoints(0), now, *h->Snapshot()); + } + Y_UNIT_TEST(HistogramValueDecode) { HistogramValueDecode("/histogram_value.json"); HistogramValueDecode("/histogram_value_inf_before_bounds.json"); } - Y_UNIT_TEST(HistogramTimeSeriesEncode) { - auto writeDocument = [](IMetricEncoder* e) { - e->OnStreamBegin(); - { - e->OnMetricBegin(EMetricType::HIST_RATE); - { - e->OnLabelsBegin(); - e->OnLabel("metric", "responseTimeMillis"); - e->OnLabelsEnd(); - } - - // {1: 1, 2: 1, 4: 2, 8: 4, 16: 8, inf: 83} - auto h = ExponentialHistogram(6, 2); - for (i64 i = 1; i < 100; i++) { - h->Collect(i); - } - e->OnHistogram(now, h->Snapshot()); - - // {1: 2, 2: 2, 4: 4, 8: 8, 16: 16, inf: 166} - for (i64 i = 1; i < 100; i++) { - h->Collect(i); - } - e->OnHistogram(now + TDuration::Seconds(15), h->Snapshot()); - - e->OnMetricEnd(); - } - e->OnStreamEnd(); - }; - - TString result1 = EncodeToString(EncoderJson, writeDocument); - UNIT_ASSERT_NO_DIFF(result1, NResource::Find("/histogram_timeseries.json")); - - TString result2 = EncodeToString(BufferedEncoderJson, writeDocument); - UNIT_ASSERT_NO_DIFF(result2, NResource::Find("/histogram_timeseries.json")); - } - - Y_UNIT_TEST(HistogramTimeSeriesDecode) { - NProto::TMultiSamplesList samples; - { - IMetricEncoderPtr e = EncoderProtobuf(&samples); - - TString testJson = NResource::Find("/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::HIST_RATE); - UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); - AssertLabelEqual(s.GetLabels(0), "metric", "responseTimeMillis"); - - UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 2); - - auto h = ExponentialHistogram(6, 2); - for (i64 i = 1; i < 100; i++) { - h->Collect(i); - } - - AssertPointEqual(s.GetPoints(0), now, *h->Snapshot()); - - for (i64 i = 1; i < 100; i++) { - h->Collect(i); - } - - AssertPointEqual(s.GetPoints(1), now + TDuration::Seconds(15), *h->Snapshot()); - } - - Y_UNIT_TEST(IntGaugeEncode) { - auto writeDocument = [](IMetricEncoder* e) { - e->OnStreamBegin(); - { - e->OnMetricBegin(EMetricType::IGAUGE); - { - e->OnLabelsBegin(); - e->OnLabel("metric", "a"); - e->OnLabelsEnd(); - } - e->OnInt64(now, Min<i64>()); - e->OnInt64(now + TDuration::Seconds(1), -1); - e->OnInt64(now + TDuration::Seconds(2), 0); - e->OnInt64(now + TDuration::Seconds(3), Max<i64>()); - e->OnMetricEnd(); - } - e->OnStreamEnd(); - }; - - TString result1 = EncodeToString(EncoderJson, writeDocument); - UNIT_ASSERT_NO_DIFF(result1, NResource::Find("/int_gauge.json")); - - TString result2 = EncodeToString(BufferedEncoderJson, writeDocument); - UNIT_ASSERT_NO_DIFF(result2, NResource::Find("/int_gauge.json")); - } - + Y_UNIT_TEST(HistogramTimeSeriesEncode) { + auto writeDocument = [](IMetricEncoder* e) { + e->OnStreamBegin(); + { + e->OnMetricBegin(EMetricType::HIST_RATE); + { + e->OnLabelsBegin(); + e->OnLabel("metric", "responseTimeMillis"); + e->OnLabelsEnd(); + } + + // {1: 1, 2: 1, 4: 2, 8: 4, 16: 8, inf: 83} + auto h = ExponentialHistogram(6, 2); + for (i64 i = 1; i < 100; i++) { + h->Collect(i); + } + e->OnHistogram(now, h->Snapshot()); + + // {1: 2, 2: 2, 4: 4, 8: 8, 16: 16, inf: 166} + for (i64 i = 1; i < 100; i++) { + h->Collect(i); + } + e->OnHistogram(now + TDuration::Seconds(15), h->Snapshot()); + + e->OnMetricEnd(); + } + e->OnStreamEnd(); + }; + + TString result1 = EncodeToString(EncoderJson, writeDocument); + UNIT_ASSERT_NO_DIFF(result1, NResource::Find("/histogram_timeseries.json")); + + TString result2 = EncodeToString(BufferedEncoderJson, writeDocument); + UNIT_ASSERT_NO_DIFF(result2, NResource::Find("/histogram_timeseries.json")); + } + + Y_UNIT_TEST(HistogramTimeSeriesDecode) { + NProto::TMultiSamplesList samples; + { + IMetricEncoderPtr e = EncoderProtobuf(&samples); + + TString testJson = NResource::Find("/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::HIST_RATE); + UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); + AssertLabelEqual(s.GetLabels(0), "metric", "responseTimeMillis"); + + UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 2); + + auto h = ExponentialHistogram(6, 2); + for (i64 i = 1; i < 100; i++) { + h->Collect(i); + } + + AssertPointEqual(s.GetPoints(0), now, *h->Snapshot()); + + for (i64 i = 1; i < 100; i++) { + h->Collect(i); + } + + AssertPointEqual(s.GetPoints(1), now + TDuration::Seconds(15), *h->Snapshot()); + } + + Y_UNIT_TEST(IntGaugeEncode) { + auto writeDocument = [](IMetricEncoder* e) { + e->OnStreamBegin(); + { + e->OnMetricBegin(EMetricType::IGAUGE); + { + e->OnLabelsBegin(); + e->OnLabel("metric", "a"); + e->OnLabelsEnd(); + } + e->OnInt64(now, Min<i64>()); + e->OnInt64(now + TDuration::Seconds(1), -1); + e->OnInt64(now + TDuration::Seconds(2), 0); + e->OnInt64(now + TDuration::Seconds(3), Max<i64>()); + e->OnMetricEnd(); + } + e->OnStreamEnd(); + }; + + TString result1 = EncodeToString(EncoderJson, writeDocument); + UNIT_ASSERT_NO_DIFF(result1, NResource::Find("/int_gauge.json")); + + TString result2 = EncodeToString(BufferedEncoderJson, writeDocument); + UNIT_ASSERT_NO_DIFF(result2, NResource::Find("/int_gauge.json")); + } + Y_UNIT_TEST(InconsistentMetricTypes) { auto emitMetrics = [](IMetricEncoder& encoder, const TString& expectedError) { encoder.OnMetricBegin(EMetricType::GAUGE); @@ -1213,33 +1213,33 @@ Y_UNIT_TEST_SUITE(TJsonTest) { } } - Y_UNIT_TEST(IntGaugeDecode) { - NProto::TMultiSamplesList samples; - { - IMetricEncoderPtr e = EncoderProtobuf(&samples); - - TString testJson = NResource::Find("/int_gauge.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::IGAUGE); - UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); - AssertLabelEqual(s.GetLabels(0), "metric", "a"); - - UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 4); - AssertPointEqual(s.GetPoints(0), now, Min<i64>()); - AssertPointEqual(s.GetPoints(1), now + TDuration::Seconds(1), i64(-1)); - AssertPointEqual(s.GetPoints(2), now + TDuration::Seconds(2), i64(0)); - AssertPointEqual(s.GetPoints(3), now + TDuration::Seconds(3), Max<i64>()); - } + Y_UNIT_TEST(IntGaugeDecode) { + NProto::TMultiSamplesList samples; + { + IMetricEncoderPtr e = EncoderProtobuf(&samples); + + TString testJson = NResource::Find("/int_gauge.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::IGAUGE); + UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1); + AssertLabelEqual(s.GetLabels(0), "metric", "a"); + + UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 4); + AssertPointEqual(s.GetPoints(0), now, Min<i64>()); + AssertPointEqual(s.GetPoints(1), now + TDuration::Seconds(1), i64(-1)); + AssertPointEqual(s.GetPoints(2), now + TDuration::Seconds(2), i64(0)); + AssertPointEqual(s.GetPoints(3), now + TDuration::Seconds(3), Max<i64>()); + } Y_UNIT_TEST(FuzzerRegression) { NProto::TMultiSamplesList samples; { - IMetricEncoderPtr e = EncoderProtobuf(&samples); + IMetricEncoderPtr e = EncoderProtobuf(&samples); for (auto f : { "/hist_crash.json", "/crash.json" }) { TString testJson = NResource::Find(f); @@ -1254,13 +1254,13 @@ Y_UNIT_TEST_SUITE(TJsonTest) { { "mode": "deriv", "value": -1, - "labels": { "metric": "SystemTime" } + "labels": { "metric": "SystemTime" } }, } ]}")"; NProto::TMultiSamplesList samples; - IMetricEncoderPtr e = EncoderProtobuf(&samples); + IMetricEncoderPtr e = EncoderProtobuf(&samples); UNIT_ASSERT_EXCEPTION(DecodeJson(input, e.Get()), yexception); } @@ -1287,4 +1287,4 @@ Y_UNIT_TEST_SUITE(TJsonTest) { } } -} +} diff --git a/library/cpp/monlib/encode/json/typed_point.h b/library/cpp/monlib/encode/json/typed_point.h index b606c82924..fbaa840c4b 100644 --- a/library/cpp/monlib/encode/json/typed_point.h +++ b/library/cpp/monlib/encode/json/typed_point.h @@ -1,123 +1,123 @@ -#pragma once - -#include <library/cpp/monlib/metrics/metric_value.h> - - -namespace NMonitoring { - - class TTypedPoint { - public: - TTypedPoint() - : Time_(TInstant::Zero()) - , ValueType_(EMetricValueType::UNKNOWN) - { - } - - template <typename T> - TTypedPoint(TInstant time, T value) - : Time_(time) - , ValueType_(TValueType<T>::Type) - , Value_(value) - { - Ref(); - } - - ~TTypedPoint() { - UnRef(); - } - - TTypedPoint(const TTypedPoint& rhs) - : Time_(rhs.Time_) - , ValueType_(rhs.ValueType_) - , Value_(rhs.Value_) - { - Ref(); - } - - TTypedPoint& operator=(const TTypedPoint& rhs) { - UnRef(); - - Time_ = rhs.Time_; - ValueType_ = rhs.ValueType_; - Value_ = rhs.Value_; - - Ref(); - return *this; - } - +#pragma once + +#include <library/cpp/monlib/metrics/metric_value.h> + + +namespace NMonitoring { + + class TTypedPoint { + public: + TTypedPoint() + : Time_(TInstant::Zero()) + , ValueType_(EMetricValueType::UNKNOWN) + { + } + + template <typename T> + TTypedPoint(TInstant time, T value) + : Time_(time) + , ValueType_(TValueType<T>::Type) + , Value_(value) + { + Ref(); + } + + ~TTypedPoint() { + UnRef(); + } + + TTypedPoint(const TTypedPoint& rhs) + : Time_(rhs.Time_) + , ValueType_(rhs.ValueType_) + , Value_(rhs.Value_) + { + Ref(); + } + + TTypedPoint& operator=(const TTypedPoint& rhs) { + UnRef(); + + Time_ = rhs.Time_; + ValueType_ = rhs.ValueType_; + Value_ = rhs.Value_; + + Ref(); + return *this; + } + TTypedPoint(TTypedPoint&& rhs) noexcept - : Time_(rhs.Time_) - , ValueType_(rhs.ValueType_) - , Value_(rhs.Value_) - { - rhs.ValueType_ = EMetricValueType::UNKNOWN; - rhs.Value_ = {}; - } - + : Time_(rhs.Time_) + , ValueType_(rhs.ValueType_) + , Value_(rhs.Value_) + { + rhs.ValueType_ = EMetricValueType::UNKNOWN; + rhs.Value_ = {}; + } + TTypedPoint& operator=(TTypedPoint&& rhs) noexcept { - UnRef(); - - Time_ = rhs.Time_; - ValueType_ = rhs.ValueType_; - Value_ = rhs.Value_; - - rhs.ValueType_ = EMetricValueType::UNKNOWN; - rhs.Value_ = {}; - - return *this; - } - - TInstant GetTime() const noexcept { - return Time_; - } - - void SetTime(TInstant time) noexcept { - Time_ = time; - } - - TMetricValue GetValue() const noexcept { - return Value_; - } - - EMetricValueType GetValueType() const noexcept { - return ValueType_; - } - - template <typename T> - void SetValue(T value) noexcept { - ValueType_ = TValueType<T>::Type; - Value_ = TMetricValue{value}; - } - - bool HasValue() { - return ValueType_ != EMetricValueType::UNKNOWN; - } - - private: - void Ref() { - if (ValueType_ == EMetricValueType::HISTOGRAM) { - Value_.AsHistogram()->Ref(); - } else if (ValueType_ == EMetricValueType::SUMMARY) { + UnRef(); + + Time_ = rhs.Time_; + ValueType_ = rhs.ValueType_; + Value_ = rhs.Value_; + + rhs.ValueType_ = EMetricValueType::UNKNOWN; + rhs.Value_ = {}; + + return *this; + } + + TInstant GetTime() const noexcept { + return Time_; + } + + void SetTime(TInstant time) noexcept { + Time_ = time; + } + + TMetricValue GetValue() const noexcept { + return Value_; + } + + EMetricValueType GetValueType() const noexcept { + return ValueType_; + } + + template <typename T> + void SetValue(T value) noexcept { + ValueType_ = TValueType<T>::Type; + Value_ = TMetricValue{value}; + } + + bool HasValue() { + return ValueType_ != EMetricValueType::UNKNOWN; + } + + private: + void Ref() { + if (ValueType_ == EMetricValueType::HISTOGRAM) { + Value_.AsHistogram()->Ref(); + } else if (ValueType_ == EMetricValueType::SUMMARY) { 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) { + } + + 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(); } - } - - private: - TInstant Time_; - EMetricValueType ValueType_; - TMetricValue Value_; - }; - -} + } + + private: + TInstant Time_; + EMetricValueType ValueType_; + TMetricValue Value_; + }; + +} diff --git a/library/cpp/monlib/encode/json/ut/expected.json b/library/cpp/monlib/encode/json/ut/expected.json index 8541479be6..ead853455b 100644 --- a/library/cpp/monlib/encode/json/ut/expected.json +++ b/library/cpp/monlib/encode/json/ut/expected.json @@ -1,42 +1,42 @@ -{ - "ts":1500000000, - "commonLabels": - { - "project":"solomon" - }, - "sensors": - [ - { - "kind":"COUNTER", - "labels": - { - "metric":"single", +{ + "ts":1500000000, + "commonLabels": + { + "project":"solomon" + }, + "sensors": + [ + { + "kind":"COUNTER", + "labels": + { + "metric":"single", "labels":"l1" - }, - "ts":1509843723, - "value":17 - }, - { - "kind":"RATE", - "labels": - { - "metric":"single", + }, + "ts":1509843723, + "value":17 + }, + { + "kind":"RATE", + "labels": + { + "metric":"single", "labels":"l2" - }, - "ts":1509843723, - "value":17 - }, - { - "kind":"GAUGE", - "labels": - { - "metric":"single", + }, + "ts":1509843723, + "value":17 + }, + { + "kind":"GAUGE", + "labels": + { + "metric":"single", "labels":"l3" - }, - "ts":1509843723, - "value":3.14 - }, - { + }, + "ts":1509843723, + "value":3.14 + }, + { "kind":"IGAUGE", "labels": { @@ -47,46 +47,46 @@ "value":42 }, { - "kind":"GAUGE", - "labels": - { - "metric":"multiple", + "kind":"GAUGE", + "labels": + { + "metric":"multiple", "labels":"l5" - }, - "timeseries": - [ - { - "ts":1509843723, - "value":"nan" - }, - { - "ts":1509843738, - "value":"inf" - }, - { - "ts":1509843753, - "value":"-inf" - } - ] - }, - { - "kind":"COUNTER", - "timeseries": - [ - { - "ts":1509843723, - "value":1337 - }, - { - "ts":1509843738, - "value":1338 - } - ], - "labels": - { - "metric":"multiple", + }, + "timeseries": + [ + { + "ts":1509843723, + "value":"nan" + }, + { + "ts":1509843738, + "value":"inf" + }, + { + "ts":1509843753, + "value":"-inf" + } + ] + }, + { + "kind":"COUNTER", + "timeseries": + [ + { + "ts":1509843723, + "value":1337 + }, + { + "ts":1509843738, + "value":1338 + } + ], + "labels": + { + "metric":"multiple", "labels":"l6" - } - } - ] -} + } + } + ] +} diff --git a/library/cpp/monlib/encode/json/ut/expected_buffered.json b/library/cpp/monlib/encode/json/ut/expected_buffered.json index 17c0f06323..9a6a1d6201 100644 --- a/library/cpp/monlib/encode/json/ut/expected_buffered.json +++ b/library/cpp/monlib/encode/json/ut/expected_buffered.json @@ -1,42 +1,42 @@ -{ - "ts":1500000000, - "commonLabels": - { - "project":"solomon" - }, - "sensors": - [ - { - "kind":"COUNTER", - "labels": - { +{ + "ts":1500000000, + "commonLabels": + { + "project":"solomon" + }, + "sensors": + [ + { + "kind":"COUNTER", + "labels": + { "labels":"l1", "metric":"single" - }, - "ts":1509843723, - "value":17 - }, - { - "kind":"RATE", - "labels": - { + }, + "ts":1509843723, + "value":17 + }, + { + "kind":"RATE", + "labels": + { "labels":"l2", "metric":"single" - }, - "ts":1509843723, - "value":17 - }, - { - "kind":"GAUGE", - "labels": - { + }, + "ts":1509843723, + "value":17 + }, + { + "kind":"GAUGE", + "labels": + { "labels":"l3", "metric":"single" - }, - "ts":1509843723, - "value":3.14 - }, - { + }, + "ts":1509843723, + "value":3.14 + }, + { "kind":"IGAUGE", "labels": { @@ -47,46 +47,46 @@ "value":42 }, { - "kind":"GAUGE", - "labels": - { + "kind":"GAUGE", + "labels": + { "labels":"l5", "metric":"multiple" - }, - "timeseries": - [ - { - "ts":1509843723, - "value":"nan" - }, - { - "ts":1509843738, - "value":"inf" - }, - { - "ts":1509843753, - "value":"-inf" - } - ] - }, - { - "kind":"COUNTER", + }, + "timeseries": + [ + { + "ts":1509843723, + "value":"nan" + }, + { + "ts":1509843738, + "value":"inf" + }, + { + "ts":1509843753, + "value":"-inf" + } + ] + }, + { + "kind":"COUNTER", "labels": { "labels":"l6", "metric":"multiple" }, - "timeseries": - [ - { - "ts":1509843723, - "value":1337 - }, - { - "ts":1509843738, - "value":1338 - } + "timeseries": + [ + { + "ts":1509843723, + "value":1337 + }, + { + "ts":1509843738, + "value":1338 + } ] - } - ] -} + } + ] +} diff --git a/library/cpp/monlib/encode/json/ut/expected_cloud.json b/library/cpp/monlib/encode/json/ut/expected_cloud.json index 5c4dc65ffc..6184811579 100644 --- a/library/cpp/monlib/encode/json/ut/expected_cloud.json +++ b/library/cpp/monlib/encode/json/ut/expected_cloud.json @@ -1,47 +1,47 @@ -{ +{ "ts":"2017-07-14T02:40:00.000000Z", "labels": - { - "project":"solomon" - }, + { + "project":"solomon" + }, "metrics": - [ - { + [ + { "type":"COUNTER", - "labels": - { + "labels": + { "labels":"l1" - }, + }, "name":"single", "ts":"2017-11-05T01:02:03.000000Z", - "value":17 - }, - { + "value":17 + }, + { "type":"RATE", - "labels": - { + "labels": + { "labels":"l2" - }, + }, "name":"single", "ts":"2017-11-05T01:02:03.000000Z", - "value":17 - }, - { + "value":17 + }, + { "type":"DGAUGE", - "labels": - { + "labels": + { "labels":"l3" - }, + }, "name":"single", "ts":"2017-11-05T01:02:03.000000Z", - "value":3.14 - }, - { + "value":3.14 + }, + { "type":"IGAUGE", - "labels": - { + "labels": + { "labels":"l4" - }, + }, "name":"single_igauge", "ts":"2017-11-05T01:02:03.000000Z", "value":42 @@ -53,40 +53,40 @@ "labels":"l5" }, "name":"multiple", - "timeseries": - [ - { + "timeseries": + [ + { "ts":"2017-11-05T01:02:03.000000Z", - "value":"nan" - }, - { + "value":"nan" + }, + { "ts":"2017-11-05T01:02:18.000000Z", - "value":"inf" - }, - { + "value":"inf" + }, + { "ts":"2017-11-05T01:02:33.000000Z", - "value":"-inf" - } - ] - }, - { + "value":"-inf" + } + ] + }, + { "type":"COUNTER", - "timeseries": - [ - { + "timeseries": + [ + { "ts":"2017-11-05T01:02:03.000000Z", - "value":1337 - }, - { + "value":1337 + }, + { "ts":"2017-11-05T01:02:18.000000Z", - "value":1338 - } - ], - "labels": - { + "value":1338 + } + ], + "labels": + { "labels":"l6" }, "name":"multiple" - } - ] -} + } + ] +} diff --git a/library/cpp/monlib/encode/json/ut/histogram_timeseries.json b/library/cpp/monlib/encode/json/ut/histogram_timeseries.json index c3a6f8e8b0..f6131ffded 100644 --- a/library/cpp/monlib/encode/json/ut/histogram_timeseries.json +++ b/library/cpp/monlib/encode/json/ut/histogram_timeseries.json @@ -1,61 +1,61 @@ -{ - "sensors": - [ - { - "kind":"HIST_RATE", - "labels": - { - "metric":"responseTimeMillis" - }, - "timeseries": - [ - { - "ts":1509843723, - "hist": - { - "bounds": - [ - 1, - 2, - 4, - 8, - 16 - ], - "buckets": - [ - 1, - 1, - 2, - 4, - 8 - ], - "inf":83 - } - }, - { - "ts":1509843738, - "hist": - { - "bounds": - [ - 1, - 2, - 4, - 8, - 16 - ], - "buckets": - [ - 2, - 2, - 4, - 8, - 16 - ], - "inf":166 - } - } - ] - } - ] -} +{ + "sensors": + [ + { + "kind":"HIST_RATE", + "labels": + { + "metric":"responseTimeMillis" + }, + "timeseries": + [ + { + "ts":1509843723, + "hist": + { + "bounds": + [ + 1, + 2, + 4, + 8, + 16 + ], + "buckets": + [ + 1, + 1, + 2, + 4, + 8 + ], + "inf":83 + } + }, + { + "ts":1509843738, + "hist": + { + "bounds": + [ + 1, + 2, + 4, + 8, + 16 + ], + "buckets": + [ + 2, + 2, + 4, + 8, + 16 + ], + "inf":166 + } + } + ] + } + ] +} diff --git a/library/cpp/monlib/encode/json/ut/histogram_value.json b/library/cpp/monlib/encode/json/ut/histogram_value.json index 91b4ffa3dd..ec1ae5cdec 100644 --- a/library/cpp/monlib/encode/json/ut/histogram_value.json +++ b/library/cpp/monlib/encode/json/ut/histogram_value.json @@ -1,33 +1,33 @@ -{ - "sensors": - [ - { - "kind":"HIST", - "labels": - { - "metric":"responseTimeMillis" - }, - "ts":1509843723, - "hist": - { - "bounds": - [ - 1, - 2, - 4, - 8, - 16 - ], - "buckets": - [ - 1, - 1, - 2, - 4, - 8 - ], - "inf":83 - } - } - ] -} +{ + "sensors": + [ + { + "kind":"HIST", + "labels": + { + "metric":"responseTimeMillis" + }, + "ts":1509843723, + "hist": + { + "bounds": + [ + 1, + 2, + 4, + 8, + 16 + ], + "buckets": + [ + 1, + 1, + 2, + 4, + 8 + ], + "inf":83 + } + } + ] +} diff --git a/library/cpp/monlib/encode/json/ut/histogram_value_inf_before_bounds.json b/library/cpp/monlib/encode/json/ut/histogram_value_inf_before_bounds.json index 87dc527985..f8a17c8831 100644 --- a/library/cpp/monlib/encode/json/ut/histogram_value_inf_before_bounds.json +++ b/library/cpp/monlib/encode/json/ut/histogram_value_inf_before_bounds.json @@ -5,7 +5,7 @@ "kind":"HIST", "labels": { - "metric":"responseTimeMillis" + "metric":"responseTimeMillis" }, "ts":1509843723, "hist": diff --git a/library/cpp/monlib/encode/json/ut/int_gauge.json b/library/cpp/monlib/encode/json/ut/int_gauge.json index 95aa12aa52..fbe57f873c 100644 --- a/library/cpp/monlib/encode/json/ut/int_gauge.json +++ b/library/cpp/monlib/encode/json/ut/int_gauge.json @@ -1,31 +1,31 @@ -{ - "sensors": - [ - { - "kind":"IGAUGE", - "labels": - { - "metric":"a" - }, - "timeseries": - [ - { - "ts":1509843723, - "value":-9223372036854775808 - }, - { - "ts":1509843724, - "value":-1 - }, - { - "ts":1509843725, - "value":0 - }, - { - "ts":1509843726, - "value":9223372036854775807 - } - ] - } - ] -} +{ + "sensors": + [ + { + "kind":"IGAUGE", + "labels": + { + "metric":"a" + }, + "timeseries": + [ + { + "ts":1509843723, + "value":-9223372036854775808 + }, + { + "ts":1509843724, + "value":-1 + }, + { + "ts":1509843725, + "value":0 + }, + { + "ts":1509843726, + "value":9223372036854775807 + } + ] + } + ] +} 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 1e2a4a113c..e811a2cc57 100644 --- a/library/cpp/monlib/encode/json/ut/log_histogram_timeseries.json +++ b/library/cpp/monlib/encode/json/ut/log_histogram_timeseries.json @@ -5,7 +5,7 @@ "kind":"LOGHIST", "labels": { - "metric":"ms" + "metric":"ms" }, "timeseries": [ 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 9835221116..002478293b 100644 --- a/library/cpp/monlib/encode/json/ut/log_histogram_value.json +++ b/library/cpp/monlib/encode/json/ut/log_histogram_value.json @@ -5,7 +5,7 @@ "kind":"LOGHIST", "labels": { - "metric":"ms" + "metric":"ms" }, "ts":1509843723, "log_hist": diff --git a/library/cpp/monlib/encode/json/ut/merged.json b/library/cpp/monlib/encode/json/ut/merged.json index 15711471f0..ea2c99a33c 100644 --- a/library/cpp/monlib/encode/json/ut/merged.json +++ b/library/cpp/monlib/encode/json/ut/merged.json @@ -5,7 +5,7 @@ "kind":"RATE", "labels": { - "metric":"hello", + "metric":"hello", "label":"world" }, "value":1 diff --git a/library/cpp/monlib/encode/json/ut/metrics.json b/library/cpp/monlib/encode/json/ut/metrics.json index 0dc423d5e9..2be4617d51 100644 --- a/library/cpp/monlib/encode/json/ut/metrics.json +++ b/library/cpp/monlib/encode/json/ut/metrics.json @@ -1,43 +1,43 @@ -{ - "labels": { - "project": "solomon", - "cluster": "man", - "service": "stockpile" - }, - "metrics": [ - { - "type": "DGAUGE", - "labels": { - "metric": "Memory" - }, - "value": 10 - }, - { - "type": "RATE", - "value": 1, - "labels": { "metric": "UserTime" } - }, - { - "type": "GAUGE", - "value": 3.14159, - "labels": { "export": "Oxygen", "metric": "QueueSize" }, - "ts": "2017-11-05T12:34:56.000Z", - "memOnly": true - }, - { - "type": "GAUGE", - "labels": { "metric": "Writes" }, - "timeseries": [ - { - "ts": "2017-08-28T12:32:11Z", - "value": -10 - }, - { - "value": 20, - "ts": 1503923187 - } - ] - } - ], - "ts": "2017-08-27T12:34:56Z" -} +{ + "labels": { + "project": "solomon", + "cluster": "man", + "service": "stockpile" + }, + "metrics": [ + { + "type": "DGAUGE", + "labels": { + "metric": "Memory" + }, + "value": 10 + }, + { + "type": "RATE", + "value": 1, + "labels": { "metric": "UserTime" } + }, + { + "type": "GAUGE", + "value": 3.14159, + "labels": { "export": "Oxygen", "metric": "QueueSize" }, + "ts": "2017-11-05T12:34:56.000Z", + "memOnly": true + }, + { + "type": "GAUGE", + "labels": { "metric": "Writes" }, + "timeseries": [ + { + "ts": "2017-08-28T12:32:11Z", + "value": -10 + }, + { + "value": 20, + "ts": 1503923187 + } + ] + } + ], + "ts": "2017-08-27T12:34:56Z" +} diff --git a/library/cpp/monlib/encode/json/ut/sensors.json b/library/cpp/monlib/encode/json/ut/sensors.json index e128593492..4d979a3c1e 100644 --- a/library/cpp/monlib/encode/json/ut/sensors.json +++ b/library/cpp/monlib/encode/json/ut/sensors.json @@ -1,40 +1,40 @@ -{ - "commonLabels": { - "project": "solomon", - "cluster": "man", - "service": "stockpile" - }, - "sensors": [ - { - "labels": { - "metric": "Memory" - }, - "value": 10 - }, - { - "mode": "deriv", - "value": 1, - "labels": { "metric": "UserTime" } - }, - { - "value": 3.14159, - "labels": { "export": "Oxygen", "metric": "QueueSize" }, - "ts": "2017-11-05T12:34:56.000Z", - "memOnly": true - }, - { - "labels": { "metric": "Writes" }, - "timeseries": [ - { - "ts": "2017-08-28T12:32:11Z", - "value": -10 - }, - { - "value": 20, - "ts": 1503923187 - } - ] - } - ], - "ts": "2017-08-27T12:34:56Z" -} +{ + "commonLabels": { + "project": "solomon", + "cluster": "man", + "service": "stockpile" + }, + "sensors": [ + { + "labels": { + "metric": "Memory" + }, + "value": 10 + }, + { + "mode": "deriv", + "value": 1, + "labels": { "metric": "UserTime" } + }, + { + "value": 3.14159, + "labels": { "export": "Oxygen", "metric": "QueueSize" }, + "ts": "2017-11-05T12:34:56.000Z", + "memOnly": true + }, + { + "labels": { "metric": "Writes" }, + "timeseries": [ + { + "ts": "2017-08-28T12:32:11Z", + "value": -10 + }, + { + "value": 20, + "ts": 1503923187 + } + ] + } + ], + "ts": "2017-08-27T12:34:56Z" +} diff --git a/library/cpp/monlib/encode/json/ut/summary_inf.json b/library/cpp/monlib/encode/json/ut/summary_inf.json index 067102b7da..625a6cd8ad 100644 --- a/library/cpp/monlib/encode/json/ut/summary_inf.json +++ b/library/cpp/monlib/encode/json/ut/summary_inf.json @@ -5,7 +5,7 @@ "kind":"DSUMMARY", "labels": { - "metric":"temperature" + "metric":"temperature" }, "ts":1509843723, "summary": diff --git a/library/cpp/monlib/encode/json/ut/summary_timeseries.json b/library/cpp/monlib/encode/json/ut/summary_timeseries.json index dd06ec8544..92007af3e6 100644 --- a/library/cpp/monlib/encode/json/ut/summary_timeseries.json +++ b/library/cpp/monlib/encode/json/ut/summary_timeseries.json @@ -5,7 +5,7 @@ "kind":"DSUMMARY", "labels": { - "metric":"temperature" + "metric":"temperature" }, "timeseries": [ diff --git a/library/cpp/monlib/encode/json/ut/summary_value.json b/library/cpp/monlib/encode/json/ut/summary_value.json index 2ad9e9ad39..366394c5e1 100644 --- a/library/cpp/monlib/encode/json/ut/summary_value.json +++ b/library/cpp/monlib/encode/json/ut/summary_value.json @@ -5,7 +5,7 @@ "kind":"DSUMMARY", "labels": { - "metric":"temperature" + "metric":"temperature" }, "ts":1509843723, "summary": diff --git a/library/cpp/monlib/encode/json/ut/test_decode_to_encode.json b/library/cpp/monlib/encode/json/ut/test_decode_to_encode.json index db1e00c6e9..65f0c5c6e2 100644 --- a/library/cpp/monlib/encode/json/ut/test_decode_to_encode.json +++ b/library/cpp/monlib/encode/json/ut/test_decode_to_encode.json @@ -7,7 +7,7 @@ "sensors": [ { "kind": "GAUGE", - "labels": { "export": "Oxygen", "metric": "QueueSize" }, + "labels": { "export": "Oxygen", "metric": "QueueSize" }, "ts": 1509885296, "value": 3.14159 } diff --git a/library/cpp/monlib/encode/json/ut/ya.make b/library/cpp/monlib/encode/json/ut/ya.make index 4a24f76650..e50c4f4903 100644 --- a/library/cpp/monlib/encode/json/ut/ya.make +++ b/library/cpp/monlib/encode/json/ut/ya.make @@ -1,30 +1,30 @@ -UNITTEST_FOR(library/cpp/monlib/encode/json) - +UNITTEST_FOR(library/cpp/monlib/encode/json) + OWNER( g:solomon jamel ) - -SRCS( + +SRCS( json_decoder_ut.cpp - json_ut.cpp -) - -RESOURCE( + json_ut.cpp +) + +RESOURCE( buffered_test.json /buffered_test.json buffered_ts_merge.json /buffered_ts_merge.json - empty_series.json /empty_series.json - expected.json /expected.json + empty_series.json /empty_series.json + expected.json /expected.json expected_buffered.json /expected_buffered.json expected_cloud.json /expected_cloud.json expected_cloud_buffered.json /expected_cloud_buffered.json merged.json /merged.json - histogram_timeseries.json /histogram_timeseries.json - histogram_value.json /histogram_value.json + histogram_timeseries.json /histogram_timeseries.json + histogram_value.json /histogram_value.json histogram_value_inf_before_bounds.json /histogram_value_inf_before_bounds.json - int_gauge.json /int_gauge.json - sensors.json /sensors.json - metrics.json /metrics.json + int_gauge.json /int_gauge.json + sensors.json /sensors.json + metrics.json /metrics.json named_metrics.json /named_metrics.json test_decode_to_encode.json /test_decode_to_encode.json crash.json /crash.json @@ -34,13 +34,13 @@ RESOURCE( summary_timeseries.json /summary_timeseries.json log_histogram_value.json /log_histogram_value.json log_histogram_timeseries.json /log_histogram_timeseries.json -) - -PEERDIR( +) + +PEERDIR( library/cpp/json library/cpp/monlib/consumers - library/cpp/monlib/encode/protobuf + library/cpp/monlib/encode/protobuf library/cpp/resource -) - -END() +) + +END() diff --git a/library/cpp/monlib/encode/json/ya.make b/library/cpp/monlib/encode/json/ya.make index 221fbc2a6a..a50fc412a9 100644 --- a/library/cpp/monlib/encode/json/ya.make +++ b/library/cpp/monlib/encode/json/ya.make @@ -1,21 +1,21 @@ -LIBRARY() - +LIBRARY() + OWNER( g:solomon jamel ) - -SRCS( - json_decoder.cpp - json_encoder.cpp -) - -PEERDIR( + +SRCS( + json_decoder.cpp + json_encoder.cpp +) + +PEERDIR( library/cpp/monlib/encode library/cpp/monlib/encode/buffered library/cpp/monlib/exception library/cpp/json library/cpp/json/writer -) - -END() +) + +END() |