diff options
| author | gusev-p <[email protected]> | 2022-02-10 16:47:20 +0300 | 
|---|---|---|
| committer | Daniil Cherednik <[email protected]> | 2022-02-10 16:47:20 +0300 | 
| commit | 47af3b5bf148ddab250833ec454d30d7c4930c31 (patch) | |
| tree | 9814fbd1c3effac9b8377c5d604b367b14e2db55 /library/cpp/monlib | |
| parent | 1715700d00b30399d3648be821fd585ae552365e (diff) | |
Restoring authorship annotation for <[email protected]>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/monlib')
24 files changed, 850 insertions, 850 deletions
| diff --git a/library/cpp/monlib/counters/counters.h b/library/cpp/monlib/counters/counters.h index 2180cf381d4..038b55f0c87 100644 --- a/library/cpp/monlib/counters/counters.h +++ b/library/cpp/monlib/counters/counters.h @@ -112,10 +112,10 @@ namespace NMonitoring {              return AtomicGet(Value) == 0;          } -        TAtomic& GetAtomic() {  -            return Value;  -        }  -  +        TAtomic& GetAtomic() { +            return Value; +        } +      private:          TAtomic Value;          bool Derivative; diff --git a/library/cpp/monlib/dynamic_counters/encode.cpp b/library/cpp/monlib/dynamic_counters/encode.cpp index f609c6160d2..ffa48d276e8 100644 --- a/library/cpp/monlib/dynamic_counters/encode.cpp +++ b/library/cpp/monlib/dynamic_counters/encode.cpp @@ -113,10 +113,10 @@ namespace NMonitoring {          }      } -    THolder<ICountableConsumer> AsCountableConsumer(IMetricEncoderPtr encoder, TCountableBase::EVisibility visibility) {  -        return MakeHolder<TConsumer>(std::move(encoder), visibility);  -    }  -  +    THolder<ICountableConsumer> AsCountableConsumer(IMetricEncoderPtr encoder, TCountableBase::EVisibility visibility) { +        return MakeHolder<TConsumer>(std::move(encoder), visibility); +    } +      void ToJson(const TDynamicCounters& counters, IOutputStream* out) {          TConsumer consumer{EncoderJson(out), TCountableBase::EVisibility::Public};          counters.Accept(TString{}, TString{}, consumer); diff --git a/library/cpp/monlib/dynamic_counters/encode.h b/library/cpp/monlib/dynamic_counters/encode.h index 1a3b2535189..c79964d7cb7 100644 --- a/library/cpp/monlib/dynamic_counters/encode.h +++ b/library/cpp/monlib/dynamic_counters/encode.h @@ -2,7 +2,7 @@  #include "counters.h" -#include <library/cpp/monlib/encode/encoder.h>  +#include <library/cpp/monlib/encode/encoder.h>  #include <library/cpp/monlib/encode/format.h>  namespace NMonitoring { @@ -13,10 +13,10 @@ namespace NMonitoring {          TCountableBase::EVisibility visibility = TCountableBase::EVisibility::Public      ); -    THolder<ICountableConsumer> AsCountableConsumer(  -        NMonitoring::IMetricEncoderPtr encoder,  -        TCountableBase::EVisibility visibility = TCountableBase::EVisibility::Public);  -  +    THolder<ICountableConsumer> AsCountableConsumer( +        NMonitoring::IMetricEncoderPtr encoder, +        TCountableBase::EVisibility visibility = TCountableBase::EVisibility::Public); +      void ToJson(const TDynamicCounters& counters, IOutputStream* out);      TString ToJson(const TDynamicCounters& counters); diff --git a/library/cpp/monlib/encode/buffered/buffered_encoder_base.cpp b/library/cpp/monlib/encode/buffered/buffered_encoder_base.cpp index 4928fdf70fd..87c832d642b 100644 --- a/library/cpp/monlib/encode/buffered/buffered_encoder_base.cpp +++ b/library/cpp/monlib/encode/buffered/buffered_encoder_base.cpp @@ -1,8 +1,8 @@  #include "buffered_encoder_base.h" -#include <util/string/join.h>  -#include <util/string/builder.h>  -  +#include <util/string/join.h> +#include <util/string/builder.h> +  namespace NMonitoring {  void TBufferedEncoderBase::OnStreamBegin() { @@ -42,8 +42,8 @@ void TBufferedEncoderBase::OnMetricEnd() {                  Y_ENSURE(existing.GetValueType() == metric.TimeSeries.GetValueType(),                      "Time series point type mismatch: expected " << existing.GetValueType() -                    << " but found " << metric.TimeSeries.GetValueType()  -                    << ", labels '" << FormatLabels(metric.Labels) << "'");  +                    << " but found " << metric.TimeSeries.GetValueType() +                    << ", labels '" << FormatLabels(metric.Labels) << "'");                  existing.CopyFrom(metric.TimeSeries);                  Metrics_.pop_back(); @@ -144,27 +144,27 @@ void TBufferedEncoderBase::OnLogHistogram(TInstant time, TLogHistogramSnapshotPt      metric.TimeSeries.Add(time, s.Get());  } -TString TBufferedEncoderBase::FormatLabels(const TPooledLabels& labels) const {  -    auto formattedLabels = TVector<TString>(Reserve(labels.size() + CommonLabels_.size()));  -    auto addLabel = [&](const TPooledLabel& l) {  -        auto formattedLabel = TStringBuilder() << LabelNamesPool_.Get(l.Key) << '=' << LabelValuesPool_.Get(l.Value);  -        formattedLabels.push_back(std::move(formattedLabel));  -    };  -  -    for (const auto& l: labels) {  -        addLabel(l);  -    }  -    for (const auto& l: CommonLabels_) {  -        const auto it = FindIf(labels, [&](const TPooledLabel& label) {  -            return label.Key == l.Key;  -        });  -        if (it == labels.end()) {  -            addLabel(l);  -        }  -    }  -    Sort(formattedLabels);  -  -    return TStringBuilder() << "{" <<  JoinSeq(", ", formattedLabels) << "}";  -}  -  +TString TBufferedEncoderBase::FormatLabels(const TPooledLabels& labels) const { +    auto formattedLabels = TVector<TString>(Reserve(labels.size() + CommonLabels_.size())); +    auto addLabel = [&](const TPooledLabel& l) { +        auto formattedLabel = TStringBuilder() << LabelNamesPool_.Get(l.Key) << '=' << LabelValuesPool_.Get(l.Value); +        formattedLabels.push_back(std::move(formattedLabel)); +    }; + +    for (const auto& l: labels) { +        addLabel(l); +    } +    for (const auto& l: CommonLabels_) { +        const auto it = FindIf(labels, [&](const TPooledLabel& label) { +            return label.Key == l.Key; +        }); +        if (it == labels.end()) { +            addLabel(l); +        } +    } +    Sort(formattedLabels); + +    return TStringBuilder() << "{" <<  JoinSeq(", ", formattedLabels) << "}"; +} +  } // namespace NMonitoring diff --git a/library/cpp/monlib/encode/buffered/buffered_encoder_base.h b/library/cpp/monlib/encode/buffered/buffered_encoder_base.h index 12f838d905c..fe3714e58f0 100644 --- a/library/cpp/monlib/encode/buffered/buffered_encoder_base.h +++ b/library/cpp/monlib/encode/buffered/buffered_encoder_base.h @@ -82,10 +82,10 @@ protected:          TMetricTimeSeries TimeSeries;      }; -protected:  -    TString FormatLabels(const TPooledLabels& labels) const;  -  -protected:  +protected: +    TString FormatLabels(const TPooledLabels& labels) const; + +protected:      TEncoderState State_;      TStringPoolBuilder LabelNamesPool_; diff --git a/library/cpp/monlib/encode/buffered/string_pool.h b/library/cpp/monlib/encode/buffered/string_pool.h index f07d0507299..00e5644608a 100644 --- a/library/cpp/monlib/encode/buffered/string_pool.h +++ b/library/cpp/monlib/encode/buffered/string_pool.h @@ -37,10 +37,10 @@ namespace NMonitoring {              return StrVector_.at(index).first;          } -        TStringBuf Get(const TValue* value) const {  -            return StrVector_.at(value->Index).first;  -        }  -  +        TStringBuf Get(const TValue* value) const { +            return StrVector_.at(value->Index).first; +        } +          template <typename TConsumer>          void ForEach(TConsumer&& c) {              Y_ENSURE(IsBuilt_, "Pool must be sorted first"); diff --git a/library/cpp/monlib/encode/json/json.h b/library/cpp/monlib/encode/json/json.h index f6968c0016c..21530f20c37 100644 --- a/library/cpp/monlib/encode/json/json.h +++ b/library/cpp/monlib/encode/json/json.h @@ -16,14 +16,14 @@ namespace NMonitoring {      /// Buffered encoder will merge series with same labels into one.      IMetricEncoderPtr BufferedEncoderJson(IOutputStream* out, int indentation = 0); -    IMetricEncoderPtr EncoderCloudJson(IOutputStream* out,  -                                       int indentation = 0,  -                                       TStringBuf metricNameLabel = "name");  -  -    IMetricEncoderPtr BufferedEncoderCloudJson(IOutputStream* out,  -                                               int indentation = 0,  -                                               TStringBuf metricNameLabel = "name");  -  +    IMetricEncoderPtr EncoderCloudJson(IOutputStream* out, +                                       int indentation = 0, +                                       TStringBuf metricNameLabel = "name"); + +    IMetricEncoderPtr BufferedEncoderCloudJson(IOutputStream* out, +                                               int indentation = 0, +                                               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 8420b93aab3..d44ff5fd286 100644 --- a/library/cpp/monlib/encode/json/json_decoder.cpp +++ b/library/cpp/monlib/encode/json/json_decoder.cpp @@ -52,7 +52,7 @@ public:          Bounds_.clear();          Values_.clear(); -        InfPresented_ = false;  +        InfPresented_ = false;          return snapshot;      } diff --git a/library/cpp/monlib/encode/json/json_decoder_ut.cpp b/library/cpp/monlib/encode/json/json_decoder_ut.cpp index 52c34e4f375..4464e1d26a4 100644 --- a/library/cpp/monlib/encode/json/json_decoder_ut.cpp +++ b/library/cpp/monlib/encode/json/json_decoder_ut.cpp @@ -120,60 +120,60 @@ Y_UNIT_TEST_SUITE(TJsonDecoderTest) {          ValidateCommonParts(std::move(commonParts), true, true);          ValidateMetrics(collector.Metrics);      } -  -    Y_UNIT_TEST(CanParseHistogramsWithInf) {  -        const char* metricsData = R"({  -"metrics":  -    [  -        {  -            "hist": {  -                "bounds": [  -                    10  -                ],  -                "buckets": [  -                    11  -                ],  -                "inf": 12  -            },  -            "name":"s1",  -            "type": "HIST_RATE"  -        },  -        {  -            "hist": {  -                "bounds": [  -                    20  -                ],  -                "buckets": [  -                    21  -                ]  -            },  -            "name":"s2",  -            "type":"HIST_RATE"  -        }  -    ]  -})";  -        TCollectingConsumer consumer(false);  -        DecodeJson(metricsData, &consumer);  -  -        UNIT_ASSERT_VALUES_EQUAL(consumer.Metrics.size(), 2);  -        {  -            const auto& m = consumer.Metrics[0];  -            UNIT_ASSERT_VALUES_EQUAL(m.Kind, EMetricType::HIST_RATE);  -            UNIT_ASSERT_VALUES_EQUAL(m.Values->Size(), 1);  -            const auto* histogram = (*m.Values)[0].GetValue().AsHistogram();  -            UNIT_ASSERT_VALUES_EQUAL(histogram->Count(), 2);  -            UNIT_ASSERT_VALUES_EQUAL(histogram->UpperBound(1), Max<TBucketBound>());  -            UNIT_ASSERT_VALUES_EQUAL(histogram->Value(0), 11);  -            UNIT_ASSERT_VALUES_EQUAL(histogram->Value(1), 12);  -        }  -        {  -            const auto& m = consumer.Metrics[1];  -            UNIT_ASSERT_VALUES_EQUAL(m.Kind, EMetricType::HIST_RATE);  -            UNIT_ASSERT_VALUES_EQUAL(m.Values->Size(), 1);  -            const auto* histogram = (*m.Values)[0].GetValue().AsHistogram();  -            UNIT_ASSERT_VALUES_EQUAL(histogram->Count(), 1);  -            UNIT_ASSERT_VALUES_EQUAL(histogram->UpperBound(0), 20);  -            UNIT_ASSERT_VALUES_EQUAL(histogram->Value(0), 21);  -        }  -    }  + +    Y_UNIT_TEST(CanParseHistogramsWithInf) { +        const char* metricsData = R"({ +"metrics": +    [ +        { +            "hist": { +                "bounds": [ +                    10 +                ], +                "buckets": [ +                    11 +                ], +                "inf": 12 +            }, +            "name":"s1", +            "type": "HIST_RATE" +        }, +        { +            "hist": { +                "bounds": [ +                    20 +                ], +                "buckets": [ +                    21 +                ] +            }, +            "name":"s2", +            "type":"HIST_RATE" +        } +    ] +})"; +        TCollectingConsumer consumer(false); +        DecodeJson(metricsData, &consumer); + +        UNIT_ASSERT_VALUES_EQUAL(consumer.Metrics.size(), 2); +        { +            const auto& m = consumer.Metrics[0]; +            UNIT_ASSERT_VALUES_EQUAL(m.Kind, EMetricType::HIST_RATE); +            UNIT_ASSERT_VALUES_EQUAL(m.Values->Size(), 1); +            const auto* histogram = (*m.Values)[0].GetValue().AsHistogram(); +            UNIT_ASSERT_VALUES_EQUAL(histogram->Count(), 2); +            UNIT_ASSERT_VALUES_EQUAL(histogram->UpperBound(1), Max<TBucketBound>()); +            UNIT_ASSERT_VALUES_EQUAL(histogram->Value(0), 11); +            UNIT_ASSERT_VALUES_EQUAL(histogram->Value(1), 12); +        } +        { +            const auto& m = consumer.Metrics[1]; +            UNIT_ASSERT_VALUES_EQUAL(m.Kind, EMetricType::HIST_RATE); +            UNIT_ASSERT_VALUES_EQUAL(m.Values->Size(), 1); +            const auto* histogram = (*m.Values)[0].GetValue().AsHistogram(); +            UNIT_ASSERT_VALUES_EQUAL(histogram->Count(), 1); +            UNIT_ASSERT_VALUES_EQUAL(histogram->UpperBound(0), 20); +            UNIT_ASSERT_VALUES_EQUAL(histogram->Value(0), 21); +        } +    }  } diff --git a/library/cpp/monlib/encode/json/json_encoder.cpp b/library/cpp/monlib/encode/json/json_encoder.cpp index fb14ffdf077..20d2bb6283f 100644 --- a/library/cpp/monlib/encode/json/json_encoder.cpp +++ b/library/cpp/monlib/encode/json/json_encoder.cpp @@ -14,21 +14,21 @@  namespace NMonitoring {      namespace { -        enum class EJsonStyle {  -            Solomon,  -            Cloud  -        };  -  +        enum class EJsonStyle { +            Solomon, +            Cloud +        }; +          ///////////////////////////////////////////////////////////////////////          // TJsonWriter          ///////////////////////////////////////////////////////////////////////          class TJsonWriter {          public: -            TJsonWriter(IOutputStream* out, int indentation, EJsonStyle style, TStringBuf metricNameLabel)  +            TJsonWriter(IOutputStream* out, int indentation, EJsonStyle style, TStringBuf metricNameLabel)                  : Buf_(NJsonWriter::HEM_UNSAFE, out) -                , Style_(style)  -                , MetricNameLabel_(metricNameLabel)  -                , CurrentMetricName_()  +                , Style_(style) +                , MetricNameLabel_(metricNameLabel) +                , CurrentMetricName_()              {                  Buf_.SetIndentSpaces(indentation);                  Buf_.SetWriteNanAsString(); @@ -37,11 +37,11 @@ namespace NMonitoring {              void WriteTime(TInstant time) {                  if (time != TInstant::Zero()) {                      Buf_.WriteKey(TStringBuf("ts")); -                    if (Style_ == EJsonStyle::Solomon) {  -                        Buf_.WriteULongLong(time.Seconds());  -                    } else {  -                        Buf_.WriteString(time.ToString());  -                    }  +                    if (Style_ == EJsonStyle::Solomon) { +                        Buf_.WriteULongLong(time.Seconds()); +                    } else { +                        Buf_.WriteString(time.ToString()); +                    }                  }              } @@ -61,8 +61,8 @@ namespace NMonitoring {              }              void WriteValue(IHistogramSnapshot* s) { -                Y_ENSURE(Style_ == EJsonStyle::Solomon);  -  +                Y_ENSURE(Style_ == EJsonStyle::Solomon); +                  Buf_.WriteKey(TStringBuf("hist"));                  Buf_.BeginObject();                  if (ui32 count = s->Count()) { @@ -94,8 +94,8 @@ namespace NMonitoring {              }              void WriteValue(ISummaryDoubleSnapshot* s) { -                Y_ENSURE(Style_ == EJsonStyle::Solomon);  -  +                Y_ENSURE(Style_ == EJsonStyle::Solomon); +                  Buf_.WriteKey(TStringBuf("summary"));                  Buf_.BeginObject(); @@ -118,8 +118,8 @@ namespace NMonitoring {              }              void WriteValue(TLogHistogramSnapshot* s) { -                Y_ENSURE(Style_ == EJsonStyle::Solomon);  -  +                Y_ENSURE(Style_ == EJsonStyle::Solomon); +                  Buf_.WriteKey(TStringBuf("log_hist"));                  Buf_.BeginObject(); @@ -169,64 +169,64 @@ namespace NMonitoring {                          break;                      case EMetricValueType::UNKNOWN: -                        ythrow yexception() << "unknown metric value type";  +                        ythrow yexception() << "unknown metric value type";                  }              }              void WriteLabel(TStringBuf name, TStringBuf value) {                  Y_ENSURE(IsUtf(name), "label name is not valid UTF-8 string");                  Y_ENSURE(IsUtf(value), "label value is not valid UTF-8 string"); -                if (Style_ == EJsonStyle::Cloud && name == MetricNameLabel_) {  -                    CurrentMetricName_ = value;  -                } else {  -                    Buf_.WriteKey(name);  -                    Buf_.WriteString(value);  -                }  -            } - -            void WriteMetricType(EMetricType type) {  -                if (Style_ == EJsonStyle::Cloud) {  -                    Buf_.WriteKey("type");  -                    Buf_.WriteString(MetricTypeToCloudStr(type));  -                } else {  -                    Buf_.WriteKey("kind");  -                    Buf_.WriteString(MetricTypeToStr(type));  -                }  -            }  -  -            void WriteName() {  -                if (Style_ != EJsonStyle::Cloud) {  -                    return;  -                }  -                if (CurrentMetricName_.Empty()) {  -                    ythrow yexception() << "label '" << MetricNameLabel_ << "' is not defined";  -                }  -                Buf_.WriteKey("name");  -                Buf_.WriteString(CurrentMetricName_);  -                CurrentMetricName_.clear();  -            }  -  -        private:  -            static TStringBuf MetricTypeToCloudStr(EMetricType type) {  -                switch (type) {  -                    case EMetricType::GAUGE:  -                        return TStringBuf("DGAUGE");  -                    case EMetricType::COUNTER:  -                        return TStringBuf("COUNTER");  -                    case EMetricType::RATE:  -                        return TStringBuf("RATE");  -                    case EMetricType::IGAUGE:  -                        return TStringBuf("IGAUGE");  -                    default:  -                        ythrow yexception() << "metric type '" << type << "' is not supported by cloud json format";  -                }  -            }  -  +                if (Style_ == EJsonStyle::Cloud && name == MetricNameLabel_) { +                    CurrentMetricName_ = value; +                } else { +                    Buf_.WriteKey(name); +                    Buf_.WriteString(value); +                } +            } + +            void WriteMetricType(EMetricType type) { +                if (Style_ == EJsonStyle::Cloud) { +                    Buf_.WriteKey("type"); +                    Buf_.WriteString(MetricTypeToCloudStr(type)); +                } else { +                    Buf_.WriteKey("kind"); +                    Buf_.WriteString(MetricTypeToStr(type)); +                } +            } + +            void WriteName() { +                if (Style_ != EJsonStyle::Cloud) { +                    return; +                } +                if (CurrentMetricName_.Empty()) { +                    ythrow yexception() << "label '" << MetricNameLabel_ << "' is not defined"; +                } +                Buf_.WriteKey("name"); +                Buf_.WriteString(CurrentMetricName_); +                CurrentMetricName_.clear(); +            } + +        private: +            static TStringBuf MetricTypeToCloudStr(EMetricType type) { +                switch (type) { +                    case EMetricType::GAUGE: +                        return TStringBuf("DGAUGE"); +                    case EMetricType::COUNTER: +                        return TStringBuf("COUNTER"); +                    case EMetricType::RATE: +                        return TStringBuf("RATE"); +                    case EMetricType::IGAUGE: +                        return TStringBuf("IGAUGE"); +                    default: +                        ythrow yexception() << "metric type '" << type << "' is not supported by cloud json format"; +                } +            } +          protected:              NJsonWriter::TBuf Buf_; -            EJsonStyle Style_;  -            TString MetricNameLabel_;  -            TString CurrentMetricName_;  +            EJsonStyle Style_; +            TString MetricNameLabel_; +            TString CurrentMetricName_;          };          /////////////////////////////////////////////////////////////////////// @@ -234,8 +234,8 @@ namespace NMonitoring {          ///////////////////////////////////////////////////////////////////////          class TEncoderJson final: public IMetricEncoder, public TJsonWriter {          public: -            TEncoderJson(IOutputStream* out, int indentation, EJsonStyle style, TStringBuf metricNameLabel)  -                : TJsonWriter{out, indentation, style, metricNameLabel}  +            TEncoderJson(IOutputStream* out, int indentation, EJsonStyle style, TStringBuf metricNameLabel) +                : TJsonWriter{out, indentation, style, metricNameLabel}              {              } @@ -267,11 +267,11 @@ namespace NMonitoring {                  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_.WriteKey(TStringBuf(Style_ == EJsonStyle::Solomon ? "sensors" : "metrics"));                      Buf_.BeginList();                  }                  Buf_.BeginObject(); -                WriteMetricType(type);  +                WriteMetricType(type);              }              void OnMetricEnd() override { @@ -300,7 +300,7 @@ namespace NMonitoring {                  }                  if (State_ == TEncoderState::EState::ROOT) {                      State_ = TEncoderState::EState::COMMON_LABELS; -                    Buf_.WriteKey(TStringBuf(Style_ == EJsonStyle::Solomon ? "commonLabels" : "labels"));  +                    Buf_.WriteKey(TStringBuf(Style_ == EJsonStyle::Solomon ? "commonLabels" : "labels"));                  } else if (State_ == TEncoderState::EState::METRIC) {                      State_ = TEncoderState::EState::METRIC_LABELS;                      Buf_.WriteKey(TStringBuf("labels")); @@ -323,9 +323,9 @@ namespace NMonitoring {                  Y_ENSURE(!EmptyLabels_, "Labels cannot be empty");                  Buf_.EndObject(); -                if (State_ == TEncoderState::EState::METRIC) {  -                    WriteName();  -                }  +                if (State_ == TEncoderState::EState::METRIC) { +                    WriteName(); +                }              }              void OnLabel(TStringBuf name, TStringBuf value) override { @@ -420,8 +420,8 @@ namespace NMonitoring {          ///////////////////////////////////////////////////////////////////////          class TBufferedJsonEncoder : public TBufferedEncoderBase, public TJsonWriter {          public: -            TBufferedJsonEncoder(IOutputStream* out, int indentation, EJsonStyle style, TStringBuf metricNameLabel)  -                : TJsonWriter{out, indentation, style, metricNameLabel}  +            TBufferedJsonEncoder(IOutputStream* out, int indentation, EJsonStyle style, TStringBuf metricNameLabel) +                : TJsonWriter{out, indentation, style, metricNameLabel}              {                  MetricsMergingMode_ = EMetricsMergingMode::MERGE_METRICS;              } @@ -464,12 +464,12 @@ namespace NMonitoring {                  WriteTime(CommonTime_);                  if (CommonLabels_.size() > 0) { -                    Buf_.WriteKey(TStringBuf(Style_ == EJsonStyle::Solomon ? "commonLabels": "labels"));  -                    WriteLabels(CommonLabels_, true);  +                    Buf_.WriteKey(TStringBuf(Style_ == EJsonStyle::Solomon ? "commonLabels": "labels")); +                    WriteLabels(CommonLabels_, true);                  }                  if (Metrics_.size() > 0) { -                    Buf_.WriteKey(TStringBuf(Style_ == EJsonStyle::Solomon ? "sensors" : "metrics"));  +                    Buf_.WriteKey(TStringBuf(Style_ == EJsonStyle::Solomon ? "sensors" : "metrics"));                      WriteMetrics();                  } @@ -488,10 +488,10 @@ namespace NMonitoring {              void WriteMetric(TMetric& metric) {                  Buf_.BeginObject(); -                WriteMetricType(metric.MetricType);  +                WriteMetricType(metric.MetricType);                  Buf_.WriteKey(TStringBuf("labels")); -                WriteLabels(metric.Labels, false);  +                WriteLabels(metric.Labels, false);                  metric.TimeSeries.SortByTs();                  if (metric.TimeSeries.Size() == 1) { @@ -515,7 +515,7 @@ namespace NMonitoring {                  Buf_.EndObject();              } -            void WriteLabels(const TPooledLabels& labels, bool isCommon) {  +            void WriteLabels(const TPooledLabels& labels, bool isCommon) {                  Buf_.BeginObject();                  for (auto i = 0u; i < labels.size(); ++i) { @@ -526,10 +526,10 @@ namespace NMonitoring {                  }                  Buf_.EndObject(); -  -                if (!isCommon) {  -                    WriteName();  -                }  + +                if (!isCommon) { +                    WriteName(); +                }              }          private: @@ -539,18 +539,18 @@ namespace NMonitoring {      }      IMetricEncoderPtr EncoderJson(IOutputStream* out, int indentation) { -        return MakeHolder<TEncoderJson>(out, indentation, EJsonStyle::Solomon, "");  +        return MakeHolder<TEncoderJson>(out, indentation, EJsonStyle::Solomon, "");      }      IMetricEncoderPtr BufferedEncoderJson(IOutputStream* out, int indentation) { -        return MakeHolder<TBufferedJsonEncoder>(out, indentation, EJsonStyle::Solomon, "");  +        return MakeHolder<TBufferedJsonEncoder>(out, indentation, EJsonStyle::Solomon, ""); +    } + +    IMetricEncoderPtr EncoderCloudJson(IOutputStream* out, int indentation, TStringBuf metricNameLabel) { +        return MakeHolder<TEncoderJson>(out, indentation, EJsonStyle::Cloud, metricNameLabel); +    } + +    IMetricEncoderPtr BufferedEncoderCloudJson(IOutputStream* out, int indentation, TStringBuf metricNameLabel) { +        return MakeHolder<TBufferedJsonEncoder>(out, indentation, EJsonStyle::Cloud, metricNameLabel);      } -  -    IMetricEncoderPtr EncoderCloudJson(IOutputStream* out, int indentation, TStringBuf metricNameLabel) {  -        return MakeHolder<TEncoderJson>(out, indentation, EJsonStyle::Cloud, metricNameLabel);  -    }  -  -    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 9bd38e5fc58..09e79092890 100644 --- a/library/cpp/monlib/encode/json/json_ut.cpp +++ b/library/cpp/monlib/encode/json/json_ut.cpp @@ -8,7 +8,7 @@  #include <library/cpp/testing/unittest/registar.h>  #include <util/stream/str.h> -#include <util/string/builder.h>  +#include <util/string/builder.h>  #include <limits> @@ -136,171 +136,171 @@ 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;  -            TStringOutput out(json);  -            auto e = cloud  -                ? (buffered ? BufferedEncoderCloudJson(&out, 2, "metric") : EncoderCloudJson(&out, 2, "metric"))  -                : (buffered ? BufferedEncoderJson(&out, 2) : EncoderJson(&out, 2));  -            e->OnStreamBegin();  -            { // common time  -                e->OnCommonTime(TInstant::Seconds(1500000000));  +        auto check = [](bool cloud, bool buffered, TStringBuf expectedResourceKey) { +            TString json; +            TStringOutput out(json); +            auto e = cloud +                ? (buffered ? BufferedEncoderCloudJson(&out, 2, "metric") : EncoderCloudJson(&out, 2, "metric")) +                : (buffered ? BufferedEncoderJson(&out, 2) : EncoderJson(&out, 2)); +            e->OnStreamBegin(); +            { // common time +                e->OnCommonTime(TInstant::Seconds(1500000000));              } -            { // common labels  +            { // common labels                  e->OnLabelsBegin(); -                e->OnLabel("project", "solomon");  +                e->OnLabel("project", "solomon");                  e->OnLabelsEnd();              } -            { // metric #1  -                e->OnMetricBegin(EMetricType::COUNTER);  -                {  -                    e->OnLabelsBegin();  -                    e->OnLabel("metric", "single");  -                    e->OnLabel("labels", "l1");  -                    e->OnLabelsEnd();  -                }  -                e->OnUint64(now, 17);  -                e->OnMetricEnd();  +            { // metric #1 +                e->OnMetricBegin(EMetricType::COUNTER); +                { +                    e->OnLabelsBegin(); +                    e->OnLabel("metric", "single"); +                    e->OnLabel("labels", "l1"); +                    e->OnLabelsEnd(); +                } +                e->OnUint64(now, 17); +                e->OnMetricEnd(); +            } +            { // metric #2 +                e->OnMetricBegin(EMetricType::RATE); +                { +                    e->OnLabelsBegin(); +                    e->OnLabel("metric", "single"); +                    e->OnLabel("labels", "l2"); +                    e->OnLabelsEnd(); +                } +                e->OnUint64(now, 17); +                e->OnMetricEnd(); +            } +            { // metric #3 +                e->OnMetricBegin(EMetricType::GAUGE); +                e->OnDouble(now, 3.14); +                { +                    e->OnLabelsBegin(); +                    e->OnLabel("metric", "single"); +                    e->OnLabel("labels", "l3"); +                    e->OnLabelsEnd(); +                } +                e->OnMetricEnd(); +            } +            { // metric #4 +                e->OnMetricBegin(EMetricType::IGAUGE); +                e->OnInt64(now, 42); +                { +                    e->OnLabelsBegin(); +                    e->OnLabel("metric", "single_igauge"); +                    e->OnLabel("labels", "l4"); +                    e->OnLabelsEnd(); +                } +                e->OnMetricEnd();              } -            { // metric #2  -                e->OnMetricBegin(EMetricType::RATE);  -                {  -                    e->OnLabelsBegin();  -                    e->OnLabel("metric", "single");  -                    e->OnLabel("labels", "l2");  -                    e->OnLabelsEnd();  -                }  -                e->OnUint64(now, 17);  -                e->OnMetricEnd();  +            { // metric #5 +                e->OnMetricBegin(EMetricType::GAUGE); +                { +                    e->OnLabelsBegin(); +                    e->OnLabel("metric", "multiple"); +                    e->OnLabel("labels", "l5"); +                    e->OnLabelsEnd(); +                } +                e->OnDouble(now, std::numeric_limits<double>::quiet_NaN()); +                e->OnDouble(now + TDuration::Seconds(15), std::numeric_limits<double>::infinity()); +                e->OnDouble(now + TDuration::Seconds(30), -std::numeric_limits<double>::infinity()); +                e->OnMetricEnd();              } -            { // metric #3  -                e->OnMetricBegin(EMetricType::GAUGE);  -                e->OnDouble(now, 3.14);  -                {  -                    e->OnLabelsBegin();  -                    e->OnLabel("metric", "single");  -                    e->OnLabel("labels", "l3");  -                    e->OnLabelsEnd();  -                }  -                e->OnMetricEnd();  -            }  -            { // metric #4  -                e->OnMetricBegin(EMetricType::IGAUGE);  -                e->OnInt64(now, 42);  -                {  -                    e->OnLabelsBegin();  -                    e->OnLabel("metric", "single_igauge");  -                    e->OnLabel("labels", "l4");  -                    e->OnLabelsEnd();  -                }  -                e->OnMetricEnd();  -            }  -            { // metric #5  -                e->OnMetricBegin(EMetricType::GAUGE);  -                {  -                    e->OnLabelsBegin();  -                    e->OnLabel("metric", "multiple");  -                    e->OnLabel("labels", "l5");  -                    e->OnLabelsEnd();  -                }  -                e->OnDouble(now, std::numeric_limits<double>::quiet_NaN());  -                e->OnDouble(now + TDuration::Seconds(15), std::numeric_limits<double>::infinity());  -                e->OnDouble(now + TDuration::Seconds(30), -std::numeric_limits<double>::infinity());  -                e->OnMetricEnd();  -            }  -  -            { // metric #6  -                e->OnMetricBegin(EMetricType::COUNTER);  -                e->OnUint64(now, 1337);  -                e->OnUint64(now + TDuration::Seconds(15), 1338);  -                {  -                    e->OnLabelsBegin();  -                    e->OnLabel("metric", "multiple");  -                    e->OnLabel("labels", "l6");  -                    e->OnLabelsEnd();  -                }  -                e->OnMetricEnd();  -            }  -            e->OnStreamEnd();  -            e->Close();  -            json += "\n";  -  + +            { // metric #6 +                e->OnMetricBegin(EMetricType::COUNTER); +                e->OnUint64(now, 1337); +                e->OnUint64(now + TDuration::Seconds(15), 1338); +                { +                    e->OnLabelsBegin(); +                    e->OnLabel("metric", "multiple"); +                    e->OnLabel("labels", "l6"); +                    e->OnLabelsEnd(); +                } +                e->OnMetricEnd(); +            } +            e->OnStreamEnd(); +            e->Close(); +            json += "\n"; +              auto parseJson = [] (auto buf) {                  NJson::TJsonValue value;                  NJson::ReadJsonTree(buf, &value, true);                  return value;              }; -            const auto expectedJson = NResource::Find(expectedResourceKey);  +            const auto expectedJson = NResource::Find(expectedResourceKey);              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");  -    }  -  -    TLogHistogramSnapshotPtr TestLogHistogram(ui32 v = 1) {  -        TVector<double> buckets{0.5 * v, 0.25 * v, 0.25 * v, 0.5 * v};  -        return MakeIntrusive<TLogHistogramSnapshot>(1.5, 1u, 0, std::move(buckets));  -    }  -  -    Y_UNIT_TEST(HistogramAndSummaryMetricTypesAreNotSupportedByCloudJson) {  -        const TInstant now = TInstant::ParseIso8601Deprecated("2017-11-05T01:02:03Z");  -  -        auto emit = [&](IMetricEncoder* encoder, EMetricType metricType) {  -            encoder->OnStreamBegin();  +        }; + +        check(false, false, "/expected.json"); +        check(false, true, "/expected_buffered.json"); +        check(true, false, "/expected_cloud.json"); +        check(true, true, "/expected_cloud_buffered.json"); +    } + +    TLogHistogramSnapshotPtr TestLogHistogram(ui32 v = 1) { +        TVector<double> buckets{0.5 * v, 0.25 * v, 0.25 * v, 0.5 * v}; +        return MakeIntrusive<TLogHistogramSnapshot>(1.5, 1u, 0, std::move(buckets)); +    } + +    Y_UNIT_TEST(HistogramAndSummaryMetricTypesAreNotSupportedByCloudJson) { +        const TInstant now = TInstant::ParseIso8601Deprecated("2017-11-05T01:02:03Z"); + +        auto emit = [&](IMetricEncoder* encoder, EMetricType metricType) { +            encoder->OnStreamBegin();              { -                encoder->OnMetricBegin(metricType);  -                {  -                    encoder->OnLabelsBegin();  -                    encoder->OnLabel("name", "m");  -                    encoder->OnLabelsEnd();  -                }  -  -                switch (metricType) {  -                    case EMetricType::HIST: {  -                        auto histogram = ExponentialHistogram(6, 2);  -                        encoder->OnHistogram(now, histogram->Snapshot());  -                        break;  -                    }  -                    case EMetricType::LOGHIST: {  -                        auto histogram = TestLogHistogram();  -                        encoder->OnLogHistogram(now, histogram);  -                        break;  -                    }  -                    case EMetricType::DSUMMARY: {  -                        auto summary = MakeIntrusive<TSummaryDoubleSnapshot>(10., -0.5, 0.5, 0.3, 30u);  -                        encoder->OnSummaryDouble(now, summary);  -                        break;  -                    }  -                    default:  -                        Y_FAIL("unexpected metric type [%s]", ToString(metricType).c_str());  -                }  -  -                encoder->OnMetricEnd();  +                encoder->OnMetricBegin(metricType); +                { +                    encoder->OnLabelsBegin(); +                    encoder->OnLabel("name", "m"); +                    encoder->OnLabelsEnd(); +                } + +                switch (metricType) { +                    case EMetricType::HIST: { +                        auto histogram = ExponentialHistogram(6, 2); +                        encoder->OnHistogram(now, histogram->Snapshot()); +                        break; +                    } +                    case EMetricType::LOGHIST: { +                        auto histogram = TestLogHistogram(); +                        encoder->OnLogHistogram(now, histogram); +                        break; +                    } +                    case EMetricType::DSUMMARY: { +                        auto summary = MakeIntrusive<TSummaryDoubleSnapshot>(10., -0.5, 0.5, 0.3, 30u); +                        encoder->OnSummaryDouble(now, summary); +                        break; +                    } +                    default: +                        Y_FAIL("unexpected metric type [%s]", ToString(metricType).c_str()); +                } + +                encoder->OnMetricEnd();              } -            encoder->OnStreamEnd();  -            encoder->Close();  -        };  - -        auto doTest = [&](bool buffered, EMetricType metricType) {  -            TString json;  -            TStringOutput out(json);  -            auto encoder = buffered ? BufferedEncoderCloudJson(&out, 2) : EncoderCloudJson(&out, 2);  -            const TString expectedMessage = TStringBuilder()  -                << "metric type '" << metricType << "' is not supported by cloud json format";  -            UNIT_ASSERT_EXCEPTION_CONTAINS_C(emit(encoder.Get(), metricType), yexception, expectedMessage,  -                                             TString("buffered: ") + ToString(buffered));  -        };  -  -        doTest(false, EMetricType::HIST);  -        doTest(false, EMetricType::LOGHIST);  -        doTest(false, EMetricType::DSUMMARY);  -        doTest(true, EMetricType::HIST);  -        doTest(true, EMetricType::LOGHIST);  -        doTest(true, EMetricType::DSUMMARY);  +            encoder->OnStreamEnd(); +            encoder->Close(); +        }; + +        auto doTest = [&](bool buffered, EMetricType metricType) { +            TString json; +            TStringOutput out(json); +            auto encoder = buffered ? BufferedEncoderCloudJson(&out, 2) : EncoderCloudJson(&out, 2); +            const TString expectedMessage = TStringBuilder() +                << "metric type '" << metricType << "' is not supported by cloud json format"; +            UNIT_ASSERT_EXCEPTION_CONTAINS_C(emit(encoder.Get(), metricType), yexception, expectedMessage, +                                             TString("buffered: ") + ToString(buffered)); +        }; + +        doTest(false, EMetricType::HIST); +        doTest(false, EMetricType::LOGHIST); +        doTest(false, EMetricType::DSUMMARY); +        doTest(true, EMetricType::HIST); +        doTest(true, EMetricType::LOGHIST); +        doTest(true, EMetricType::DSUMMARY);      }      Y_UNIT_TEST(MetricsWithDifferentLabelOrderGetMerged) { @@ -369,13 +369,13 @@ Y_UNIT_TEST_SUITE(TJsonTest) {          UNIT_ASSERT_VALUES_EQUAL(samples.CommonLabelsSize(), 1);          AssertLabelEqual(samples.GetCommonLabels(0), "project", "solomon"); -        UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 6);  +        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"); -            AssertLabelEqual(s.GetLabels(1), "labels", "l1");  +            AssertLabelEqual(s.GetLabels(1), "labels", "l1");              UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1);              AssertPointEqual(s.GetPoints(0), now, ui64(17)); @@ -385,7 +385,7 @@ Y_UNIT_TEST_SUITE(TJsonTest) {              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");  +            AssertLabelEqual(s.GetLabels(1), "labels", "l2");              UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1);              AssertPointEqual(s.GetPoints(0), now, ui64(17)); @@ -395,27 +395,27 @@ Y_UNIT_TEST_SUITE(TJsonTest) {              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");  +            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_EQUAL(s.GetMetricType(), NProto::IGAUGE);  -            UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 2);  -            AssertLabelEqual(s.GetLabels(0), "metric", "single_igauge");  -            AssertLabelEqual(s.GetLabels(1), "labels", "l4");  -  -            UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1);  -            AssertPointEqual(s.GetPoints(0), now, i64(42));  -        }  -        {  -            const NProto::TMultiSample& s = samples.GetSamples(4);  +            UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::IGAUGE); +            UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 2); +            AssertLabelEqual(s.GetLabels(0), "metric", "single_igauge"); +            AssertLabelEqual(s.GetLabels(1), "labels", "l4"); + +            UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); +            AssertPointEqual(s.GetPoints(0), now, i64(42)); +        } +        { +            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"); -            AssertLabelEqual(s.GetLabels(1), "labels", "l5");  +            AssertLabelEqual(s.GetLabels(1), "labels", "l5");              UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 3);              AssertPointEqualNan(s.GetPoints(0), now); @@ -423,11 +423,11 @@ Y_UNIT_TEST_SUITE(TJsonTest) {              AssertPointEqualInf(s.GetPoints(2), now + TDuration::Seconds(30), -11);          }          { -            const NProto::TMultiSample& s = samples.GetSamples(5);  +            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"); -            AssertLabelEqual(s.GetLabels(1), "labels", "l6");  +            AssertLabelEqual(s.GetLabels(1), "labels", "l6");              UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 2);              AssertPointEqual(s.GetPoints(0), now, ui64(1337)); @@ -1158,61 +1158,61 @@ Y_UNIT_TEST_SUITE(TJsonTest) {          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);  -            {  -                encoder.OnLabelsBegin();  -                encoder.OnLabel("name", "m");  -                encoder.OnLabel("l1", "v1");  -                encoder.OnLabel("l2", "v2");  -                encoder.OnLabelsEnd();  -            }  -            encoder.OnDouble(now, 1.0);  -            encoder.OnMetricEnd();  -  -            encoder.OnMetricBegin(EMetricType::COUNTER);  -            {  -                encoder.OnLabelsBegin();  -                encoder.OnLabel("name", "m");  -                encoder.OnLabel("l1", "v1");  -                encoder.OnLabel("l2", "v2");  -                encoder.OnLabelsEnd();  -            }  -            encoder.OnUint64(now, 1);  -  -            UNIT_ASSERT_EXCEPTION_CONTAINS(encoder.OnMetricEnd(),  -                yexception,  -                expectedError);  -        };  -  -        {  -            TStringStream out;  -            auto encoder = BufferedEncoderJson(&out);  -  -            encoder->OnStreamBegin();  -            encoder->OnLabelsBegin();  -            encoder->OnLabel("c", "cv");  -            encoder->OnLabelsEnd();  -            emitMetrics(*encoder,  -                "Time series point type mismatch: expected DOUBLE but found UINT64, "  -                "labels '{c=cv, l1=v1, l2=v2, name=m}'");  -        }  -  -        {  -            TStringStream out;  -            auto encoder = BufferedEncoderJson(&out);  -  -            encoder->OnStreamBegin();  -            encoder->OnLabelsBegin();  -            encoder->OnLabel("l1", "v100");  -            encoder->OnLabelsEnd();  -            emitMetrics(*encoder,  -                "Time series point type mismatch: expected DOUBLE but found UINT64, "  -                "labels '{l1=v1, l2=v2, name=m}'");  -        }  -    }  -  +    Y_UNIT_TEST(InconsistentMetricTypes) { +        auto emitMetrics = [](IMetricEncoder& encoder, const TString& expectedError) { +            encoder.OnMetricBegin(EMetricType::GAUGE); +            { +                encoder.OnLabelsBegin(); +                encoder.OnLabel("name", "m"); +                encoder.OnLabel("l1", "v1"); +                encoder.OnLabel("l2", "v2"); +                encoder.OnLabelsEnd(); +            } +            encoder.OnDouble(now, 1.0); +            encoder.OnMetricEnd(); + +            encoder.OnMetricBegin(EMetricType::COUNTER); +            { +                encoder.OnLabelsBegin(); +                encoder.OnLabel("name", "m"); +                encoder.OnLabel("l1", "v1"); +                encoder.OnLabel("l2", "v2"); +                encoder.OnLabelsEnd(); +            } +            encoder.OnUint64(now, 1); + +            UNIT_ASSERT_EXCEPTION_CONTAINS(encoder.OnMetricEnd(), +                yexception, +                expectedError); +        }; + +        { +            TStringStream out; +            auto encoder = BufferedEncoderJson(&out); + +            encoder->OnStreamBegin(); +            encoder->OnLabelsBegin(); +            encoder->OnLabel("c", "cv"); +            encoder->OnLabelsEnd(); +            emitMetrics(*encoder, +                "Time series point type mismatch: expected DOUBLE but found UINT64, " +                "labels '{c=cv, l1=v1, l2=v2, name=m}'"); +        } + +        { +            TStringStream out; +            auto encoder = BufferedEncoderJson(&out); + +            encoder->OnStreamBegin(); +            encoder->OnLabelsBegin(); +            encoder->OnLabel("l1", "v100"); +            encoder->OnLabelsEnd(); +            emitMetrics(*encoder, +                "Time series point type mismatch: expected DOUBLE but found UINT64, " +                "labels '{l1=v1, l2=v2, name=m}'"); +        } +    } +      Y_UNIT_TEST(IntGaugeDecode) {          NProto::TMultiSamplesList samples;          { diff --git a/library/cpp/monlib/encode/json/ut/expected.json b/library/cpp/monlib/encode/json/ut/expected.json index 30c405b0bde..ead853455ba 100644 --- a/library/cpp/monlib/encode/json/ut/expected.json +++ b/library/cpp/monlib/encode/json/ut/expected.json @@ -11,7 +11,7 @@          "labels":            {              "metric":"single", -            "labels":"l1"  +            "labels":"l1"            },          "ts":1509843723,          "value":17 @@ -21,7 +21,7 @@          "labels":            {              "metric":"single", -            "labels":"l2"  +            "labels":"l2"            },          "ts":1509843723,          "value":17 @@ -31,27 +31,27 @@          "labels":            {              "metric":"single", -            "labels":"l3"  +            "labels":"l3"            },          "ts":1509843723,          "value":3.14        },        { -        "kind":"IGAUGE",  -        "labels":  -          {  -            "metric":"single_igauge",  -            "labels":"l4"  -          },  -        "ts":1509843723,  -        "value":42  -      },  -      {  +        "kind":"IGAUGE", +        "labels": +          { +            "metric":"single_igauge", +            "labels":"l4" +          }, +        "ts":1509843723, +        "value":42 +      }, +      {          "kind":"GAUGE",          "labels":            {              "metric":"multiple", -            "labels":"l5"  +            "labels":"l5"            },          "timeseries":            [ @@ -85,7 +85,7 @@          "labels":            {              "metric":"multiple", -            "labels":"l6"  +            "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 0a338424379..9a6a1d6201d 100644 --- a/library/cpp/monlib/encode/json/ut/expected_buffered.json +++ b/library/cpp/monlib/encode/json/ut/expected_buffered.json @@ -37,16 +37,16 @@          "value":3.14        },        { -        "kind":"IGAUGE",  -        "labels":  -          {  +        "kind":"IGAUGE", +        "labels": +          {              "labels":"l4",              "metric":"single_igauge" -          },  -        "ts":1509843723,  -        "value":42  -      },  -      {  +          }, +        "ts":1509843723, +        "value":42 +      }, +      {          "kind":"GAUGE",          "labels":            { @@ -71,11 +71,11 @@        },        {          "kind":"COUNTER", -        "labels":  -          {  +        "labels": +          {              "labels":"l6",              "metric":"multiple" -          },  +          },          "timeseries":            [              { @@ -86,7 +86,7 @@                "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 78e90ec3bef..61848115790 100644 --- a/library/cpp/monlib/encode/json/ut/expected_cloud.json +++ b/library/cpp/monlib/encode/json/ut/expected_cloud.json @@ -1,92 +1,92 @@  { -  "ts":"2017-07-14T02:40:00.000000Z",  -  "labels":  +  "ts":"2017-07-14T02:40:00.000000Z", +  "labels":      {        "project":"solomon"      }, -  "metrics":  +  "metrics":      [        { -        "type":"COUNTER",  +        "type":"COUNTER",          "labels":            { -            "labels":"l1"  +            "labels":"l1"            }, -        "name":"single",  -        "ts":"2017-11-05T01:02:03.000000Z",  +        "name":"single", +        "ts":"2017-11-05T01:02:03.000000Z",          "value":17        },        { -        "type":"RATE",  +        "type":"RATE",          "labels":            { -            "labels":"l2"  +            "labels":"l2"            }, -        "name":"single",  -        "ts":"2017-11-05T01:02:03.000000Z",  +        "name":"single", +        "ts":"2017-11-05T01:02:03.000000Z",          "value":17        },        { -        "type":"DGAUGE",  +        "type":"DGAUGE",          "labels":            { -            "labels":"l3"  +            "labels":"l3"            }, -        "name":"single",  -        "ts":"2017-11-05T01:02:03.000000Z",  +        "name":"single", +        "ts":"2017-11-05T01:02:03.000000Z",          "value":3.14        },        { -        "type":"IGAUGE",  +        "type":"IGAUGE",          "labels":            { -            "labels":"l4"  +            "labels":"l4"            }, -        "name":"single_igauge",  -        "ts":"2017-11-05T01:02:03.000000Z",  -        "value":42  -      },  -      {  -        "type":"DGAUGE",  -        "labels":  -          {  -            "labels":"l5"  -          },  -        "name":"multiple",  +        "name":"single_igauge", +        "ts":"2017-11-05T01:02:03.000000Z", +        "value":42 +      }, +      { +        "type":"DGAUGE", +        "labels": +          { +            "labels":"l5" +          }, +        "name":"multiple",          "timeseries":            [              { -              "ts":"2017-11-05T01:02:03.000000Z",  +              "ts":"2017-11-05T01:02:03.000000Z",                "value":"nan"              },              { -              "ts":"2017-11-05T01:02:18.000000Z",  +              "ts":"2017-11-05T01:02:18.000000Z",                "value":"inf"              },              { -              "ts":"2017-11-05T01:02:33.000000Z",  +              "ts":"2017-11-05T01:02:33.000000Z",                "value":"-inf"              }            ]        },        { -        "type":"COUNTER",  +        "type":"COUNTER",          "timeseries":            [              { -              "ts":"2017-11-05T01:02:03.000000Z",  +              "ts":"2017-11-05T01:02:03.000000Z",                "value":1337              },              { -              "ts":"2017-11-05T01:02:18.000000Z",  +              "ts":"2017-11-05T01:02:18.000000Z",                "value":1338              }            ],          "labels":            { -            "labels":"l6"  -          },  -        "name":"multiple"  +            "labels":"l6" +          }, +        "name":"multiple"        }      ]  } diff --git a/library/cpp/monlib/encode/json/ut/expected_cloud_buffered.json b/library/cpp/monlib/encode/json/ut/expected_cloud_buffered.json index 5b7aeb836e4..be237d522bb 100644 --- a/library/cpp/monlib/encode/json/ut/expected_cloud_buffered.json +++ b/library/cpp/monlib/encode/json/ut/expected_cloud_buffered.json @@ -1,92 +1,92 @@ -{  -  "ts":"2017-07-14T02:40:00.000000Z",  -  "labels":  -    {  -      "project":"solomon"  -    },  -  "metrics":  -    [  -      {  -        "type":"COUNTER",  -        "labels":  -          {  -            "labels":"l1"  -          },  -        "name":"single",  -        "ts":"2017-11-05T01:02:03.000000Z",  -        "value":17  -      },  -      {  -        "type":"RATE",  -        "labels":  -          {  -            "labels":"l2"  -          },  -        "name":"single",  -        "ts":"2017-11-05T01:02:03.000000Z",  -        "value":17  -      },  -      {  -        "type":"DGAUGE",  -        "labels":  -          {  -            "labels":"l3"  -          },  -        "name":"single",  -        "ts":"2017-11-05T01:02:03.000000Z",  -        "value":3.14  -      },  -      {  -        "type":"IGAUGE",  -        "labels":  -          {  -            "labels":"l4"  -          },  -        "name":"single_igauge",  -        "ts":"2017-11-05T01:02:03.000000Z",  -        "value":42  -      },  -      {  -        "type":"DGAUGE",  -        "labels":  -          {  -            "labels":"l5"  -          },  -        "name":"multiple",  -        "timeseries":  -          [  -            {  -              "ts":"2017-11-05T01:02:03.000000Z",  -              "value":"nan"  -            },  -            {  -              "ts":"2017-11-05T01:02:18.000000Z",  -              "value":"inf"  -            },  -            {  -              "ts":"2017-11-05T01:02:33.000000Z",  -              "value":"-inf"  -            }  -          ]  -      },  -      {  -        "type":"COUNTER",  -        "labels":  -          {  -            "labels":"l6"  -          },  -        "name":"multiple",  -        "timeseries":  -          [  -            {  -              "ts":"2017-11-05T01:02:03.000000Z",  -              "value":1337  -            },  -            {  -              "ts":"2017-11-05T01:02:18.000000Z",  -              "value":1338  -            }  -          ]  -      }  -    ]  -}  +{ +  "ts":"2017-07-14T02:40:00.000000Z", +  "labels": +    { +      "project":"solomon" +    }, +  "metrics": +    [ +      { +        "type":"COUNTER", +        "labels": +          { +            "labels":"l1" +          }, +        "name":"single", +        "ts":"2017-11-05T01:02:03.000000Z", +        "value":17 +      }, +      { +        "type":"RATE", +        "labels": +          { +            "labels":"l2" +          }, +        "name":"single", +        "ts":"2017-11-05T01:02:03.000000Z", +        "value":17 +      }, +      { +        "type":"DGAUGE", +        "labels": +          { +            "labels":"l3" +          }, +        "name":"single", +        "ts":"2017-11-05T01:02:03.000000Z", +        "value":3.14 +      }, +      { +        "type":"IGAUGE", +        "labels": +          { +            "labels":"l4" +          }, +        "name":"single_igauge", +        "ts":"2017-11-05T01:02:03.000000Z", +        "value":42 +      }, +      { +        "type":"DGAUGE", +        "labels": +          { +            "labels":"l5" +          }, +        "name":"multiple", +        "timeseries": +          [ +            { +              "ts":"2017-11-05T01:02:03.000000Z", +              "value":"nan" +            }, +            { +              "ts":"2017-11-05T01:02:18.000000Z", +              "value":"inf" +            }, +            { +              "ts":"2017-11-05T01:02:33.000000Z", +              "value":"-inf" +            } +          ] +      }, +      { +        "type":"COUNTER", +        "labels": +          { +            "labels":"l6" +          }, +        "name":"multiple", +        "timeseries": +          [ +            { +              "ts":"2017-11-05T01:02:03.000000Z", +              "value":1337 +            }, +            { +              "ts":"2017-11-05T01:02:18.000000Z", +              "value":1338 +            } +          ] +      } +    ] +} diff --git a/library/cpp/monlib/encode/json/ut/ya.make b/library/cpp/monlib/encode/json/ut/ya.make index 851443ae62e..e50c4f4903b 100644 --- a/library/cpp/monlib/encode/json/ut/ya.make +++ b/library/cpp/monlib/encode/json/ut/ya.make @@ -15,9 +15,9 @@ RESOURCE(      buffered_ts_merge.json /buffered_ts_merge.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  +    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 diff --git a/library/cpp/monlib/encode/prometheus/prometheus.h b/library/cpp/monlib/encode/prometheus/prometheus.h index 21248f5fef6..2e7fa31c28c 100644 --- a/library/cpp/monlib/encode/prometheus/prometheus.h +++ b/library/cpp/monlib/encode/prometheus/prometheus.h @@ -11,8 +11,8 @@ namespace NMonitoring {      class TPrometheusDecodeException: public yexception {      }; -    IMetricEncoderPtr EncoderPrometheus(IOutputStream* out, TStringBuf metricNameLabel = "sensor");  +    IMetricEncoderPtr EncoderPrometheus(IOutputStream* out, TStringBuf metricNameLabel = "sensor"); -    void DecodePrometheus(TStringBuf data, IMetricConsumer* c, TStringBuf metricNameLabel = "sensor");  +    void DecodePrometheus(TStringBuf data, IMetricConsumer* c, TStringBuf metricNameLabel = "sensor");  } diff --git a/library/cpp/monlib/encode/prometheus/prometheus_decoder.cpp b/library/cpp/monlib/encode/prometheus/prometheus_decoder.cpp index 5dc0bc8033e..7e81357dbda 100644 --- a/library/cpp/monlib/encode/prometheus/prometheus_decoder.cpp +++ b/library/cpp/monlib/encode/prometheus/prometheus_decoder.cpp @@ -168,10 +168,10 @@ namespace NMonitoring {          ///////////////////////////////////////////////////////////////////////          class TPrometheusReader {          public: -            TPrometheusReader(TStringBuf data, IMetricConsumer* c, TStringBuf metricNameLabel)  +            TPrometheusReader(TStringBuf data, IMetricConsumer* c, TStringBuf metricNameLabel)                  : Data_(data)                  , Consumer_(c) -                , MetricNameLabel_(metricNameLabel)  +                , MetricNameLabel_(metricNameLabel)              {              } @@ -516,12 +516,12 @@ namespace NMonitoring {              }              void ConsumeLabels(TStringBuf name, const TLabelsMap& labels) { -                Y_PARSER_ENSURE(labels.count(MetricNameLabel_) == 0,  -                    "label name '" << MetricNameLabel_ <<  +                Y_PARSER_ENSURE(labels.count(MetricNameLabel_) == 0, +                    "label name '" << MetricNameLabel_ <<                      "' is reserved, but is used with metric: " << name << LabelsToStr(labels));                  Consumer_->OnLabelsBegin(); -                Consumer_->OnLabel(MetricNameLabel_, TString(name)); // TODO: remove this string allocation  +                Consumer_->OnLabel(MetricNameLabel_, TString(name)); // TODO: remove this string allocation                  for (const auto& it: labels) {                      Consumer_->OnLabel(it.first, it.second);                  } @@ -579,7 +579,7 @@ namespace NMonitoring {          private:              TStringBuf Data_;              IMetricConsumer* Consumer_; -            TStringBuf MetricNameLabel_;  +            TStringBuf MetricNameLabel_;              THashMap<TString, EPrometheusMetricType> SeenTypes_;              THistogramBuilder HistogramBuilder_; @@ -589,8 +589,8 @@ namespace NMonitoring {          };      } // namespace -void DecodePrometheus(TStringBuf data, IMetricConsumer* c, TStringBuf metricNameLabel) {  -    TPrometheusReader reader(data, c, metricNameLabel);  +void DecodePrometheus(TStringBuf data, IMetricConsumer* c, TStringBuf metricNameLabel) { +    TPrometheusReader reader(data, c, metricNameLabel);      reader.Read();  } diff --git a/library/cpp/monlib/encode/prometheus/prometheus_encoder.cpp b/library/cpp/monlib/encode/prometheus/prometheus_encoder.cpp index c370fd2b6ed..15efeb8c034 100644 --- a/library/cpp/monlib/encode/prometheus/prometheus_encoder.cpp +++ b/library/cpp/monlib/encode/prometheus/prometheus_encoder.cpp @@ -249,9 +249,9 @@ namespace NMonitoring {          ///////////////////////////////////////////////////////////////////////          class TPrometheusEncoder final: public IMetricEncoder {          public: -            explicit TPrometheusEncoder(IOutputStream* out, TStringBuf metricNameLabel)  +            explicit TPrometheusEncoder(IOutputStream* out, TStringBuf metricNameLabel)                  : Writer_(out) -                , MetricNameLabel_(metricNameLabel)  +                , MetricNameLabel_(metricNameLabel)              {              } @@ -358,10 +358,10 @@ namespace NMonitoring {                      MetricState_.Labels.Add(l.Name(), l.Value());                  } -                TMaybe<TLabel> nameLabel = MetricState_.Labels.Extract(MetricNameLabel_);  +                TMaybe<TLabel> nameLabel = MetricState_.Labels.Extract(MetricNameLabel_);                  Y_ENSURE(nameLabel,                           "labels " << MetricState_.Labels << -                         " does not contain label '" << MetricNameLabel_ << '\'');  +                         " does not contain label '" << MetricNameLabel_ << '\'');                  const TString& metricName = ToString(nameLabel->Value());                  if (MetricState_.Type != EMetricType::DSUMMARY) { @@ -399,15 +399,15 @@ namespace NMonitoring {          private:              TEncoderState State_;              TPrometheusWriter Writer_; -            TString MetricNameLabel_;  +            TString MetricNameLabel_;              TInstant CommonTime_ = TInstant::Zero();              TLabels CommonLabels_;              TMetricState MetricState_;          };      } -    IMetricEncoderPtr EncoderPrometheus(IOutputStream* out, TStringBuf metricNameLabel) {  -        return MakeHolder<TPrometheusEncoder>(out, metricNameLabel);  +    IMetricEncoderPtr EncoderPrometheus(IOutputStream* out, TStringBuf metricNameLabel) { +        return MakeHolder<TPrometheusEncoder>(out, metricNameLabel);      }  } // namespace NMonitoring diff --git a/library/cpp/monlib/encode/spack/spack_v1.h b/library/cpp/monlib/encode/spack/spack_v1.h index e962d214676..cf1c9417b90 100644 --- a/library/cpp/monlib/encode/spack/spack_v1.h +++ b/library/cpp/monlib/encode/spack/spack_v1.h @@ -78,7 +78,7 @@ namespace NMonitoring {      ///////////////////////////////////////////////////////////////////////////////      struct Y_PACKED TSpackHeader {          ui16 Magic = 0x5053;   // "SP" -        ui16 Version;          // MSB - major version, LSB - minor version  +        ui16 Version;          // MSB - major version, LSB - minor version          ui16 HeaderSize = sizeof(TSpackHeader);          ui8 TimePrecision;          ui8 Compression; @@ -89,12 +89,12 @@ namespace NMonitoring {          // add new fields here      }; -    enum ESpackV1Version: ui16 {  -        SV1_00 = 0x0100,  -        SV1_01 = 0x0101,  -        SV1_02 = 0x0102  -    };  -  +    enum ESpackV1Version: ui16 { +        SV1_00 = 0x0100, +        SV1_01 = 0x0101, +        SV1_02 = 0x0102 +    }; +      IMetricEncoderPtr EncoderSpackV1(          IOutputStream* out,          ETimePrecision timePrecision, @@ -102,14 +102,14 @@ namespace NMonitoring {          EMetricsMergingMode mergingMode = EMetricsMergingMode::DEFAULT      ); -    IMetricEncoderPtr EncoderSpackV12(  -        IOutputStream* out,  -        ETimePrecision timePrecision,  -        ECompression compression,  -        EMetricsMergingMode mergingMode = EMetricsMergingMode::DEFAULT,  -        TStringBuf metricNameLabel = "name"  -    );  +    IMetricEncoderPtr EncoderSpackV12( +        IOutputStream* out, +        ETimePrecision timePrecision, +        ECompression compression, +        EMetricsMergingMode mergingMode = EMetricsMergingMode::DEFAULT, +        TStringBuf metricNameLabel = "name" +    ); + +    void DecodeSpackV1(IInputStream* in, IMetricConsumer* c, TStringBuf metricNameLabel = "name"); -    void DecodeSpackV1(IInputStream* in, IMetricConsumer* c, TStringBuf metricNameLabel = "name");  -   } diff --git a/library/cpp/monlib/encode/spack/spack_v1_decoder.cpp b/library/cpp/monlib/encode/spack/spack_v1_decoder.cpp index 330e81a7a25..1f445fc80da 100644 --- a/library/cpp/monlib/encode/spack/spack_v1_decoder.cpp +++ b/library/cpp/monlib/encode/spack/spack_v1_decoder.cpp @@ -27,9 +27,9 @@ namespace NMonitoring {          ///////////////////////////////////////////////////////////////////////          class TDecoderSpackV1 {          public: -            TDecoderSpackV1(IInputStream* in, TStringBuf metricNameLabel)  +            TDecoderSpackV1(IInputStream* in, TStringBuf metricNameLabel)                  : In_(in) -                , MetricNameLabel_(metricNameLabel)  +                , MetricNameLabel_(metricNameLabel)              {              } @@ -83,9 +83,9 @@ namespace NMonitoring {                  // (4) read common labels                  if (ui32 commonLabelsCount = ReadVarint()) { -                    c->OnLabelsBegin();  +                    c->OnLabelsBegin();                      ReadLabels(labelNames, labelValues, commonLabelsCount, c); -                    c->OnLabelsEnd();  +                    c->OnLabelsEnd();                  }                  // (5) read metrics @@ -110,20 +110,20 @@ namespace NMonitoring {                      // TODO: use it                      ReadFixed<ui8>(); // skip flags byte -                    auto metricNameValueIndex = std::numeric_limits<ui32>::max();  -                    if (Header_.Version >= SV1_02) {  -                        metricNameValueIndex = ReadVarint();  -                    }  -  +                    auto metricNameValueIndex = std::numeric_limits<ui32>::max(); +                    if (Header_.Version >= SV1_02) { +                        metricNameValueIndex = ReadVarint(); +                    } +                      // (5.2) labels                      ui32 labelsCount = ReadVarint(); -                    DECODE_ENSURE(Header_.Version >= SV1_02 || labelsCount > 0, "metric #" << i << " has no labels");  -                    c->OnLabelsBegin();  -                    if (Header_.Version >= SV1_02) {  -                        c->OnLabel(MetricNameLabel_, labelValues.Get(metricNameValueIndex));  -                    }  +                    DECODE_ENSURE(Header_.Version >= SV1_02 || labelsCount > 0, "metric #" << i << " has no labels"); +                    c->OnLabelsBegin(); +                    if (Header_.Version >= SV1_02) { +                        c->OnLabel(MetricNameLabel_, labelValues.Get(metricNameValueIndex)); +                    }                      ReadLabels(labelNames, labelValues, labelsCount, c); -                    c->OnLabelsEnd();  +                    c->OnLabelsEnd();                      // (5.3) values                      switch (valueType) { @@ -214,7 +214,7 @@ namespace NMonitoring {                  ui32 bucketsCount = ReadVarint();                  auto s = TExplicitHistogramSnapshot::New(bucketsCount); -                if (SV1_00 == Header_.Version) { // v1.0  +                if (SV1_00 == Header_.Version) { // v1.0                      for (ui32 i = 0; i < bucketsCount; i++) {                          i64 bound = ReadFixed<i64>();                          double doubleBound = (bound != Max<i64>()) @@ -275,7 +275,7 @@ namespace NMonitoring {          private:              IInputStream* In_; -            TString MetricNameLabel_;  +            TString MetricNameLabel_;              ETimePrecision TimePrecision_;              TSpackHeader Header_;          }; // class TDecoderSpackV1 @@ -450,8 +450,8 @@ namespace NMonitoring {          }      } -    void DecodeSpackV1(IInputStream* in, IMetricConsumer* c, TStringBuf metricNameLabel) {  -        TDecoderSpackV1 decoder(in, metricNameLabel);  +    void DecodeSpackV1(IInputStream* in, IMetricConsumer* c, TStringBuf metricNameLabel) { +        TDecoderSpackV1 decoder(in, metricNameLabel);          decoder.Decode(c);      } diff --git a/library/cpp/monlib/encode/spack/spack_v1_encoder.cpp b/library/cpp/monlib/encode/spack/spack_v1_encoder.cpp index 3e7a2fc7ea0..a2b0bb5f50a 100644 --- a/library/cpp/monlib/encode/spack/spack_v1_encoder.cpp +++ b/library/cpp/monlib/encode/spack/spack_v1_encoder.cpp @@ -23,15 +23,15 @@ namespace NMonitoring {                  IOutputStream* out,                  ETimePrecision timePrecision,                  ECompression compression, -                EMetricsMergingMode mergingMode,  -                ESpackV1Version version,  -                TStringBuf metricNameLabel  +                EMetricsMergingMode mergingMode, +                ESpackV1Version version, +                TStringBuf metricNameLabel              )                  : Out_(out)                  , TimePrecision_(timePrecision)                  , Compression_(compression) -                , Version_(version)  -                , MetricName_(Version_ >= SV1_02 ? LabelNamesPool_.PutIfAbsent(metricNameLabel) : nullptr)  +                , Version_(version) +                , MetricName_(Version_ >= SV1_02 ? LabelNamesPool_.PutIfAbsent(metricNameLabel) : nullptr)              {                  MetricsMergingMode_ = mergingMode; @@ -89,7 +89,7 @@ namespace NMonitoring {                  // (1) write header                  TSpackHeader header; -                header.Version = Version_;  +                header.Version = Version_;                  header.TimePrecision = EncodeTimePrecision(TimePrecision_);                  header.Compression = EncodeCompression(Compression_);                  header.LabelNamesSize = static_cast<ui32>( @@ -119,7 +119,7 @@ namespace NMonitoring {                  WriteTime(CommonTime_);                  // (4) write common labels' indexes -                WriteLabels(CommonLabels_, nullptr);  +                WriteLabels(CommonLabels_, nullptr);                  // (5) write metrics                  // metrics count already written in header @@ -132,19 +132,19 @@ namespace NMonitoring {                      ui8 flagsByte = 0x00;                      Out_->Write(&flagsByte, sizeof(flagsByte)); -                    // v1.2 format addition — metric name  -                    if (Version_ >= SV1_02) {  -                        const auto it = FindIf(metric.Labels, [&](const auto& l) {  -                            return l.Key == MetricName_;  -                        });  -                        Y_ENSURE(it != metric.Labels.end(),  -                                 "metric name label '" << LabelNamesPool_.Get(MetricName_->Index) << "' not found, "  -                                 << "all metric labels '" << FormatLabels(metric.Labels) << "'");  -                        WriteVarUInt32(Out_, it->Value->Index);  -                    }  -  +                    // v1.2 format addition — metric name +                    if (Version_ >= SV1_02) { +                        const auto it = FindIf(metric.Labels, [&](const auto& l) { +                            return l.Key == MetricName_; +                        }); +                        Y_ENSURE(it != metric.Labels.end(), +                                 "metric name label '" << LabelNamesPool_.Get(MetricName_->Index) << "' not found, " +                                 << "all metric labels '" << FormatLabels(metric.Labels) << "'"); +                        WriteVarUInt32(Out_, it->Value->Index); +                    } +                      // (5.2) labels -                    WriteLabels(metric.Labels, MetricName_);  +                    WriteLabels(metric.Labels, MetricName_);                      // (5.3) values                      switch (metric.TimeSeries.Size()) { @@ -190,12 +190,12 @@ namespace NMonitoring {                  return (static_cast<ui8>(metric.MetricType) << 2) | static_cast<ui8>(valueType);              } -            void WriteLabels(const TPooledLabels& labels, const TPooledStr* skipKey) {  -                WriteVarUInt32(Out_, static_cast<ui32>(skipKey ? labels.size() - 1 : labels.size()));  +            void WriteLabels(const TPooledLabels& labels, const TPooledStr* skipKey) { +                WriteVarUInt32(Out_, static_cast<ui32>(skipKey ? labels.size() - 1 : labels.size()));                  for (auto&& label : labels) { -                    if (label.Key == skipKey) {  -                        continue;  -                    }  +                    if (label.Key == skipKey) { +                        continue; +                    }                      WriteVarUInt32(Out_, label.Key->Index);                      WriteVarUInt32(Out_, label.Value->Index);                  } @@ -289,8 +289,8 @@ namespace NMonitoring {              IOutputStream* Out_;              ETimePrecision TimePrecision_;              ECompression Compression_; -            ESpackV1Version Version_;  -            const TPooledStr* MetricName_;  +            ESpackV1Version Version_; +            const TPooledStr* MetricName_;              bool Closed_ = false;          }; @@ -302,17 +302,17 @@ namespace NMonitoring {          ECompression compression,          EMetricsMergingMode mergingMode      ) { -        return MakeHolder<TEncoderSpackV1>(out, timePrecision, compression, mergingMode, SV1_01, "");  +        return MakeHolder<TEncoderSpackV1>(out, timePrecision, compression, mergingMode, SV1_01, "");      } -    IMetricEncoderPtr EncoderSpackV12(  -        IOutputStream* out,  -        ETimePrecision timePrecision,  -        ECompression compression,  -        EMetricsMergingMode mergingMode,  -        TStringBuf metricNameLabel  -    ) {  -        Y_ENSURE(!metricNameLabel.Empty(), "metricNameLabel can't be empty");  -        return MakeHolder<TEncoderSpackV1>(out, timePrecision, compression, mergingMode, SV1_02, metricNameLabel);  -    }  +    IMetricEncoderPtr EncoderSpackV12( +        IOutputStream* out, +        ETimePrecision timePrecision, +        ECompression compression, +        EMetricsMergingMode mergingMode, +        TStringBuf metricNameLabel +    ) { +        Y_ENSURE(!metricNameLabel.Empty(), "metricNameLabel can't be empty"); +        return MakeHolder<TEncoderSpackV1>(out, timePrecision, compression, mergingMode, SV1_02, metricNameLabel); +    }  } diff --git a/library/cpp/monlib/encode/spack/spack_v1_ut.cpp b/library/cpp/monlib/encode/spack/spack_v1_ut.cpp index c6748cb0f5e..fe778eb7e02 100644 --- a/library/cpp/monlib/encode/spack/spack_v1_ut.cpp +++ b/library/cpp/monlib/encode/spack/spack_v1_ut.cpp @@ -468,7 +468,7 @@ Y_UNIT_TEST_SUITE(TSpackTest) {          IMetricEncoderPtr e = EncoderProtobuf(&samples);          TBuffer data(expectedSize); -        if (SV1_00 == version) { // v1.0  +        if (SV1_00 == version) { // v1.0              data.Append(reinterpret_cast<char*>(expectedHeader_v1_0), Y_ARRAY_SIZE(expectedHeader_v1_0));          } else {              data.Append(reinterpret_cast<char*>(expectedHeader), Y_ARRAY_SIZE(expectedHeader)); @@ -480,7 +480,7 @@ Y_UNIT_TEST_SUITE(TSpackTest) {          data.Append(reinterpret_cast<char*>(expectedMetric2), Y_ARRAY_SIZE(expectedMetric2));          data.Append(reinterpret_cast<char*>(expectedMetric3), Y_ARRAY_SIZE(expectedMetric3));          data.Append(reinterpret_cast<char*>(expectedMetric4), Y_ARRAY_SIZE(expectedMetric4)); -        if (SV1_00 == version) { // v1.0  +        if (SV1_00 == version) { // v1.0              data.Append(reinterpret_cast<char*>(expectedMetric5_v1_0), Y_ARRAY_SIZE(expectedMetric5_v1_0));          } else {              data.Append(reinterpret_cast<char*>(expectedMetric5), Y_ARRAY_SIZE(expectedMetric5)); @@ -494,7 +494,7 @@ Y_UNIT_TEST_SUITE(TSpackTest) {      void DecodeDataToSamples(NProto::TMultiSamplesList & samples) {          TSpackHeader header; -        header.Version = SV1_01;  +        header.Version = SV1_01;          DecodeDataToSamples(samples, header.Version);      } @@ -689,7 +689,7 @@ Y_UNIT_TEST_SUITE(TSpackTest) {          // Check that histogram bounds decoded from different versions are the same          NProto::TMultiSamplesList samples, samples_v1_0;          DecodeDataToSamples(samples); -        DecodeDataToSamples(samples_v1_0, /*version = */ SV1_00);  +        DecodeDataToSamples(samples_v1_0, /*version = */ SV1_00);          const NProto::THistogram& pointHistogram = samples.GetSamples(4).GetPoints(0).GetHistogram();          const NProto::THistogram& pointHistogram_v1_0 = samples_v1_0.GetSamples(4).GetPoints(0).GetHistogram(); @@ -698,148 +698,148 @@ Y_UNIT_TEST_SUITE(TSpackTest) {              UNIT_ASSERT_DOUBLES_EQUAL(pointHistogram.GetBounds(i), pointHistogram_v1_0.GetBounds(i), Min<double>());          }      } -  -    Y_UNIT_TEST(SimpleV12) {  -        ui8 expectedSerialized[] = {  -            // header  -            0x53, 0x50,             // magic "SP"                     (fixed ui16)  -        // minor, major  -            0x02, 0x01,             // version                        (fixed ui16)  -            0x18, 0x00,             // header size                    (fixed ui16)  -            0x00,                   // time precision                 (fixed ui8)  -            0x00,                   // compression algorithm          (fixed ui8)  -            0x0A, 0x00, 0x00, 0x00, // label names size   (fixed ui32)  -            0x14, 0x00, 0x00, 0x00, // labels values size (fixed ui32)  -            0x01, 0x00, 0x00, 0x00, // metric count       (fixed ui32)  -            0x01, 0x00, 0x00, 0x00, // points count       (fixed ui32)  -  -            // string pools  -            0x73, 0x00,                                     // "s\0"  -            0x70, 0x72, 0x6f, 0x6a, 0x65, 0x63, 0x74, 0x00, // "project\0"  -            0x73, 0x6f, 0x6c, 0x6f, 0x6d, 0x6f, 0x6e, 0x00, // "solomon\0"  -            0x74, 0x65, 0x6D, 0x70, 0x65, 0x72, 0x61, 0x74, // temperature  -            0x75, 0x72, 0x65, 0x00,  -  -            // common time  -            0x00, 0x2f, 0x68, 0x59,                         // common time in seconds                  (fixed ui32)  -  -            // common labels  -            0x00,                                           // common labels count                     (varint)  -  -            // metric  -            0x09,                                           // types (COUNTER | ONE_WITHOUT_TS)        (fixed ui8)  -            0x00,                                           // flags                                   (fixed ui8)  -            0x01,                                           // name index                              (varint)  -            0x01,                                           // metric labels count                     (varint)  -            0x01,                                           // 'project' label name index              (varint)  -            0x00,                                           // 'project' label value index             (varint)  -            0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // value (fixed ui64)  -        };  -  -        // encode  -        {  -            TBuffer actualSerialized;  -            {  -                TBufferOutput out(actualSerialized);  -                auto e = EncoderSpackV12(  -                    &out,  -                    ETimePrecision::SECONDS,  -                    ECompression::IDENTITY,  -                    EMetricsMergingMode::DEFAULT,  -                    "s");  -  -                e->OnStreamBegin();  -                e->OnCommonTime(TInstant::Seconds(1500000000));  -  -                {  -                    e->OnMetricBegin(EMetricType::COUNTER);  -                    {  -                        e->OnLabelsBegin();  + +    Y_UNIT_TEST(SimpleV12) { +        ui8 expectedSerialized[] = { +            // header +            0x53, 0x50,             // magic "SP"                     (fixed ui16) +        // minor, major +            0x02, 0x01,             // version                        (fixed ui16) +            0x18, 0x00,             // header size                    (fixed ui16) +            0x00,                   // time precision                 (fixed ui8) +            0x00,                   // compression algorithm          (fixed ui8) +            0x0A, 0x00, 0x00, 0x00, // label names size   (fixed ui32) +            0x14, 0x00, 0x00, 0x00, // labels values size (fixed ui32) +            0x01, 0x00, 0x00, 0x00, // metric count       (fixed ui32) +            0x01, 0x00, 0x00, 0x00, // points count       (fixed ui32) + +            // string pools +            0x73, 0x00,                                     // "s\0" +            0x70, 0x72, 0x6f, 0x6a, 0x65, 0x63, 0x74, 0x00, // "project\0" +            0x73, 0x6f, 0x6c, 0x6f, 0x6d, 0x6f, 0x6e, 0x00, // "solomon\0" +            0x74, 0x65, 0x6D, 0x70, 0x65, 0x72, 0x61, 0x74, // temperature +            0x75, 0x72, 0x65, 0x00, + +            // common time +            0x00, 0x2f, 0x68, 0x59,                         // common time in seconds                  (fixed ui32) + +            // common labels +            0x00,                                           // common labels count                     (varint) + +            // metric +            0x09,                                           // types (COUNTER | ONE_WITHOUT_TS)        (fixed ui8) +            0x00,                                           // flags                                   (fixed ui8) +            0x01,                                           // name index                              (varint) +            0x01,                                           // metric labels count                     (varint) +            0x01,                                           // 'project' label name index              (varint) +            0x00,                                           // 'project' label value index             (varint) +            0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // value (fixed ui64) +        }; + +        // encode +        { +            TBuffer actualSerialized; +            { +                TBufferOutput out(actualSerialized); +                auto e = EncoderSpackV12( +                    &out, +                    ETimePrecision::SECONDS, +                    ECompression::IDENTITY, +                    EMetricsMergingMode::DEFAULT, +                    "s"); + +                e->OnStreamBegin(); +                e->OnCommonTime(TInstant::Seconds(1500000000)); + +                { +                    e->OnMetricBegin(EMetricType::COUNTER); +                    { +                        e->OnLabelsBegin(); +                        e->OnLabel("project", "solomon"); +                        e->OnLabel("s", "temperature"); +                        e->OnLabelsEnd(); +                    } +                    // Only the last value will be encoded +                    e->OnUint64(TInstant::Zero(), 10); +                    e->OnUint64(TInstant::Zero(), 13); +                    e->OnUint64(TInstant::Zero(), 17); +                    e->OnMetricEnd(); +                } + +                e->OnStreamEnd(); +                e->Close(); +            } + +            UNIT_ASSERT_VALUES_EQUAL(actualSerialized.Size(), Y_ARRAY_SIZE(expectedSerialized)); +            UNIT_ASSERT_BINARY_EQUALS(actualSerialized.Data(), expectedSerialized); +        } + +        // decode +        { +            NProto::TMultiSamplesList samples; +            { +                auto input = TMemoryInput(expectedSerialized, Y_ARRAY_SIZE(expectedSerialized)); +                auto encoder = EncoderProtobuf(&samples); +                DecodeSpackV1(&input, encoder.Get(), "s"); +            } + +            UNIT_ASSERT_VALUES_EQUAL(TInstant::MilliSeconds(samples.GetCommonTime()), +                                     TInstant::Seconds(1500000000)); + +            UNIT_ASSERT_VALUES_EQUAL(samples.CommonLabelsSize(), 0); + +            UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 1); +            { +                const auto& s = samples.GetSamples(0); +                UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::COUNTER); +                UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 2); +                AssertLabelEqual(s.GetLabels(0), "s", "temperature"); +                AssertLabelEqual(s.GetLabels(1), "project", "solomon"); + +                UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1); +                AssertPointEqual(s.GetPoints(0), TInstant::Zero(), ui64(17)); +            } +        } +    } + +    Y_UNIT_TEST(V12MissingNameForOneMetric) { +        TBuffer b; +        TBufferOutput out(b); +        auto e = EncoderSpackV12( +            &out, +            ETimePrecision::SECONDS, +            ECompression::IDENTITY, +            EMetricsMergingMode::DEFAULT, +            "s"); + +        UNIT_ASSERT_EXCEPTION_CONTAINS( +            [&]() { +                e->OnStreamBegin(); +                { +                    e->OnMetricBegin(EMetricType::COUNTER); +                    { +                        e->OnLabelsBegin(); +                        e->OnLabel("s", "s1"); +                        e->OnLabelsEnd(); +                    } +                    e->OnUint64(TInstant::Zero(), 1); +                    e->OnMetricEnd(); + +                    e->OnMetricBegin(EMetricType::COUNTER); +                    { +                        e->OnLabelsBegin();                          e->OnLabel("project", "solomon"); -                        e->OnLabel("s", "temperature");  -                        e->OnLabelsEnd();  -                    }  -                    // Only the last value will be encoded  -                    e->OnUint64(TInstant::Zero(), 10);  -                    e->OnUint64(TInstant::Zero(), 13);  -                    e->OnUint64(TInstant::Zero(), 17);  -                    e->OnMetricEnd();  -                }  -  -                e->OnStreamEnd();  -                e->Close();  -            }  -  -            UNIT_ASSERT_VALUES_EQUAL(actualSerialized.Size(), Y_ARRAY_SIZE(expectedSerialized));  -            UNIT_ASSERT_BINARY_EQUALS(actualSerialized.Data(), expectedSerialized);  -        }  -  -        // decode  -        {  -            NProto::TMultiSamplesList samples;  -            {  -                auto input = TMemoryInput(expectedSerialized, Y_ARRAY_SIZE(expectedSerialized));  -                auto encoder = EncoderProtobuf(&samples);  -                DecodeSpackV1(&input, encoder.Get(), "s");  -            }  -  -            UNIT_ASSERT_VALUES_EQUAL(TInstant::MilliSeconds(samples.GetCommonTime()),  -                                     TInstant::Seconds(1500000000));  -  -            UNIT_ASSERT_VALUES_EQUAL(samples.CommonLabelsSize(), 0);  -  -            UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 1);  -            {  -                const auto& s = samples.GetSamples(0);  -                UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::COUNTER);  -                UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 2);  -                AssertLabelEqual(s.GetLabels(0), "s", "temperature");  -                AssertLabelEqual(s.GetLabels(1), "project", "solomon");  -  -                UNIT_ASSERT_VALUES_EQUAL(s.PointsSize(), 1);  -                AssertPointEqual(s.GetPoints(0), TInstant::Zero(), ui64(17));  -            }  -        }  -    }  -  -    Y_UNIT_TEST(V12MissingNameForOneMetric) {  -        TBuffer b;  -        TBufferOutput out(b);  -        auto e = EncoderSpackV12(  -            &out,  -            ETimePrecision::SECONDS,  -            ECompression::IDENTITY,  -            EMetricsMergingMode::DEFAULT,  -            "s");  -  -        UNIT_ASSERT_EXCEPTION_CONTAINS(  -            [&]() {  -                e->OnStreamBegin();  -                {  -                    e->OnMetricBegin(EMetricType::COUNTER);  -                    {  -                        e->OnLabelsBegin();  -                        e->OnLabel("s", "s1");  -                        e->OnLabelsEnd();  -                    }  -                    e->OnUint64(TInstant::Zero(), 1);  -                    e->OnMetricEnd();  -  -                    e->OnMetricBegin(EMetricType::COUNTER);  -                    {  -                        e->OnLabelsBegin();  -                        e->OnLabel("project", "solomon");  -                        e->OnLabel("m", "v");  -                        e->OnLabelsEnd();  -                    }  -                    e->OnUint64(TInstant::Zero(), 2);  -                    e->OnMetricEnd();  -                }  -  -                e->OnStreamEnd();  -                e->Close();  -            }(),  -            yexception,  -            "metric name label 's' not found, all metric labels '{m=v, project=solomon}'");  -    }  +                        e->OnLabel("m", "v"); +                        e->OnLabelsEnd(); +                    } +                    e->OnUint64(TInstant::Zero(), 2); +                    e->OnMetricEnd(); +                } + +                e->OnStreamEnd(); +                e->Close(); +            }(), +            yexception, +            "metric name label 's' not found, all metric labels '{m=v, project=solomon}'"); +    }  } diff --git a/library/cpp/monlib/metrics/metric_value.h b/library/cpp/monlib/metrics/metric_value.h index 5f235e7a0b3..607fcc86022 100644 --- a/library/cpp/monlib/metrics/metric_value.h +++ b/library/cpp/monlib/metrics/metric_value.h @@ -384,35 +384,35 @@ namespace NMonitoring {          template <typename T>          void Add(TInstant time, T value) { -            Add(TPoint(time, value), TValueType<T>::Type);  -        }  -  -        void Add(TPoint point, EMetricValueType valueType) {  +            Add(TPoint(time, value), TValueType<T>::Type); +        } + +        void Add(TPoint point, EMetricValueType valueType) {              if (Empty()) { -                ValueType_ = valueType;  +                ValueType_ = valueType;              } else { -                CheckTypes(ValueType_, valueType);  +                CheckTypes(ValueType_, valueType);              } -            Points_.push_back(point);  +            Points_.push_back(point);              if (ValueType_ == EMetricValueType::SUMMARY) { -                TPoint& p = Points_.back();  -                p.GetValue().AsSummaryDouble()->Ref();  +                TPoint& p = Points_.back(); +                p.GetValue().AsSummaryDouble()->Ref();              } else if (ValueType_ == EMetricValueType::HISTOGRAM) { -                TPoint& p = Points_.back();  -                p.GetValue().AsHistogram()->Ref();  +                TPoint& p = Points_.back(); +                p.GetValue().AsHistogram()->Ref();              } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { -                TPoint& p = Points_.back();  -                p.GetValue().AsLogHistogram()->Ref();  +                TPoint& p = Points_.back(); +                p.GetValue().AsLogHistogram()->Ref();              }          }          void CopyFrom(const TMetricTimeSeries& other) { -            if (Empty()) {  -                ValueType_ = other.ValueType_;  -            } else {  -                CheckTypes(GetValueType(), other.GetValueType());  -            }  +            if (Empty()) { +                ValueType_ = other.ValueType_; +            } else { +                CheckTypes(GetValueType(), other.GetValueType()); +            }              size_t prevSize = Points_.size();              Copy(std::begin(other.Points_), std::end(other.Points_), | 
