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/monlib/metrics | |
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/monlib/metrics')
20 files changed, 688 insertions, 688 deletions
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 |