diff options
author | ivanzhukov <ivanzhukov@yandex-team.ru> | 2022-02-10 16:49:40 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:40 +0300 |
commit | 0892d79ab411592ad25175c4bdadbcb09b466cf5 (patch) | |
tree | 98dfdd45463c9bd747101748a9ca25d2917390fd /library/cpp | |
parent | 1b7466cb957659079ebebbb5d76e64e51f3306f0 (diff) | |
download | ydb-0892d79ab411592ad25175c4bdadbcb09b466cf5.tar.gz |
Restoring authorship annotation for <ivanzhukov@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp')
63 files changed, 2373 insertions, 2373 deletions
diff --git a/library/cpp/getopt/last_getopt_demo/demo.cpp b/library/cpp/getopt/last_getopt_demo/demo.cpp index 79426a9cc9..878225714c 100644 --- a/library/cpp/getopt/last_getopt_demo/demo.cpp +++ b/library/cpp/getopt/last_getopt_demo/demo.cpp @@ -45,7 +45,7 @@ class TMain: public TMainClassArgs { protected: void RegisterOptions(NLastGetopt::TOpts& opts) override { - // Brief description for the whole program, will appear in the beginning of a help message. + // Brief description for the whole program, will appear in the beginning of a help message. opts.SetTitle("last_getopt_demo -- like wget, but doesn't actually do anything"); // Built-in options. @@ -153,7 +153,7 @@ protected: .CompletionArgHelp("URL for download") .Completer(NLastGetopt::NComp::Url()); - // Let's add more text to our help. A nice description and examples. + // Let's add more text to our help. A nice description and examples. opts.AddSection( "Description", diff --git a/library/cpp/http/misc/http_headers.h b/library/cpp/http/misc/http_headers.h index ff359937fa..9a16f2bcf4 100644 --- a/library/cpp/http/misc/http_headers.h +++ b/library/cpp/http/misc/http_headers.h @@ -1,14 +1,14 @@ -#pragma once - -#include <util/generic/strbuf.h> - - -/* Taken from SpringFramework's HttpHeaders. Docs: - * https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/HttpHeaders.html - * Source: - * https://github.com/spring-projects/spring-framework/blob/816bbee8de584676250e2bc5dcff6da6cd81623f/spring-web/src/main/java/org/springframework/http/HttpHeaders.java - */ -namespace NHttpHeaders { +#pragma once + +#include <util/generic/strbuf.h> + + +/* Taken from SpringFramework's HttpHeaders. Docs: + * https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/HttpHeaders.html + * Source: + * https://github.com/spring-projects/spring-framework/blob/816bbee8de584676250e2bc5dcff6da6cd81623f/spring-web/src/main/java/org/springframework/http/HttpHeaders.java + */ +namespace NHttpHeaders { constexpr TStringBuf ACCEPT = "Accept"; constexpr TStringBuf ACCEPT_CHARSET = "Accept-Charset"; constexpr TStringBuf ACCEPT_ENCODING = "Accept-Encoding"; @@ -69,4 +69,4 @@ namespace NHttpHeaders { constexpr TStringBuf VIA = "Via"; constexpr TStringBuf WARNING = "Warning"; constexpr TStringBuf WWW_AUTHENTICATE = "WWW-Authenticate"; -} // namespace HttpHeaders +} // namespace HttpHeaders diff --git a/library/cpp/http/misc/httpcodes.cpp b/library/cpp/http/misc/httpcodes.cpp index ad8c80ac1e..2c251bdf33 100644 --- a/library/cpp/http/misc/httpcodes.cpp +++ b/library/cpp/http/misc/httpcodes.cpp @@ -95,7 +95,7 @@ TStringBuf HttpCodeStrEx(int code) noexcept { return TStringBuf("423 Locked"); case HTTP_FAILED_DEPENDENCY: return TStringBuf("424 Failed Dependency"); - case HTTP_UNORDERED_COLLECTION: + case HTTP_UNORDERED_COLLECTION: return TStringBuf("425 Unordered Collection"); case HTTP_UPGRADE_REQUIRED: return TStringBuf("426 Upgrade Required"); @@ -103,7 +103,7 @@ TStringBuf HttpCodeStrEx(int code) noexcept { return TStringBuf("428 Precondition Required"); case HTTP_TOO_MANY_REQUESTS: return TStringBuf("429 Too Many Requests"); - case HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE: + case HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE: return TStringBuf("431 Request Header Fields Too Large"); case HTTP_UNAVAILABLE_FOR_LEGAL_REASONS: return TStringBuf("451 Unavailable For Legal Reason"); diff --git a/library/cpp/http/misc/httpcodes.h b/library/cpp/http/misc/httpcodes.h index cbfbaa1188..1da60825f2 100644 --- a/library/cpp/http/misc/httpcodes.h +++ b/library/cpp/http/misc/httpcodes.h @@ -51,11 +51,11 @@ enum HttpCodes { HTTP_UNPROCESSABLE_ENTITY = 422, HTTP_LOCKED = 423, HTTP_FAILED_DEPENDENCY = 424, - HTTP_UNORDERED_COLLECTION = 425, + HTTP_UNORDERED_COLLECTION = 425, HTTP_UPGRADE_REQUIRED = 426, HTTP_PRECONDITION_REQUIRED = 428, HTTP_TOO_MANY_REQUESTS = 429, - HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431, + HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431, HTTP_UNAVAILABLE_FOR_LEGAL_REASONS = 451, HTTP_INTERNAL_SERVER_ERROR = 500, diff --git a/library/cpp/monlib/encode/buffered/buffered_encoder_base.cpp b/library/cpp/monlib/encode/buffered/buffered_encoder_base.cpp index 87c832d642..9da1bf0754 100644 --- a/library/cpp/monlib/encode/buffered/buffered_encoder_base.cpp +++ b/library/cpp/monlib/encode/buffered/buffered_encoder_base.cpp @@ -6,15 +6,15 @@ namespace NMonitoring { void TBufferedEncoderBase::OnStreamBegin() { - State_.Expect(TEncoderState::EState::ROOT); + State_.Expect(TEncoderState::EState::ROOT); } void TBufferedEncoderBase::OnStreamEnd() { - State_.Expect(TEncoderState::EState::ROOT); + State_.Expect(TEncoderState::EState::ROOT); } void TBufferedEncoderBase::OnCommonTime(TInstant time) { - State_.Expect(TEncoderState::EState::ROOT); + State_.Expect(TEncoderState::EState::ROOT); CommonTime_ = time; } @@ -26,41 +26,41 @@ void TBufferedEncoderBase::OnMetricBegin(EMetricType type) { void TBufferedEncoderBase::OnMetricEnd() { State_.Switch(TEncoderState::EState::METRIC, TEncoderState::EState::ROOT); - + switch (MetricsMergingMode_) { case EMetricsMergingMode::MERGE_METRICS: { auto& metric = Metrics_.back(); Sort(metric.Labels, [] (const TPooledLabel& lhs, const TPooledLabel& rhs) { - return std::tie(lhs.Key, lhs.Value) < std::tie(rhs.Key, rhs.Value); - }); - + return std::tie(lhs.Key, lhs.Value) < std::tie(rhs.Key, rhs.Value); + }); + auto it = MetricMap_.find(metric.Labels); if (it == std::end(MetricMap_)) { MetricMap_.emplace(metric.Labels, Metrics_.size() - 1); - } else { + } else { auto& existing = Metrics_[it->second].TimeSeries; - + Y_ENSURE(existing.GetValueType() == metric.TimeSeries.GetValueType(), "Time series point type mismatch: expected " << existing.GetValueType() << " but found " << metric.TimeSeries.GetValueType() << ", labels '" << FormatLabels(metric.Labels) << "'"); - + existing.CopyFrom(metric.TimeSeries); Metrics_.pop_back(); - } - - break; - } + } + + break; + } case EMetricsMergingMode::DEFAULT: - break; - } + break; + } } void TBufferedEncoderBase::OnLabelsBegin() { if (State_ == TEncoderState::EState::METRIC) { State_ = TEncoderState::EState::METRIC_LABELS; - } else if (State_ == TEncoderState::EState::ROOT) { - State_ = TEncoderState::EState::COMMON_LABELS; + } else if (State_ == TEncoderState::EState::ROOT) { + State_ = TEncoderState::EState::COMMON_LABELS; } else { State_.ThrowInvalid("expected METRIC or ROOT"); } @@ -69,8 +69,8 @@ void TBufferedEncoderBase::OnLabelsBegin() { void TBufferedEncoderBase::OnLabelsEnd() { if (State_ == TEncoderState::EState::METRIC_LABELS) { State_ = TEncoderState::EState::METRIC; - } else if (State_ == TEncoderState::EState::COMMON_LABELS) { - State_ = TEncoderState::EState::ROOT; + } else if (State_ == TEncoderState::EState::COMMON_LABELS) { + State_ = TEncoderState::EState::ROOT; } else { State_.ThrowInvalid("expected LABELS or COMMON_LABELS"); } @@ -80,7 +80,7 @@ void TBufferedEncoderBase::OnLabel(TStringBuf name, TStringBuf value) { TPooledLabels* labels; if (State_ == TEncoderState::EState::METRIC_LABELS) { labels = &Metrics_.back().Labels; - } else if (State_ == TEncoderState::EState::COMMON_LABELS) { + } else if (State_ == TEncoderState::EState::COMMON_LABELS) { labels = &CommonLabels_; } else { State_.ThrowInvalid("expected LABELS or COMMON_LABELS"); diff --git a/library/cpp/monlib/encode/encoder_state.h b/library/cpp/monlib/encode/encoder_state.h index e6a098f404..fcfff53ede 100644 --- a/library/cpp/monlib/encode/encoder_state.h +++ b/library/cpp/monlib/encode/encoder_state.h @@ -1,24 +1,24 @@ #pragma once -#include "encoder_state_enum.h" - -#include <util/generic/serialized_enum.h> +#include "encoder_state_enum.h" + +#include <util/generic/serialized_enum.h> #include <util/generic/yexception.h> - + namespace NMonitoring { - - template <typename EEncoderState> - class TEncoderStateImpl { + + template <typename EEncoderState> + class TEncoderStateImpl { public: - using EState = EEncoderState; + using EState = EEncoderState; - explicit TEncoderStateImpl(EEncoderState state = EEncoderState::ROOT) + explicit TEncoderStateImpl(EEncoderState state = EEncoderState::ROOT) : State_(state) { } - TEncoderStateImpl& operator=(EEncoderState rhs) noexcept { + TEncoderStateImpl& operator=(EEncoderState rhs) noexcept { State_ = rhs; return *this; } @@ -40,7 +40,7 @@ namespace NMonitoring { if (Y_UNLIKELY(State_ != expected)) { ythrow yexception() << "invalid encoder state: " << ToStr() - << ", expected: " << TEncoderStateImpl(expected).ToStr(); + << ", expected: " << TEncoderStateImpl(expected).ToStr(); } } @@ -49,14 +49,14 @@ namespace NMonitoring { State_ = to; } - TStringBuf ToStr() const noexcept { - return NEnumSerializationRuntime::GetEnumNamesImpl<EEncoderState>().at(State_); - } + TStringBuf ToStr() const noexcept { + return NEnumSerializationRuntime::GetEnumNamesImpl<EEncoderState>().at(State_); + } private: EEncoderState State_; }; - using TEncoderState = TEncoderStateImpl<EEncoderState>; - -} // namespace NMonitoring + using TEncoderState = TEncoderStateImpl<EEncoderState>; + +} // namespace NMonitoring diff --git a/library/cpp/monlib/encode/encoder_state_enum.h b/library/cpp/monlib/encode/encoder_state_enum.h index 471604f91d..a384ea0c51 100644 --- a/library/cpp/monlib/encode/encoder_state_enum.h +++ b/library/cpp/monlib/encode/encoder_state_enum.h @@ -1,12 +1,12 @@ -#pragma once - -namespace NMonitoring { - - enum class EEncoderState { - ROOT, - COMMON_LABELS, +#pragma once + +namespace NMonitoring { + + enum class EEncoderState { + ROOT, + COMMON_LABELS, METRIC, METRIC_LABELS, - }; - -} // namespace NMonitoring + }; + +} // namespace NMonitoring diff --git a/library/cpp/monlib/encode/format.h b/library/cpp/monlib/encode/format.h index 495d42d786..86f05e8f5f 100644 --- a/library/cpp/monlib/encode/format.h +++ b/library/cpp/monlib/encode/format.h @@ -87,30 +87,30 @@ namespace NMonitoring { }; enum class EMetricsMergingMode { - /** + /** * Do not merge metric batches. If several points of the same metric were - * added multiple times accross different writes, paste them as + * added multiple times accross different writes, paste them as * separate metrics. - * - * Example: - * COUNTER [(ts1, val1)] | COUNTER [(ts1, val1)] - * COUNTER [(ts2, val2)] | --> COUNTER [(ts2, val2)] - * COUNTER [(ts3, val3)] | COUNTER [(ts3, val3)] - */ - DEFAULT, - - /** + * + * Example: + * COUNTER [(ts1, val1)] | COUNTER [(ts1, val1)] + * COUNTER [(ts2, val2)] | --> COUNTER [(ts2, val2)] + * COUNTER [(ts3, val3)] | COUNTER [(ts3, val3)] + */ + DEFAULT, + + /** * If several points of the same metric were added multiple times across - * different writes, merge all values to one timeseries. - * - * Example: - * COUNTER [(ts1, val1)] | - * COUNTER [(ts2, val2)] | --> COUNTER [(ts1, val1), (ts2, val2), (ts3, val3)] - * COUNTER [(ts3, val3)] | - */ + * different writes, merge all values to one timeseries. + * + * Example: + * COUNTER [(ts1, val1)] | + * COUNTER [(ts2, val2)] | --> COUNTER [(ts1, val1), (ts2, val2), (ts3, val3)] + * COUNTER [(ts3, val3)] | + */ MERGE_METRICS, - }; - + }; + /** * Matches serialization format by the given "Accept" header value. * diff --git a/library/cpp/monlib/encode/json/json.h b/library/cpp/monlib/encode/json/json.h index 21530f20c3..b76900b8ab 100644 --- a/library/cpp/monlib/encode/json/json.h +++ b/library/cpp/monlib/encode/json/json.h @@ -3,14 +3,14 @@ #include <library/cpp/monlib/encode/encoder.h> #include <library/cpp/monlib/encode/format.h> - + class IOutputStream; namespace NMonitoring { - - class TJsonDecodeError: public yexception { - }; - + + class TJsonDecodeError: public yexception { + }; + IMetricEncoderPtr EncoderJson(IOutputStream* out, int indentation = 0); /// Buffered encoder will merge series with same labels into one. diff --git a/library/cpp/monlib/encode/json/json_decoder.cpp b/library/cpp/monlib/encode/json/json_decoder.cpp index d44ff5fd28..7f0d83e018 100644 --- a/library/cpp/monlib/encode/json/json_decoder.cpp +++ b/library/cpp/monlib/encode/json/json_decoder.cpp @@ -1,8 +1,8 @@ #include "json.h" #include "typed_point.h" - -#include <library/cpp/monlib/exception/exception.h> + +#include <library/cpp/monlib/exception/exception.h> #include <library/cpp/monlib/metrics/labels.h> #include <library/cpp/monlib/metrics/metric_value.h> @@ -14,1149 +14,1149 @@ #include <limits> namespace NMonitoring { - -#define DECODE_ENSURE(COND, ...) MONLIB_ENSURE_EX(COND, TJsonDecodeError() << __VA_ARGS__) - -namespace { - -/////////////////////////////////////////////////////////////////////// -// THistogramBuilder -/////////////////////////////////////////////////////////////////////// -class THistogramBuilder { -public: - void AddBound(TBucketBound bound) { - if (!Bounds_.empty()) { - DECODE_ENSURE(Bounds_.back() < bound, - "non sorted bounds, " << Bounds_.back() << - " >= " << bound); - } - Bounds_.push_back(bound); - } - - void AddValue(TBucketValue value) { - Values_.push_back(value); - } - - void AddInf(TBucketValue value) { - InfPresented_ = true; - InfValue_ = value; - } - - IHistogramSnapshotPtr Build() { - if (InfPresented_) { - Bounds_.push_back(Max<TBucketBound>()); - Values_.push_back(InfValue_); - } - - auto snapshot = ExplicitHistogramSnapshot(Bounds_, Values_); - - Bounds_.clear(); - Values_.clear(); + +#define DECODE_ENSURE(COND, ...) MONLIB_ENSURE_EX(COND, TJsonDecodeError() << __VA_ARGS__) + +namespace { + +/////////////////////////////////////////////////////////////////////// +// THistogramBuilder +/////////////////////////////////////////////////////////////////////// +class THistogramBuilder { +public: + void AddBound(TBucketBound bound) { + if (!Bounds_.empty()) { + DECODE_ENSURE(Bounds_.back() < bound, + "non sorted bounds, " << Bounds_.back() << + " >= " << bound); + } + Bounds_.push_back(bound); + } + + void AddValue(TBucketValue value) { + Values_.push_back(value); + } + + void AddInf(TBucketValue value) { + InfPresented_ = true; + InfValue_ = value; + } + + IHistogramSnapshotPtr Build() { + if (InfPresented_) { + Bounds_.push_back(Max<TBucketBound>()); + Values_.push_back(InfValue_); + } + + auto snapshot = ExplicitHistogramSnapshot(Bounds_, Values_); + + Bounds_.clear(); + Values_.clear(); InfPresented_ = false; - return snapshot; - } - - bool Empty() const noexcept { - return Bounds_.empty() && Values_.empty(); - } - - void Clear() { - Bounds_.clear(); - Values_.clear(); - } - -private: - TBucketBounds Bounds_; - TBucketValues Values_; - - bool InfPresented_ = false; - TBucketValue InfValue_; -}; - -class TSummaryDoubleBuilder { -public: - ISummaryDoubleSnapshotPtr Build() const { - return MakeIntrusive<TSummaryDoubleSnapshot>(Sum_, Min_, Max_, Last_, Count_); - } - - void SetSum(double sum) { - Empty_ = false; - Sum_ = sum; - } - - void SetMin(double min) { - Empty_ = false; - Min_ = min; - } - - void SetMax(double max) { - Empty_ = false; - Max_ = max; - } - - void SetLast(double last) { - Empty_ = false; - Last_ = last; - } - - void SetCount(ui64 count) { - Empty_ = false; - Count_ = count; - } - - void Clear() { - Empty_ = true; - Sum_ = 0; - Min_ = 0; - Max_ = 0; - Last_ = 0; - Count_ = 0; - } - - bool Empty() const { - return Empty_; - } - -private: - double Sum_ = 0; - double Min_ = 0; - double Max_ = 0; - double Last_ = 0; - ui64 Count_ = 0; - bool Empty_ = true; -}; - -class TLogHistogramBuilder { -public: - void SetBase(double base) { - DECODE_ENSURE(base > 0, "base must be positive"); - Base_ = base; - } - - void SetZerosCount(ui64 zerosCount) { - DECODE_ENSURE(zerosCount >= 0, "zeros count must be positive"); - ZerosCount_ = zerosCount; - } - - void SetStartPower(int startPower) { - StartPower_ = startPower; - } - - void AddBucketValue(double value) { - DECODE_ENSURE(value > 0.0, "bucket values must be positive"); - DECODE_ENSURE(value < std::numeric_limits<double>::max(), "bucket values must be finite"); - Buckets_.push_back(value); - } - - void Clear() { - Buckets_.clear(); - Base_ = 1.5; - ZerosCount_ = 0; - StartPower_ = 0; - } - - bool Empty() const { - return Buckets_.empty() && ZerosCount_ == 0; - } - - TLogHistogramSnapshotPtr Build() { - return MakeIntrusive<TLogHistogramSnapshot>(Base_, ZerosCount_, StartPower_, std::move(Buckets_)); - } - -private: - double Base_ = 1.5; - ui64 ZerosCount_ = 0; - int StartPower_ = 0; - TVector<double> Buckets_; -}; - -std::pair<double, bool> ParseSpecDouble(TStringBuf string) { + return snapshot; + } + + bool Empty() const noexcept { + return Bounds_.empty() && Values_.empty(); + } + + void Clear() { + Bounds_.clear(); + Values_.clear(); + } + +private: + TBucketBounds Bounds_; + TBucketValues Values_; + + bool InfPresented_ = false; + TBucketValue InfValue_; +}; + +class TSummaryDoubleBuilder { +public: + ISummaryDoubleSnapshotPtr Build() const { + return MakeIntrusive<TSummaryDoubleSnapshot>(Sum_, Min_, Max_, Last_, Count_); + } + + void SetSum(double sum) { + Empty_ = false; + Sum_ = sum; + } + + void SetMin(double min) { + Empty_ = false; + Min_ = min; + } + + void SetMax(double max) { + Empty_ = false; + Max_ = max; + } + + void SetLast(double last) { + Empty_ = false; + Last_ = last; + } + + void SetCount(ui64 count) { + Empty_ = false; + Count_ = count; + } + + void Clear() { + Empty_ = true; + Sum_ = 0; + Min_ = 0; + Max_ = 0; + Last_ = 0; + Count_ = 0; + } + + bool Empty() const { + return Empty_; + } + +private: + double Sum_ = 0; + double Min_ = 0; + double Max_ = 0; + double Last_ = 0; + ui64 Count_ = 0; + bool Empty_ = true; +}; + +class TLogHistogramBuilder { +public: + void SetBase(double base) { + DECODE_ENSURE(base > 0, "base must be positive"); + Base_ = base; + } + + void SetZerosCount(ui64 zerosCount) { + DECODE_ENSURE(zerosCount >= 0, "zeros count must be positive"); + ZerosCount_ = zerosCount; + } + + void SetStartPower(int startPower) { + StartPower_ = startPower; + } + + void AddBucketValue(double value) { + DECODE_ENSURE(value > 0.0, "bucket values must be positive"); + DECODE_ENSURE(value < std::numeric_limits<double>::max(), "bucket values must be finite"); + Buckets_.push_back(value); + } + + void Clear() { + Buckets_.clear(); + Base_ = 1.5; + ZerosCount_ = 0; + StartPower_ = 0; + } + + bool Empty() const { + return Buckets_.empty() && ZerosCount_ == 0; + } + + TLogHistogramSnapshotPtr Build() { + return MakeIntrusive<TLogHistogramSnapshot>(Base_, ZerosCount_, StartPower_, std::move(Buckets_)); + } + +private: + double Base_ = 1.5; + ui64 ZerosCount_ = 0; + int StartPower_ = 0; + TVector<double> Buckets_; +}; + +std::pair<double, bool> ParseSpecDouble(TStringBuf string) { if (string == TStringBuf("nan") || string == TStringBuf("NaN")) { - return {std::numeric_limits<double>::quiet_NaN(), true}; + return {std::numeric_limits<double>::quiet_NaN(), true}; } else if (string == TStringBuf("inf") || string == TStringBuf("Infinity")) { - return {std::numeric_limits<double>::infinity(), true}; + return {std::numeric_limits<double>::infinity(), true}; } else if (string == TStringBuf("-inf") || string == TStringBuf("-Infinity")) { - return {-std::numeric_limits<double>::infinity(), true}; - } else { - return {0, false}; - } -} - -/////////////////////////////////////////////////////////////////////// -// TMetricCollector -/////////////////////////////////////////////////////////////////////// -struct TMetricCollector { - EMetricType Type = EMetricType::UNKNOWN; - TLabels Labels; - THistogramBuilder HistogramBuilder; - TSummaryDoubleBuilder SummaryBuilder; - TLogHistogramBuilder LogHistBuilder; - TTypedPoint LastPoint; - TVector<TTypedPoint> TimeSeries; - - bool SeenTsOrValue = false; - bool SeenTimeseries = false; - - void Clear() { - Type = EMetricType::UNKNOWN; - Labels.Clear(); - SeenTsOrValue = false; - SeenTimeseries = false; - TimeSeries.clear(); - LastPoint = {}; - HistogramBuilder.Clear(); - SummaryBuilder.Clear(); - LogHistBuilder.Clear(); - } - - void AddLabel(const TLabel& label) { - Labels.Add(label.Name(), label.Value()); - } - - void SetLastTime(TInstant time) { - LastPoint.SetTime(time); - } - - template <typename T> - void SetLastValue(T value) { - LastPoint.SetValue(value); - } - - void SaveLastPoint() { - DECODE_ENSURE(LastPoint.GetTime() != TInstant::Zero(), - "cannot add point without or zero timestamp"); - if (!HistogramBuilder.Empty()) { - auto histogram = HistogramBuilder.Build(); - TimeSeries.emplace_back(LastPoint.GetTime(), histogram.Get()); - } else if (!SummaryBuilder.Empty()) { - auto summary = SummaryBuilder.Build(); - TimeSeries.emplace_back(LastPoint.GetTime(), summary.Get()); - } else if (!LogHistBuilder.Empty()) { - auto logHist = LogHistBuilder.Build(); - TimeSeries.emplace_back(LastPoint.GetTime(), logHist.Get()); - } else { - TimeSeries.push_back(std::move(LastPoint)); - } - } - - template <typename TConsumer> - void Consume(TConsumer&& consumer) { - if (TimeSeries.empty()) { - const auto& p = LastPoint; - consumer(p.GetTime(), p.GetValueType(), p.GetValue()); - } else { - for (const auto& p: TimeSeries) { - consumer(p.GetTime(), p.GetValueType(), p.GetValue()); + return {-std::numeric_limits<double>::infinity(), true}; + } else { + return {0, false}; + } +} + +/////////////////////////////////////////////////////////////////////// +// TMetricCollector +/////////////////////////////////////////////////////////////////////// +struct TMetricCollector { + EMetricType Type = EMetricType::UNKNOWN; + TLabels Labels; + THistogramBuilder HistogramBuilder; + TSummaryDoubleBuilder SummaryBuilder; + TLogHistogramBuilder LogHistBuilder; + TTypedPoint LastPoint; + TVector<TTypedPoint> TimeSeries; + + bool SeenTsOrValue = false; + bool SeenTimeseries = false; + + void Clear() { + Type = EMetricType::UNKNOWN; + Labels.Clear(); + SeenTsOrValue = false; + SeenTimeseries = false; + TimeSeries.clear(); + LastPoint = {}; + HistogramBuilder.Clear(); + SummaryBuilder.Clear(); + LogHistBuilder.Clear(); + } + + void AddLabel(const TLabel& label) { + Labels.Add(label.Name(), label.Value()); + } + + void SetLastTime(TInstant time) { + LastPoint.SetTime(time); + } + + template <typename T> + void SetLastValue(T value) { + LastPoint.SetValue(value); + } + + void SaveLastPoint() { + DECODE_ENSURE(LastPoint.GetTime() != TInstant::Zero(), + "cannot add point without or zero timestamp"); + if (!HistogramBuilder.Empty()) { + auto histogram = HistogramBuilder.Build(); + TimeSeries.emplace_back(LastPoint.GetTime(), histogram.Get()); + } else if (!SummaryBuilder.Empty()) { + auto summary = SummaryBuilder.Build(); + TimeSeries.emplace_back(LastPoint.GetTime(), summary.Get()); + } else if (!LogHistBuilder.Empty()) { + auto logHist = LogHistBuilder.Build(); + TimeSeries.emplace_back(LastPoint.GetTime(), logHist.Get()); + } else { + TimeSeries.push_back(std::move(LastPoint)); + } + } + + template <typename TConsumer> + void Consume(TConsumer&& consumer) { + if (TimeSeries.empty()) { + const auto& p = LastPoint; + consumer(p.GetTime(), p.GetValueType(), p.GetValue()); + } else { + for (const auto& p: TimeSeries) { + consumer(p.GetTime(), p.GetValueType(), p.GetValue()); } - } - } -}; - -struct TCommonParts { - TInstant CommonTime; - TLabels CommonLabels; -}; - -class IHaltableMetricConsumer: public IMetricConsumer { -public: - virtual bool NeedToStop() const = 0; -}; - -// TODO(ivanzhukov@): check all states for cases when a json document is invalid -// e.g. "metrics" or "commonLabels" keys are specified multiple times -class TCommonPartsCollector: public IHaltableMetricConsumer { -public: - TCommonParts&& CommonParts() { - return std::move(CommonParts_); - } - -private: - bool NeedToStop() const override { - return TInstant::Zero() != CommonParts_.CommonTime && !CommonParts_.CommonLabels.Empty(); - } - - void OnStreamBegin() override { - } - - void OnStreamEnd() override { - } - - void OnCommonTime(TInstant time) override { - CommonParts_.CommonTime = time; - } - - void OnMetricBegin(EMetricType) override { - IsMetric_ = true; - } - - void OnMetricEnd() override { - IsMetric_ = false; - } - - void OnLabelsBegin() override { - } - - void OnLabelsEnd() override { - } - - void OnLabel(TStringBuf name, TStringBuf value) override { - if (!IsMetric_) { - CommonParts_.CommonLabels.Add(std::move(name), std::move(value)); - } - } - - void OnDouble(TInstant, double) override { - } - - void OnInt64(TInstant, i64) override { - } - - void OnUint64(TInstant, ui64) override { - } - - void OnHistogram(TInstant, IHistogramSnapshotPtr) override { - } - - void OnLogHistogram(TInstant, TLogHistogramSnapshotPtr) override { - } - - void OnSummaryDouble(TInstant, ISummaryDoubleSnapshotPtr) override { - } - -private: - TCommonParts CommonParts_; - bool IsMetric_{false}; -}; - -class TCommonPartsProxy: public IHaltableMetricConsumer { -public: - TCommonPartsProxy(TCommonParts&& commonParts, IMetricConsumer* c) - : CommonParts_{std::move(commonParts)} - , Consumer_{c} - {} - -private: - bool NeedToStop() const override { - return false; - } - - void OnStreamBegin() override { - Consumer_->OnStreamBegin(); - - if (!CommonParts_.CommonLabels.Empty()) { - Consumer_->OnLabelsBegin(); - - for (auto&& label : CommonParts_.CommonLabels) { - Consumer_->OnLabel(label.Name(), label.Value()); - } - - Consumer_->OnLabelsEnd(); - } - - if (TInstant::Zero() != CommonParts_.CommonTime) { - Consumer_->OnCommonTime(CommonParts_.CommonTime); - } - } - - void OnStreamEnd() override { - Consumer_->OnStreamEnd(); - } - - void OnCommonTime(TInstant) override { - } - - void OnMetricBegin(EMetricType type) override { - IsMetric_ = true; - - Consumer_->OnMetricBegin(type); - } - - void OnMetricEnd() override { - IsMetric_ = false; - - Consumer_->OnMetricEnd(); - } - - void OnLabelsBegin() override { - if (IsMetric_) { - Consumer_->OnLabelsBegin(); - } - } - - void OnLabelsEnd() override { - if (IsMetric_) { - Consumer_->OnLabelsEnd(); - } - } - - void OnLabel(TStringBuf name, TStringBuf value) override { - if (IsMetric_) { - Consumer_->OnLabel(std::move(name), std::move(value)); - } - } - - void OnDouble(TInstant time, double value) override { - Consumer_->OnDouble(time, value); - } - - void OnInt64(TInstant time, i64 value) override { - Consumer_->OnInt64(time, value); - } - - void OnUint64(TInstant time, ui64 value) override { - Consumer_->OnUint64(time, value); - } - - void OnHistogram(TInstant time, IHistogramSnapshotPtr snapshot) override { - Consumer_->OnHistogram(time, std::move(snapshot)); - } - - void OnLogHistogram(TInstant time, TLogHistogramSnapshotPtr snapshot) override { - Consumer_->OnLogHistogram(time, std::move(snapshot)); - } - - void OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr snapshot) override { - Consumer_->OnSummaryDouble(time, std::move(snapshot)); - } - -private: - const TCommonParts CommonParts_; - IMetricConsumer* Consumer_; - bool IsMetric_{false}; -}; - -/////////////////////////////////////////////////////////////////////// -// TDecoderJson -/////////////////////////////////////////////////////////////////////// -class TDecoderJson final: public NJson::TJsonCallbacks { - struct TState { - enum EState { - ROOT_OBJECT = 0x01, - - COMMON_LABELS, - COMMON_TS, - METRICS_ARRAY, - - METRIC_OBJECT, + } + } +}; + +struct TCommonParts { + TInstant CommonTime; + TLabels CommonLabels; +}; + +class IHaltableMetricConsumer: public IMetricConsumer { +public: + virtual bool NeedToStop() const = 0; +}; + +// TODO(ivanzhukov@): check all states for cases when a json document is invalid +// e.g. "metrics" or "commonLabels" keys are specified multiple times +class TCommonPartsCollector: public IHaltableMetricConsumer { +public: + TCommonParts&& CommonParts() { + return std::move(CommonParts_); + } + +private: + bool NeedToStop() const override { + return TInstant::Zero() != CommonParts_.CommonTime && !CommonParts_.CommonLabels.Empty(); + } + + void OnStreamBegin() override { + } + + void OnStreamEnd() override { + } + + void OnCommonTime(TInstant time) override { + CommonParts_.CommonTime = time; + } + + void OnMetricBegin(EMetricType) override { + IsMetric_ = true; + } + + void OnMetricEnd() override { + IsMetric_ = false; + } + + void OnLabelsBegin() override { + } + + void OnLabelsEnd() override { + } + + void OnLabel(TStringBuf name, TStringBuf value) override { + if (!IsMetric_) { + CommonParts_.CommonLabels.Add(std::move(name), std::move(value)); + } + } + + void OnDouble(TInstant, double) override { + } + + void OnInt64(TInstant, i64) override { + } + + void OnUint64(TInstant, ui64) override { + } + + void OnHistogram(TInstant, IHistogramSnapshotPtr) override { + } + + void OnLogHistogram(TInstant, TLogHistogramSnapshotPtr) override { + } + + void OnSummaryDouble(TInstant, ISummaryDoubleSnapshotPtr) override { + } + +private: + TCommonParts CommonParts_; + bool IsMetric_{false}; +}; + +class TCommonPartsProxy: public IHaltableMetricConsumer { +public: + TCommonPartsProxy(TCommonParts&& commonParts, IMetricConsumer* c) + : CommonParts_{std::move(commonParts)} + , Consumer_{c} + {} + +private: + bool NeedToStop() const override { + return false; + } + + void OnStreamBegin() override { + Consumer_->OnStreamBegin(); + + if (!CommonParts_.CommonLabels.Empty()) { + Consumer_->OnLabelsBegin(); + + for (auto&& label : CommonParts_.CommonLabels) { + Consumer_->OnLabel(label.Name(), label.Value()); + } + + Consumer_->OnLabelsEnd(); + } + + if (TInstant::Zero() != CommonParts_.CommonTime) { + Consumer_->OnCommonTime(CommonParts_.CommonTime); + } + } + + void OnStreamEnd() override { + Consumer_->OnStreamEnd(); + } + + void OnCommonTime(TInstant) override { + } + + void OnMetricBegin(EMetricType type) override { + IsMetric_ = true; + + Consumer_->OnMetricBegin(type); + } + + void OnMetricEnd() override { + IsMetric_ = false; + + Consumer_->OnMetricEnd(); + } + + void OnLabelsBegin() override { + if (IsMetric_) { + Consumer_->OnLabelsBegin(); + } + } + + void OnLabelsEnd() override { + if (IsMetric_) { + Consumer_->OnLabelsEnd(); + } + } + + void OnLabel(TStringBuf name, TStringBuf value) override { + if (IsMetric_) { + Consumer_->OnLabel(std::move(name), std::move(value)); + } + } + + void OnDouble(TInstant time, double value) override { + Consumer_->OnDouble(time, value); + } + + void OnInt64(TInstant time, i64 value) override { + Consumer_->OnInt64(time, value); + } + + void OnUint64(TInstant time, ui64 value) override { + Consumer_->OnUint64(time, value); + } + + void OnHistogram(TInstant time, IHistogramSnapshotPtr snapshot) override { + Consumer_->OnHistogram(time, std::move(snapshot)); + } + + void OnLogHistogram(TInstant time, TLogHistogramSnapshotPtr snapshot) override { + Consumer_->OnLogHistogram(time, std::move(snapshot)); + } + + void OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr snapshot) override { + Consumer_->OnSummaryDouble(time, std::move(snapshot)); + } + +private: + const TCommonParts CommonParts_; + IMetricConsumer* Consumer_; + bool IsMetric_{false}; +}; + +/////////////////////////////////////////////////////////////////////// +// TDecoderJson +/////////////////////////////////////////////////////////////////////// +class TDecoderJson final: public NJson::TJsonCallbacks { + struct TState { + enum EState { + ROOT_OBJECT = 0x01, + + COMMON_LABELS, + COMMON_TS, + METRICS_ARRAY, + + METRIC_OBJECT, METRIC_NAME, - METRIC_LABELS, - METRIC_TYPE, - METRIC_MODE, // TODO: must be deleted - METRIC_TIMESERIES, - METRIC_TS, - METRIC_VALUE, - METRIC_HIST, - METRIC_HIST_BOUNDS, - METRIC_HIST_BUCKETS, - METRIC_HIST_INF, - METRIC_DSUMMARY, - METRIC_DSUMMARY_SUM, - METRIC_DSUMMARY_MIN, - METRIC_DSUMMARY_MAX, - METRIC_DSUMMARY_LAST, - METRIC_DSUMMARY_COUNT, - METRIC_LOG_HIST, - METRIC_LOG_HIST_BASE, - METRIC_LOG_HIST_ZEROS, - METRIC_LOG_HIST_START_POWER, - METRIC_LOG_HIST_BUCKETS, - }; - - constexpr EState Current() const noexcept { - return static_cast<EState>(State_ & 0xFF); - } - - void ToNext(EState state) noexcept { - constexpr auto bitSize = 8 * sizeof(ui8); - State_ = (State_ << bitSize) | static_cast<ui8>(state); - } - - void ToPrev() noexcept { - constexpr auto bitSize = 8 * sizeof(ui8); - State_ = State_ >> bitSize; - } - - private: - ui64 State_ = static_cast<ui64>(ROOT_OBJECT); - }; - -public: + METRIC_LABELS, + METRIC_TYPE, + METRIC_MODE, // TODO: must be deleted + METRIC_TIMESERIES, + METRIC_TS, + METRIC_VALUE, + METRIC_HIST, + METRIC_HIST_BOUNDS, + METRIC_HIST_BUCKETS, + METRIC_HIST_INF, + METRIC_DSUMMARY, + METRIC_DSUMMARY_SUM, + METRIC_DSUMMARY_MIN, + METRIC_DSUMMARY_MAX, + METRIC_DSUMMARY_LAST, + METRIC_DSUMMARY_COUNT, + METRIC_LOG_HIST, + METRIC_LOG_HIST_BASE, + METRIC_LOG_HIST_ZEROS, + METRIC_LOG_HIST_START_POWER, + METRIC_LOG_HIST_BUCKETS, + }; + + constexpr EState Current() const noexcept { + return static_cast<EState>(State_ & 0xFF); + } + + void ToNext(EState state) noexcept { + constexpr auto bitSize = 8 * sizeof(ui8); + State_ = (State_ << bitSize) | static_cast<ui8>(state); + } + + void ToPrev() noexcept { + constexpr auto bitSize = 8 * sizeof(ui8); + State_ = State_ >> bitSize; + } + + private: + ui64 State_ = static_cast<ui64>(ROOT_OBJECT); + }; + +public: TDecoderJson(TStringBuf data, IHaltableMetricConsumer* metricConsumer, TStringBuf metricNameLabel) - : Data_(data) - , MetricConsumer_(metricConsumer) + : Data_(data) + , MetricConsumer_(metricConsumer) , MetricNameLabel_(metricNameLabel) - { - } - -private: -#define PARSE_ENSURE(CONDITION, ...) \ -do { \ -if (Y_UNLIKELY(!(CONDITION))) { \ - ErrorMsg_ = TStringBuilder() << __VA_ARGS__; \ - return false; \ -} \ -} while (false) - - bool OnInteger(long long value) override { - switch (State_.Current()) { - case TState::COMMON_TS: - PARSE_ENSURE(value >= 0, "unexpected negative number in a common timestamp: " << value); - MetricConsumer_->OnCommonTime(TInstant::Seconds(value)); - State_.ToPrev(); - - if (MetricConsumer_->NeedToStop()) { - IsIntentionallyHalted_ = true; - return false; - } - - break; - - case TState::METRIC_TS: - PARSE_ENSURE(value >= 0, "unexpected negative number in a metric timestamp: " << value); - LastMetric_.SetLastTime(TInstant::Seconds(value)); - State_.ToPrev(); - break; - - case TState::METRIC_VALUE: - LastMetric_.SetLastValue(static_cast<i64>(value)); - State_.ToPrev(); - break; - - case TState::METRIC_HIST_BOUNDS: - LastMetric_.HistogramBuilder.AddBound(static_cast<double>(value)); - break; - - case TState::METRIC_HIST_BUCKETS: - PARSE_ENSURE(value >= 0 && static_cast<ui64>(value) <= Max<TBucketValues::value_type>(), "value is out of bounds " << value); - LastMetric_.HistogramBuilder.AddValue(value); - break; - - case TState::METRIC_HIST_INF: - PARSE_ENSURE(value >= 0, "unexpected negative number in histogram inf: " << value); - LastMetric_.HistogramBuilder.AddInf(value); - State_.ToPrev(); - break; - - case TState::METRIC_DSUMMARY_COUNT: - LastMetric_.SummaryBuilder.SetCount(value); - State_.ToPrev(); - break; - - case TState::METRIC_DSUMMARY_SUM: - LastMetric_.SummaryBuilder.SetSum(value); - State_.ToPrev(); - break; - case TState::METRIC_DSUMMARY_MIN: - LastMetric_.SummaryBuilder.SetMin(value); - State_.ToPrev(); - break; - case TState::METRIC_DSUMMARY_MAX: - LastMetric_.SummaryBuilder.SetMax(value); - State_.ToPrev(); - break; - case TState::METRIC_DSUMMARY_LAST: - LastMetric_.SummaryBuilder.SetLast(value); - State_.ToPrev(); - break; - - case TState::METRIC_LOG_HIST_BASE: - LastMetric_.LogHistBuilder.SetBase(value); - State_.ToPrev(); - break; - - case TState::METRIC_LOG_HIST_ZEROS: - LastMetric_.LogHistBuilder.SetZerosCount(value); - State_.ToPrev(); - break; - - case TState::METRIC_LOG_HIST_START_POWER: - LastMetric_.LogHistBuilder.SetStartPower(value); - State_.ToPrev(); - break; - - case TState::METRIC_LOG_HIST_BUCKETS: - LastMetric_.LogHistBuilder.AddBucketValue(value); - break; - - default: - return false; - } - return true; - } - - bool OnUInteger(unsigned long long value) override { - switch (State_.Current()) { - case TState::COMMON_TS: - MetricConsumer_->OnCommonTime(TInstant::Seconds(value)); - State_.ToPrev(); - - if (MetricConsumer_->NeedToStop()) { - IsIntentionallyHalted_ = true; - return false; - } - - break; - - case TState::METRIC_TS: - LastMetric_.SetLastTime(TInstant::Seconds(value)); - State_.ToPrev(); - break; - - case TState::METRIC_VALUE: - PARSE_ENSURE(value <= Max<ui64>(), "Metric value is out of bounds: " << value); - LastMetric_.SetLastValue(static_cast<ui64>(value)); - State_.ToPrev(); - break; - - case TState::METRIC_HIST_BOUNDS: - LastMetric_.HistogramBuilder.AddBound(static_cast<double>(value)); - break; - - case TState::METRIC_HIST_BUCKETS: - PARSE_ENSURE(value <= Max<TBucketValues::value_type>(), "Histogram bucket value is out of bounds: " << value); - LastMetric_.HistogramBuilder.AddValue(value); - break; - - case TState::METRIC_HIST_INF: - LastMetric_.HistogramBuilder.AddInf(value); - State_.ToPrev(); - break; - - case TState::METRIC_DSUMMARY_COUNT: - LastMetric_.SummaryBuilder.SetCount(value); - State_.ToPrev(); - break; - - case TState::METRIC_DSUMMARY_SUM: - LastMetric_.SummaryBuilder.SetSum(value); - State_.ToPrev(); - break; - case TState::METRIC_DSUMMARY_MIN: - LastMetric_.SummaryBuilder.SetMin(value); - State_.ToPrev(); - break; - case TState::METRIC_DSUMMARY_MAX: - LastMetric_.SummaryBuilder.SetMax(value); - State_.ToPrev(); - break; - case TState::METRIC_DSUMMARY_LAST: - LastMetric_.SummaryBuilder.SetLast(value); - State_.ToPrev(); - break; - - case TState::METRIC_LOG_HIST_BASE: - LastMetric_.LogHistBuilder.SetBase(value); - State_.ToPrev(); - break; - - case TState::METRIC_LOG_HIST_ZEROS: - LastMetric_.LogHistBuilder.SetZerosCount(value); - State_.ToPrev(); - break; - - case TState::METRIC_LOG_HIST_START_POWER: - LastMetric_.LogHistBuilder.SetStartPower(value); - State_.ToPrev(); - break; - - case TState::METRIC_LOG_HIST_BUCKETS: - LastMetric_.LogHistBuilder.AddBucketValue(value); - break; - - default: - return false; - } - return true; - } - - bool OnDouble(double value) override { - switch (State_.Current()) { - case TState::METRIC_VALUE: - LastMetric_.SetLastValue(value); - State_.ToPrev(); - break; - - case TState::METRIC_HIST_BOUNDS: - LastMetric_.HistogramBuilder.AddBound(value); - break; - - case TState::METRIC_DSUMMARY_SUM: - LastMetric_.SummaryBuilder.SetSum(value); - State_.ToPrev(); - break; - case TState::METRIC_DSUMMARY_MIN: - LastMetric_.SummaryBuilder.SetMin(value); - State_.ToPrev(); - break; - case TState::METRIC_DSUMMARY_MAX: - LastMetric_.SummaryBuilder.SetMax(value); - State_.ToPrev(); - break; - case TState::METRIC_DSUMMARY_LAST: - LastMetric_.SummaryBuilder.SetLast(value); - State_.ToPrev(); - break; - - case TState::METRIC_LOG_HIST_BASE: - LastMetric_.LogHistBuilder.SetBase(value); - State_.ToPrev(); - break; - - case TState::METRIC_LOG_HIST_BUCKETS: - LastMetric_.LogHistBuilder.AddBucketValue(value); - break; - - default: - return false; - } - return true; - } - - bool OnString(const TStringBuf& value) override { - switch (State_.Current()) { - case TState::COMMON_LABELS: - PARSE_ENSURE(!LastLabelName_.empty(), "empty label name in common labels"); - MetricConsumer_->OnLabel(LastLabelName_, TString{value}); - break; - - case TState::METRIC_LABELS: - PARSE_ENSURE(!LastLabelName_.empty(), "empty label name in metric labels"); - LastMetric_.Labels.Add(LastLabelName_, TString{value}); - break; - + { + } + +private: +#define PARSE_ENSURE(CONDITION, ...) \ +do { \ +if (Y_UNLIKELY(!(CONDITION))) { \ + ErrorMsg_ = TStringBuilder() << __VA_ARGS__; \ + return false; \ +} \ +} while (false) + + bool OnInteger(long long value) override { + switch (State_.Current()) { + case TState::COMMON_TS: + PARSE_ENSURE(value >= 0, "unexpected negative number in a common timestamp: " << value); + MetricConsumer_->OnCommonTime(TInstant::Seconds(value)); + State_.ToPrev(); + + if (MetricConsumer_->NeedToStop()) { + IsIntentionallyHalted_ = true; + return false; + } + + break; + + case TState::METRIC_TS: + PARSE_ENSURE(value >= 0, "unexpected negative number in a metric timestamp: " << value); + LastMetric_.SetLastTime(TInstant::Seconds(value)); + State_.ToPrev(); + break; + + case TState::METRIC_VALUE: + LastMetric_.SetLastValue(static_cast<i64>(value)); + State_.ToPrev(); + break; + + case TState::METRIC_HIST_BOUNDS: + LastMetric_.HistogramBuilder.AddBound(static_cast<double>(value)); + break; + + case TState::METRIC_HIST_BUCKETS: + PARSE_ENSURE(value >= 0 && static_cast<ui64>(value) <= Max<TBucketValues::value_type>(), "value is out of bounds " << value); + LastMetric_.HistogramBuilder.AddValue(value); + break; + + case TState::METRIC_HIST_INF: + PARSE_ENSURE(value >= 0, "unexpected negative number in histogram inf: " << value); + LastMetric_.HistogramBuilder.AddInf(value); + State_.ToPrev(); + break; + + case TState::METRIC_DSUMMARY_COUNT: + LastMetric_.SummaryBuilder.SetCount(value); + State_.ToPrev(); + break; + + case TState::METRIC_DSUMMARY_SUM: + LastMetric_.SummaryBuilder.SetSum(value); + State_.ToPrev(); + break; + case TState::METRIC_DSUMMARY_MIN: + LastMetric_.SummaryBuilder.SetMin(value); + State_.ToPrev(); + break; + case TState::METRIC_DSUMMARY_MAX: + LastMetric_.SummaryBuilder.SetMax(value); + State_.ToPrev(); + break; + case TState::METRIC_DSUMMARY_LAST: + LastMetric_.SummaryBuilder.SetLast(value); + State_.ToPrev(); + break; + + case TState::METRIC_LOG_HIST_BASE: + LastMetric_.LogHistBuilder.SetBase(value); + State_.ToPrev(); + break; + + case TState::METRIC_LOG_HIST_ZEROS: + LastMetric_.LogHistBuilder.SetZerosCount(value); + State_.ToPrev(); + break; + + case TState::METRIC_LOG_HIST_START_POWER: + LastMetric_.LogHistBuilder.SetStartPower(value); + State_.ToPrev(); + break; + + case TState::METRIC_LOG_HIST_BUCKETS: + LastMetric_.LogHistBuilder.AddBucketValue(value); + break; + + default: + return false; + } + return true; + } + + bool OnUInteger(unsigned long long value) override { + switch (State_.Current()) { + case TState::COMMON_TS: + MetricConsumer_->OnCommonTime(TInstant::Seconds(value)); + State_.ToPrev(); + + if (MetricConsumer_->NeedToStop()) { + IsIntentionallyHalted_ = true; + return false; + } + + break; + + case TState::METRIC_TS: + LastMetric_.SetLastTime(TInstant::Seconds(value)); + State_.ToPrev(); + break; + + case TState::METRIC_VALUE: + PARSE_ENSURE(value <= Max<ui64>(), "Metric value is out of bounds: " << value); + LastMetric_.SetLastValue(static_cast<ui64>(value)); + State_.ToPrev(); + break; + + case TState::METRIC_HIST_BOUNDS: + LastMetric_.HistogramBuilder.AddBound(static_cast<double>(value)); + break; + + case TState::METRIC_HIST_BUCKETS: + PARSE_ENSURE(value <= Max<TBucketValues::value_type>(), "Histogram bucket value is out of bounds: " << value); + LastMetric_.HistogramBuilder.AddValue(value); + break; + + case TState::METRIC_HIST_INF: + LastMetric_.HistogramBuilder.AddInf(value); + State_.ToPrev(); + break; + + case TState::METRIC_DSUMMARY_COUNT: + LastMetric_.SummaryBuilder.SetCount(value); + State_.ToPrev(); + break; + + case TState::METRIC_DSUMMARY_SUM: + LastMetric_.SummaryBuilder.SetSum(value); + State_.ToPrev(); + break; + case TState::METRIC_DSUMMARY_MIN: + LastMetric_.SummaryBuilder.SetMin(value); + State_.ToPrev(); + break; + case TState::METRIC_DSUMMARY_MAX: + LastMetric_.SummaryBuilder.SetMax(value); + State_.ToPrev(); + break; + case TState::METRIC_DSUMMARY_LAST: + LastMetric_.SummaryBuilder.SetLast(value); + State_.ToPrev(); + break; + + case TState::METRIC_LOG_HIST_BASE: + LastMetric_.LogHistBuilder.SetBase(value); + State_.ToPrev(); + break; + + case TState::METRIC_LOG_HIST_ZEROS: + LastMetric_.LogHistBuilder.SetZerosCount(value); + State_.ToPrev(); + break; + + case TState::METRIC_LOG_HIST_START_POWER: + LastMetric_.LogHistBuilder.SetStartPower(value); + State_.ToPrev(); + break; + + case TState::METRIC_LOG_HIST_BUCKETS: + LastMetric_.LogHistBuilder.AddBucketValue(value); + break; + + default: + return false; + } + return true; + } + + bool OnDouble(double value) override { + switch (State_.Current()) { + case TState::METRIC_VALUE: + LastMetric_.SetLastValue(value); + State_.ToPrev(); + break; + + case TState::METRIC_HIST_BOUNDS: + LastMetric_.HistogramBuilder.AddBound(value); + break; + + case TState::METRIC_DSUMMARY_SUM: + LastMetric_.SummaryBuilder.SetSum(value); + State_.ToPrev(); + break; + case TState::METRIC_DSUMMARY_MIN: + LastMetric_.SummaryBuilder.SetMin(value); + State_.ToPrev(); + break; + case TState::METRIC_DSUMMARY_MAX: + LastMetric_.SummaryBuilder.SetMax(value); + State_.ToPrev(); + break; + case TState::METRIC_DSUMMARY_LAST: + LastMetric_.SummaryBuilder.SetLast(value); + State_.ToPrev(); + break; + + case TState::METRIC_LOG_HIST_BASE: + LastMetric_.LogHistBuilder.SetBase(value); + State_.ToPrev(); + break; + + case TState::METRIC_LOG_HIST_BUCKETS: + LastMetric_.LogHistBuilder.AddBucketValue(value); + break; + + default: + return false; + } + return true; + } + + bool OnString(const TStringBuf& value) override { + switch (State_.Current()) { + case TState::COMMON_LABELS: + PARSE_ENSURE(!LastLabelName_.empty(), "empty label name in common labels"); + MetricConsumer_->OnLabel(LastLabelName_, TString{value}); + break; + + case TState::METRIC_LABELS: + PARSE_ENSURE(!LastLabelName_.empty(), "empty label name in metric labels"); + LastMetric_.Labels.Add(LastLabelName_, TString{value}); + break; + case TState::METRIC_NAME: PARSE_ENSURE(!value.empty(), "empty metric name"); LastMetric_.Labels.Add(MetricNameLabel_, TString{value}); State_.ToPrev(); break; - case TState::COMMON_TS: - MetricConsumer_->OnCommonTime(TInstant::ParseIso8601(value)); - State_.ToPrev(); - - if (MetricConsumer_->NeedToStop()) { - IsIntentionallyHalted_ = true; - return false; + case TState::COMMON_TS: + MetricConsumer_->OnCommonTime(TInstant::ParseIso8601(value)); + State_.ToPrev(); + + if (MetricConsumer_->NeedToStop()) { + IsIntentionallyHalted_ = true; + return false; + } + + break; + + case TState::METRIC_TS: + LastMetric_.SetLastTime(TInstant::ParseIso8601(value)); + State_.ToPrev(); + break; + + case TState::METRIC_VALUE: + if (auto [doubleValue, ok] = ParseSpecDouble(value); ok) { + LastMetric_.SetLastValue(doubleValue); + } else { + return false; } + State_.ToPrev(); + break; - break; - - case TState::METRIC_TS: - LastMetric_.SetLastTime(TInstant::ParseIso8601(value)); - State_.ToPrev(); - break; + case TState::METRIC_TYPE: + LastMetric_.Type = MetricTypeFromStr(value); + State_.ToPrev(); + break; - case TState::METRIC_VALUE: - if (auto [doubleValue, ok] = ParseSpecDouble(value); ok) { - LastMetric_.SetLastValue(doubleValue); - } else { - return false; - } - State_.ToPrev(); - break; - - case TState::METRIC_TYPE: - LastMetric_.Type = MetricTypeFromStr(value); - State_.ToPrev(); - break; - - case TState::METRIC_MODE: + case TState::METRIC_MODE: if (value == TStringBuf("deriv")) { - LastMetric_.Type = EMetricType::RATE; - } - State_.ToPrev(); - break; - - case TState::METRIC_DSUMMARY_SUM: - if (auto [doubleValue, ok] = ParseSpecDouble(value); ok) { - LastMetric_.SummaryBuilder.SetSum(doubleValue); - } else { - return false; - } - State_.ToPrev(); - break; - - case TState::METRIC_DSUMMARY_MIN: - if (auto [doubleValue, ok] = ParseSpecDouble(value); ok) { - LastMetric_.SummaryBuilder.SetMin(doubleValue); - } else { - return false; - } - State_.ToPrev(); - break; - - case TState::METRIC_DSUMMARY_MAX: - if (auto [doubleValue, ok] = ParseSpecDouble(value); ok) { - LastMetric_.SummaryBuilder.SetMax(doubleValue); - } else { - return false; - } - State_.ToPrev(); - break; - - case TState::METRIC_DSUMMARY_LAST: - if (auto [doubleValue, ok] = ParseSpecDouble(value); ok) { - LastMetric_.SummaryBuilder.SetLast(doubleValue); - } else { - return false; - } - State_.ToPrev(); - break; - - default: - return false; - } - - return true; - } - - bool OnMapKey(const TStringBuf& key) override { - switch (State_.Current()) { - case TState::ROOT_OBJECT: + LastMetric_.Type = EMetricType::RATE; + } + State_.ToPrev(); + break; + + case TState::METRIC_DSUMMARY_SUM: + if (auto [doubleValue, ok] = ParseSpecDouble(value); ok) { + LastMetric_.SummaryBuilder.SetSum(doubleValue); + } else { + return false; + } + State_.ToPrev(); + break; + + case TState::METRIC_DSUMMARY_MIN: + if (auto [doubleValue, ok] = ParseSpecDouble(value); ok) { + LastMetric_.SummaryBuilder.SetMin(doubleValue); + } else { + return false; + } + State_.ToPrev(); + break; + + case TState::METRIC_DSUMMARY_MAX: + if (auto [doubleValue, ok] = ParseSpecDouble(value); ok) { + LastMetric_.SummaryBuilder.SetMax(doubleValue); + } else { + return false; + } + State_.ToPrev(); + break; + + case TState::METRIC_DSUMMARY_LAST: + if (auto [doubleValue, ok] = ParseSpecDouble(value); ok) { + LastMetric_.SummaryBuilder.SetLast(doubleValue); + } else { + return false; + } + State_.ToPrev(); + break; + + default: + return false; + } + + return true; + } + + bool OnMapKey(const TStringBuf& key) override { + switch (State_.Current()) { + case TState::ROOT_OBJECT: if (key == TStringBuf("commonLabels") || key == TStringBuf("labels")) { - State_.ToNext(TState::COMMON_LABELS); + State_.ToNext(TState::COMMON_LABELS); } else if (key == TStringBuf("ts")) { - State_.ToNext(TState::COMMON_TS); + State_.ToNext(TState::COMMON_TS); } else if (key == TStringBuf("sensors") || key == TStringBuf("metrics")) { - State_.ToNext(TState::METRICS_ARRAY); - } - break; + State_.ToNext(TState::METRICS_ARRAY); + } + break; - case TState::COMMON_LABELS: - case TState::METRIC_LABELS: - LastLabelName_ = key; - break; + case TState::COMMON_LABELS: + case TState::METRIC_LABELS: + LastLabelName_ = key; + break; - case TState::METRIC_OBJECT: + case TState::METRIC_OBJECT: if (key == TStringBuf("labels")) { - State_.ToNext(TState::METRIC_LABELS); + State_.ToNext(TState::METRIC_LABELS); } else if (key == TStringBuf("name")) { State_.ToNext(TState::METRIC_NAME); } else if (key == TStringBuf("ts")) { - PARSE_ENSURE(!LastMetric_.SeenTimeseries, - "mixed timeseries and ts attributes"); - LastMetric_.SeenTsOrValue = true; - State_.ToNext(TState::METRIC_TS); + PARSE_ENSURE(!LastMetric_.SeenTimeseries, + "mixed timeseries and ts attributes"); + LastMetric_.SeenTsOrValue = true; + State_.ToNext(TState::METRIC_TS); } else if (key == TStringBuf("value")) { - PARSE_ENSURE(!LastMetric_.SeenTimeseries, - "mixed timeseries and value attributes"); - LastMetric_.SeenTsOrValue = true; - State_.ToNext(TState::METRIC_VALUE); + PARSE_ENSURE(!LastMetric_.SeenTimeseries, + "mixed timeseries and value attributes"); + LastMetric_.SeenTsOrValue = true; + State_.ToNext(TState::METRIC_VALUE); } else if (key == TStringBuf("timeseries")) { - PARSE_ENSURE(!LastMetric_.SeenTsOrValue, - "mixed timeseries and ts/value attributes"); - LastMetric_.SeenTimeseries = true; - State_.ToNext(TState::METRIC_TIMESERIES); + PARSE_ENSURE(!LastMetric_.SeenTsOrValue, + "mixed timeseries and ts/value attributes"); + LastMetric_.SeenTimeseries = true; + State_.ToNext(TState::METRIC_TIMESERIES); } else if (key == TStringBuf("mode")) { - State_.ToNext(TState::METRIC_MODE); + State_.ToNext(TState::METRIC_MODE); } else if (key == TStringBuf("kind") || key == TStringBuf("type")) { - State_.ToNext(TState::METRIC_TYPE); + State_.ToNext(TState::METRIC_TYPE); } else if (key == TStringBuf("hist")) { - State_.ToNext(TState::METRIC_HIST); + State_.ToNext(TState::METRIC_HIST); } else if (key == TStringBuf("summary")) { - State_.ToNext(TState::METRIC_DSUMMARY); + State_.ToNext(TState::METRIC_DSUMMARY); } else if (key == TStringBuf("log_hist")) { - State_.ToNext(TState::METRIC_LOG_HIST); + State_.ToNext(TState::METRIC_LOG_HIST); } else if (key == TStringBuf("memOnly")) { - // deprecated. Skip it without errors for backward compatibility - } else { - ErrorMsg_ = TStringBuilder() << "unexpected key \"" << key << "\" in a metric schema"; - return false; - } - break; - - case TState::METRIC_TIMESERIES: + // deprecated. Skip it without errors for backward compatibility + } else { + ErrorMsg_ = TStringBuilder() << "unexpected key \"" << key << "\" in a metric schema"; + return false; + } + break; + + case TState::METRIC_TIMESERIES: if (key == TStringBuf("ts")) { - State_.ToNext(TState::METRIC_TS); + State_.ToNext(TState::METRIC_TS); } else if (key == TStringBuf("value")) { - State_.ToNext(TState::METRIC_VALUE); + State_.ToNext(TState::METRIC_VALUE); } else if (key == TStringBuf("hist")) { - State_.ToNext(TState::METRIC_HIST); + State_.ToNext(TState::METRIC_HIST); } else if (key == TStringBuf("summary")) { - State_.ToNext(TState::METRIC_DSUMMARY); + State_.ToNext(TState::METRIC_DSUMMARY); } else if (key == TStringBuf("log_hist")) { - State_.ToNext(TState::METRIC_LOG_HIST); + State_.ToNext(TState::METRIC_LOG_HIST); } - break; + break; - case TState::METRIC_HIST: + case TState::METRIC_HIST: if (key == TStringBuf("bounds")) { - State_.ToNext(TState::METRIC_HIST_BOUNDS); + State_.ToNext(TState::METRIC_HIST_BOUNDS); } else if (key == TStringBuf("buckets")) { - State_.ToNext(TState::METRIC_HIST_BUCKETS); + State_.ToNext(TState::METRIC_HIST_BUCKETS); } else if (key == TStringBuf("inf")) { - State_.ToNext(TState::METRIC_HIST_INF); - } - break; + State_.ToNext(TState::METRIC_HIST_INF); + } + break; - case TState::METRIC_LOG_HIST: + case TState::METRIC_LOG_HIST: if (key == TStringBuf("base")) { - State_.ToNext(TState::METRIC_LOG_HIST_BASE); + State_.ToNext(TState::METRIC_LOG_HIST_BASE); } else if (key == TStringBuf("zeros_count")) { - State_.ToNext(TState::METRIC_LOG_HIST_ZEROS); + State_.ToNext(TState::METRIC_LOG_HIST_ZEROS); } else if (key == TStringBuf("start_power")) { - State_.ToNext(TState::METRIC_LOG_HIST_START_POWER); + State_.ToNext(TState::METRIC_LOG_HIST_START_POWER); } else if (key == TStringBuf("buckets")) { - State_.ToNext(TState::METRIC_LOG_HIST_BUCKETS); - } - break; + State_.ToNext(TState::METRIC_LOG_HIST_BUCKETS); + } + break; - case TState::METRIC_DSUMMARY: + case TState::METRIC_DSUMMARY: if (key == TStringBuf("sum")) { - State_.ToNext(TState::METRIC_DSUMMARY_SUM); + State_.ToNext(TState::METRIC_DSUMMARY_SUM); } else if (key == TStringBuf("min")) { - State_.ToNext(TState::METRIC_DSUMMARY_MIN); + State_.ToNext(TState::METRIC_DSUMMARY_MIN); } else if (key == TStringBuf("max")) { - State_.ToNext(TState::METRIC_DSUMMARY_MAX); + State_.ToNext(TState::METRIC_DSUMMARY_MAX); } else if (key == TStringBuf("last")) { - State_.ToNext(TState::METRIC_DSUMMARY_LAST); + State_.ToNext(TState::METRIC_DSUMMARY_LAST); } else if (key == TStringBuf("count")) { - State_.ToNext(TState::METRIC_DSUMMARY_COUNT); - } - - break; - - - default: - return false; - } - - return true; - } - - bool OnOpenMap() override { - switch (State_.Current()) { - case TState::ROOT_OBJECT: - MetricConsumer_->OnStreamBegin(); - break; - - case TState::COMMON_LABELS: - MetricConsumer_->OnLabelsBegin(); - break; - - case TState::METRICS_ARRAY: - State_.ToNext(TState::METRIC_OBJECT); - LastMetric_.Clear(); - break; - - default: - break; - } - return true; - } - - bool OnCloseMap() override { - switch (State_.Current()) { - case TState::ROOT_OBJECT: - MetricConsumer_->OnStreamEnd(); - break; - - case TState::METRIC_LABELS: - State_.ToPrev(); - break; - - case TState::COMMON_LABELS: - MetricConsumer_->OnLabelsEnd(); - State_.ToPrev(); - - if (MetricConsumer_->NeedToStop()) { - IsIntentionallyHalted_ = true; - return false; - } - - break; - - case TState::METRIC_OBJECT: - ConsumeMetric(); - State_.ToPrev(); - break; - - case TState::METRIC_TIMESERIES: - LastMetric_.SaveLastPoint(); - break; - - case TState::METRIC_HIST: - case TState::METRIC_DSUMMARY: - case TState::METRIC_LOG_HIST: - State_.ToPrev(); - break; - - default: - break; - } - return true; - } - - bool OnOpenArray() override { - auto currentState = State_.Current(); - PARSE_ENSURE( - currentState == TState::METRICS_ARRAY || - currentState == TState::METRIC_TIMESERIES || - currentState == TState::METRIC_HIST_BOUNDS || - currentState == TState::METRIC_HIST_BUCKETS || - currentState == TState::METRIC_LOG_HIST_BUCKETS, - "unexpected array begin"); - return true; - } - - bool OnCloseArray() override { - switch (State_.Current()) { - case TState::METRICS_ARRAY: - case TState::METRIC_TIMESERIES: - case TState::METRIC_HIST_BOUNDS: - case TState::METRIC_HIST_BUCKETS: - case TState::METRIC_LOG_HIST_BUCKETS: - State_.ToPrev(); - break; - - default: - return false; - } - return true; - } - - void OnError(size_t off, TStringBuf reason) override { - if (IsIntentionallyHalted_) { - return; - } - - size_t snippetBeg = (off < 20) ? 0 : (off - 20); - TStringBuf snippet = Data_.SubStr(snippetBeg, 40); - - throw TJsonDecodeError() - << "cannot parse JSON, error at: " << off - << ", reason: " << (ErrorMsg_.empty() ? reason : TStringBuf{ErrorMsg_}) - << "\nsnippet: ..." << snippet << "..."; - } - - bool OnEnd() override { - return true; - } - - void ConsumeMetric() { - // for backwad compatibility all unknown metrics treated as gauges - if (LastMetric_.Type == EMetricType::UNKNOWN) { - if (LastMetric_.HistogramBuilder.Empty()) { - LastMetric_.Type = EMetricType::GAUGE; - } else { - LastMetric_.Type = EMetricType::HIST; + State_.ToNext(TState::METRIC_DSUMMARY_COUNT); + } + + break; + + + default: + return false; + } + + return true; + } + + bool OnOpenMap() override { + switch (State_.Current()) { + case TState::ROOT_OBJECT: + MetricConsumer_->OnStreamBegin(); + break; + + case TState::COMMON_LABELS: + MetricConsumer_->OnLabelsBegin(); + break; + + case TState::METRICS_ARRAY: + State_.ToNext(TState::METRIC_OBJECT); + LastMetric_.Clear(); + break; + + default: + break; + } + return true; + } + + bool OnCloseMap() override { + switch (State_.Current()) { + case TState::ROOT_OBJECT: + MetricConsumer_->OnStreamEnd(); + break; + + case TState::METRIC_LABELS: + State_.ToPrev(); + break; + + case TState::COMMON_LABELS: + MetricConsumer_->OnLabelsEnd(); + State_.ToPrev(); + + if (MetricConsumer_->NeedToStop()) { + IsIntentionallyHalted_ = true; + return false; + } + + break; + + case TState::METRIC_OBJECT: + ConsumeMetric(); + State_.ToPrev(); + break; + + case TState::METRIC_TIMESERIES: + LastMetric_.SaveLastPoint(); + break; + + case TState::METRIC_HIST: + case TState::METRIC_DSUMMARY: + case TState::METRIC_LOG_HIST: + State_.ToPrev(); + break; + + default: + break; + } + return true; + } + + bool OnOpenArray() override { + auto currentState = State_.Current(); + PARSE_ENSURE( + currentState == TState::METRICS_ARRAY || + currentState == TState::METRIC_TIMESERIES || + currentState == TState::METRIC_HIST_BOUNDS || + currentState == TState::METRIC_HIST_BUCKETS || + currentState == TState::METRIC_LOG_HIST_BUCKETS, + "unexpected array begin"); + return true; + } + + bool OnCloseArray() override { + switch (State_.Current()) { + case TState::METRICS_ARRAY: + case TState::METRIC_TIMESERIES: + case TState::METRIC_HIST_BOUNDS: + case TState::METRIC_HIST_BUCKETS: + case TState::METRIC_LOG_HIST_BUCKETS: + State_.ToPrev(); + break; + + default: + return false; + } + return true; + } + + void OnError(size_t off, TStringBuf reason) override { + if (IsIntentionallyHalted_) { + return; + } + + size_t snippetBeg = (off < 20) ? 0 : (off - 20); + TStringBuf snippet = Data_.SubStr(snippetBeg, 40); + + throw TJsonDecodeError() + << "cannot parse JSON, error at: " << off + << ", reason: " << (ErrorMsg_.empty() ? reason : TStringBuf{ErrorMsg_}) + << "\nsnippet: ..." << snippet << "..."; + } + + bool OnEnd() override { + return true; + } + + void ConsumeMetric() { + // for backwad compatibility all unknown metrics treated as gauges + if (LastMetric_.Type == EMetricType::UNKNOWN) { + if (LastMetric_.HistogramBuilder.Empty()) { + LastMetric_.Type = EMetricType::GAUGE; + } else { + LastMetric_.Type = EMetricType::HIST; } - } + } - // (1) begin metric - MetricConsumer_->OnMetricBegin(LastMetric_.Type); + // (1) begin metric + MetricConsumer_->OnMetricBegin(LastMetric_.Type); - // (2) labels - if (!LastMetric_.Labels.empty()) { - MetricConsumer_->OnLabelsBegin(); - for (auto&& label : LastMetric_.Labels) { - MetricConsumer_->OnLabel(label.Name(), label.Value()); + // (2) labels + if (!LastMetric_.Labels.empty()) { + MetricConsumer_->OnLabelsBegin(); + for (auto&& label : LastMetric_.Labels) { + MetricConsumer_->OnLabel(label.Name(), label.Value()); } - MetricConsumer_->OnLabelsEnd(); - } - - // (3) values - switch (LastMetric_.Type) { - case EMetricType::GAUGE: - LastMetric_.Consume([this](TInstant time, EMetricValueType valueType, TMetricValue value) { - MetricConsumer_->OnDouble(time, value.AsDouble(valueType)); - }); - break; - - case EMetricType::IGAUGE: - LastMetric_.Consume([this](TInstant time, EMetricValueType valueType, TMetricValue value) { - MetricConsumer_->OnInt64(time, value.AsInt64(valueType)); - }); - break; - - case EMetricType::COUNTER: - case EMetricType::RATE: - LastMetric_.Consume([this](TInstant time, EMetricValueType valueType, TMetricValue value) { - MetricConsumer_->OnUint64(time, value.AsUint64(valueType)); - }); - break; - - case EMetricType::HIST: - case EMetricType::HIST_RATE: - if (LastMetric_.TimeSeries.empty()) { - auto time = LastMetric_.LastPoint.GetTime(); - auto histogram = LastMetric_.HistogramBuilder.Build(); - MetricConsumer_->OnHistogram(time, histogram); - } else { - for (const auto& p : LastMetric_.TimeSeries) { - DECODE_ENSURE(p.GetValueType() == EMetricValueType::HISTOGRAM, "Value is not a histogram"); - MetricConsumer_->OnHistogram(p.GetTime(), p.GetValue().AsHistogram()); + MetricConsumer_->OnLabelsEnd(); + } + + // (3) values + switch (LastMetric_.Type) { + case EMetricType::GAUGE: + LastMetric_.Consume([this](TInstant time, EMetricValueType valueType, TMetricValue value) { + MetricConsumer_->OnDouble(time, value.AsDouble(valueType)); + }); + break; + + case EMetricType::IGAUGE: + LastMetric_.Consume([this](TInstant time, EMetricValueType valueType, TMetricValue value) { + MetricConsumer_->OnInt64(time, value.AsInt64(valueType)); + }); + break; + + case EMetricType::COUNTER: + case EMetricType::RATE: + LastMetric_.Consume([this](TInstant time, EMetricValueType valueType, TMetricValue value) { + MetricConsumer_->OnUint64(time, value.AsUint64(valueType)); + }); + break; + + case EMetricType::HIST: + case EMetricType::HIST_RATE: + if (LastMetric_.TimeSeries.empty()) { + auto time = LastMetric_.LastPoint.GetTime(); + auto histogram = LastMetric_.HistogramBuilder.Build(); + MetricConsumer_->OnHistogram(time, histogram); + } else { + for (const auto& p : LastMetric_.TimeSeries) { + DECODE_ENSURE(p.GetValueType() == EMetricValueType::HISTOGRAM, "Value is not a histogram"); + MetricConsumer_->OnHistogram(p.GetTime(), p.GetValue().AsHistogram()); } } - break; - - case EMetricType::DSUMMARY: - if (LastMetric_.TimeSeries.empty()) { - auto time = LastMetric_.LastPoint.GetTime(); - auto summary = LastMetric_.SummaryBuilder.Build(); - MetricConsumer_->OnSummaryDouble(time, summary); - } else { - for (const auto& p : LastMetric_.TimeSeries) { - DECODE_ENSURE(p.GetValueType() == EMetricValueType::SUMMARY, "Value is not a summary"); - MetricConsumer_->OnSummaryDouble(p.GetTime(), p.GetValue().AsSummaryDouble()); + break; + + case EMetricType::DSUMMARY: + if (LastMetric_.TimeSeries.empty()) { + auto time = LastMetric_.LastPoint.GetTime(); + auto summary = LastMetric_.SummaryBuilder.Build(); + MetricConsumer_->OnSummaryDouble(time, summary); + } else { + for (const auto& p : LastMetric_.TimeSeries) { + DECODE_ENSURE(p.GetValueType() == EMetricValueType::SUMMARY, "Value is not a summary"); + MetricConsumer_->OnSummaryDouble(p.GetTime(), p.GetValue().AsSummaryDouble()); + } + } + break; + + case EMetricType::LOGHIST: + if (LastMetric_.TimeSeries.empty()) { + auto time = LastMetric_.LastPoint.GetTime(); + auto logHist = LastMetric_.LogHistBuilder.Build(); + MetricConsumer_->OnLogHistogram(time, logHist); + } else { + for (const auto& p : LastMetric_.TimeSeries) { + DECODE_ENSURE(p.GetValueType() == EMetricValueType::LOGHISTOGRAM, "Value is not a log_histogram"); + MetricConsumer_->OnLogHistogram(p.GetTime(), p.GetValue().AsLogHistogram()); } } - break; + break; - case EMetricType::LOGHIST: - if (LastMetric_.TimeSeries.empty()) { - auto time = LastMetric_.LastPoint.GetTime(); - auto logHist = LastMetric_.LogHistBuilder.Build(); - MetricConsumer_->OnLogHistogram(time, logHist); - } else { - for (const auto& p : LastMetric_.TimeSeries) { - DECODE_ENSURE(p.GetValueType() == EMetricValueType::LOGHISTOGRAM, "Value is not a log_histogram"); - MetricConsumer_->OnLogHistogram(p.GetTime(), p.GetValue().AsLogHistogram()); - } - } - break; + case EMetricType::UNKNOWN: + // TODO: output metric labels + ythrow yexception() << "unknown metric type"; + } - case EMetricType::UNKNOWN: - // TODO: output metric labels - ythrow yexception() << "unknown metric type"; - } + // (4) end metric + MetricConsumer_->OnMetricEnd(); + } - // (4) end metric - MetricConsumer_->OnMetricEnd(); - } - -private: - TStringBuf Data_; - IHaltableMetricConsumer* MetricConsumer_; +private: + TStringBuf Data_; + IHaltableMetricConsumer* MetricConsumer_; TString MetricNameLabel_; - TState State_; - TString LastLabelName_; - TMetricCollector LastMetric_; - TString ErrorMsg_; - bool IsIntentionallyHalted_{false}; -}; + TState State_; + TString LastLabelName_; + TMetricCollector LastMetric_; + TString ErrorMsg_; + bool IsIntentionallyHalted_{false}; +}; -} // namespace +} // namespace void DecodeJson(TStringBuf data, IMetricConsumer* c, TStringBuf metricNameLabel) { - TCommonPartsCollector commonPartsCollector; - { - TMemoryInput memIn(data); + TCommonPartsCollector commonPartsCollector; + { + TMemoryInput memIn(data); TDecoderJson decoder(data, &commonPartsCollector, metricNameLabel); - // no need to check a return value. If there is an error, a TJsonDecodeError is thrown - NJson::ReadJson(&memIn, &decoder); - } + // no need to check a return value. If there is an error, a TJsonDecodeError is thrown + NJson::ReadJson(&memIn, &decoder); + } - TCommonPartsProxy commonPartsProxy(std::move(commonPartsCollector.CommonParts()), c); - { - TMemoryInput memIn(data); + TCommonPartsProxy commonPartsProxy(std::move(commonPartsCollector.CommonParts()), c); + { + TMemoryInput memIn(data); TDecoderJson decoder(data, &commonPartsProxy, metricNameLabel); - // no need to check a return value. If there is an error, a TJsonDecodeError is thrown - NJson::ReadJson(&memIn, &decoder); - } -} - -#undef DECODE_ENSURE + // no need to check a return value. If there is an error, a TJsonDecodeError is thrown + NJson::ReadJson(&memIn, &decoder); + } +} +#undef DECODE_ENSURE + } diff --git a/library/cpp/monlib/encode/json/json_decoder_ut.cpp b/library/cpp/monlib/encode/json/json_decoder_ut.cpp index 4464e1d26a..1f9513664a 100644 --- a/library/cpp/monlib/encode/json/json_decoder_ut.cpp +++ b/library/cpp/monlib/encode/json/json_decoder_ut.cpp @@ -1,125 +1,125 @@ -#include "json_decoder.cpp" - -#include <library/cpp/monlib/consumers/collecting_consumer.h> -#include <library/cpp/testing/unittest/registar.h> - -#include <array> - - -using namespace NMonitoring; - -enum EJsonPart : ui8 { - METRICS = 0, - COMMON_TS = 1, - COMMON_LABELS = 2, -}; - -constexpr std::array<TStringBuf, 3> JSON_PARTS = { +#include "json_decoder.cpp" + +#include <library/cpp/monlib/consumers/collecting_consumer.h> +#include <library/cpp/testing/unittest/registar.h> + +#include <array> + + +using namespace NMonitoring; + +enum EJsonPart : ui8 { + METRICS = 0, + COMMON_TS = 1, + COMMON_LABELS = 2, +}; + +constexpr std::array<TStringBuf, 3> JSON_PARTS = { TStringBuf(R"("metrics": [{ - "labels": { "key": "value" }, - "type": "GAUGE", - "value": 123 - }])"), - + "labels": { "key": "value" }, + "type": "GAUGE", + "value": 123 + }])"), + TStringBuf(R"("ts": 1)"), - + TStringBuf(R"("commonLabels": { - "key1": "value1", - "key2": "value2" - })"), -}; - -TString BuildJson(std::initializer_list<EJsonPart> parts) { - TString data = "{"; - - for (auto it = parts.begin(); it != parts.end(); ++it) { - data += JSON_PARTS[*it]; - - if (it + 1 != parts.end()) { - data += ","; - } - } - - data += "}"; - return data; -} - -void ValidateCommonParts(TCommonParts&& commonParts, bool checkLabels, bool checkTs) { - if (checkTs) { - UNIT_ASSERT_VALUES_EQUAL(commonParts.CommonTime.MilliSeconds(), 1000); - } - - if (checkLabels) { - auto& labels = commonParts.CommonLabels; - UNIT_ASSERT_VALUES_EQUAL(labels.Size(), 2); + "key1": "value1", + "key2": "value2" + })"), +}; + +TString BuildJson(std::initializer_list<EJsonPart> parts) { + TString data = "{"; + + for (auto it = parts.begin(); it != parts.end(); ++it) { + data += JSON_PARTS[*it]; + + if (it + 1 != parts.end()) { + data += ","; + } + } + + data += "}"; + return data; +} + +void ValidateCommonParts(TCommonParts&& commonParts, bool checkLabels, bool checkTs) { + if (checkTs) { + UNIT_ASSERT_VALUES_EQUAL(commonParts.CommonTime.MilliSeconds(), 1000); + } + + if (checkLabels) { + auto& labels = commonParts.CommonLabels; + UNIT_ASSERT_VALUES_EQUAL(labels.Size(), 2); UNIT_ASSERT(labels.Has(TStringBuf("key1"))); UNIT_ASSERT(labels.Has(TStringBuf("key2"))); UNIT_ASSERT_VALUES_EQUAL(labels.Get(TStringBuf("key1")).value()->Value(), "value1"); UNIT_ASSERT_VALUES_EQUAL(labels.Get(TStringBuf("key2")).value()->Value(), "value2"); - } -} - -void ValidateMetrics(const TVector<TMetricData>& metrics) { - UNIT_ASSERT_VALUES_EQUAL(metrics.size(), 1); - - auto& m = metrics[0]; - UNIT_ASSERT_VALUES_EQUAL(m.Kind, EMetricType::GAUGE); - auto& l = m.Labels; - UNIT_ASSERT_VALUES_EQUAL(l.Size(), 1); - UNIT_ASSERT_VALUES_EQUAL(l.Get(0)->Name(), "key"); - UNIT_ASSERT_VALUES_EQUAL(l.Get(0)->Value(), "value"); - - UNIT_ASSERT_VALUES_EQUAL(m.Values->Size(), 1); - UNIT_ASSERT_VALUES_EQUAL((*m.Values)[0].GetValue().AsDouble(), 123); -} - + } +} + +void ValidateMetrics(const TVector<TMetricData>& metrics) { + UNIT_ASSERT_VALUES_EQUAL(metrics.size(), 1); + + auto& m = metrics[0]; + UNIT_ASSERT_VALUES_EQUAL(m.Kind, EMetricType::GAUGE); + auto& l = m.Labels; + UNIT_ASSERT_VALUES_EQUAL(l.Size(), 1); + UNIT_ASSERT_VALUES_EQUAL(l.Get(0)->Name(), "key"); + UNIT_ASSERT_VALUES_EQUAL(l.Get(0)->Value(), "value"); + + UNIT_ASSERT_VALUES_EQUAL(m.Values->Size(), 1); + UNIT_ASSERT_VALUES_EQUAL((*m.Values)[0].GetValue().AsDouble(), 123); +} + void CheckCommonPartsCollector(TString data, bool shouldBeStopped, bool checkLabels = true, bool checkTs = true, TStringBuf metricNameLabel = "name") { - TCommonPartsCollector commonPartsCollector; - TMemoryInput memIn(data); + TCommonPartsCollector commonPartsCollector; + TMemoryInput memIn(data); TDecoderJson decoder(data, &commonPartsCollector, metricNameLabel); - - bool isOk{false}; - UNIT_ASSERT_NO_EXCEPTION(isOk = NJson::ReadJson(&memIn, &decoder)); - UNIT_ASSERT_VALUES_EQUAL(isOk, !shouldBeStopped); - - ValidateCommonParts(commonPartsCollector.CommonParts(), checkLabels, checkTs); -} - -Y_UNIT_TEST_SUITE(TJsonDecoderTest) { - Y_UNIT_TEST(FullCommonParts) { - CheckCommonPartsCollector(BuildJson({COMMON_LABELS, COMMON_TS, METRICS}), true); - CheckCommonPartsCollector(BuildJson({COMMON_TS, COMMON_LABELS, METRICS}), true); - - CheckCommonPartsCollector(BuildJson({METRICS, COMMON_TS, COMMON_LABELS}), true); - CheckCommonPartsCollector(BuildJson({METRICS, COMMON_LABELS, COMMON_TS}), true); - - CheckCommonPartsCollector(BuildJson({COMMON_LABELS, METRICS, COMMON_TS}), true); - CheckCommonPartsCollector(BuildJson({COMMON_TS, METRICS, COMMON_LABELS}), true); - } - - Y_UNIT_TEST(PartialCommonParts) { - CheckCommonPartsCollector(BuildJson({COMMON_TS, METRICS}), false, false, true); - CheckCommonPartsCollector(BuildJson({COMMON_LABELS, METRICS}), false, true, false); - - CheckCommonPartsCollector(BuildJson({METRICS, COMMON_LABELS}), false, true, false); - CheckCommonPartsCollector(BuildJson({METRICS, COMMON_TS}), false, false, true); - - CheckCommonPartsCollector(BuildJson({METRICS}), false, false, false); - } - - Y_UNIT_TEST(CheckCommonPartsAndMetrics) { - auto data = BuildJson({COMMON_LABELS, COMMON_TS, METRICS}); - TCollectingConsumer collector; - - DecodeJson(data, &collector); - - TCommonParts commonParts; - commonParts.CommonTime = collector.CommonTime; - commonParts.CommonLabels = collector.CommonLabels; - - ValidateCommonParts(std::move(commonParts), true, true); - ValidateMetrics(collector.Metrics); - } + + bool isOk{false}; + UNIT_ASSERT_NO_EXCEPTION(isOk = NJson::ReadJson(&memIn, &decoder)); + UNIT_ASSERT_VALUES_EQUAL(isOk, !shouldBeStopped); + + ValidateCommonParts(commonPartsCollector.CommonParts(), checkLabels, checkTs); +} + +Y_UNIT_TEST_SUITE(TJsonDecoderTest) { + Y_UNIT_TEST(FullCommonParts) { + CheckCommonPartsCollector(BuildJson({COMMON_LABELS, COMMON_TS, METRICS}), true); + CheckCommonPartsCollector(BuildJson({COMMON_TS, COMMON_LABELS, METRICS}), true); + + CheckCommonPartsCollector(BuildJson({METRICS, COMMON_TS, COMMON_LABELS}), true); + CheckCommonPartsCollector(BuildJson({METRICS, COMMON_LABELS, COMMON_TS}), true); + + CheckCommonPartsCollector(BuildJson({COMMON_LABELS, METRICS, COMMON_TS}), true); + CheckCommonPartsCollector(BuildJson({COMMON_TS, METRICS, COMMON_LABELS}), true); + } + + Y_UNIT_TEST(PartialCommonParts) { + CheckCommonPartsCollector(BuildJson({COMMON_TS, METRICS}), false, false, true); + CheckCommonPartsCollector(BuildJson({COMMON_LABELS, METRICS}), false, true, false); + + CheckCommonPartsCollector(BuildJson({METRICS, COMMON_LABELS}), false, true, false); + CheckCommonPartsCollector(BuildJson({METRICS, COMMON_TS}), false, false, true); + + CheckCommonPartsCollector(BuildJson({METRICS}), false, false, false); + } + + Y_UNIT_TEST(CheckCommonPartsAndMetrics) { + auto data = BuildJson({COMMON_LABELS, COMMON_TS, METRICS}); + TCollectingConsumer collector; + + DecodeJson(data, &collector); + + TCommonParts commonParts; + commonParts.CommonTime = collector.CommonTime; + commonParts.CommonLabels = collector.CommonLabels; + + ValidateCommonParts(std::move(commonParts), true, true); + ValidateMetrics(collector.Metrics); + } Y_UNIT_TEST(CanParseHistogramsWithInf) { const char* metricsData = R"({ @@ -176,4 +176,4 @@ Y_UNIT_TEST_SUITE(TJsonDecoderTest) { 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 20d2bb6283..db39a2a910 100644 --- a/library/cpp/monlib/encode/json/json_encoder.cpp +++ b/library/cpp/monlib/encode/json/json_encoder.cpp @@ -9,7 +9,7 @@ #include <library/cpp/json/writer/json.h> -#include <util/charset/utf8.h> +#include <util/charset/utf8.h> #include <util/generic/algorithm.h> namespace NMonitoring { @@ -245,12 +245,12 @@ namespace NMonitoring { private: void OnStreamBegin() override { - State_.Expect(TEncoderState::EState::ROOT); + State_.Expect(TEncoderState::EState::ROOT); Buf_.BeginObject(); } void OnStreamEnd() override { - State_.Expect(TEncoderState::EState::ROOT); + State_.Expect(TEncoderState::EState::ROOT); if (!Buf_.KeyExpected()) { // not closed metrics array Buf_.EndList(); @@ -259,7 +259,7 @@ namespace NMonitoring { } void OnCommonTime(TInstant time) override { - State_.Expect(TEncoderState::EState::ROOT); + State_.Expect(TEncoderState::EState::ROOT); WriteTime(time); } @@ -298,8 +298,8 @@ namespace NMonitoring { // not closed metrics or timeseries array if labels go after values Buf_.EndList(); } - if (State_ == TEncoderState::EState::ROOT) { - State_ = TEncoderState::EState::COMMON_LABELS; + if (State_ == TEncoderState::EState::ROOT) { + State_ = TEncoderState::EState::COMMON_LABELS; Buf_.WriteKey(TStringBuf(Style_ == EJsonStyle::Solomon ? "commonLabels" : "labels")); } else if (State_ == TEncoderState::EState::METRIC) { State_ = TEncoderState::EState::METRIC_LABELS; @@ -315,8 +315,8 @@ namespace NMonitoring { void OnLabelsEnd() override { if (State_ == TEncoderState::EState::METRIC_LABELS) { State_ = TEncoderState::EState::METRIC; - } else if (State_ == TEncoderState::EState::COMMON_LABELS) { - State_ = TEncoderState::EState::ROOT; + } else if (State_ == TEncoderState::EState::COMMON_LABELS) { + State_ = TEncoderState::EState::ROOT; } else { State_.ThrowInvalid("expected LABELS or COMMON_LABELS"); } @@ -430,16 +430,16 @@ namespace NMonitoring { Close(); } - void OnLabelsBegin() override { - TBufferedEncoderBase::OnLabelsBegin(); - EmptyLabels_ = true; - } - + void OnLabelsBegin() override { + TBufferedEncoderBase::OnLabelsBegin(); + EmptyLabels_ = true; + } + void OnLabel(TStringBuf name, TStringBuf value) override { - TBufferedEncoderBase::OnLabel(name, value); - EmptyLabels_ = false; - } - + TBufferedEncoderBase::OnLabel(name, value); + EmptyLabels_ = false; + } + void OnLabel(ui32 name, ui32 value) override { TBufferedEncoderBase::OnLabel(name, value); EmptyLabels_ = false; @@ -447,10 +447,10 @@ namespace NMonitoring { void OnLabelsEnd() override { TBufferedEncoderBase::OnLabelsEnd(); - Y_ENSURE(!EmptyLabels_, "Labels cannot be empty"); + Y_ENSURE(!EmptyLabels_, "Labels cannot be empty"); } - void Close() final { + void Close() final { if (Closed_) { return; } @@ -534,7 +534,7 @@ namespace NMonitoring { private: bool Closed_{false}; - bool EmptyLabels_ = false; + bool EmptyLabels_ = false; }; } diff --git a/library/cpp/monlib/encode/json/json_ut.cpp b/library/cpp/monlib/encode/json/json_ut.cpp index 09e7909289..5de8470fcf 100644 --- a/library/cpp/monlib/encode/json/json_ut.cpp +++ b/library/cpp/monlib/encode/json/json_ut.cpp @@ -559,21 +559,21 @@ Y_UNIT_TEST_SUITE(TJsonTest) { } } - Y_UNIT_TEST(DecodeToEncoder) { - auto testJson = NResource::Find("/test_decode_to_encode.json"); - - TStringStream Stream_; - auto encoder = BufferedEncoderJson(&Stream_, 4); - DecodeJson(testJson, encoder.Get()); - - encoder->Close(); - - auto val1 = NJson::ReadJsonFastTree(testJson, true); - auto val2 = NJson::ReadJsonFastTree(Stream_.Str(), true); - - UNIT_ASSERT_VALUES_EQUAL(val1, val2); - } - + Y_UNIT_TEST(DecodeToEncoder) { + auto testJson = NResource::Find("/test_decode_to_encode.json"); + + TStringStream Stream_; + auto encoder = BufferedEncoderJson(&Stream_, 4); + DecodeJson(testJson, encoder.Get()); + + encoder->Close(); + + auto val1 = NJson::ReadJsonFastTree(testJson, true); + auto val2 = NJson::ReadJsonFastTree(Stream_.Str(), true); + + UNIT_ASSERT_VALUES_EQUAL(val1, val2); + } + void WriteEmptySeries(const IMetricEncoderPtr& e) { e->OnStreamBegin(); { @@ -1033,12 +1033,12 @@ Y_UNIT_TEST_SUITE(TJsonTest) { AssertPointEqual(s.GetPoints(1), now + TDuration::Seconds(15), *logHist); } - void HistogramValueDecode(const TString& filePath) { + void HistogramValueDecode(const TString& filePath) { NProto::TMultiSamplesList samples; { IMetricEncoderPtr e = EncoderProtobuf(&samples); - TString testJson = NResource::Find(filePath); + TString testJson = NResource::Find(filePath); DecodeJson(testJson, e.Get()); } @@ -1059,11 +1059,11 @@ Y_UNIT_TEST_SUITE(TJsonTest) { AssertPointEqual(s.GetPoints(0), now, *h->Snapshot()); } - Y_UNIT_TEST(HistogramValueDecode) { - HistogramValueDecode("/histogram_value.json"); - HistogramValueDecode("/histogram_value_inf_before_bounds.json"); - } - + Y_UNIT_TEST(HistogramValueDecode) { + HistogramValueDecode("/histogram_value.json"); + HistogramValueDecode("/histogram_value_inf_before_bounds.json"); + } + Y_UNIT_TEST(HistogramTimeSeriesEncode) { auto writeDocument = [](IMetricEncoder* e) { e->OnStreamBegin(); diff --git a/library/cpp/monlib/encode/json/typed_point.h b/library/cpp/monlib/encode/json/typed_point.h index fbaa840c4b..8da50217f0 100644 --- a/library/cpp/monlib/encode/json/typed_point.h +++ b/library/cpp/monlib/encode/json/typed_point.h @@ -45,7 +45,7 @@ namespace NMonitoring { return *this; } - TTypedPoint(TTypedPoint&& rhs) noexcept + TTypedPoint(TTypedPoint&& rhs) noexcept : Time_(rhs.Time_) , ValueType_(rhs.ValueType_) , Value_(rhs.Value_) @@ -54,7 +54,7 @@ namespace NMonitoring { rhs.Value_ = {}; } - TTypedPoint& operator=(TTypedPoint&& rhs) noexcept { + TTypedPoint& operator=(TTypedPoint&& rhs) noexcept { UnRef(); Time_ = rhs.Time_; diff --git a/library/cpp/monlib/encode/json/ut/buffered_ts_merge.json b/library/cpp/monlib/encode/json/ut/buffered_ts_merge.json index 1d27efacb0..cdcca789fe 100644 --- a/library/cpp/monlib/encode/json/ut/buffered_ts_merge.json +++ b/library/cpp/monlib/encode/json/ut/buffered_ts_merge.json @@ -7,7 +7,7 @@ { "foo":"bar" }, - "value":45 + "value":45 } ] } diff --git a/library/cpp/monlib/encode/json/ut/histogram_value_inf_before_bounds.json b/library/cpp/monlib/encode/json/ut/histogram_value_inf_before_bounds.json index f8a17c8831..7fb99fe768 100644 --- a/library/cpp/monlib/encode/json/ut/histogram_value_inf_before_bounds.json +++ b/library/cpp/monlib/encode/json/ut/histogram_value_inf_before_bounds.json @@ -1,33 +1,33 @@ -{ - "sensors": - [ - { - "kind":"HIST", - "labels": - { +{ + "sensors": + [ + { + "kind":"HIST", + "labels": + { "metric":"responseTimeMillis" - }, - "ts":1509843723, - "hist": - { - "inf":83, - "bounds": - [ - 1, - 2, - 4, - 8, - 16 - ], - "buckets": - [ - 1, - 1, - 2, - 4, - 8 - ] - } - } - ] -} + }, + "ts":1509843723, + "hist": + { + "inf":83, + "bounds": + [ + 1, + 2, + 4, + 8, + 16 + ], + "buckets": + [ + 1, + 1, + 2, + 4, + 8 + ] + } + } + ] +} diff --git a/library/cpp/monlib/encode/json/ut/test_decode_to_encode.json b/library/cpp/monlib/encode/json/ut/test_decode_to_encode.json index 65f0c5c6e2..d127981c97 100644 --- a/library/cpp/monlib/encode/json/ut/test_decode_to_encode.json +++ b/library/cpp/monlib/encode/json/ut/test_decode_to_encode.json @@ -1,16 +1,16 @@ -{ - "commonLabels": { - "project": "solomon", - "cluster": "man", - "service": "stockpile" - }, - "sensors": [ - { - "kind": "GAUGE", +{ + "commonLabels": { + "project": "solomon", + "cluster": "man", + "service": "stockpile" + }, + "sensors": [ + { + "kind": "GAUGE", "labels": { "export": "Oxygen", "metric": "QueueSize" }, - "ts": 1509885296, - "value": 3.14159 - } - ], - "ts": 1503837296 -} + "ts": 1509885296, + "value": 3.14159 + } + ], + "ts": 1503837296 +} diff --git a/library/cpp/monlib/encode/json/ut/ya.make b/library/cpp/monlib/encode/json/ut/ya.make index e50c4f4903..bfa12d159e 100644 --- a/library/cpp/monlib/encode/json/ut/ya.make +++ b/library/cpp/monlib/encode/json/ut/ya.make @@ -6,7 +6,7 @@ OWNER( ) SRCS( - json_decoder_ut.cpp + json_decoder_ut.cpp json_ut.cpp ) @@ -21,12 +21,12 @@ RESOURCE( merged.json /merged.json histogram_timeseries.json /histogram_timeseries.json histogram_value.json /histogram_value.json - histogram_value_inf_before_bounds.json /histogram_value_inf_before_bounds.json + histogram_value_inf_before_bounds.json /histogram_value_inf_before_bounds.json int_gauge.json /int_gauge.json sensors.json /sensors.json metrics.json /metrics.json named_metrics.json /named_metrics.json - test_decode_to_encode.json /test_decode_to_encode.json + test_decode_to_encode.json /test_decode_to_encode.json crash.json /crash.json hist_crash.json /hist_crash.json summary_value.json /summary_value.json @@ -38,7 +38,7 @@ RESOURCE( PEERDIR( library/cpp/json - library/cpp/monlib/consumers + library/cpp/monlib/consumers library/cpp/monlib/encode/protobuf library/cpp/resource ) diff --git a/library/cpp/monlib/encode/json/ya.make b/library/cpp/monlib/encode/json/ya.make index a50fc412a9..9229ff1292 100644 --- a/library/cpp/monlib/encode/json/ya.make +++ b/library/cpp/monlib/encode/json/ya.make @@ -11,9 +11,9 @@ SRCS( ) PEERDIR( - library/cpp/monlib/encode - library/cpp/monlib/encode/buffered - library/cpp/monlib/exception + library/cpp/monlib/encode + library/cpp/monlib/encode/buffered + library/cpp/monlib/exception library/cpp/json library/cpp/json/writer ) diff --git a/library/cpp/monlib/encode/legacy_protobuf/legacy_proto_decoder.cpp b/library/cpp/monlib/encode/legacy_protobuf/legacy_proto_decoder.cpp index f87a2d7e8f..5f7352e8a8 100644 --- a/library/cpp/monlib/encode/legacy_protobuf/legacy_proto_decoder.cpp +++ b/library/cpp/monlib/encode/legacy_protobuf/legacy_proto_decoder.cpp @@ -224,8 +224,8 @@ namespace NMonitoring { if (metricMeta.HasCustomPath()) { if (const auto& nodePath = metricMeta.GetCustomPath()) { - child.AppendPath(nodePath); - } + child.AppendPath(nodePath); + } } else if (metricMeta.GetPath()) { child.AppendPath(name); } diff --git a/library/cpp/monlib/encode/legacy_protobuf/protos/metric_meta.proto b/library/cpp/monlib/encode/legacy_protobuf/protos/metric_meta.proto index fd23eb372b..684ff98364 100644 --- a/library/cpp/monlib/encode/legacy_protobuf/protos/metric_meta.proto +++ b/library/cpp/monlib/encode/legacy_protobuf/protos/metric_meta.proto @@ -22,7 +22,7 @@ message TMetricMeta { optional string Keys = 3; optional bool MemOnly = 4; optional bool IgnorePath = 5; - optional string CustomPath = 6; + optional string CustomPath = 6; } enum THistogramBase { diff --git a/library/cpp/monlib/encode/prometheus/prometheus_decoder.cpp b/library/cpp/monlib/encode/prometheus/prometheus_decoder.cpp index 7e81357dbd..cbbecbb044 100644 --- a/library/cpp/monlib/encode/prometheus/prometheus_decoder.cpp +++ b/library/cpp/monlib/encode/prometheus/prometheus_decoder.cpp @@ -135,12 +135,12 @@ namespace NMonitoring { Time_ = TInstant::Zero(); PrevBucket_ = ZERO_BUCKET; Labels_.Clear(); - auto snapshot = ExplicitHistogramSnapshot(Bounds_, Values_); - - Bounds_.clear(); - Values_.clear(); - - return snapshot; + auto snapshot = ExplicitHistogramSnapshot(Bounds_, Values_); + + Bounds_.clear(); + Values_.clear(); + + return snapshot; } private: diff --git a/library/cpp/monlib/encode/prometheus/prometheus_encoder.cpp b/library/cpp/monlib/encode/prometheus/prometheus_encoder.cpp index 15efeb8c03..b91a25f856 100644 --- a/library/cpp/monlib/encode/prometheus/prometheus_encoder.cpp +++ b/library/cpp/monlib/encode/prometheus/prometheus_encoder.cpp @@ -257,16 +257,16 @@ namespace NMonitoring { private: void OnStreamBegin() override { - State_.Expect(TEncoderState::EState::ROOT); + State_.Expect(TEncoderState::EState::ROOT); } void OnStreamEnd() override { - State_.Expect(TEncoderState::EState::ROOT); + State_.Expect(TEncoderState::EState::ROOT); Writer_.WriteLn(); } void OnCommonTime(TInstant time) override { - State_.Expect(TEncoderState::EState::ROOT); + State_.Expect(TEncoderState::EState::ROOT); CommonTime_ = time; } @@ -284,8 +284,8 @@ namespace NMonitoring { void OnLabelsBegin() override { if (State_ == TEncoderState::EState::METRIC) { State_ = TEncoderState::EState::METRIC_LABELS; - } else if (State_ == TEncoderState::EState::ROOT) { - State_ = TEncoderState::EState::COMMON_LABELS; + } else if (State_ == TEncoderState::EState::ROOT) { + State_ = TEncoderState::EState::COMMON_LABELS; } else { State_.ThrowInvalid("expected METRIC or ROOT"); } @@ -294,8 +294,8 @@ namespace NMonitoring { void OnLabelsEnd() override { if (State_ == TEncoderState::EState::METRIC_LABELS) { State_ = TEncoderState::EState::METRIC; - } else if (State_ == TEncoderState::EState::COMMON_LABELS) { - State_ = TEncoderState::EState::ROOT; + } else if (State_ == TEncoderState::EState::COMMON_LABELS) { + State_ = TEncoderState::EState::ROOT; } else { State_.ThrowInvalid("expected LABELS or COMMON_LABELS"); } @@ -304,7 +304,7 @@ namespace NMonitoring { void OnLabel(TStringBuf name, TStringBuf value) override { if (State_ == TEncoderState::EState::METRIC_LABELS) { MetricState_.Labels.Add(name, value); - } else if (State_ == TEncoderState::EState::COMMON_LABELS) { + } else if (State_ == TEncoderState::EState::COMMON_LABELS) { CommonLabels_.Add(name, value); } else { State_.ThrowInvalid("expected LABELS or COMMON_LABELS"); diff --git a/library/cpp/monlib/encode/spack/compression.cpp b/library/cpp/monlib/encode/spack/compression.cpp index 0d2152fc85..ff8c043bc4 100644 --- a/library/cpp/monlib/encode/spack/compression.cpp +++ b/library/cpp/monlib/encode/spack/compression.cpp @@ -241,7 +241,7 @@ namespace NMonitoring { // TFramedOutputStream /////////////////////////////////////////////////////////////////////////////// template <typename TCodecAlg, typename TCheckSumAlg> - class TFramedCompressStream final: public IFramedCompressStream { + class TFramedCompressStream final: public IFramedCompressStream { public: explicit TFramedCompressStream(IOutputStream* out) : Out_(out) @@ -277,13 +277,13 @@ namespace NMonitoring { } } - void FlushWithoutEmptyFrame() override { + void FlushWithoutEmptyFrame() override { if (Out_ && !Uncompressed_.Empty()) { WriteCompressedFrame(); } } - void FinishAndWriteEmptyFrame() override { + void FinishAndWriteEmptyFrame() override { if (Out_) { Y_DEFER { Out_ = nullptr; @@ -297,14 +297,14 @@ namespace NMonitoring { } } - void DoFlush() override { - FlushWithoutEmptyFrame(); - } - - void DoFinish() override { - FinishAndWriteEmptyFrame(); - } - + void DoFlush() override { + FlushWithoutEmptyFrame(); + } + + void DoFinish() override { + FinishAndWriteEmptyFrame(); + } + void WriteCompressedFrame() { static const auto framePayload = sizeof(TFrameHeader) + sizeof(TFrameFooter); const auto maxFrameSize = ui64(TCodecAlg::MaxCompressedLength(Uncompressed_.Size())) + framePayload; @@ -364,7 +364,7 @@ namespace NMonitoring { Y_FAIL("invalid compression algorithm"); } - THolder<IFramedCompressStream> CompressedOutput(IOutputStream* out, ECompression alg) { + THolder<IFramedCompressStream> CompressedOutput(IOutputStream* out, ECompression alg) { switch (alg) { case ECompression::IDENTITY: return nullptr; diff --git a/library/cpp/monlib/encode/spack/compression.h b/library/cpp/monlib/encode/spack/compression.h index f74d8b424e..dc14cb561a 100644 --- a/library/cpp/monlib/encode/spack/compression.h +++ b/library/cpp/monlib/encode/spack/compression.h @@ -7,13 +7,13 @@ namespace NMonitoring { -class IFramedCompressStream: public IOutputStream { -public: - virtual void FlushWithoutEmptyFrame() = 0; - virtual void FinishAndWriteEmptyFrame() = 0; -}; - -THolder<IInputStream> CompressedInput(IInputStream* in, ECompression alg); -THolder<IFramedCompressStream> CompressedOutput(IOutputStream* out, ECompression alg); - -} // namespace NMonitoring +class IFramedCompressStream: public IOutputStream { +public: + virtual void FlushWithoutEmptyFrame() = 0; + virtual void FinishAndWriteEmptyFrame() = 0; +}; + +THolder<IInputStream> CompressedInput(IInputStream* in, ECompression alg); +THolder<IFramedCompressStream> CompressedOutput(IOutputStream* out, ECompression alg); + +} // namespace NMonitoring diff --git a/library/cpp/monlib/encode/spack/spack_v1.h b/library/cpp/monlib/encode/spack/spack_v1.h index cf1c9417b9..ddc305fc0d 100644 --- a/library/cpp/monlib/encode/spack/spack_v1.h +++ b/library/cpp/monlib/encode/spack/spack_v1.h @@ -15,9 +15,9 @@ class IInputStream; class IOutputStream; namespace NMonitoring { - class TSpackDecodeError: public yexception { - }; - + class TSpackDecodeError: public yexception { + }; + constexpr auto EncodeMetricType(EMetricType mt) noexcept { return static_cast<std::underlying_type_t<EMetricType>>(mt); } @@ -98,9 +98,9 @@ namespace NMonitoring { IMetricEncoderPtr EncoderSpackV1( IOutputStream* out, ETimePrecision timePrecision, - ECompression compression, + ECompression compression, EMetricsMergingMode mergingMode = EMetricsMergingMode::DEFAULT - ); + ); IMetricEncoderPtr EncoderSpackV12( IOutputStream* out, diff --git a/library/cpp/monlib/encode/spack/spack_v1_decoder.cpp b/library/cpp/monlib/encode/spack/spack_v1_decoder.cpp index 1f445fc80d..e1d0629694 100644 --- a/library/cpp/monlib/encode/spack/spack_v1_decoder.cpp +++ b/library/cpp/monlib/encode/spack/spack_v1_decoder.cpp @@ -3,9 +3,9 @@ #include "compression.h" #include <library/cpp/monlib/encode/buffered/string_pool.h> -#include <library/cpp/monlib/exception/exception.h> +#include <library/cpp/monlib/exception/exception.h> #include <library/cpp/monlib/metrics/histogram_collector.h> -#include <library/cpp/monlib/metrics/metric.h> +#include <library/cpp/monlib/metrics/metric.h> #include <util/generic/yexception.h> #include <util/generic/buffer.h> @@ -18,8 +18,8 @@ namespace NMonitoring { namespace { -#define DECODE_ENSURE(COND, ...) MONLIB_ENSURE_EX(COND, TSpackDecodeError() << __VA_ARGS__) - +#define DECODE_ENSURE(COND, ...) MONLIB_ENSURE_EX(COND, TSpackDecodeError() << __VA_ARGS__) + constexpr ui64 LABEL_SIZE_LIMIT = 128_MB; /////////////////////////////////////////////////////////////////////// @@ -37,15 +37,15 @@ namespace NMonitoring { c->OnStreamBegin(); // (1) read header - size_t readBytes = In_->Read(&Header_, sizeof(Header_)); - DECODE_ENSURE(readBytes == sizeof(Header_), "not enough data in input stream to read header"); + size_t readBytes = In_->Read(&Header_, sizeof(Header_)); + DECODE_ENSURE(readBytes == sizeof(Header_), "not enough data in input stream to read header"); - ui8 version = ((Header_.Version >> 8) & 0xff); - DECODE_ENSURE(version == 1, "versions mismatch (expected: 1, got: " << +version << ')'); + ui8 version = ((Header_.Version >> 8) & 0xff); + DECODE_ENSURE(version == 1, "versions mismatch (expected: 1, got: " << +version << ')'); - DECODE_ENSURE(Header_.HeaderSize >= sizeof(Header_), "invalid header size"); - if (size_t skipBytes = Header_.HeaderSize - sizeof(Header_)) { - DECODE_ENSURE(In_->Skip(skipBytes) == skipBytes, "input stream unexpectedly ended"); + DECODE_ENSURE(Header_.HeaderSize >= sizeof(Header_), "invalid header size"); + if (size_t skipBytes = Header_.HeaderSize - sizeof(Header_)) { + DECODE_ENSURE(In_->Skip(skipBytes) == skipBytes, "input stream unexpectedly ended"); } if (Header_.MetricCount == 0) { @@ -55,27 +55,27 @@ namespace NMonitoring { } // if compression enabled all below reads must go throught decompressor - auto compressedIn = CompressedInput(In_, DecodeCompression(Header_.Compression)); + auto compressedIn = CompressedInput(In_, DecodeCompression(Header_.Compression)); if (compressedIn) { In_ = compressedIn.Get(); } - TimePrecision_ = DecodeTimePrecision(Header_.TimePrecision); + TimePrecision_ = DecodeTimePrecision(Header_.TimePrecision); - const ui64 labelSizeTotal = ui64(Header_.LabelNamesSize) + Header_.LabelValuesSize; + const ui64 labelSizeTotal = ui64(Header_.LabelNamesSize) + Header_.LabelValuesSize; - DECODE_ENSURE(labelSizeTotal <= LABEL_SIZE_LIMIT, "Label names & values size of " << HumanReadableSize(labelSizeTotal, SF_BYTES) + DECODE_ENSURE(labelSizeTotal <= LABEL_SIZE_LIMIT, "Label names & values size of " << HumanReadableSize(labelSizeTotal, SF_BYTES) << " exceeds the limit which is " << HumanReadableSize(LABEL_SIZE_LIMIT, SF_BYTES)); // (2) read string pools TVector<char> namesBuf(Header_.LabelNamesSize); readBytes = In_->Load(namesBuf.data(), namesBuf.size()); - DECODE_ENSURE(readBytes == Header_.LabelNamesSize, "not enough data to read label names pool"); + DECODE_ENSURE(readBytes == Header_.LabelNamesSize, "not enough data to read label names pool"); TStringPool labelNames(namesBuf.data(), namesBuf.size()); TVector<char> valuesBuf(Header_.LabelValuesSize); readBytes = In_->Load(valuesBuf.data(), valuesBuf.size()); - DECODE_ENSURE(readBytes == Header_.LabelValuesSize, "not enough data to read label values pool"); + DECODE_ENSURE(readBytes == Header_.LabelValuesSize, "not enough data to read label values pool"); TStringPool labelValues(valuesBuf.data(), valuesBuf.size()); // (3) read common time @@ -180,7 +180,7 @@ namespace NMonitoring { break; default: - throw TSpackDecodeError() << "Unsupported metric type: " << metricType; + throw TSpackDecodeError() << "Unsupported metric type: " << metricType; } } @@ -212,25 +212,25 @@ namespace NMonitoring { IHistogramSnapshotPtr ReadHistogram() { ui32 bucketsCount = ReadVarint(); - auto s = TExplicitHistogramSnapshot::New(bucketsCount); + auto s = TExplicitHistogramSnapshot::New(bucketsCount); if (SV1_00 == Header_.Version) { // v1.0 - for (ui32 i = 0; i < bucketsCount; i++) { - i64 bound = ReadFixed<i64>(); - double doubleBound = (bound != Max<i64>()) - ? static_cast<double>(bound) - : Max<double>(); - - (*s)[i].first = doubleBound; - } - } else { - for (ui32 i = 0; i < bucketsCount; i++) { - double doubleBound = ReadFixed<double>(); - (*s)[i].first = doubleBound; - } + for (ui32 i = 0; i < bucketsCount; i++) { + i64 bound = ReadFixed<i64>(); + double doubleBound = (bound != Max<i64>()) + ? static_cast<double>(bound) + : Max<double>(); + + (*s)[i].first = doubleBound; + } + } else { + for (ui32 i = 0; i < bucketsCount; i++) { + double doubleBound = ReadFixed<double>(); + (*s)[i].first = doubleBound; + } } - + // values for (ui32 i = 0; i < bucketsCount; i++) { (*s)[i].second = ReadFixed<ui64>(); @@ -265,7 +265,7 @@ namespace NMonitoring { inline T ReadFixed() { T value; size_t readBytes = In_->Load(&value, sizeof(T)); - DECODE_ENSURE(readBytes == sizeof(T), "no enough data to read " << TypeName<T>()); + DECODE_ENSURE(readBytes == sizeof(T), "no enough data to read " << TypeName<T>()); return value; } @@ -277,16 +277,16 @@ namespace NMonitoring { IInputStream* In_; TString MetricNameLabel_; ETimePrecision TimePrecision_; - TSpackHeader Header_; - }; // class TDecoderSpackV1 + TSpackHeader Header_; + }; // class TDecoderSpackV1 -#undef DECODE_ENSURE - } // namespace +#undef DECODE_ENSURE + } // namespace EValueType DecodeValueType(ui8 byte) { EValueType result; if (!TryDecodeValueType(byte, &result)) { - throw TSpackDecodeError() << "unknown value type: " << byte; + throw TSpackDecodeError() << "unknown value type: " << byte; } return result; } @@ -320,7 +320,7 @@ namespace NMonitoring { ETimePrecision DecodeTimePrecision(ui8 byte) { ETimePrecision result; if (!TryDecodeTimePrecision(byte, &result)) { - throw TSpackDecodeError() << "unknown time precision: " << byte; + throw TSpackDecodeError() << "unknown time precision: " << byte; } return result; } @@ -344,7 +344,7 @@ namespace NMonitoring { EMetricType DecodeMetricType(ui8 byte) { EMetricType result; if (!TryDecodeMetricType(byte, &result)) { - throw TSpackDecodeError() << "unknown metric type: " << byte; + throw TSpackDecodeError() << "unknown metric type: " << byte; } return result; } @@ -419,7 +419,7 @@ namespace NMonitoring { ECompression DecodeCompression(ui8 byte) { ECompression result; if (!TryDecodeCompression(byte, &result)) { - throw TSpackDecodeError() << "unknown compression alg: " << byte; + throw TSpackDecodeError() << "unknown compression alg: " << byte; } return result; } diff --git a/library/cpp/monlib/encode/spack/spack_v1_encoder.cpp b/library/cpp/monlib/encode/spack/spack_v1_encoder.cpp index a2b0bb5f50..a965739ad7 100644 --- a/library/cpp/monlib/encode/spack/spack_v1_encoder.cpp +++ b/library/cpp/monlib/encode/spack/spack_v1_encoder.cpp @@ -22,11 +22,11 @@ namespace NMonitoring { TEncoderSpackV1( IOutputStream* out, ETimePrecision timePrecision, - ECompression compression, + ECompression compression, EMetricsMergingMode mergingMode, ESpackV1Version version, TStringBuf metricNameLabel - ) + ) : Out_(out) , TimePrecision_(timePrecision) , Compression_(compression) @@ -34,7 +34,7 @@ namespace NMonitoring { , MetricName_(Version_ >= SV1_02 ? LabelNamesPool_.PutIfAbsent(metricNameLabel) : nullptr) { MetricsMergingMode_ = mergingMode; - + LabelNamesPool_.SetSorted(true); LabelValuesPool_.SetSorted(true); } @@ -77,16 +77,16 @@ namespace NMonitoring { LabelNamesPool_.Build(); LabelValuesPool_.Build(); - // Sort all points uniquely by ts -- the size can decrease - ui64 pointsCount = 0; + // Sort all points uniquely by ts -- the size can decrease + ui64 pointsCount = 0; for (TMetric& metric : Metrics_) { if (metric.TimeSeries.Size() > 1) { metric.TimeSeries.SortByTs(); - } - + } + pointsCount += metric.TimeSeries.Size(); - } - + } + // (1) write header TSpackHeader header; header.Version = Version_; @@ -97,7 +97,7 @@ namespace NMonitoring { header.LabelValuesSize = static_cast<ui32>( LabelValuesPool_.BytesSize() + LabelValuesPool_.Count()); header.MetricCount = Metrics_.size(); - header.PointsCount = pointsCount; + header.PointsCount = pointsCount; Out_->Write(&header, sizeof(header)); // if compression enabled all below writes must go throught compressor @@ -258,7 +258,7 @@ namespace NMonitoring { WriteVarUInt32(Out_, count); for (ui32 i = 0; i < count; i++) { - double bound = histogram.UpperBound(i); + double bound = histogram.UpperBound(i); Out_->Write(&bound, sizeof(bound)); } for (ui32 i = 0; i < count; i++) { @@ -299,9 +299,9 @@ namespace NMonitoring { IMetricEncoderPtr EncoderSpackV1( IOutputStream* out, ETimePrecision timePrecision, - ECompression compression, + ECompression compression, EMetricsMergingMode mergingMode - ) { + ) { return MakeHolder<TEncoderSpackV1>(out, timePrecision, compression, mergingMode, SV1_01, ""); } diff --git a/library/cpp/monlib/encode/spack/spack_v1_ut.cpp b/library/cpp/monlib/encode/spack/spack_v1_ut.cpp index fe778eb7e0..c75f89e48e 100644 --- a/library/cpp/monlib/encode/spack/spack_v1_ut.cpp +++ b/library/cpp/monlib/encode/spack/spack_v1_ut.cpp @@ -49,9 +49,9 @@ void AssertPointEqual(const NProto::TPoint& p, TInstant time, i64 value) { } Y_UNIT_TEST_SUITE(TSpackTest) { - ui8 expectedHeader_v1_0[] = { + ui8 expectedHeader_v1_0[] = { 0x53, 0x50, // magic "SP" (fixed ui16) - // minor, major + // minor, major 0x00, 0x01, // version (fixed ui16) 0x18, 0x00, // header size (fixed ui16) 0x00, // time precision (fixed ui8) @@ -62,19 +62,19 @@ Y_UNIT_TEST_SUITE(TSpackTest) { 0x08, 0x00, 0x00, 0x00, // points count (fixed ui32) }; - ui8 expectedHeader[] = { - 0x53, 0x50, // magic "SP" (fixed ui16) - // minor, major - 0x01, 0x01, // version (fixed ui16) - 0x18, 0x00, // header size (fixed ui16) - 0x00, // time precision (fixed ui8) - 0x00, // compression algorithm (fixed ui8) + ui8 expectedHeader[] = { + 0x53, 0x50, // magic "SP" (fixed ui16) + // minor, major + 0x01, 0x01, // version (fixed ui16) + 0x18, 0x00, // header size (fixed ui16) + 0x00, // time precision (fixed ui8) + 0x00, // compression algorithm (fixed ui8) 0x0d, 0x00, 0x00, 0x00, // label names size (fixed ui32) 0x40, 0x00, 0x00, 0x00, // labels values size (fixed ui32) 0x08, 0x00, 0x00, 0x00, // metric count (fixed ui32) 0x08, 0x00, 0x00, 0x00, // points count (fixed ui32) - }; - + }; + ui8 expectedStringPools[] = { 0x6e, 0x61, 0x6d, 0x65, 0x00, // "name\0" 0x70, 0x72, 0x6f, 0x6a, 0x65, 0x63, 0x74, 0x00, // "project\0" @@ -150,7 +150,7 @@ Y_UNIT_TEST_SUITE(TSpackTest) { 0x05, // label value index (varint) 0x0b, 0x63, 0xfe, 0x59, // time in seconds (fixed ui32) 0x06, // histogram buckets count (varint) - 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // histogram bucket bounds (array of fixed ui64) + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // histogram bucket bounds (array of fixed ui64) 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -165,27 +165,27 @@ Y_UNIT_TEST_SUITE(TSpackTest) { }; ui8 expectedMetric5[] = { - 0x16, // types (HIST | ONE_WITH_TS) (fixed ui8) - 0x00, // flags (fixed ui8) + 0x16, // types (HIST | ONE_WITH_TS) (fixed ui8) + 0x00, // flags (fixed ui8) 0x01, // metric labels count (varint) - 0x00, // label name index (varint) + 0x00, // label name index (varint) 0x05, // label value index (varint) - 0x0b, 0x63, 0xfe, 0x59, // time in seconds (fixed ui32) - 0x06, // histogram buckets count (varint) - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, // histogram bucket bounds (array of doubles) - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x40, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x40, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x40, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0x7f, - 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // histogram bucket values - 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - }; - + 0x0b, 0x63, 0xfe, 0x59, // time in seconds (fixed ui32) + 0x06, // histogram buckets count (varint) + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, // histogram bucket bounds (array of doubles) + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x40, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x40, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x40, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0x7f, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // histogram bucket values + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + }; + ui8 expectedMetric6[] = { 0x12, // types (IGAUGE | ONE_WITH_TS) (fixed ui8) 0x00, // flags (fixed ui8) @@ -292,9 +292,9 @@ Y_UNIT_TEST_SUITE(TSpackTest) { e->OnLabel("name", "q2"); e->OnLabelsEnd(); } - // Only the last value will be encoded - e->OnUint64(TInstant::Zero(), 10); - e->OnUint64(TInstant::Zero(), 13); + // Only the last value will be encoded + e->OnUint64(TInstant::Zero(), 10); + e->OnUint64(TInstant::Zero(), 13); e->OnUint64(TInstant::Zero(), 17); e->OnMetricEnd(); } @@ -305,8 +305,8 @@ Y_UNIT_TEST_SUITE(TSpackTest) { e->OnLabel("name", "q3"); e->OnLabelsEnd(); } - e->OnUint64(now, 10); - e->OnUint64(now, 13); + e->OnUint64(now, 10); + e->OnUint64(now, 13); e->OnUint64(now, 17); e->OnMetricEnd(); } @@ -410,98 +410,98 @@ Y_UNIT_TEST_SUITE(TSpackTest) { } NProto::TMultiSamplesList GetMergingMetricSamples(EMetricsMergingMode mergingMode) { - TBuffer buffer; - TBufferOutput out(buffer); - - auto e = EncoderSpackV1( - &out, - ETimePrecision::SECONDS, - ECompression::IDENTITY, - mergingMode - ); - - e->OnStreamBegin(); - for (size_t i = 0; i != 3; ++i) { + TBuffer buffer; + TBufferOutput out(buffer); + + auto e = EncoderSpackV1( + &out, + ETimePrecision::SECONDS, + ECompression::IDENTITY, + mergingMode + ); + + e->OnStreamBegin(); + for (size_t i = 0; i != 3; ++i) { e->OnMetricBegin(EMetricType::COUNTER); - { - e->OnLabelsBegin(); + { + e->OnLabelsBegin(); e->OnLabel("name", "my_counter"); - e->OnLabelsEnd(); - } - e->OnUint64(TInstant::Zero() + TDuration::Seconds(i), i + 1); + e->OnLabelsEnd(); + } + e->OnUint64(TInstant::Zero() + TDuration::Seconds(i), i + 1); e->OnMetricEnd(); - } - e->OnStreamEnd(); - e->Close(); - - NProto::TMultiSamplesList samples; + } + e->OnStreamEnd(); + e->Close(); + + NProto::TMultiSamplesList samples; IMetricEncoderPtr eProto = EncoderProtobuf(&samples); - TBufferInput in(buffer); - DecodeSpackV1(&in, eProto.Get()); - - return samples; - } - + TBufferInput in(buffer); + DecodeSpackV1(&in, eProto.Get()); + + return samples; + } + Y_UNIT_TEST(SpackEncoderMergesMetrics) { - { + { NProto::TMultiSamplesList samples = GetMergingMetricSamples(EMetricsMergingMode::DEFAULT); - - UNIT_ASSERT_EQUAL(samples.SamplesSize(), 3); - UNIT_ASSERT_EQUAL(samples.GetSamples(0).GetPoints(0).GetUint64(), 1); - UNIT_ASSERT_EQUAL(samples.GetSamples(1).GetPoints(0).GetUint64(), 2); - UNIT_ASSERT_EQUAL(samples.GetSamples(2).GetPoints(0).GetUint64(), 3); - } - - { + + UNIT_ASSERT_EQUAL(samples.SamplesSize(), 3); + UNIT_ASSERT_EQUAL(samples.GetSamples(0).GetPoints(0).GetUint64(), 1); + UNIT_ASSERT_EQUAL(samples.GetSamples(1).GetPoints(0).GetUint64(), 2); + UNIT_ASSERT_EQUAL(samples.GetSamples(2).GetPoints(0).GetUint64(), 3); + } + + { NProto::TMultiSamplesList samples = GetMergingMetricSamples(EMetricsMergingMode::MERGE_METRICS); - - UNIT_ASSERT_EQUAL(samples.SamplesSize(), 1); - - auto sample0 = samples.GetSamples(0); - UNIT_ASSERT_EQUAL(sample0.GetPoints(0).GetUint64(), 1); - UNIT_ASSERT_EQUAL(sample0.GetPoints(1).GetUint64(), 2); - UNIT_ASSERT_EQUAL(sample0.GetPoints(2).GetUint64(), 3); - } - } - - void DecodeDataToSamples(NProto::TMultiSamplesList & samples, ui16 version) { + + UNIT_ASSERT_EQUAL(samples.SamplesSize(), 1); + + auto sample0 = samples.GetSamples(0); + UNIT_ASSERT_EQUAL(sample0.GetPoints(0).GetUint64(), 1); + UNIT_ASSERT_EQUAL(sample0.GetPoints(1).GetUint64(), 2); + UNIT_ASSERT_EQUAL(sample0.GetPoints(2).GetUint64(), 3); + } + } + + void DecodeDataToSamples(NProto::TMultiSamplesList & samples, ui16 version) { IMetricEncoderPtr e = EncoderProtobuf(&samples); - TBuffer data(expectedSize); + TBuffer data(expectedSize); 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_v1_0), Y_ARRAY_SIZE(expectedHeader_v1_0)); + } else { data.Append(reinterpret_cast<char*>(expectedHeader), Y_ARRAY_SIZE(expectedHeader)); - } - data.Append(reinterpret_cast<char*>(expectedStringPools), Y_ARRAY_SIZE(expectedStringPools)); - data.Append(reinterpret_cast<char*>(expectedCommonTime), Y_ARRAY_SIZE(expectedCommonTime)); - data.Append(reinterpret_cast<char*>(expectedCommonLabels), Y_ARRAY_SIZE(expectedCommonLabels)); + } + data.Append(reinterpret_cast<char*>(expectedStringPools), Y_ARRAY_SIZE(expectedStringPools)); + data.Append(reinterpret_cast<char*>(expectedCommonTime), Y_ARRAY_SIZE(expectedCommonTime)); + data.Append(reinterpret_cast<char*>(expectedCommonLabels), Y_ARRAY_SIZE(expectedCommonLabels)); data.Append(reinterpret_cast<char*>(expectedMetric1), Y_ARRAY_SIZE(expectedMetric1)); 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 data.Append(reinterpret_cast<char*>(expectedMetric5_v1_0), Y_ARRAY_SIZE(expectedMetric5_v1_0)); - } else { + } else { data.Append(reinterpret_cast<char*>(expectedMetric5), Y_ARRAY_SIZE(expectedMetric5)); } data.Append(reinterpret_cast<char*>(expectedMetric6), Y_ARRAY_SIZE(expectedMetric6)); data.Append(reinterpret_cast<char*>(expectedMetric7), Y_ARRAY_SIZE(expectedMetric7)); data.Append(reinterpret_cast<char*>(expectedMetric8), Y_ARRAY_SIZE(expectedMetric8)); - TBufferInput in(data); - DecodeSpackV1(&in, e.Get()); - } - - void DecodeDataToSamples(NProto::TMultiSamplesList & samples) { - TSpackHeader header; + TBufferInput in(data); + DecodeSpackV1(&in, e.Get()); + } + + void DecodeDataToSamples(NProto::TMultiSamplesList & samples) { + TSpackHeader header; header.Version = SV1_01; - DecodeDataToSamples(samples, header.Version); - } - - Y_UNIT_TEST(Decode) { - NProto::TMultiSamplesList samples; - DecodeDataToSamples(samples); - + DecodeDataToSamples(samples, header.Version); + } + + Y_UNIT_TEST(Decode) { + NProto::TMultiSamplesList samples; + DecodeDataToSamples(samples); + UNIT_ASSERT_VALUES_EQUAL( TInstant::MilliSeconds(samples.GetCommonTime()), TInstant::Seconds(1500000000)); @@ -684,20 +684,20 @@ Y_UNIT_TEST_SUITE(TSpackTest) { Y_UNIT_TEST(CompressionLz4) { TestCompression(ECompression::LZ4); } - - Y_UNIT_TEST(Decode_v1_0_histograms) { - // Check that histogram bounds decoded from different versions are the same - NProto::TMultiSamplesList samples, samples_v1_0; - DecodeDataToSamples(samples); + + Y_UNIT_TEST(Decode_v1_0_histograms) { + // 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); - - const NProto::THistogram& pointHistogram = samples.GetSamples(4).GetPoints(0).GetHistogram(); - const NProto::THistogram& pointHistogram_v1_0 = samples_v1_0.GetSamples(4).GetPoints(0).GetHistogram(); - - for (size_t i = 0; i < pointHistogram.BoundsSize(); i++) { - UNIT_ASSERT_DOUBLES_EQUAL(pointHistogram.GetBounds(i), pointHistogram_v1_0.GetBounds(i), Min<double>()); - } - } + + const NProto::THistogram& pointHistogram = samples.GetSamples(4).GetPoints(0).GetHistogram(); + const NProto::THistogram& pointHistogram_v1_0 = samples_v1_0.GetSamples(4).GetPoints(0).GetHistogram(); + + for (size_t i = 0; i < pointHistogram.BoundsSize(); i++) { + UNIT_ASSERT_DOUBLES_EQUAL(pointHistogram.GetBounds(i), pointHistogram_v1_0.GetBounds(i), Min<double>()); + } + } Y_UNIT_TEST(SimpleV12) { ui8 expectedSerialized[] = { diff --git a/library/cpp/monlib/encode/spack/ya.make b/library/cpp/monlib/encode/spack/ya.make index 78d3061291..f6439c5b03 100644 --- a/library/cpp/monlib/encode/spack/ya.make +++ b/library/cpp/monlib/encode/spack/ya.make @@ -13,9 +13,9 @@ SRCS( ) PEERDIR( - library/cpp/monlib/encode/buffered - library/cpp/monlib/exception - + library/cpp/monlib/encode/buffered + library/cpp/monlib/exception + contrib/libs/lz4 contrib/libs/xxhash contrib/libs/zlib diff --git a/library/cpp/monlib/encode/text/text_encoder.cpp b/library/cpp/monlib/encode/text/text_encoder.cpp index 10336261f0..5f53b5b9f0 100644 --- a/library/cpp/monlib/encode/text/text_encoder.cpp +++ b/library/cpp/monlib/encode/text/text_encoder.cpp @@ -19,15 +19,15 @@ namespace NMonitoring { private: void OnStreamBegin() override { - State_.Expect(TEncoderState::EState::ROOT); + State_.Expect(TEncoderState::EState::ROOT); } void OnStreamEnd() override { - State_.Expect(TEncoderState::EState::ROOT); + State_.Expect(TEncoderState::EState::ROOT); } void OnCommonTime(TInstant time) override { - State_.Expect(TEncoderState::EState::ROOT); + State_.Expect(TEncoderState::EState::ROOT); CommonTime_ = time; if (time != TInstant::Zero()) { Out_->Write(TStringBuf("common time: ")); @@ -50,8 +50,8 @@ namespace NMonitoring { void OnLabelsBegin() override { if (State_ == TEncoderState::EState::METRIC) { State_ = TEncoderState::EState::METRIC_LABELS; - } else if (State_ == TEncoderState::EState::ROOT) { - State_ = TEncoderState::EState::COMMON_LABELS; + } else if (State_ == TEncoderState::EState::ROOT) { + State_ = TEncoderState::EState::COMMON_LABELS; } else { State_.ThrowInvalid("expected METRIC or ROOT"); } @@ -60,8 +60,8 @@ namespace NMonitoring { void OnLabelsEnd() override { if (State_ == TEncoderState::EState::METRIC_LABELS) { State_ = TEncoderState::EState::METRIC; - } else if (State_ == TEncoderState::EState::COMMON_LABELS) { - State_ = TEncoderState::EState::ROOT; + } else if (State_ == TEncoderState::EState::COMMON_LABELS) { + State_ = TEncoderState::EState::ROOT; Out_->Write(TStringBuf("common labels: ")); WriteLabels(); Out_->Write('\n'); diff --git a/library/cpp/monlib/encode/unistat/unistat_decoder.cpp b/library/cpp/monlib/encode/unistat/unistat_decoder.cpp index b2344b0905..eab6b0a32f 100644 --- a/library/cpp/monlib/encode/unistat/unistat_decoder.cpp +++ b/library/cpp/monlib/encode/unistat/unistat_decoder.cpp @@ -49,7 +49,7 @@ namespace NMonitoring { auto ExtractDouble = ExtractNumber<double>; auto ExtractUi64 = ExtractNumber<ui64>; - class THistogramBuilder { + class THistogramBuilder { public: void Add(TBucketBound bound, TBucketValue value) { /// XXX: yasm uses left-closed intervals, while in monlib we use right-closed ones, @@ -67,14 +67,14 @@ namespace NMonitoring { NextValue_ = value; } - IHistogramSnapshotPtr Finalize() { + IHistogramSnapshotPtr Finalize() { Bounds_.push_back(std::numeric_limits<TBucketBound>::max()); Values_.push_back(NextValue_); - Y_ENSURE(Bounds_.size() <= HISTOGRAM_MAX_BUCKETS_COUNT, - "Histogram is only allowed to have " << HISTOGRAM_MAX_BUCKETS_COUNT << " buckets, but has " << Bounds_.size()); + Y_ENSURE(Bounds_.size() <= HISTOGRAM_MAX_BUCKETS_COUNT, + "Histogram is only allowed to have " << HISTOGRAM_MAX_BUCKETS_COUNT << " buckets, but has " << Bounds_.size()); - return ExplicitHistogramSnapshot(Bounds_, Values_); + return ExplicitHistogramSnapshot(Bounds_, Values_); } public: @@ -135,7 +135,7 @@ namespace NMonitoring { MetricContext_.Type = EMetricType::HIST; } - auto histogramBuilder = THistogramBuilder(); + auto histogramBuilder = THistogramBuilder(); for (auto&& bucket : jsonHist.GetArray()) { Y_ENSURE(bucket.IsArray(), "Expected an array, but found " << bucket.GetType()); @@ -143,10 +143,10 @@ namespace NMonitoring { Y_ENSURE(arr.size() == 2, "Histogram bucket must be an array of 2 elements"); const auto bound = ExtractDouble(arr[0]); const auto weight = ExtractUi64(arr[1]); - histogramBuilder.Add(bound, weight); + histogramBuilder.Add(bound, weight); } - MetricContext_.Histogram = histogramBuilder.Finalize(); + MetricContext_.Histogram = histogramBuilder.Finalize(); MetricContext_.Value = TMetricValue{MetricContext_.Histogram.Get()}; } @@ -233,7 +233,7 @@ namespace NMonitoring { TMetricValue Value; bool IsDeriv{false}; TLabels Labels; - IHistogramSnapshotPtr Histogram; + IHistogramSnapshotPtr Histogram; } MetricContext_; }; diff --git a/library/cpp/monlib/encode/ya.make b/library/cpp/monlib/encode/ya.make index d1bb09f07b..208c10d440 100644 --- a/library/cpp/monlib/encode/ya.make +++ b/library/cpp/monlib/encode/ya.make @@ -15,6 +15,6 @@ PEERDIR( library/cpp/monlib/metrics ) -GENERATE_ENUM_SERIALIZATION_WITH_HEADER(encoder_state_enum.h) - +GENERATE_ENUM_SERIALIZATION_WITH_HEADER(encoder_state_enum.h) + END() diff --git a/library/cpp/monlib/exception/exception.cpp b/library/cpp/monlib/exception/exception.cpp index 5a8d53ceb0..8482b1d4f1 100644 --- a/library/cpp/monlib/exception/exception.cpp +++ b/library/cpp/monlib/exception/exception.cpp @@ -1 +1 @@ -#include "exception.h" +#include "exception.h" diff --git a/library/cpp/monlib/exception/exception.h b/library/cpp/monlib/exception/exception.h index 027c22b27d..6cc36ca7da 100644 --- a/library/cpp/monlib/exception/exception.h +++ b/library/cpp/monlib/exception/exception.h @@ -1,13 +1,13 @@ -#pragma once - - -namespace NMonitoring { - -#define MONLIB_ENSURE_EX(CONDITION, THROW_EXPRESSION) \ - do { \ - if (Y_UNLIKELY(!(CONDITION))) { \ - throw THROW_EXPRESSION; \ - } \ - } while (false) - -} // namespace NSolomon +#pragma once + + +namespace NMonitoring { + +#define MONLIB_ENSURE_EX(CONDITION, THROW_EXPRESSION) \ + do { \ + if (Y_UNLIKELY(!(CONDITION))) { \ + throw THROW_EXPRESSION; \ + } \ + } while (false) + +} // namespace NSolomon diff --git a/library/cpp/monlib/exception/ya.make b/library/cpp/monlib/exception/ya.make index 78660711d3..6e7e812ce1 100644 --- a/library/cpp/monlib/exception/ya.make +++ b/library/cpp/monlib/exception/ya.make @@ -1,12 +1,12 @@ -LIBRARY() - -OWNER(g:solomon) - -SRCS( - exception.cpp -) - -PEERDIR( -) - -END() +LIBRARY() + +OWNER(g:solomon) + +SRCS( + exception.cpp +) + +PEERDIR( +) + +END() diff --git a/library/cpp/monlib/metrics/histogram_collector.h b/library/cpp/monlib/metrics/histogram_collector.h index 9f6bbbdfb7..db7ed8aaac 100644 --- a/library/cpp/monlib/metrics/histogram_collector.h +++ b/library/cpp/monlib/metrics/histogram_collector.h @@ -14,7 +14,7 @@ namespace NMonitoring { /** * Store {@code count} times given {@code value} in this collector. */ - virtual void Collect(double value, ui32 count) = 0; + virtual void Collect(double value, ui32 count) = 0; /** * Store given {@code value} in this collector. @@ -114,6 +114,6 @@ namespace NMonitoring { * each bucket. The value must be >= 1. */ IHistogramCollectorPtr LinearHistogram( - ui32 bucketsCount, TBucketBound startValue, TBucketBound bucketWidth); + ui32 bucketsCount, TBucketBound startValue, TBucketBound bucketWidth); -} // namespace NMonitoring +} // namespace NMonitoring diff --git a/library/cpp/monlib/metrics/histogram_collector_explicit.cpp b/library/cpp/monlib/metrics/histogram_collector_explicit.cpp index 377fc233ef..6a9c0adf9f 100644 --- a/library/cpp/monlib/metrics/histogram_collector_explicit.cpp +++ b/library/cpp/monlib/metrics/histogram_collector_explicit.cpp @@ -14,15 +14,15 @@ namespace NMonitoring { class TExplicitHistogramCollector: public IHistogramCollector { public: TExplicitHistogramCollector(TBucketBounds bounds) - : Values_(bounds.size() + 1) - , Bounds_(std::move(bounds)) + : Values_(bounds.size() + 1) + , Bounds_(std::move(bounds)) { // add one bucket as +INF Bounds_.push_back(Max<TBucketBound>()); } - void Collect(double value, ui32 count) override { - auto it = LowerBound(Bounds_.begin(), Bounds_.end(), value); + void Collect(double value, ui32 count) override { + auto it = LowerBound(Bounds_.begin(), Bounds_.end(), value); auto index = std::distance(Bounds_.begin(), it); Values_.Add(index, count); } @@ -37,15 +37,15 @@ namespace NMonitoring { } private: - TAtomicsArray Values_; + TAtomicsArray Values_; TBucketBounds Bounds_; }; IHistogramCollectorPtr ExplicitHistogram(TBucketBounds bounds) { Y_ENSURE(bounds.size() >= 1, "explicit histogram must contain at least one bucket"); - Y_ENSURE(bounds.size() <= HISTOGRAM_MAX_BUCKETS_COUNT, - "buckets count must be <=" << HISTOGRAM_MAX_BUCKETS_COUNT + Y_ENSURE(bounds.size() <= HISTOGRAM_MAX_BUCKETS_COUNT, + "buckets count must be <=" << HISTOGRAM_MAX_BUCKETS_COUNT << ", but got: " << bounds.size()); Y_ENSURE(IsSorted(bounds.begin(), bounds.end()), "bounds for explicit histogram must be sorted"); diff --git a/library/cpp/monlib/metrics/histogram_collector_exponential.cpp b/library/cpp/monlib/metrics/histogram_collector_exponential.cpp index 2f8a50a5f9..9ae5327650 100644 --- a/library/cpp/monlib/metrics/histogram_collector_exponential.cpp +++ b/library/cpp/monlib/metrics/histogram_collector_exponential.cpp @@ -16,13 +16,13 @@ namespace NMonitoring { : Values_(bucketsCount) , Base_(base) , Scale_(scale) - , MinValue_(scale) - , MaxValue_(scale * std::pow(base, bucketsCount - 2)) + , MinValue_(scale) + , MaxValue_(scale * std::pow(base, bucketsCount - 2)) , LogOfBase_(std::log(base)) { } - void Collect(double value, ui32 count) override { + void Collect(double value, ui32 count) override { ui32 index = Max<ui32>(); if (value <= MinValue_) { index = 0; @@ -47,8 +47,8 @@ namespace NMonitoring { TAtomicsArray Values_; double Base_; double Scale_; - TBucketBound MinValue_; - TBucketBound MaxValue_; + TBucketBound MinValue_; + TBucketBound MaxValue_; double LogOfBase_; }; @@ -57,8 +57,8 @@ namespace NMonitoring { { Y_ENSURE(bucketsCount >= 2, "exponential histogram must contain at least two buckets"); - Y_ENSURE(bucketsCount <= HISTOGRAM_MAX_BUCKETS_COUNT, - "buckets count must be <=" << HISTOGRAM_MAX_BUCKETS_COUNT + Y_ENSURE(bucketsCount <= HISTOGRAM_MAX_BUCKETS_COUNT, + "buckets count must be <=" << HISTOGRAM_MAX_BUCKETS_COUNT << ", but got: " << bucketsCount); Y_ENSURE(base > 1.0, "base must be > 1.0, got: " << base); Y_ENSURE(scale >= 1.0, "scale must be >= 1.0, got: " << scale); diff --git a/library/cpp/monlib/metrics/histogram_collector_linear.cpp b/library/cpp/monlib/metrics/histogram_collector_linear.cpp index f8ad86f3a4..3f43920524 100644 --- a/library/cpp/monlib/metrics/histogram_collector_linear.cpp +++ b/library/cpp/monlib/metrics/histogram_collector_linear.cpp @@ -15,7 +15,7 @@ namespace NMonitoring { class TLinearHistogramCollector: public IHistogramCollector { public: TLinearHistogramCollector( - ui32 bucketsCount, TBucketBound startValue, TBucketBound bucketWidth) + ui32 bucketsCount, TBucketBound startValue, TBucketBound bucketWidth) : Values_(bucketsCount) , StartValue_(startValue) , BucketWidth_(bucketWidth) @@ -23,14 +23,14 @@ namespace NMonitoring { { } - void Collect(double value, ui32 count) override { + void Collect(double value, ui32 count) override { ui32 index = Max<ui32>(); if (value <= StartValue_) { index = 0; } else if (value > MaxValue_) { index = Values_.Size() - 1; } else { - double buckets = (value - StartValue_) / BucketWidth_; + double buckets = (value - StartValue_) / BucketWidth_; index = static_cast<ui32>(std::ceil(buckets)); } Values_.Add(index, count); @@ -47,18 +47,18 @@ namespace NMonitoring { private: TAtomicsArray Values_; - TBucketBound StartValue_; - double BucketWidth_; - TBucketBound MaxValue_; + TBucketBound StartValue_; + double BucketWidth_; + TBucketBound MaxValue_; }; IHistogramCollectorPtr LinearHistogram( - ui32 bucketsCount, TBucketBound startValue, TBucketBound bucketWidth) + ui32 bucketsCount, TBucketBound startValue, TBucketBound bucketWidth) { Y_ENSURE(bucketsCount >= 2, "linear histogram must contain at least two buckets"); - Y_ENSURE(bucketsCount <= HISTOGRAM_MAX_BUCKETS_COUNT, - "buckets count must be <=" << HISTOGRAM_MAX_BUCKETS_COUNT + Y_ENSURE(bucketsCount <= HISTOGRAM_MAX_BUCKETS_COUNT, + "buckets count must be <=" << HISTOGRAM_MAX_BUCKETS_COUNT << ", but got: " << bucketsCount); Y_ENSURE(bucketWidth >= 1, "bucketWidth must be >= 1, got: " << bucketWidth); diff --git a/library/cpp/monlib/metrics/histogram_snapshot.cpp b/library/cpp/monlib/metrics/histogram_snapshot.cpp index 75b5811546..1d6cb90c37 100644 --- a/library/cpp/monlib/metrics/histogram_snapshot.cpp +++ b/library/cpp/monlib/metrics/histogram_snapshot.cpp @@ -2,62 +2,62 @@ #include <util/stream/output.h> -#include <iostream> - - +#include <iostream> + + namespace NMonitoring { IHistogramSnapshotPtr ExplicitHistogramSnapshot(TConstArrayRef<TBucketBound> bounds, TConstArrayRef<TBucketValue> values) { - Y_ENSURE(bounds.size() == values.size(), - "mismatched sizes: bounds(" << bounds.size() << - ") != buckets(" << values.size() << ')'); + Y_ENSURE(bounds.size() == values.size(), + "mismatched sizes: bounds(" << bounds.size() << + ") != buckets(" << values.size() << ')'); - auto snapshot = TExplicitHistogramSnapshot::New(bounds.size()); + auto snapshot = TExplicitHistogramSnapshot::New(bounds.size()); - for (size_t i = 0; i != bounds.size(); ++i) { - (*snapshot)[i].first = bounds[i]; - (*snapshot)[i].second = values[i]; - } + for (size_t i = 0; i != bounds.size(); ++i) { + (*snapshot)[i].first = bounds[i]; + (*snapshot)[i].second = values[i]; + } - return snapshot; + return snapshot; } -} // namespace NMonitoring +} // namespace NMonitoring -namespace { +namespace { -template <typename TStream> -auto& Output(TStream& os, const NMonitoring::IHistogramSnapshot& hist) { +template <typename TStream> +auto& Output(TStream& os, const NMonitoring::IHistogramSnapshot& hist) { os << TStringBuf("{"); - ui32 i = 0; - ui32 count = hist.Count(); + ui32 i = 0; + ui32 count = hist.Count(); - if (count > 0) { - for (; i < count - 1; ++i) { + if (count > 0) { + for (; i < count - 1; ++i) { os << hist.UpperBound(i) << TStringBuf(": ") << hist.Value(i); os << TStringBuf(", "); - } - - if (hist.UpperBound(i) == Max<NMonitoring::TBucketBound>()) { + } + + if (hist.UpperBound(i) == Max<NMonitoring::TBucketBound>()) { os << TStringBuf("inf: ") << hist.Value(i); - } else { + } else { os << hist.UpperBound(i) << TStringBuf(": ") << hist.Value(i); - } + } } os << TStringBuf("}"); - - return os; -} - -} // namespace - -std::ostream& operator<<(std::ostream& os, const NMonitoring::IHistogramSnapshot& hist) { - return Output(os, hist); -} - -template <> -void Out<NMonitoring::IHistogramSnapshot>(IOutputStream& os, const NMonitoring::IHistogramSnapshot& hist) { - Output(os, hist); + + return os; } + +} // namespace + +std::ostream& operator<<(std::ostream& os, const NMonitoring::IHistogramSnapshot& hist) { + return Output(os, hist); +} + +template <> +void Out<NMonitoring::IHistogramSnapshot>(IOutputStream& os, const NMonitoring::IHistogramSnapshot& hist) { + Output(os, hist); +} diff --git a/library/cpp/monlib/metrics/histogram_snapshot.h b/library/cpp/monlib/metrics/histogram_snapshot.h index e8acf6ac2b..494634eedd 100644 --- a/library/cpp/monlib/metrics/histogram_snapshot.h +++ b/library/cpp/monlib/metrics/histogram_snapshot.h @@ -3,12 +3,12 @@ #include <util/generic/array_ref.h> #include <util/generic/ptr.h> #include <util/generic/vector.h> -#include <util/generic/yexception.h> +#include <util/generic/yexception.h> -#include <cmath> +#include <cmath> #include <limits> - + namespace NMonitoring { using TBucketBound = double; @@ -45,166 +45,166 @@ namespace NMonitoring { using IHistogramSnapshotPtr = TIntrusivePtr<IHistogramSnapshot>; - /////////////////////////////////////////////////////////////////////////////// - // TLinearHistogramSnapshot - /////////////////////////////////////////////////////////////////////////////// - class TLinearHistogramSnapshot: public IHistogramSnapshot { - public: - TLinearHistogramSnapshot( - TBucketBound startValue, TBucketBound bucketWidth, TBucketValues values) - : StartValue_(startValue) - , BucketWidth_(bucketWidth) - , Values_(std::move(values)) - { - } - - ui32 Count() const override { - return static_cast<ui32>(Values_.size()); - } - - TBucketBound UpperBound(ui32 index) const override { - Y_ASSERT(index < Values_.size()); - if (index == Count() - 1) { - return Max<TBucketBound>(); - } - return StartValue_ + BucketWidth_ * index; - } - - TBucketValue Value(ui32 index) const override { - Y_ASSERT(index < Values_.size()); - return Values_[index]; - } - - ui64 MemorySizeBytes() { - return sizeof(*this) + Values_.capacity() * sizeof(decltype(Values_)::value_type); - } - - private: - TBucketBound StartValue_; - TBucketBound BucketWidth_; - TBucketValues Values_; - }; - + /////////////////////////////////////////////////////////////////////////////// + // TLinearHistogramSnapshot + /////////////////////////////////////////////////////////////////////////////// + class TLinearHistogramSnapshot: public IHistogramSnapshot { + public: + TLinearHistogramSnapshot( + TBucketBound startValue, TBucketBound bucketWidth, TBucketValues values) + : StartValue_(startValue) + , BucketWidth_(bucketWidth) + , Values_(std::move(values)) + { + } + + ui32 Count() const override { + return static_cast<ui32>(Values_.size()); + } + + TBucketBound UpperBound(ui32 index) const override { + Y_ASSERT(index < Values_.size()); + if (index == Count() - 1) { + return Max<TBucketBound>(); + } + return StartValue_ + BucketWidth_ * index; + } + + TBucketValue Value(ui32 index) const override { + Y_ASSERT(index < Values_.size()); + return Values_[index]; + } + + ui64 MemorySizeBytes() { + return sizeof(*this) + Values_.capacity() * sizeof(decltype(Values_)::value_type); + } + + private: + TBucketBound StartValue_; + TBucketBound BucketWidth_; + TBucketValues Values_; + }; + /////////////////////////////////////////////////////////////////////////// - // TExponentialHistogramSnapshot + // TExponentialHistogramSnapshot /////////////////////////////////////////////////////////////////////////// - class TExponentialHistogramSnapshot: public IHistogramSnapshot { - public: - TExponentialHistogramSnapshot( - double base, double scale, TBucketValues values) - : Base_(base) - , Scale_(scale) - , Values_(std::move(values)) - { - } - - ui32 Count() const override { - return static_cast<ui32>(Values_.size()); - } - - TBucketBound UpperBound(ui32 index) const override { - Y_ASSERT(index < Values_.size()); - if (index == Values_.size() - 1) { - return Max<TBucketBound>(); - } - return std::round(Scale_ * std::pow(Base_, index)); - } - - TBucketValue Value(ui32 index) const override { - Y_ASSERT(index < Values_.size()); - return Values_[index]; - } - - ui64 MemorySizeBytes() { - return sizeof(*this) + Values_.capacity() * sizeof(decltype(Values_)::value_type); - } - - private: - double Base_; - double Scale_; - TBucketValues Values_; - }; - - using TBucket = std::pair<TBucketBound, TBucketValue>; - - /////////////////////////////////////////////////////////////////////// - // TExplicitHistogramSnapshot - /////////////////////////////////////////////////////////////////////// - // - // Memory layout (single contiguous block): - // - // +------+-----------+--------------+--------+--------+- -+--------+--------+ - // | vptr | RefsCount | BucketsCount | Bound1 | Value1 | ... | BoundN | ValueN | - // +------+-----------+--------------+--------+--------+- -+--------+--------+ - // - class TExplicitHistogramSnapshot: public IHistogramSnapshot, private TNonCopyable { - public: - static TIntrusivePtr<TExplicitHistogramSnapshot> New(ui32 bucketsCount) { - size_t bucketsSize = bucketsCount * sizeof(TBucket); - Y_ENSURE(bucketsCount <= HISTOGRAM_MAX_BUCKETS_COUNT, "Cannot allocate a histogram with " << bucketsCount - << " buckets. Bucket count is limited to " << HISTOGRAM_MAX_BUCKETS_COUNT); - - return new(bucketsSize) TExplicitHistogramSnapshot(bucketsCount); - } - - TBucket& operator[](ui32 index) noexcept { - return Bucket(index); - } - - ui32 Count() const override { - return BucketsCount_; - } - - TBucketBound UpperBound(ui32 index) const override { - return Bucket(index).first; - } - - TBucketValue Value(ui32 index) const override { - return Bucket(index).second; - } - - ui64 MemorySizeBytes() const { - return sizeof(*this) + BucketsCount_ * sizeof(TBucket); - } - - private: - explicit TExplicitHistogramSnapshot(ui32 bucketsCount) noexcept - : BucketsCount_(bucketsCount) - { - } - - static void* operator new(size_t size, size_t bucketsSize) { - return ::operator new(size + bucketsSize); - } - - static void operator delete(void* mem) { - ::operator delete(mem); - } - - static void operator delete(void* mem, size_t, size_t) { - // this operator can be called as paired for custom new operator - ::operator delete(mem); - } - - TBucket& Bucket(ui32 index) noexcept { - Y_VERIFY_DEBUG(index < BucketsCount_); - return *(reinterpret_cast<TBucket*>(this + 1) + index); - } - - const TBucket& Bucket(ui32 index) const noexcept { - Y_VERIFY_DEBUG(index < BucketsCount_); - return *(reinterpret_cast<const TBucket*>(this + 1) + index); - } - - private: - ui32 BucketsCount_; - }; - - static_assert(alignof(TExplicitHistogramSnapshot) == alignof(TBucket), - "mismatched alingments of THistogramSnapshot and TBucket"); - + class TExponentialHistogramSnapshot: public IHistogramSnapshot { + public: + TExponentialHistogramSnapshot( + double base, double scale, TBucketValues values) + : Base_(base) + , Scale_(scale) + , Values_(std::move(values)) + { + } + + ui32 Count() const override { + return static_cast<ui32>(Values_.size()); + } + + TBucketBound UpperBound(ui32 index) const override { + Y_ASSERT(index < Values_.size()); + if (index == Values_.size() - 1) { + return Max<TBucketBound>(); + } + return std::round(Scale_ * std::pow(Base_, index)); + } + + TBucketValue Value(ui32 index) const override { + Y_ASSERT(index < Values_.size()); + return Values_[index]; + } + + ui64 MemorySizeBytes() { + return sizeof(*this) + Values_.capacity() * sizeof(decltype(Values_)::value_type); + } + + private: + double Base_; + double Scale_; + TBucketValues Values_; + }; + + using TBucket = std::pair<TBucketBound, TBucketValue>; + + /////////////////////////////////////////////////////////////////////// + // TExplicitHistogramSnapshot + /////////////////////////////////////////////////////////////////////// + // + // Memory layout (single contiguous block): + // + // +------+-----------+--------------+--------+--------+- -+--------+--------+ + // | vptr | RefsCount | BucketsCount | Bound1 | Value1 | ... | BoundN | ValueN | + // +------+-----------+--------------+--------+--------+- -+--------+--------+ + // + class TExplicitHistogramSnapshot: public IHistogramSnapshot, private TNonCopyable { + public: + static TIntrusivePtr<TExplicitHistogramSnapshot> New(ui32 bucketsCount) { + size_t bucketsSize = bucketsCount * sizeof(TBucket); + Y_ENSURE(bucketsCount <= HISTOGRAM_MAX_BUCKETS_COUNT, "Cannot allocate a histogram with " << bucketsCount + << " buckets. Bucket count is limited to " << HISTOGRAM_MAX_BUCKETS_COUNT); + + return new(bucketsSize) TExplicitHistogramSnapshot(bucketsCount); + } + + TBucket& operator[](ui32 index) noexcept { + return Bucket(index); + } + + ui32 Count() const override { + return BucketsCount_; + } + + TBucketBound UpperBound(ui32 index) const override { + return Bucket(index).first; + } + + TBucketValue Value(ui32 index) const override { + return Bucket(index).second; + } + + ui64 MemorySizeBytes() const { + return sizeof(*this) + BucketsCount_ * sizeof(TBucket); + } + + private: + explicit TExplicitHistogramSnapshot(ui32 bucketsCount) noexcept + : BucketsCount_(bucketsCount) + { + } + + static void* operator new(size_t size, size_t bucketsSize) { + return ::operator new(size + bucketsSize); + } + + static void operator delete(void* mem) { + ::operator delete(mem); + } + + static void operator delete(void* mem, size_t, size_t) { + // this operator can be called as paired for custom new operator + ::operator delete(mem); + } + + TBucket& Bucket(ui32 index) noexcept { + Y_VERIFY_DEBUG(index < BucketsCount_); + return *(reinterpret_cast<TBucket*>(this + 1) + index); + } + + const TBucket& Bucket(ui32 index) const noexcept { + Y_VERIFY_DEBUG(index < BucketsCount_); + return *(reinterpret_cast<const TBucket*>(this + 1) + index); + } + + private: + ui32 BucketsCount_; + }; + + static_assert(alignof(TExplicitHistogramSnapshot) == alignof(TBucket), + "mismatched alingments of THistogramSnapshot and TBucket"); + IHistogramSnapshotPtr ExplicitHistogramSnapshot(TConstArrayRef<TBucketBound> bounds, TConstArrayRef<TBucketValue> values); - -} // namespace NMonitoring - -std::ostream& operator<<(std::ostream& os, const NMonitoring::IHistogramSnapshot& hist); + +} // namespace NMonitoring + +std::ostream& operator<<(std::ostream& os, const NMonitoring::IHistogramSnapshot& hist); diff --git a/library/cpp/monlib/metrics/labels.h b/library/cpp/monlib/metrics/labels.h index 63dc997c28..fb2b789667 100644 --- a/library/cpp/monlib/metrics/labels.h +++ b/library/cpp/monlib/metrics/labels.h @@ -7,8 +7,8 @@ #include <util/generic/string.h> #include <util/generic/vector.h> #include <util/stream/output.h> -#include <util/string/builder.h> -#include <util/string/strip.h> +#include <util/string/builder.h> +#include <util/string/strip.h> #include <optional> #include <type_traits> @@ -24,24 +24,24 @@ namespace NMonitoring { /////////////////////////////////////////////////////////////////////////// // TLabel /////////////////////////////////////////////////////////////////////////// - template <typename TStringBackend> + template <typename TStringBackend> class TLabelImpl: public ILabel { public: - using TStringType = TStringBackend; + using TStringType = TStringBackend; + + TLabelImpl() = default; - TLabelImpl() = default; - - inline TLabelImpl(TStringBuf name, TStringBuf value) - : Name_{name} - , Value_{value} + inline TLabelImpl(TStringBuf name, TStringBuf value) + : Name_{name} + , Value_{value} { } - inline bool operator==(const TLabelImpl& rhs) const noexcept { + inline bool operator==(const TLabelImpl& rhs) const noexcept { return Name_ == rhs.Name_ && Value_ == rhs.Value_; } - inline bool operator!=(const TLabelImpl& rhs) const noexcept { + inline bool operator!=(const TLabelImpl& rhs) const noexcept { return !(*this == rhs); } @@ -65,55 +65,55 @@ namespace NMonitoring { return MultiHash(Name_, Value_); } - TStringBackend ToString() const { - TStringBackend buf = Name_; - buf += '='; - buf += Value_; - - return buf; - } - - static TLabelImpl FromString(TStringBuf str) { - TStringBuf name, value; - Y_ENSURE(str.TrySplit('=', name, value), - "invalid label string format: '" << str << '\''); - - TStringBuf nameStripped = StripString(name); - Y_ENSURE(!nameStripped.empty(), "label name cannot be empty"); - - TStringBuf valueStripped = StripString(value); - Y_ENSURE(!valueStripped.empty(), "label value cannot be empty"); - - return {nameStripped, valueStripped}; - } - - static bool TryFromString(TStringBuf str, TLabelImpl& label) { - TStringBuf name, value; - if (!str.TrySplit('=', name, value)) { - return false; - } - - TStringBuf nameStripped = StripString(name); - if (nameStripped.empty()) { - return false; - } - - TStringBuf valueStripped = StripString(value); - if (valueStripped.empty()) { - return false; - } - - label = {nameStripped, valueStripped}; - return true; - } - + TStringBackend ToString() const { + TStringBackend buf = Name_; + buf += '='; + buf += Value_; + + return buf; + } + + static TLabelImpl FromString(TStringBuf str) { + TStringBuf name, value; + Y_ENSURE(str.TrySplit('=', name, value), + "invalid label string format: '" << str << '\''); + + TStringBuf nameStripped = StripString(name); + Y_ENSURE(!nameStripped.empty(), "label name cannot be empty"); + + TStringBuf valueStripped = StripString(value); + Y_ENSURE(!valueStripped.empty(), "label value cannot be empty"); + + return {nameStripped, valueStripped}; + } + + static bool TryFromString(TStringBuf str, TLabelImpl& label) { + TStringBuf name, value; + if (!str.TrySplit('=', name, value)) { + return false; + } + + TStringBuf nameStripped = StripString(name); + if (nameStripped.empty()) { + return false; + } + + TStringBuf valueStripped = StripString(value); + if (valueStripped.empty()) { + return false; + } + + label = {nameStripped, valueStripped}; + return true; + } + private: - TStringBackend Name_; - TStringBackend Value_; + TStringBackend Name_; + TStringBackend Value_; }; - using TLabel = TLabelImpl<TString>; - + using TLabel = TLabelImpl<TString>; + struct ILabels { struct TIterator { TIterator() = default; @@ -140,11 +140,11 @@ namespace NMonitoring { return !(*this == other); } - const ILabel* operator->() const noexcept { - Y_VERIFY_DEBUG(Labels_); - return Labels_->Get(Idx_); - } - + const ILabel* operator->() const noexcept { + Y_VERIFY_DEBUG(Labels_); + return Labels_->Get(Idx_); + } + const ILabel& operator*() const noexcept { Y_VERIFY_DEBUG(Labels_); return *Labels_->Get(Idx_); @@ -174,7 +174,7 @@ namespace NMonitoring { virtual std::optional<const ILabel*> Get(TStringBuf name) const = 0; // NB: there's no guarantee that indices are preserved after any object modification - virtual const ILabel* Get(size_t idx) const = 0; + virtual const ILabel* Get(size_t idx) const = 0; TIterator begin() const { return TIterator{this}; @@ -191,7 +191,7 @@ namespace NMonitoring { /////////////////////////////////////////////////////////////////////////// // TLabels /////////////////////////////////////////////////////////////////////////// - template <typename TStringBackend> + template <typename TStringBackend> class TLabelsImpl: public ILabels { public: using value_type = TLabelImpl<TStringBackend>; @@ -220,50 +220,50 @@ namespace NMonitoring { TLabelsImpl(TLabelsImpl&&) noexcept = default; TLabelsImpl& operator=(TLabelsImpl&&) noexcept = default; - inline bool operator==(const TLabelsImpl& rhs) const { + inline bool operator==(const TLabelsImpl& rhs) const { return Labels_ == rhs.Labels_; } - inline bool operator!=(const TLabelsImpl& rhs) const { + inline bool operator!=(const TLabelsImpl& rhs) const { return Labels_ != rhs.Labels_; } bool Add(TStringBuf name, TStringBuf value) noexcept override { - if (Has(name)) { - return false; - } - + if (Has(name)) { + return false; + } + Labels_.emplace_back(name, value); - return true; - } - + return true; + } + using ILabels::Add; - + bool Has(TStringBuf name) const noexcept override { auto it = FindIf(Labels_, [name](const TLabelImpl<TStringBackend>& label) { - return name == TStringBuf{label.Name()}; - }); + return name == TStringBuf{label.Name()}; + }); return it != Labels_.end(); - } - - bool Has(const TString& name) const noexcept { + } + + bool Has(const TString& name) const noexcept { auto it = FindIf(Labels_, [name](const TLabelImpl<TStringBackend>& label) { - return name == TStringBuf{label.Name()}; - }); + return name == TStringBuf{label.Name()}; + }); return it != Labels_.end(); - } - + } + // XXX for backward compatibility - TMaybe<TLabelImpl<TStringBackend>> Find(TStringBuf name) const { + TMaybe<TLabelImpl<TStringBackend>> Find(TStringBuf name) const { auto it = FindIf(Labels_, [name](const TLabelImpl<TStringBackend>& label) { - return name == TStringBuf{label.Name()}; - }); + return name == TStringBuf{label.Name()}; + }); if (it == Labels_.end()) { - return Nothing(); - } - return *it; - } - + return Nothing(); + } + return *it; + } + std::optional<const ILabel*> Get(TStringBuf name) const override { auto it = FindIf(Labels_, [name] (auto&& l) { return name == l.Name(); @@ -280,18 +280,18 @@ namespace NMonitoring { return &(*this)[idx]; } - TMaybe<TLabelImpl<TStringBackend>> Extract(TStringBuf name) { + TMaybe<TLabelImpl<TStringBackend>> Extract(TStringBuf name) { auto it = FindIf(Labels_, [name](const TLabelImpl<TStringBackend>& label) { - return name == TStringBuf{label.Name()}; - }); + return name == TStringBuf{label.Name()}; + }); if (it == Labels_.end()) { - return Nothing(); - } - TLabel tmp = *it; + return Nothing(); + } + TLabel tmp = *it; Labels_.erase(it); - return tmp; - } - + return tmp; + } + void SortByName() { std::sort(Labels_.begin(), Labels_.end(), [](const auto& lhs, const auto& rhs) { return lhs.Name() < rhs.Name(); @@ -380,20 +380,20 @@ namespace NMonitoring { using iterator = ILabels::TIterator; using const_iterator = iterator; - protected: + protected: TVector<TLabelImpl<TStringBackend>>& AsVector() { return Labels_; } - + const TVector<TLabelImpl<TStringBackend>>& AsVector() const { return Labels_; - } + } private: TVector<TLabelImpl<TStringBackend>> Labels_; }; - - using TLabels = TLabelsImpl<TString>; + + using TLabels = TLabelsImpl<TString>; using ILabelsPtr = THolder<ILabels>; template <typename T> @@ -424,13 +424,13 @@ struct THash<NMonitoring::ILabelsPtr> { template<typename TStringBackend> struct THash<NMonitoring::TLabelsImpl<TStringBackend>> { - size_t operator()(const NMonitoring::TLabelsImpl<TStringBackend>& labels) const noexcept { + size_t operator()(const NMonitoring::TLabelsImpl<TStringBackend>& labels) const noexcept { return labels.Hash(); } }; -template <typename TStringBackend> -struct THash<NMonitoring::TLabelImpl<TStringBackend>> { +template <typename TStringBackend> +struct THash<NMonitoring::TLabelImpl<TStringBackend>> { inline size_t operator()(const NMonitoring::TLabelImpl<TStringBackend>& label) const noexcept { return label.Hash(); } @@ -469,15 +469,15 @@ struct TEqualTo<NMonitoring::ILabelsPtr> { return lhs == *rhs; } }; - + #define Y_MONLIB_DEFINE_LABELS_OUT(T) \ -template <> \ +template <> \ void Out<T>(IOutputStream& out, const T& labels) { \ Out<NMonitoring::ILabels>(out, labels); \ -} - +} + #define Y_MONLIB_DEFINE_LABEL_OUT(T) \ -template <> \ +template <> \ void Out<T>(IOutputStream& out, const T& label) { \ Out<NMonitoring::ILabel>(out, label); \ -} +} diff --git a/library/cpp/monlib/metrics/log_histogram_snapshot.cpp b/library/cpp/monlib/metrics/log_histogram_snapshot.cpp index 21cf2ca2bb..ee2f99973c 100644 --- a/library/cpp/monlib/metrics/log_histogram_snapshot.cpp +++ b/library/cpp/monlib/metrics/log_histogram_snapshot.cpp @@ -2,16 +2,16 @@ #include <util/stream/output.h> -#include <iostream> +#include <iostream> - -namespace { - -template <typename TStream> -auto& Output(TStream& o, const NMonitoring::TLogHistogramSnapshot& hist) { + +namespace { + +template <typename TStream> +auto& Output(TStream& o, const NMonitoring::TLogHistogramSnapshot& hist) { o << TStringBuf("{"); - - for (auto i = 0u; i < hist.Count(); ++i) { + + for (auto i = 0u; i < hist.Count(); ++i) { o << hist.UpperBound(i) << TStringBuf(": ") << hist.Bucket(i); o << TStringBuf(", "); } @@ -19,17 +19,17 @@ auto& Output(TStream& o, const NMonitoring::TLogHistogramSnapshot& hist) { o << TStringBuf("zeros: ") << hist.ZerosCount(); o << TStringBuf("}"); - - return o; -} - -} // namespace - -std::ostream& operator<<(std::ostream& os, const NMonitoring::TLogHistogramSnapshot& hist) { - return Output(os, hist); -} - -template <> -void Out<NMonitoring::TLogHistogramSnapshot>(IOutputStream& os, const NMonitoring::TLogHistogramSnapshot& hist) { - Output(os, hist); + + return o; } + +} // namespace + +std::ostream& operator<<(std::ostream& os, const NMonitoring::TLogHistogramSnapshot& hist) { + return Output(os, hist); +} + +template <> +void Out<NMonitoring::TLogHistogramSnapshot>(IOutputStream& os, const NMonitoring::TLogHistogramSnapshot& hist) { + Output(os, hist); +} diff --git a/library/cpp/monlib/metrics/log_histogram_snapshot.h b/library/cpp/monlib/metrics/log_histogram_snapshot.h index 7673b43751..811eed6109 100644 --- a/library/cpp/monlib/metrics/log_histogram_snapshot.h +++ b/library/cpp/monlib/metrics/log_histogram_snapshot.h @@ -67,5 +67,5 @@ namespace NMonitoring { using TLogHistogramSnapshotPtr = TIntrusivePtr<TLogHistogramSnapshot>; } - -std::ostream& operator<<(std::ostream& os, const NMonitoring::TLogHistogramSnapshot& hist); + +std::ostream& operator<<(std::ostream& os, const NMonitoring::TLogHistogramSnapshot& hist); diff --git a/library/cpp/monlib/metrics/metric.h b/library/cpp/monlib/metrics/metric.h index b8ce12d753..d91dbac9a2 100644 --- a/library/cpp/monlib/metrics/metric.h +++ b/library/cpp/monlib/metrics/metric.h @@ -152,14 +152,14 @@ namespace NMonitoring { double Add(double n) noexcept override { double newValue; double oldValue = Get(); - - do { + + do { newValue = oldValue + n; } while (!Value_.compare_exchange_weak(oldValue, newValue, std::memory_order_release, std::memory_order_consume)); - + return newValue; - } - + } + void Set(double n) noexcept override { Value_.store(n, std::memory_order_relaxed); } @@ -209,8 +209,8 @@ namespace NMonitoring { i64 Add(i64 n) noexcept override { return Value_.fetch_add(n, std::memory_order_relaxed) + n; - } - + } + void Set(i64 value) noexcept override { Value_.store(value, std::memory_order_relaxed); } diff --git a/library/cpp/monlib/metrics/metric_registry.cpp b/library/cpp/monlib/metrics/metric_registry.cpp index b083163a7b..683ee4ea5b 100644 --- a/library/cpp/monlib/metrics/metric_registry.cpp +++ b/library/cpp/monlib/metrics/metric_registry.cpp @@ -190,8 +190,8 @@ namespace NMonitoring { void TMetricRegistry::RemoveMetric(const ILabels& labels) noexcept { TWriteGuard g{Lock_}; Metrics_.erase(labels); - } - + } + void TMetricRegistry::Accept(TInstant time, IMetricConsumer* consumer) const { consumer->OnStreamBegin(); diff --git a/library/cpp/monlib/metrics/metric_registry.h b/library/cpp/monlib/metrics/metric_registry.h index 670cf8651e..cc02a16f05 100644 --- a/library/cpp/monlib/metrics/metric_registry.h +++ b/library/cpp/monlib/metrics/metric_registry.h @@ -96,7 +96,7 @@ namespace NMonitoring { } void RemoveMetric(const ILabels& labels) noexcept override; - + private: TGauge* Gauge(ILabelsPtr labels) override; TLazyGauge* LazyGauge(ILabelsPtr labels, std::function<double()> supplier) override; diff --git a/library/cpp/monlib/metrics/metric_registry_ut.cpp b/library/cpp/monlib/metrics/metric_registry_ut.cpp index 86d9a52ec0..58512ccace 100644 --- a/library/cpp/monlib/metrics/metric_registry_ut.cpp +++ b/library/cpp/monlib/metrics/metric_registry_ut.cpp @@ -45,16 +45,16 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) { UNIT_ASSERT_DOUBLES_EQUAL(g->Get(), 0.0, 1E-6); g->Set(12.34); UNIT_ASSERT_DOUBLES_EQUAL(g->Get(), 12.34, 1E-6); - - double val; - - val = g->Add(1.2); - UNIT_ASSERT_DOUBLES_EQUAL(g->Get(), 13.54, 1E-6); - UNIT_ASSERT_DOUBLES_EQUAL(g->Get(), val, 1E-6); - - val = g->Add(-3.47); - UNIT_ASSERT_DOUBLES_EQUAL(g->Get(), 10.07, 1E-6); - UNIT_ASSERT_DOUBLES_EQUAL(g->Get(), val, 1E-6); + + double val; + + val = g->Add(1.2); + UNIT_ASSERT_DOUBLES_EQUAL(g->Get(), 13.54, 1E-6); + UNIT_ASSERT_DOUBLES_EQUAL(g->Get(), val, 1E-6); + + val = g->Add(-3.47); + UNIT_ASSERT_DOUBLES_EQUAL(g->Get(), 10.07, 1E-6); + UNIT_ASSERT_DOUBLES_EQUAL(g->Get(), val, 1E-6); } Y_UNIT_TEST(LazyGauge) { @@ -75,35 +75,35 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) { UNIT_ASSERT_DOUBLES_EQUAL(g->Get(), val, 1E-6); } - Y_UNIT_TEST(IntGauge) { + Y_UNIT_TEST(IntGauge) { TMetricRegistry registry(TLabels{{"common", "label"}}); - TIntGauge* g = registry.IntGauge({{"my", "gauge"}}); - + TIntGauge* g = registry.IntGauge({{"my", "gauge"}}); + UNIT_ASSERT_VALUES_EQUAL(g->Get(), 0); - - i64 val; - - val = g->Inc(); + + i64 val; + + val = g->Inc(); UNIT_ASSERT_VALUES_EQUAL(g->Get(), 1); UNIT_ASSERT_VALUES_EQUAL(g->Get(), val); - - val = g->Dec(); + + val = g->Dec(); UNIT_ASSERT_VALUES_EQUAL(g->Get(), 0); UNIT_ASSERT_VALUES_EQUAL(g->Get(), val); - - val = g->Add(1); + + val = g->Add(1); UNIT_ASSERT_VALUES_EQUAL(g->Get(), 1); UNIT_ASSERT_VALUES_EQUAL(g->Get(), val); - - val = g->Add(2); + + val = g->Add(2); UNIT_ASSERT_VALUES_EQUAL(g->Get(), 3); UNIT_ASSERT_VALUES_EQUAL(g->Get(), val); - - val = g->Add(-5); + + val = g->Add(-5); UNIT_ASSERT_VALUES_EQUAL(g->Get(), -2); UNIT_ASSERT_VALUES_EQUAL(g->Get(), val); - } - + } + Y_UNIT_TEST(LazyIntGauge) { TMetricRegistry registry(TLabels{{"common", "label"}}); i64 val = 0; diff --git a/library/cpp/monlib/metrics/metric_value.cpp b/library/cpp/monlib/metrics/metric_value.cpp index b95d7011c6..12a003e5ea 100644 --- a/library/cpp/monlib/metrics/metric_value.cpp +++ b/library/cpp/monlib/metrics/metric_value.cpp @@ -3,7 +3,7 @@ namespace NMonitoring { void TMetricTimeSeries::SortByTs() { - SortPointsByTs(ValueType_, Points_); + SortPointsByTs(ValueType_, Points_); } void TMetricTimeSeries::Clear() noexcept { diff --git a/library/cpp/monlib/metrics/metric_value.h b/library/cpp/monlib/metrics/metric_value.h index 607fcc8602..912d9abb6e 100644 --- a/library/cpp/monlib/metrics/metric_value.h +++ b/library/cpp/monlib/metrics/metric_value.h @@ -19,10 +19,10 @@ namespace NMonitoring { Y_ENSURE(::IsValidFloat(d) && d >= Min<T>() && d <= MaxFloor<T>(), "Cannot convert " << d << " to an integer value"); return static_cast<T>(d); } - - inline auto POINT_KEY_FN = [](auto& p) { - return p.GetTime(); - }; + + inline auto POINT_KEY_FN = [](auto& p) { + return p.GetTime(); + }; } // namespace NPrivate template <typename T, typename Enable = void> @@ -62,11 +62,11 @@ namespace NMonitoring { // TMetricValue /////////////////////////////////////////////////////////////////////////// // TMetricValue represents a generic value. It does not contain type - // information about a value. This is done to minimize object footprint. - // To read an actual value from the object the type must be checked - // first or provided to AsXxxx(type) member-functions. - // This class does not hold an ownership of an IHistogramSnapshot or - // SummarySnapshot, so this must be done somewhere outside. + // information about a value. This is done to minimize object footprint. + // To read an actual value from the object the type must be checked + // first or provided to AsXxxx(type) member-functions. + // This class does not hold an ownership of an IHistogramSnapshot or + // SummarySnapshot, so this must be done somewhere outside. class TMetricValue { public: TMetricValue() noexcept { @@ -181,24 +181,24 @@ namespace NMonitoring { IHistogramSnapshot* AsHistogram(EMetricValueType type) const { if (type != EMetricValueType::HISTOGRAM) { - ythrow yexception() << type << " cannot be casted to Histogram"; - } - - return Value_.Histogram; - } - + ythrow yexception() << type << " cannot be casted to Histogram"; + } + + return Value_.Histogram; + } + ISummaryDoubleSnapshot* AsSummaryDouble() const noexcept { return Value_.Summary; } ISummaryDoubleSnapshot* AsSummaryDouble(EMetricValueType type) const { if (type != EMetricValueType::SUMMARY) { - ythrow yexception() << type << " cannot be casted to SummaryDouble"; - } - - return Value_.Summary; - } - + ythrow yexception() << type << " cannot be casted to SummaryDouble"; + } + + return Value_.Summary; + } + TLogHistogramSnapshot* AsLogHistogram() const noexcept { return Value_.LogHistogram; } @@ -222,103 +222,103 @@ namespace NMonitoring { } Value_; }; - /////////////////////////////////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////////// // TMetricValueWithType - /////////////////////////////////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////////// // Same as TMetricValue, but this type holds an ownership of - // snapshots and contains value type information. + // snapshots and contains value type information. class TMetricValueWithType: private TMetricValue, public TMoveOnly { - public: + public: using TBase = TMetricValue; - - template <typename T> + + template <typename T> explicit TMetricValueWithType(T value) - : TBase(value) - , ValueType_{TValueType<T>::Type} - { - Ref(); - } - + : TBase(value) + , ValueType_{TValueType<T>::Type} + { + Ref(); + } + TMetricValueWithType(TMetricValueWithType&& other) - : TBase(std::move(other)) - , ValueType_{other.ValueType_} - { - Ref(); - other.Clear(); - } - + : TBase(std::move(other)) + , ValueType_{other.ValueType_} + { + Ref(); + other.Clear(); + } + TMetricValueWithType& operator=(TMetricValueWithType&& other) { - TBase::operator=(other); - ValueType_ = other.ValueType_; - - Ref(); - other.Clear(); - - return *this; - } - + TBase::operator=(other); + ValueType_ = other.ValueType_; + + Ref(); + other.Clear(); + + return *this; + } + ~TMetricValueWithType() { - UnRef(); - } - - void Clear() { - UnRef(); + UnRef(); + } + + void Clear() { + UnRef(); ValueType_ = EMetricValueType::UNKNOWN; - } - + } + EMetricValueType GetType() const noexcept { - return ValueType_; - } - - double AsDouble() const { - return TBase::AsDouble(ValueType_); - } - - ui64 AsUint64() const { - return TBase::AsUint64(ValueType_); - } - - i64 AsInt64() const { - return TBase::AsInt64(ValueType_); - } - - IHistogramSnapshot* AsHistogram() const { - return TBase::AsHistogram(ValueType_); - } - - ISummaryDoubleSnapshot* AsSummaryDouble() const { - return TBase::AsSummaryDouble(ValueType_); - } - + return ValueType_; + } + + double AsDouble() const { + return TBase::AsDouble(ValueType_); + } + + ui64 AsUint64() const { + return TBase::AsUint64(ValueType_); + } + + i64 AsInt64() const { + return TBase::AsInt64(ValueType_); + } + + IHistogramSnapshot* AsHistogram() const { + return TBase::AsHistogram(ValueType_); + } + + ISummaryDoubleSnapshot* AsSummaryDouble() const { + return TBase::AsSummaryDouble(ValueType_); + } + TLogHistogramSnapshot* AsLogHistogram() const { return TBase::AsLogHistogram(ValueType_); } - private: - void Ref() { + private: + void Ref() { if (ValueType_ == EMetricValueType::SUMMARY) { - TBase::AsSummaryDouble()->Ref(); + TBase::AsSummaryDouble()->Ref(); } else if (ValueType_ == EMetricValueType::HISTOGRAM) { - TBase::AsHistogram()->Ref(); + TBase::AsHistogram()->Ref(); } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { TBase::AsLogHistogram()->Ref(); - } - } - - void UnRef() { + } + } + + void UnRef() { if (ValueType_ == EMetricValueType::SUMMARY) { - TBase::AsSummaryDouble()->UnRef(); + TBase::AsSummaryDouble()->UnRef(); } else if (ValueType_ == EMetricValueType::HISTOGRAM) { - TBase::AsHistogram()->UnRef(); + TBase::AsHistogram()->UnRef(); } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { TBase::AsLogHistogram()->UnRef(); - } - } - - private: + } + } + + private: EMetricValueType ValueType_ = EMetricValueType::UNKNOWN; - }; - + }; + static_assert(sizeof(TMetricValue) == sizeof(ui64), "expected size of TMetricValue is one machine word"); @@ -451,10 +451,10 @@ namespace NMonitoring { return Points_.size(); } - size_t Capacity() const noexcept { - return Points_.capacity(); - } - + size_t Capacity() const noexcept { + return Points_.capacity(); + } + const TPoint& operator[](size_t index) const noexcept { return Points_[index]; } @@ -518,18 +518,18 @@ namespace NMonitoring { template <typename TPoint> void SortPointsByTs(EMetricValueType valueType, TVector<TPoint>& points) { - if (points.size() < 2) { - return; - } - + if (points.size() < 2) { + return; + } + if (valueType != EMetricValueType::HISTOGRAM && valueType != EMetricValueType::SUMMARY && valueType != EMetricValueType::LOGHISTOGRAM) { - // Stable sort + saving only the last point inside a group of duplicates - StableSortBy(points, NPrivate::POINT_KEY_FN); - auto it = UniqueBy(points.rbegin(), points.rend(), NPrivate::POINT_KEY_FN); - points.erase(points.begin(), it.base()); - } else { - StableSortBy(points, NPrivate::POINT_KEY_FN); + // Stable sort + saving only the last point inside a group of duplicates + StableSortBy(points, NPrivate::POINT_KEY_FN); + auto it = UniqueBy(points.rbegin(), points.rend(), NPrivate::POINT_KEY_FN); + points.erase(points.begin(), it.base()); + } else { + StableSortBy(points, NPrivate::POINT_KEY_FN); if (valueType == EMetricValueType::HISTOGRAM) { EraseDuplicates<EMetricValueType::HISTOGRAM>(points); } else if (valueType == EMetricValueType::LOGHISTOGRAM) { @@ -537,6 +537,6 @@ namespace NMonitoring { } else { EraseDuplicates<EMetricValueType::SUMMARY>(points); } - } - } + } + } } diff --git a/library/cpp/monlib/metrics/metric_value_type.h b/library/cpp/monlib/metrics/metric_value_type.h index ab30a958c2..c389f058ad 100644 --- a/library/cpp/monlib/metrics/metric_value_type.h +++ b/library/cpp/monlib/metrics/metric_value_type.h @@ -1,16 +1,16 @@ -#pragma once - - -namespace NMonitoring { - +#pragma once + + +namespace NMonitoring { + enum class EMetricValueType { - UNKNOWN, - DOUBLE, - INT64, - UINT64, - HISTOGRAM, - SUMMARY, + UNKNOWN, + DOUBLE, + INT64, + UINT64, + HISTOGRAM, + SUMMARY, LOGHISTOGRAM, -}; - -} // namespace NMonitoring +}; + +} // namespace NMonitoring diff --git a/library/cpp/monlib/metrics/metric_value_ut.cpp b/library/cpp/monlib/metrics/metric_value_ut.cpp index 49b47c4057..e800ffb97f 100644 --- a/library/cpp/monlib/metrics/metric_value_ut.cpp +++ b/library/cpp/monlib/metrics/metric_value_ut.cpp @@ -7,14 +7,14 @@ using namespace NMonitoring; Y_UNIT_TEST_SUITE(TMetricValueTest) { class TTestHistogram: public IHistogramSnapshot { - public: - TTestHistogram(ui32 count = 1) - : Count_{count} - {} - - private: + public: + TTestHistogram(ui32 count = 1) + : Count_{count} + {} + + private: ui32 Count() const override { - return Count_; + return Count_; } TBucketBound UpperBound(ui32 /*index*/) const override { @@ -24,14 +24,14 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { TBucketValue Value(ui32 /*index*/) const override { return 42; } - - ui32 Count_{0}; + + ui32 Count_{0}; }; - IHistogramSnapshotPtr MakeHistogramSnapshot() { - return MakeIntrusive<TTestHistogram>(); - } - + IHistogramSnapshotPtr MakeHistogramSnapshot() { + return MakeIntrusive<TTestHistogram>(); + } + ISummaryDoubleSnapshotPtr MakeSummarySnapshot(ui64 count = 0u) { return MakeIntrusive<TSummaryDoubleSnapshot>(0.0, 0.0, 0.0, 0.0, count); } @@ -59,7 +59,7 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { UNIT_ASSERT_EQUAL(timeSeries.Size(), 2); UNIT_ASSERT_EQUAL(ts1, timeSeries[0].GetTime()); - UNIT_ASSERT_DOUBLES_EQUAL(6.28318, timeSeries[0].GetValue().AsDouble(), Min<double>()); + UNIT_ASSERT_DOUBLES_EQUAL(6.28318, timeSeries[0].GetValue().AsDouble(), Min<double>()); UNIT_ASSERT_EQUAL(ts2, timeSeries[1].GetTime()); UNIT_ASSERT_DOUBLES_EQUAL(2.71828, timeSeries[1].GetValue().AsDouble(), Min<double>()); @@ -269,35 +269,35 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { auto ts4 = ts3 + TDuration::Seconds(1); auto ts5 = ts4 + TDuration::Seconds(1); - auto h1 = MakeIntrusive<TTestHistogram>(1u); - auto h2 = MakeIntrusive<TTestHistogram>(2u); - auto h3 = MakeIntrusive<TTestHistogram>(3u); - auto h4 = MakeIntrusive<TTestHistogram>(4u); - auto h5 = MakeIntrusive<TTestHistogram>(5u); - auto h6 = MakeIntrusive<TTestHistogram>(6u); - auto h7 = MakeIntrusive<TTestHistogram>(7u); + auto h1 = MakeIntrusive<TTestHistogram>(1u); + auto h2 = MakeIntrusive<TTestHistogram>(2u); + auto h3 = MakeIntrusive<TTestHistogram>(3u); + auto h4 = MakeIntrusive<TTestHistogram>(4u); + auto h5 = MakeIntrusive<TTestHistogram>(5u); + auto h6 = MakeIntrusive<TTestHistogram>(6u); + auto h7 = MakeIntrusive<TTestHistogram>(7u); { TMetricTimeSeries timeSeries; timeSeries.Add(ts1, h1.Get()); timeSeries.Add(ts1, h2.Get()); - - timeSeries.Add(ts2, h3.Get()); - - timeSeries.Add(ts3, h4.Get()); + + timeSeries.Add(ts2, h3.Get()); + + timeSeries.Add(ts3, h4.Get()); timeSeries.Add(ts3, h5.Get()); - + timeSeries.Add(ts4, h6.Get()); timeSeries.Add(ts5, h7.Get()); timeSeries.SortByTs(); UNIT_ASSERT_EQUAL(timeSeries.Size(), 5); - UNIT_ASSERT_EQUAL(timeSeries[0].GetValue().AsHistogram()->Count(), 2); - UNIT_ASSERT_EQUAL(timeSeries[1].GetValue().AsHistogram()->Count(), 3); - UNIT_ASSERT_EQUAL(timeSeries[2].GetValue().AsHistogram()->Count(), 5); - UNIT_ASSERT_EQUAL(timeSeries[3].GetValue().AsHistogram()->Count(), 6); - UNIT_ASSERT_EQUAL(timeSeries[4].GetValue().AsHistogram()->Count(), 7); + UNIT_ASSERT_EQUAL(timeSeries[0].GetValue().AsHistogram()->Count(), 2); + UNIT_ASSERT_EQUAL(timeSeries[1].GetValue().AsHistogram()->Count(), 3); + UNIT_ASSERT_EQUAL(timeSeries[2].GetValue().AsHistogram()->Count(), 5); + UNIT_ASSERT_EQUAL(timeSeries[3].GetValue().AsHistogram()->Count(), 6); + UNIT_ASSERT_EQUAL(timeSeries[4].GetValue().AsHistogram()->Count(), 7); } } @@ -378,130 +378,130 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { UNIT_ASSERT_EQUAL(timeSeries[4].GetValue().AsSummaryDouble()->GetCount(), 7); } } - + Y_UNIT_TEST(TMetricValueWithType) { - // correct usage - { - double value = 1.23; + // correct usage + { + double value = 1.23; TMetricValueWithType v{value}; - + UNIT_ASSERT_VALUES_EQUAL(v.GetType(), EMetricValueType::DOUBLE); - UNIT_ASSERT_VALUES_EQUAL(v.AsDouble(), value); - } - { - ui64 value = 12; + UNIT_ASSERT_VALUES_EQUAL(v.AsDouble(), value); + } + { + ui64 value = 12; TMetricValueWithType v{value}; - + UNIT_ASSERT_VALUES_EQUAL(v.GetType(), EMetricValueType::UINT64); - UNIT_ASSERT_VALUES_EQUAL(v.AsUint64(), value); - } - { - i64 value = i64(-12); + UNIT_ASSERT_VALUES_EQUAL(v.AsUint64(), value); + } + { + i64 value = i64(-12); TMetricValueWithType v{value}; - + UNIT_ASSERT_VALUES_EQUAL(v.GetType(), EMetricValueType::INT64); - UNIT_ASSERT_VALUES_EQUAL(v.AsInt64(), value); - } - { - auto h = MakeHistogramSnapshot(); - UNIT_ASSERT_VALUES_EQUAL(h.RefCount(), 1); - - { - auto value = h.Get(); + UNIT_ASSERT_VALUES_EQUAL(v.AsInt64(), value); + } + { + auto h = MakeHistogramSnapshot(); + UNIT_ASSERT_VALUES_EQUAL(h.RefCount(), 1); + + { + auto value = h.Get(); TMetricValueWithType v{value}; - - UNIT_ASSERT_VALUES_EQUAL(h.RefCount(), 2); - + + UNIT_ASSERT_VALUES_EQUAL(h.RefCount(), 2); + UNIT_ASSERT_VALUES_EQUAL(v.GetType(), EMetricValueType::HISTOGRAM); - UNIT_ASSERT_VALUES_EQUAL(v.AsHistogram(), value); - } - - UNIT_ASSERT_VALUES_EQUAL(h.RefCount(), 1); - } - { - auto s = MakeSummarySnapshot(); - auto value = s.Get(); - - UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 1); - - { + UNIT_ASSERT_VALUES_EQUAL(v.AsHistogram(), value); + } + + UNIT_ASSERT_VALUES_EQUAL(h.RefCount(), 1); + } + { + auto s = MakeSummarySnapshot(); + auto value = s.Get(); + + UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 1); + + { TMetricValueWithType v{value}; - - UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 2); - + + UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 2); + UNIT_ASSERT_VALUES_EQUAL(v.GetType(), EMetricValueType::SUMMARY); - UNIT_ASSERT_VALUES_EQUAL(v.AsSummaryDouble(), value); - } - - UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 1); - } - { - auto s = MakeSummarySnapshot(); - auto value = s.Get(); - - UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 1); - - { + UNIT_ASSERT_VALUES_EQUAL(v.AsSummaryDouble(), value); + } + + UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 1); + } + { + auto s = MakeSummarySnapshot(); + auto value = s.Get(); + + UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 1); + + { TMetricValueWithType v{value}; - - UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 2); - - v.Clear(); - UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 1); - } - - UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 1); - } - { - auto s = MakeSummarySnapshot(); - auto value = s.Get(); - - { + + UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 2); + + v.Clear(); + UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 1); + } + + UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 1); + } + { + auto s = MakeSummarySnapshot(); + auto value = s.Get(); + + { TMetricValueWithType v1{ui64{1}}; - - UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 1); - - { + + UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 1); + + { TMetricValueWithType v2{value}; - UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 2); - - v1 = std::move(v2); - UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 2); - UNIT_ASSERT_VALUES_EQUAL(v1.AsSummaryDouble(), value); + UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 2); + + v1 = std::move(v2); + UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 2); + UNIT_ASSERT_VALUES_EQUAL(v1.AsSummaryDouble(), value); UNIT_ASSERT_VALUES_EQUAL(v1.GetType(), EMetricValueType::SUMMARY); UNIT_ASSERT_VALUES_EQUAL(v2.GetType(), EMetricValueType::UNKNOWN); - } - - UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 2); - } - - UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 1); - } - - // incorrect usage - { + } + + UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 2); + } + + UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 1); + } + + // incorrect usage + { TMetricValueWithType v{1.23}; - - UNIT_ASSERT_EXCEPTION(v.AsHistogram(), yexception); - UNIT_ASSERT_EXCEPTION(v.AsSummaryDouble(), yexception); - } - { - auto h = MakeHistogramSnapshot(); + + UNIT_ASSERT_EXCEPTION(v.AsHistogram(), yexception); + UNIT_ASSERT_EXCEPTION(v.AsSummaryDouble(), yexception); + } + { + auto h = MakeHistogramSnapshot(); TMetricValueWithType v{h.Get()}; - - UNIT_ASSERT_EXCEPTION(v.AsUint64(), yexception); - UNIT_ASSERT_EXCEPTION(v.AsInt64(), yexception); - UNIT_ASSERT_EXCEPTION(v.AsDouble(), yexception); - UNIT_ASSERT_EXCEPTION(v.AsSummaryDouble(), yexception); - } - { - auto s = MakeSummarySnapshot(); + + UNIT_ASSERT_EXCEPTION(v.AsUint64(), yexception); + UNIT_ASSERT_EXCEPTION(v.AsInt64(), yexception); + UNIT_ASSERT_EXCEPTION(v.AsDouble(), yexception); + UNIT_ASSERT_EXCEPTION(v.AsSummaryDouble(), yexception); + } + { + auto s = MakeSummarySnapshot(); TMetricValueWithType v{s.Get()}; - - UNIT_ASSERT_EXCEPTION(v.AsUint64(), yexception); - UNIT_ASSERT_EXCEPTION(v.AsInt64(), yexception); - UNIT_ASSERT_EXCEPTION(v.AsDouble(), yexception); - UNIT_ASSERT_EXCEPTION(v.AsHistogram(), yexception); - } - } + + UNIT_ASSERT_EXCEPTION(v.AsUint64(), yexception); + UNIT_ASSERT_EXCEPTION(v.AsInt64(), yexception); + UNIT_ASSERT_EXCEPTION(v.AsDouble(), yexception); + UNIT_ASSERT_EXCEPTION(v.AsHistogram(), yexception); + } + } } diff --git a/library/cpp/monlib/metrics/summary_snapshot.cpp b/library/cpp/monlib/metrics/summary_snapshot.cpp index 0b13263337..24ab9a951e 100644 --- a/library/cpp/monlib/metrics/summary_snapshot.cpp +++ b/library/cpp/monlib/metrics/summary_snapshot.cpp @@ -2,15 +2,15 @@ #include <util/stream/output.h> -#include <iostream> +#include <iostream> - -namespace { - -template <typename TStream> -auto& Output(TStream& o, const NMonitoring::ISummaryDoubleSnapshot& s) { + +namespace { + +template <typename TStream> +auto& Output(TStream& o, const NMonitoring::ISummaryDoubleSnapshot& s) { o << TStringBuf("{"); - + o << TStringBuf("sum: ") << s.GetSum() << TStringBuf(", "); o << TStringBuf("min: ") << s.GetMin() << TStringBuf(", "); o << TStringBuf("max: ") << s.GetMax() << TStringBuf(", "); @@ -18,17 +18,17 @@ auto& Output(TStream& o, const NMonitoring::ISummaryDoubleSnapshot& s) { o << TStringBuf("count: ") << s.GetCount(); o << TStringBuf("}"); - - return o; -} - -} // namespace - -std::ostream& operator<<(std::ostream& o, const NMonitoring::ISummaryDoubleSnapshot& s) { - return Output(o, s); -} - -template <> -void Out<NMonitoring::ISummaryDoubleSnapshot>(IOutputStream& o, const NMonitoring::ISummaryDoubleSnapshot& s) { - Output(o, s); + + return o; } + +} // namespace + +std::ostream& operator<<(std::ostream& o, const NMonitoring::ISummaryDoubleSnapshot& s) { + return Output(o, s); +} + +template <> +void Out<NMonitoring::ISummaryDoubleSnapshot>(IOutputStream& o, const NMonitoring::ISummaryDoubleSnapshot& s) { + Output(o, s); +} diff --git a/library/cpp/monlib/metrics/summary_snapshot.h b/library/cpp/monlib/metrics/summary_snapshot.h index afcc895fd3..f35bf79968 100644 --- a/library/cpp/monlib/metrics/summary_snapshot.h +++ b/library/cpp/monlib/metrics/summary_snapshot.h @@ -68,5 +68,5 @@ namespace NMonitoring { }; } - -std::ostream& operator<<(std::ostream& os, const NMonitoring::ISummaryDoubleSnapshot& s); + +std::ostream& operator<<(std::ostream& os, const NMonitoring::ISummaryDoubleSnapshot& s); diff --git a/library/cpp/monlib/metrics/ya.make b/library/cpp/monlib/metrics/ya.make index 0e1fa143f9..ecc804c1ca 100644 --- a/library/cpp/monlib/metrics/ya.make +++ b/library/cpp/monlib/metrics/ya.make @@ -6,7 +6,7 @@ OWNER( ) GENERATE_ENUM_SERIALIZATION_WITH_HEADER(metric_value_type.h) - + SRCS( ewma.cpp fake.cpp diff --git a/library/cpp/monlib/service/monservice.cpp b/library/cpp/monlib/service/monservice.cpp index d1b9cda1d2..212f3a21c4 100644 --- a/library/cpp/monlib/service/monservice.cpp +++ b/library/cpp/monlib/service/monservice.cpp @@ -5,7 +5,7 @@ #include <library/cpp/svnversion/svnversion.h> #include <util/generic/map.h> -#include <util/generic/ptr.h> +#include <util/generic/ptr.h> #include <util/system/hostname.h> #include <google/protobuf/text_format.h> @@ -29,16 +29,16 @@ TMonService2::TMonService2(const THttpServerOptions& options, const TString& tit } TMonService2::TMonService2(const THttpServerOptions& options, TSimpleSharedPtr<IThreadPool> pool, const TString& title, THolder<IAuthProvider> auth) - : NMonitoring::TMtHttpServer(options, std::bind(&TMonService2::ServeRequest, this, std::placeholders::_1, std::placeholders::_2), std::move(pool)) - , Title(title) - , IndexMonPage(new TIndexMonPage("", Title)) + : NMonitoring::TMtHttpServer(options, std::bind(&TMonService2::ServeRequest, this, std::placeholders::_1, std::placeholders::_2), std::move(pool)) + , Title(title) + , IndexMonPage(new TIndexMonPage("", Title)) , AuthProvider_{std::move(auth)} -{ - Y_VERIFY(!!title); - time_t t = time(nullptr); - ctime_r(&t, StartTime); -} - +{ + Y_VERIFY(!!title); + time_t t = time(nullptr); + ctime_r(&t, StartTime); +} + TMonService2::TMonService2(ui16 port, ui32 threads, const TString& title, THolder<IAuthProvider> auth) : TMonService2(port, TString(), threads, title, std::move(auth)) { @@ -109,7 +109,7 @@ void TMonService2::Register(IMonPage* page) { } void TMonService2::Register(TMonPagePtr page) { - IndexMonPage->Register(std::move(page)); + IndexMonPage->Register(std::move(page)); } TIndexMonPage* TMonService2::RegisterIndexPage(const TString& path, const TString& title) { diff --git a/library/cpp/monlib/service/pages/registry_mon_page.cpp b/library/cpp/monlib/service/pages/registry_mon_page.cpp index c59e50f622..f6c35b808d 100644 --- a/library/cpp/monlib/service/pages/registry_mon_page.cpp +++ b/library/cpp/monlib/service/pages/registry_mon_page.cpp @@ -30,9 +30,9 @@ namespace NMonitoring { } out.Write(resp); - RegistryRawPtr_->Accept(TInstant::Zero(), encoder.Get()); - - encoder->Close(); + RegistryRawPtr_->Accept(TInstant::Zero(), encoder.Get()); + + encoder->Close(); } else { THtmlMonPage::Output(request); } diff --git a/library/cpp/monlib/service/pages/registry_mon_page.h b/library/cpp/monlib/service/pages/registry_mon_page.h index 2d26d3319c..b8caff9ad2 100644 --- a/library/cpp/monlib/service/pages/registry_mon_page.h +++ b/library/cpp/monlib/service/pages/registry_mon_page.h @@ -8,25 +8,25 @@ namespace NMonitoring { // For now this class can only enumerate all metrics without any grouping or serve JSON/Spack/Prometheus class TMetricRegistryPage: public TPreMonPage { public: - TMetricRegistryPage(const TString& path, const TString& title, TAtomicSharedPtr<IMetricSupplier> registry) + TMetricRegistryPage(const TString& path, const TString& title, TAtomicSharedPtr<IMetricSupplier> registry) : TPreMonPage(path, title) - , Registry_(registry) - , RegistryRawPtr_(Registry_.Get()) - { - } - - TMetricRegistryPage(const TString& path, const TString& title, IMetricSupplier* registry) - : TPreMonPage(path, title) - , RegistryRawPtr_(registry) + , Registry_(registry) + , RegistryRawPtr_(Registry_.Get()) { } + TMetricRegistryPage(const TString& path, const TString& title, IMetricSupplier* registry) + : TPreMonPage(path, title) + , RegistryRawPtr_(registry) + { + } + void Output(NMonitoring::IMonHttpRequest& request) override; void OutputText(IOutputStream& out, NMonitoring::IMonHttpRequest&) override; private: - TAtomicSharedPtr<IMetricSupplier> Registry_; - IMetricSupplier* RegistryRawPtr_; + TAtomicSharedPtr<IMetricSupplier> Registry_; + IMetricSupplier* RegistryRawPtr_; }; } diff --git a/library/cpp/monlib/service/pages/templates.h b/library/cpp/monlib/service/pages/templates.h index b4656f059f..cb1ec4d532 100644 --- a/library/cpp/monlib/service/pages/templates.h +++ b/library/cpp/monlib/service/pages/templates.h @@ -6,7 +6,7 @@ #define WITH_SCOPED(var, value) WITH_SCOPED_I(var, value, Y_GENERATE_UNIQUE_ID(WITH_SCOPED_LABEL_)) #define WITH_SCOPED_I(var, value, label) \ - if (auto var = (value)) { \ + if (auto var = (value)) { \ Y_UNUSED(var); \ goto label; \ } else \ diff --git a/library/cpp/monlib/service/service.cpp b/library/cpp/monlib/service/service.cpp index 929efbf816..633b8403aa 100644 --- a/library/cpp/monlib/service/service.cpp +++ b/library/cpp/monlib/service/service.cpp @@ -136,7 +136,7 @@ namespace NMonitoring { TCoHttpServer::TCoHttpServer(TContExecutor& executor, const TString& bindAddr, TIpPort port, THandler handler) : Executor(executor) , Listener(this, &executor) - , Handler(std::move(handler)) + , Handler(std::move(handler)) , BindAddr(bindAddr) , Port(port) { @@ -205,16 +205,16 @@ namespace NMonitoring { TMtHttpServer::TMtHttpServer(const TOptions& options, THandler handler, IThreadFactory* pool) : THttpServer(this, options, pool) - , Handler(std::move(handler)) + , Handler(std::move(handler)) { } TMtHttpServer::TMtHttpServer(const TOptions& options, THandler handler, TSimpleSharedPtr<IThreadPool> pool) - : THttpServer(this, /* mainWorkers = */pool, /* failWorkers = */pool, options) - , Handler(std::move(handler)) - { - } - + : THttpServer(this, /* mainWorkers = */pool, /* failWorkers = */pool, options) + , Handler(std::move(handler)) + { + } + bool TMtHttpServer::Start() { return THttpServer::Start(); } @@ -241,9 +241,9 @@ namespace NMonitoring { TMonService::TMonService(TContExecutor& executor, TIpPort internalPort, TIpPort externalPort, THandler coHandler, THandler mtHandler) - : CoServer(executor, "127.0.0.1", internalPort, std::move(coHandler)) + : CoServer(executor, "127.0.0.1", internalPort, std::move(coHandler)) , MtServer(THttpServerOptions(externalPort), std::bind(&TMonService::DispatchRequest, this, std::placeholders::_1, std::placeholders::_2)) - , MtHandler(std::move(mtHandler)) + , MtHandler(std::move(mtHandler)) { } diff --git a/library/cpp/monlib/ya.make b/library/cpp/monlib/ya.make index 9bd236d6fd..3d9e92857a 100644 --- a/library/cpp/monlib/ya.make +++ b/library/cpp/monlib/ya.make @@ -4,7 +4,7 @@ OWNER( ) RECURSE( - consumers + consumers counters counters/ut deprecated @@ -32,7 +32,7 @@ RECURSE( encode/unistat/ut encode/ut example - exception + exception libtimestats/ut metrics metrics/ut diff --git a/library/cpp/threading/light_rw_lock/lightrwlock.h b/library/cpp/threading/light_rw_lock/lightrwlock.h index 931a1817bc..cad972b4f8 100644 --- a/library/cpp/threading/light_rw_lock/lightrwlock.h +++ b/library/cpp/threading/light_rw_lock/lightrwlock.h @@ -1,7 +1,7 @@ #pragma once #include <util/system/rwlock.h> -#include <util/system/sanitizers.h> +#include <util/system/sanitizers.h> #if defined(_linux_) /* TLightRWLock is optimized for read lock and very fast lock/unlock switching. @@ -48,10 +48,10 @@ namespace NS_LightRWLock { : "+m"(item), "=rm"(ret) : "r"(bit) : "cc"); - - // msan doesn't treat ret as initialized - NSan::Unpoison(&ret, sizeof(ret)); - + + // msan doesn't treat ret as initialized + NSan::Unpoison(&ret, sizeof(ret)); + return ret; } @@ -63,10 +63,10 @@ namespace NS_LightRWLock { : "+m"(item), "=rm"(ret) : "r"(bit) : "cc"); - - // msan doesn't treat ret as initialized - NSan::Unpoison(&ret, sizeof(ret)); - + + // msan doesn't treat ret as initialized + NSan::Unpoison(&ret, sizeof(ret)); + return ret; } |