diff options
author | msherbakov <msherbakov@yandex-team.ru> | 2022-02-10 16:49:17 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:17 +0300 |
commit | a0ffafe83b7d6229709a32fa942c71d672ac989c (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library/cpp/monlib/metrics | |
parent | c224a621661ddd69699f9476922eb316607ef57e (diff) | |
download | ydb-a0ffafe83b7d6229709a32fa942c71d672ac989c.tar.gz |
Restoring authorship annotation for <msherbakov@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/monlib/metrics')
24 files changed, 1098 insertions, 1098 deletions
diff --git a/library/cpp/monlib/metrics/atomics_array.h b/library/cpp/monlib/metrics/atomics_array.h index a2e6566c96..f19aebf291 100644 --- a/library/cpp/monlib/metrics/atomics_array.h +++ b/library/cpp/monlib/metrics/atomics_array.h @@ -9,17 +9,17 @@ namespace NMonitoring { class TAtomicsArray { public: explicit TAtomicsArray(size_t size) - : Values_(new std::atomic<ui64>[size]) + : Values_(new std::atomic<ui64>[size]) , Size_(size) { for (size_t i = 0; i < Size_; i++) { - Values_[i].store(0, std::memory_order_relaxed); + Values_[i].store(0, std::memory_order_relaxed); } } ui64 operator[](size_t index) const noexcept { Y_VERIFY_DEBUG(index < Size_); - return Values_[index].load(std::memory_order_relaxed); + return Values_[index].load(std::memory_order_relaxed); } size_t Size() const noexcept { @@ -28,7 +28,7 @@ namespace NMonitoring { void Add(size_t index, ui32 count) noexcept { Y_VERIFY_DEBUG(index < Size_); - Values_[index].fetch_add(count, std::memory_order_relaxed); + Values_[index].fetch_add(count, std::memory_order_relaxed); } void Reset() noexcept { @@ -40,13 +40,13 @@ namespace NMonitoring { TVector<ui64> Copy() const { TVector<ui64> copy(Reserve(Size_)); for (size_t i = 0; i < Size_; i++) { - copy.push_back(Values_[i].load(std::memory_order_relaxed)); + copy.push_back(Values_[i].load(std::memory_order_relaxed)); } return copy; } private: - TArrayHolder<std::atomic<ui64>> Values_; + TArrayHolder<std::atomic<ui64>> Values_; size_t Size_; }; } diff --git a/library/cpp/monlib/metrics/ewma.cpp b/library/cpp/monlib/metrics/ewma.cpp index 4838b40d6e..8a296c3225 100644 --- a/library/cpp/monlib/metrics/ewma.cpp +++ b/library/cpp/monlib/metrics/ewma.cpp @@ -1,150 +1,150 @@ -#include "ewma.h" +#include "ewma.h" #include "metric.h" - -#include <atomic> -#include <cmath> - -namespace NMonitoring { -namespace { - constexpr auto DEFAULT_INTERVAL = TDuration::Seconds(5); - - const double ALPHA1 = 1. - std::exp(-double(DEFAULT_INTERVAL.Seconds())/60./1.); - const double ALPHA5 = 1. - std::exp(-double(DEFAULT_INTERVAL.Seconds())/60./5.); - const double ALPHA15 = 1. - std::exp(-double(DEFAULT_INTERVAL.Seconds())/60./15.); - - class TExpMovingAverage final: public IExpMovingAverage { - public: + +#include <atomic> +#include <cmath> + +namespace NMonitoring { +namespace { + constexpr auto DEFAULT_INTERVAL = TDuration::Seconds(5); + + const double ALPHA1 = 1. - std::exp(-double(DEFAULT_INTERVAL.Seconds())/60./1.); + const double ALPHA5 = 1. - std::exp(-double(DEFAULT_INTERVAL.Seconds())/60./5.); + const double ALPHA15 = 1. - std::exp(-double(DEFAULT_INTERVAL.Seconds())/60./15.); + + class TExpMovingAverage final: public IExpMovingAverage { + public: explicit TExpMovingAverage(IGauge* metric, double alpha, TDuration interval) : Metric_{metric} - , Alpha_{alpha} - , Interval_{interval.Seconds()} - { + , Alpha_{alpha} + , Interval_{interval.Seconds()} + { Y_VERIFY(metric != nullptr, "Passing nullptr metric is not allowed"); - } - + } + ~TExpMovingAverage() override = default; - - // This method NOT thread safe - void Tick() override { - const auto current = Uncounted_.fetch_and(0); - const double instantRate = double(current) / Interval_; - - if (Y_UNLIKELY(!IsInitialized())) { + + // This method NOT thread safe + void Tick() override { + const auto current = Uncounted_.fetch_and(0); + const double instantRate = double(current) / Interval_; + + if (Y_UNLIKELY(!IsInitialized())) { Metric_->Set(instantRate); - Init_ = true; - } else { + Init_ = true; + } else { const double currentRate = Metric_->Get(); Metric_->Set(Alpha_ * (instantRate - currentRate) + currentRate); - } - - } - - void Update(i64 value) override { - Uncounted_ += value; - } - - double Rate() const override { + } + + } + + void Update(i64 value) override { + Uncounted_ += value; + } + + double Rate() const override { return Metric_->Get(); - } - - void Reset() override { - Init_ = false; - Uncounted_ = 0; - } - - private: - bool IsInitialized() const { - return Init_; - } - - private: - std::atomic<i64> Uncounted_{0}; - std::atomic<bool> Init_{false}; - + } + + void Reset() override { + Init_ = false; + Uncounted_ = 0; + } + + private: + bool IsInitialized() const { + return Init_; + } + + private: + std::atomic<i64> Uncounted_{0}; + std::atomic<bool> Init_{false}; + IGauge* Metric_{nullptr}; - double Alpha_; - ui64 Interval_; - }; - - struct TFakeEwma: IExpMovingAverage { - void Tick() override {} - void Update(i64) override {} - double Rate() const override { return 0; } - void Reset() override {} - }; - -} // namespace - - TEwmaMeter::TEwmaMeter() - : Ewma_{MakeHolder<TFakeEwma>()} - { - } - - TEwmaMeter::TEwmaMeter(IExpMovingAveragePtr&& ewma) - : Ewma_{std::move(ewma)} - { - } - - TEwmaMeter::TEwmaMeter(TEwmaMeter&& other) { - if (&other == this) { - return; - } - - *this = std::move(other); - } - - TEwmaMeter& TEwmaMeter::operator=(TEwmaMeter&& other) { - Ewma_ = std::move(other.Ewma_); - LastTick_.store(other.LastTick_); - return *this; - } - - void TEwmaMeter::TickIfNeeded() { - constexpr ui64 INTERVAL_SECONDS = DEFAULT_INTERVAL.Seconds(); - - const auto now = TInstant::Now().Seconds(); - ui64 old = LastTick_.load(); - const auto secondsSinceLastTick = now - old; - - if (secondsSinceLastTick > INTERVAL_SECONDS) { - // round to the interval grid - const ui64 newLast = now - (secondsSinceLastTick % INTERVAL_SECONDS); - if (LastTick_.compare_exchange_strong(old, newLast)) { - for (size_t i = 0; i < secondsSinceLastTick / INTERVAL_SECONDS; ++i) { - Ewma_->Tick(); - } - } - } - } - - void TEwmaMeter::Mark() { - TickIfNeeded(); - Ewma_->Update(1); - } - - void TEwmaMeter::Mark(i64 value) { - TickIfNeeded(); - Ewma_->Update(value); - } - - double TEwmaMeter::Get() { - TickIfNeeded(); - return Ewma_->Rate(); - } - + double Alpha_; + ui64 Interval_; + }; + + struct TFakeEwma: IExpMovingAverage { + void Tick() override {} + void Update(i64) override {} + double Rate() const override { return 0; } + void Reset() override {} + }; + +} // namespace + + TEwmaMeter::TEwmaMeter() + : Ewma_{MakeHolder<TFakeEwma>()} + { + } + + TEwmaMeter::TEwmaMeter(IExpMovingAveragePtr&& ewma) + : Ewma_{std::move(ewma)} + { + } + + TEwmaMeter::TEwmaMeter(TEwmaMeter&& other) { + if (&other == this) { + return; + } + + *this = std::move(other); + } + + TEwmaMeter& TEwmaMeter::operator=(TEwmaMeter&& other) { + Ewma_ = std::move(other.Ewma_); + LastTick_.store(other.LastTick_); + return *this; + } + + void TEwmaMeter::TickIfNeeded() { + constexpr ui64 INTERVAL_SECONDS = DEFAULT_INTERVAL.Seconds(); + + const auto now = TInstant::Now().Seconds(); + ui64 old = LastTick_.load(); + const auto secondsSinceLastTick = now - old; + + if (secondsSinceLastTick > INTERVAL_SECONDS) { + // round to the interval grid + const ui64 newLast = now - (secondsSinceLastTick % INTERVAL_SECONDS); + if (LastTick_.compare_exchange_strong(old, newLast)) { + for (size_t i = 0; i < secondsSinceLastTick / INTERVAL_SECONDS; ++i) { + Ewma_->Tick(); + } + } + } + } + + void TEwmaMeter::Mark() { + TickIfNeeded(); + Ewma_->Update(1); + } + + void TEwmaMeter::Mark(i64 value) { + TickIfNeeded(); + Ewma_->Update(value); + } + + double TEwmaMeter::Get() { + TickIfNeeded(); + return Ewma_->Rate(); + } + IExpMovingAveragePtr OneMinuteEwma(IGauge* metric) { return MakeHolder<TExpMovingAverage>(metric, ALPHA1, DEFAULT_INTERVAL); - } - + } + IExpMovingAveragePtr FiveMinuteEwma(IGauge* metric) { return MakeHolder<TExpMovingAverage>(metric, ALPHA5, DEFAULT_INTERVAL); - } - + } + IExpMovingAveragePtr FiveteenMinuteEwma(IGauge* metric) { return MakeHolder<TExpMovingAverage>(metric, ALPHA15, DEFAULT_INTERVAL); - } - + } + IExpMovingAveragePtr CreateEwma(IGauge* metric, double alpha, TDuration interval) { return MakeHolder<TExpMovingAverage>(metric, alpha, interval); - } -} // namespace NMonitoring + } +} // namespace NMonitoring diff --git a/library/cpp/monlib/metrics/ewma.h b/library/cpp/monlib/metrics/ewma.h index 06a2bc5058..9b2dad7cc5 100644 --- a/library/cpp/monlib/metrics/ewma.h +++ b/library/cpp/monlib/metrics/ewma.h @@ -1,47 +1,47 @@ -#pragma once - -#include <util/datetime/base.h> -#include <util/generic/ptr.h> - -#include <atomic> - -namespace NMonitoring { - class IGauge; - - class IExpMovingAverage { - public: - virtual ~IExpMovingAverage() = default; - virtual void Tick() = 0; - virtual void Update(i64 value) = 0; - virtual double Rate() const = 0; - virtual void Reset() = 0; - }; - - using IExpMovingAveragePtr = THolder<IExpMovingAverage>; - - class TEwmaMeter { - public: - // Creates a fake EWMA that will always return 0. Mostly for usage convenience - TEwmaMeter(); - explicit TEwmaMeter(IExpMovingAveragePtr&& ewma); - - TEwmaMeter(TEwmaMeter&& other); - TEwmaMeter& operator=(TEwmaMeter&& other); - - void Mark(); - void Mark(i64 value); - - double Get(); - - private: - void TickIfNeeded(); - - private: - IExpMovingAveragePtr Ewma_; - std::atomic<ui64> LastTick_{TInstant::Now().Seconds()}; - }; - - IExpMovingAveragePtr OneMinuteEwma(IGauge* gauge); - IExpMovingAveragePtr FiveMinuteEwma(IGauge* gauge); - IExpMovingAveragePtr FiveteenMinuteEwma(IGauge* gauge); -} // namespace NMonitoring +#pragma once + +#include <util/datetime/base.h> +#include <util/generic/ptr.h> + +#include <atomic> + +namespace NMonitoring { + class IGauge; + + class IExpMovingAverage { + public: + virtual ~IExpMovingAverage() = default; + virtual void Tick() = 0; + virtual void Update(i64 value) = 0; + virtual double Rate() const = 0; + virtual void Reset() = 0; + }; + + using IExpMovingAveragePtr = THolder<IExpMovingAverage>; + + class TEwmaMeter { + public: + // Creates a fake EWMA that will always return 0. Mostly for usage convenience + TEwmaMeter(); + explicit TEwmaMeter(IExpMovingAveragePtr&& ewma); + + TEwmaMeter(TEwmaMeter&& other); + TEwmaMeter& operator=(TEwmaMeter&& other); + + void Mark(); + void Mark(i64 value); + + double Get(); + + private: + void TickIfNeeded(); + + private: + IExpMovingAveragePtr Ewma_; + std::atomic<ui64> LastTick_{TInstant::Now().Seconds()}; + }; + + IExpMovingAveragePtr OneMinuteEwma(IGauge* gauge); + IExpMovingAveragePtr FiveMinuteEwma(IGauge* gauge); + IExpMovingAveragePtr FiveteenMinuteEwma(IGauge* gauge); +} // namespace NMonitoring diff --git a/library/cpp/monlib/metrics/ewma_ut.cpp b/library/cpp/monlib/metrics/ewma_ut.cpp index 2765c68eae..01ef2478f7 100644 --- a/library/cpp/monlib/metrics/ewma_ut.cpp +++ b/library/cpp/monlib/metrics/ewma_ut.cpp @@ -1,112 +1,112 @@ -#include "ewma.h" +#include "ewma.h" #include "metric.h" - + #include <library/cpp/testing/unittest/registar.h> - - -using namespace NMonitoring; - -const auto EPS = 1e-6; -void ElapseMinute(IExpMovingAverage& ewma) { - for (auto i = 0; i < 12; ++i) { - ewma.Tick(); - } -} - -Y_UNIT_TEST_SUITE(TEwmaTest) { - Y_UNIT_TEST(OneMinute) { + + +using namespace NMonitoring; + +const auto EPS = 1e-6; +void ElapseMinute(IExpMovingAverage& ewma) { + for (auto i = 0; i < 12; ++i) { + ewma.Tick(); + } +} + +Y_UNIT_TEST_SUITE(TEwmaTest) { + Y_UNIT_TEST(OneMinute) { TGauge gauge; - + auto ewma = OneMinuteEwma(&gauge); - ewma->Update(3); - ewma->Tick(); - - TVector<double> expectedValues { - 0.6, - 0.22072766, - 0.08120117, - 0.02987224, - 0.01098938, - 0.00404277, - 0.00148725, - 0.00054713, - 0.00020128, - 0.00007405, - 0.00002724, - 0.00001002, - 0.00000369, - 0.00000136, - 0.00000050, - 0.00000018, - }; - - for (auto expectedValue : expectedValues) { - UNIT_ASSERT_DOUBLES_EQUAL(ewma->Rate(), expectedValue, EPS); - ElapseMinute(*ewma); - } - } - - Y_UNIT_TEST(FiveMinutes) { + ewma->Update(3); + ewma->Tick(); + + TVector<double> expectedValues { + 0.6, + 0.22072766, + 0.08120117, + 0.02987224, + 0.01098938, + 0.00404277, + 0.00148725, + 0.00054713, + 0.00020128, + 0.00007405, + 0.00002724, + 0.00001002, + 0.00000369, + 0.00000136, + 0.00000050, + 0.00000018, + }; + + for (auto expectedValue : expectedValues) { + UNIT_ASSERT_DOUBLES_EQUAL(ewma->Rate(), expectedValue, EPS); + ElapseMinute(*ewma); + } + } + + Y_UNIT_TEST(FiveMinutes) { TGauge gauge; - + auto ewma = FiveMinuteEwma(&gauge); - ewma->Update(3); - ewma->Tick(); - - TVector<double> expectedValues { - 0.6, - 0.49123845, - 0.40219203, - 0.32928698, - 0.26959738, - 0.22072766, - 0.18071653, - 0.14795818, - 0.12113791, - 0.09917933, - 0.08120117, - 0.06648190, - 0.05443077, - 0.04456415, - 0.03648604, - 0.02987224, - }; - - for (auto expectedValue : expectedValues) { - UNIT_ASSERT_DOUBLES_EQUAL(ewma->Rate(), expectedValue, EPS); - ElapseMinute(*ewma); - } - } - - Y_UNIT_TEST(FiveteenMinutes) { + ewma->Update(3); + ewma->Tick(); + + TVector<double> expectedValues { + 0.6, + 0.49123845, + 0.40219203, + 0.32928698, + 0.26959738, + 0.22072766, + 0.18071653, + 0.14795818, + 0.12113791, + 0.09917933, + 0.08120117, + 0.06648190, + 0.05443077, + 0.04456415, + 0.03648604, + 0.02987224, + }; + + for (auto expectedValue : expectedValues) { + UNIT_ASSERT_DOUBLES_EQUAL(ewma->Rate(), expectedValue, EPS); + ElapseMinute(*ewma); + } + } + + Y_UNIT_TEST(FiveteenMinutes) { TGauge gauge; - + auto ewma = FiveteenMinuteEwma(&gauge); - ewma->Update(3); - ewma->Tick(); - - TVector<double> expectedValues { - 0.6, - 0.56130419, - 0.52510399, - 0.49123845, - 0.45955700, - 0.42991879, - 0.40219203, - 0.37625345, - 0.35198773, - 0.32928698, - 0.30805027, - 0.28818318, - 0.26959738, - 0.25221023, - 0.23594443, - 0.22072766, - }; - - for (auto expectedValue : expectedValues) { - UNIT_ASSERT_DOUBLES_EQUAL(ewma->Rate(), expectedValue, EPS); - ElapseMinute(*ewma); - } - } -}; + ewma->Update(3); + ewma->Tick(); + + TVector<double> expectedValues { + 0.6, + 0.56130419, + 0.52510399, + 0.49123845, + 0.45955700, + 0.42991879, + 0.40219203, + 0.37625345, + 0.35198773, + 0.32928698, + 0.30805027, + 0.28818318, + 0.26959738, + 0.25221023, + 0.23594443, + 0.22072766, + }; + + for (auto expectedValue : expectedValues) { + UNIT_ASSERT_DOUBLES_EQUAL(ewma->Rate(), expectedValue, EPS); + ElapseMinute(*ewma); + } + } +}; diff --git a/library/cpp/monlib/metrics/fake.cpp b/library/cpp/monlib/metrics/fake.cpp index 6890ca5ab0..b6f5e37af8 100644 --- a/library/cpp/monlib/metrics/fake.cpp +++ b/library/cpp/monlib/metrics/fake.cpp @@ -1,11 +1,11 @@ -#include "fake.h" - -namespace NMonitoring { +#include "fake.h" + +namespace NMonitoring { IGauge* TFakeMetricRegistry::Gauge(ILabelsPtr labels) { return Metric<TFakeGauge, EMetricType::GAUGE>(std::move(labels)); - } - + } + ILazyGauge* TFakeMetricRegistry::LazyGauge(ILabelsPtr labels, std::function<double()> supplier) { Y_UNUSED(supplier); return Metric<TFakeLazyGauge, EMetricType::GAUGE>(std::move(labels)); @@ -13,8 +13,8 @@ namespace NMonitoring { IIntGauge* TFakeMetricRegistry::IntGauge(ILabelsPtr labels) { return Metric<TFakeIntGauge, EMetricType::IGAUGE>(std::move(labels)); - } - + } + ILazyIntGauge* TFakeMetricRegistry::LazyIntGauge(ILabelsPtr labels, std::function<i64()> supplier) { Y_UNUSED(supplier); return Metric<TFakeLazyIntGauge, EMetricType::IGAUGE>(std::move(labels)); @@ -22,8 +22,8 @@ namespace NMonitoring { ICounter* TFakeMetricRegistry::Counter(ILabelsPtr labels) { return Metric<TFakeCounter, EMetricType::COUNTER>(std::move(labels)); - } - + } + ILazyCounter* TFakeMetricRegistry::LazyCounter(ILabelsPtr labels, std::function<ui64()> supplier) { Y_UNUSED(supplier); return Metric<TFakeLazyCounter, EMetricType::COUNTER>(std::move(labels)); @@ -31,70 +31,70 @@ namespace NMonitoring { IRate* TFakeMetricRegistry::Rate(ILabelsPtr labels) { return Metric<TFakeRate, EMetricType::RATE>(std::move(labels)); - } - + } + ILazyRate* TFakeMetricRegistry::LazyRate(ILabelsPtr labels, std::function<ui64()> supplier) { Y_UNUSED(supplier); return Metric<TFakeLazyRate, EMetricType::RATE>(std::move(labels)); } IHistogram* TFakeMetricRegistry::HistogramCounter(ILabelsPtr labels, IHistogramCollectorPtr collector) { - Y_UNUSED(collector); + Y_UNUSED(collector); return Metric<TFakeHistogram, EMetricType::HIST>(std::move(labels), false); - } - + } + void TFakeMetricRegistry::RemoveMetric(const ILabels& labels) noexcept { - TWriteGuard g{Lock_}; + TWriteGuard g{Lock_}; Metrics_.erase(labels); - } - + } + void TFakeMetricRegistry::Accept(TInstant time, IMetricConsumer* consumer) const { - Y_UNUSED(time); - consumer->OnStreamBegin(); - consumer->OnStreamEnd(); - } - + Y_UNUSED(time); + consumer->OnStreamBegin(); + consumer->OnStreamEnd(); + } + IHistogram* TFakeMetricRegistry::HistogramRate(ILabelsPtr labels, IHistogramCollectorPtr collector) { - Y_UNUSED(collector); + Y_UNUSED(collector); return Metric<TFakeHistogram, EMetricType::HIST_RATE>(std::move(labels), true); - } - + } + void TFakeMetricRegistry::Append(TInstant time, IMetricConsumer* consumer) const { - Y_UNUSED(time, consumer); - } - + Y_UNUSED(time, consumer); + } + const TLabels& TFakeMetricRegistry::CommonLabels() const noexcept { - return CommonLabels_; - } - + return CommonLabels_; + } + template <typename TMetric, EMetricType type, typename TLabelsType, typename... Args> TMetric* TFakeMetricRegistry::Metric(TLabelsType&& labels, Args&&... args) { - { - TReadGuard g{Lock_}; - + { + TReadGuard g{Lock_}; + auto it = Metrics_.find(labels); if (it != Metrics_.end()) { Y_ENSURE(it->second->Type() == type, "cannot create metric " << labels << " with type " << MetricTypeToStr(type) << ", because registry already has same metric with type " << MetricTypeToStr(it->second->Type())); return static_cast<TMetric*>(it->second.Get()); - } - } - - { - TWriteGuard g{Lock_}; - + } + } + + { + TWriteGuard g{Lock_}; + IMetricPtr metric = MakeHolder<TMetric>(std::forward<Args>(args)...); - + // decltype(Metrics_)::iterator breaks build on windows THashMap<ILabelsPtr, IMetricPtr>::iterator it; - if constexpr (!std::is_convertible_v<TLabelsType, ILabelsPtr>) { + if constexpr (!std::is_convertible_v<TLabelsType, ILabelsPtr>) { it = Metrics_.emplace(new TLabels{std::forward<TLabelsType>(labels)}, std::move(metric)).first; - } else { + } else { it = Metrics_.emplace(std::forward<TLabelsType>(labels), std::move(metric)).first; - } - + } + return static_cast<TMetric*>(it->second.Get()); - } - } -} // namespace NMonitoring + } + } +} // namespace NMonitoring diff --git a/library/cpp/monlib/metrics/fake.h b/library/cpp/monlib/metrics/fake.h index 8787d37cb6..61ba4f2bd4 100644 --- a/library/cpp/monlib/metrics/fake.h +++ b/library/cpp/monlib/metrics/fake.h @@ -1,11 +1,11 @@ -#pragma once - +#pragma once + #include "metric.h" #include "metric_registry.h" - -namespace NMonitoring { + +namespace NMonitoring { class TFakeMetricRegistry: public IMetricRegistry { - public: + public: TFakeMetricRegistry() noexcept : CommonLabels_{0} { @@ -16,150 +16,150 @@ namespace NMonitoring { { } - IGauge* Gauge(ILabelsPtr labels) override; + IGauge* Gauge(ILabelsPtr labels) override; ILazyGauge* LazyGauge(ILabelsPtr labels, std::function<double()> supplier) override; - IIntGauge* IntGauge(ILabelsPtr labels) override; + IIntGauge* IntGauge(ILabelsPtr labels) override; ILazyIntGauge* LazyIntGauge(ILabelsPtr labels, std::function<i64()> supplier) override; - ICounter* Counter(ILabelsPtr labels) override; + ICounter* Counter(ILabelsPtr labels) override; ILazyCounter* LazyCounter(ILabelsPtr labels, std::function<ui64()> supplier) override; - IRate* Rate(ILabelsPtr labels) override; + IRate* Rate(ILabelsPtr labels) override; ILazyRate* LazyRate(ILabelsPtr labels, std::function<ui64()> supplier) override; - - IHistogram* HistogramCounter( - ILabelsPtr labels, - IHistogramCollectorPtr collector) override; - - IHistogram* HistogramRate( - ILabelsPtr labels, - IHistogramCollectorPtr collector) override; + + IHistogram* HistogramCounter( + ILabelsPtr labels, + IHistogramCollectorPtr collector) override; + + IHistogram* HistogramRate( + ILabelsPtr labels, + IHistogramCollectorPtr collector) override; void Accept(TInstant time, IMetricConsumer* consumer) const override; void Append(TInstant time, IMetricConsumer* consumer) const override; - - const TLabels& CommonLabels() const noexcept override; + + const TLabels& CommonLabels() const noexcept override; void RemoveMetric(const ILabels& labels) noexcept override; - - private: - TRWMutex Lock_; + + private: + TRWMutex Lock_; THashMap<ILabelsPtr, IMetricPtr> Metrics_; - + template <typename TMetric, EMetricType type, typename TLabelsType, typename... Args> TMetric* Metric(TLabelsType&& labels, Args&&... args); - - const TLabels CommonLabels_; - }; - - template <typename TBase> - struct TFakeAcceptor: TBase { + + const TLabels CommonLabels_; + }; + + template <typename TBase> + struct TFakeAcceptor: TBase { void Accept(TInstant time, IMetricConsumer* consumer) const override { - Y_UNUSED(time, consumer); - } - }; - - struct TFakeIntGauge final: public TFakeAcceptor<IIntGauge> { - i64 Add(i64 n) noexcept override { - Y_UNUSED(n); - return 0; - } - - void Set(i64 n) noexcept override { - Y_UNUSED(n); - } - - i64 Get() const noexcept override { - return 0; - } - }; - + Y_UNUSED(time, consumer); + } + }; + + struct TFakeIntGauge final: public TFakeAcceptor<IIntGauge> { + i64 Add(i64 n) noexcept override { + Y_UNUSED(n); + return 0; + } + + void Set(i64 n) noexcept override { + Y_UNUSED(n); + } + + i64 Get() const noexcept override { + return 0; + } + }; + struct TFakeLazyIntGauge final: public TFakeAcceptor<ILazyIntGauge> { i64 Get() const noexcept override { return 0; } }; - struct TFakeRate final: public TFakeAcceptor<IRate> { - ui64 Add(ui64 n) noexcept override { - Y_UNUSED(n); - return 0; - } - - ui64 Get() const noexcept override { - return 0; - } + struct TFakeRate final: public TFakeAcceptor<IRate> { + ui64 Add(ui64 n) noexcept override { + Y_UNUSED(n); + return 0; + } + + ui64 Get() const noexcept override { + return 0; + } void Reset() noexcept override { } - }; - + }; + struct TFakeLazyRate final: public TFakeAcceptor<ILazyRate> { ui64 Get() const noexcept override { return 0; } }; - struct TFakeGauge final: public TFakeAcceptor<IGauge> { - double Add(double n) noexcept override { - Y_UNUSED(n); - return 0; - } - - void Set(double n) noexcept override { - Y_UNUSED(n); - } - - double Get() const noexcept override { - return 0; - } - }; - + struct TFakeGauge final: public TFakeAcceptor<IGauge> { + double Add(double n) noexcept override { + Y_UNUSED(n); + return 0; + } + + void Set(double n) noexcept override { + Y_UNUSED(n); + } + + double Get() const noexcept override { + return 0; + } + }; + struct TFakeLazyGauge final: public TFakeAcceptor<ILazyGauge> { double Get() const noexcept override { return 0; } }; - struct TFakeHistogram final: public IHistogram { - TFakeHistogram(bool isRate = false) - : IHistogram{isRate} - { - } - + struct TFakeHistogram final: public IHistogram { + TFakeHistogram(bool isRate = false) + : IHistogram{isRate} + { + } + void Record(double value) override { - Y_UNUSED(value); - } - + Y_UNUSED(value); + } + void Record(double value, ui32 count) override { - Y_UNUSED(value, count); - } - - IHistogramSnapshotPtr TakeSnapshot() const override { - return nullptr; - } - + Y_UNUSED(value, count); + } + + IHistogramSnapshotPtr TakeSnapshot() const override { + return nullptr; + } + void Accept(TInstant time, IMetricConsumer* consumer) const override { - Y_UNUSED(time, consumer); - } - - void Reset() override { - } - }; - - struct TFakeCounter final: public TFakeAcceptor<ICounter> { - ui64 Add(ui64 n) noexcept override { - Y_UNUSED(n); - return 0; - } - - ui64 Get() const noexcept override { - return 0; - } - - void Reset() noexcept override { - } - }; + Y_UNUSED(time, consumer); + } + + void Reset() override { + } + }; + + struct TFakeCounter final: public TFakeAcceptor<ICounter> { + ui64 Add(ui64 n) noexcept override { + Y_UNUSED(n); + return 0; + } + + ui64 Get() const noexcept override { + return 0; + } + + void Reset() noexcept override { + } + }; struct TFakeLazyCounter final: public TFakeAcceptor<ILazyCounter> { ui64 Get() const noexcept override { return 0; } }; -} // namespace NMonitoring +} // namespace NMonitoring diff --git a/library/cpp/monlib/metrics/histogram_collector.h b/library/cpp/monlib/metrics/histogram_collector.h index 6e19e2da63..9f6bbbdfb7 100644 --- a/library/cpp/monlib/metrics/histogram_collector.h +++ b/library/cpp/monlib/metrics/histogram_collector.h @@ -68,7 +68,7 @@ namespace NMonitoring { * * @param bounds array of upper bounds for buckets. Values must be sorted. */ - IHistogramCollectorPtr ExplicitHistogram(TBucketBounds bounds); + IHistogramCollectorPtr ExplicitHistogram(TBucketBounds bounds); /** * <p>Creates histogram collector for a sequence of buckets that have a diff --git a/library/cpp/monlib/metrics/histogram_collector_explicit.cpp b/library/cpp/monlib/metrics/histogram_collector_explicit.cpp index bfeeb2531d..377fc233ef 100644 --- a/library/cpp/monlib/metrics/histogram_collector_explicit.cpp +++ b/library/cpp/monlib/metrics/histogram_collector_explicit.cpp @@ -13,7 +13,7 @@ namespace NMonitoring { /////////////////////////////////////////////////////////////////////////// class TExplicitHistogramCollector: public IHistogramCollector { public: - TExplicitHistogramCollector(TBucketBounds bounds) + TExplicitHistogramCollector(TBucketBounds bounds) : Values_(bounds.size() + 1) , Bounds_(std::move(bounds)) { @@ -32,8 +32,8 @@ namespace NMonitoring { } IHistogramSnapshotPtr Snapshot() const override { - auto values = Values_.Copy(); - return ExplicitHistogramSnapshot(Bounds_, values); + auto values = Values_.Copy(); + return ExplicitHistogramSnapshot(Bounds_, values); } private: diff --git a/library/cpp/monlib/metrics/histogram_snapshot.cpp b/library/cpp/monlib/metrics/histogram_snapshot.cpp index f00b972b78..75b5811546 100644 --- a/library/cpp/monlib/metrics/histogram_snapshot.cpp +++ b/library/cpp/monlib/metrics/histogram_snapshot.cpp @@ -7,7 +7,7 @@ namespace NMonitoring { - IHistogramSnapshotPtr ExplicitHistogramSnapshot(TConstArrayRef<TBucketBound> bounds, TConstArrayRef<TBucketValue> values) { + IHistogramSnapshotPtr ExplicitHistogramSnapshot(TConstArrayRef<TBucketBound> bounds, TConstArrayRef<TBucketValue> values) { Y_ENSURE(bounds.size() == values.size(), "mismatched sizes: bounds(" << bounds.size() << ") != buckets(" << values.size() << ')'); diff --git a/library/cpp/monlib/metrics/histogram_snapshot.h b/library/cpp/monlib/metrics/histogram_snapshot.h index 6fc4e51083..e8acf6ac2b 100644 --- a/library/cpp/monlib/metrics/histogram_snapshot.h +++ b/library/cpp/monlib/metrics/histogram_snapshot.h @@ -1,6 +1,6 @@ #pragma once -#include <util/generic/array_ref.h> +#include <util/generic/array_ref.h> #include <util/generic/ptr.h> #include <util/generic/vector.h> #include <util/generic/yexception.h> @@ -203,7 +203,7 @@ namespace NMonitoring { static_assert(alignof(TExplicitHistogramSnapshot) == alignof(TBucket), "mismatched alingments of THistogramSnapshot and TBucket"); - IHistogramSnapshotPtr ExplicitHistogramSnapshot(TConstArrayRef<TBucketBound> bounds, TConstArrayRef<TBucketValue> values); + IHistogramSnapshotPtr ExplicitHistogramSnapshot(TConstArrayRef<TBucketBound> bounds, TConstArrayRef<TBucketValue> values); } // namespace NMonitoring diff --git a/library/cpp/monlib/metrics/labels.cpp b/library/cpp/monlib/metrics/labels.cpp index 4dc27a11fc..1eaadb7cba 100644 --- a/library/cpp/monlib/metrics/labels.cpp +++ b/library/cpp/monlib/metrics/labels.cpp @@ -1,21 +1,21 @@ #include "labels.h" -#include <util/stream/output.h> -#include <util/string/split.h> +#include <util/stream/output.h> +#include <util/string/split.h> static void OutputLabels(IOutputStream& out, const NMonitoring::ILabels& labels) { size_t i = 0; - out << '{'; + out << '{'; for (const auto& label: labels) { if (i++ > 0) { out << TStringBuf(", "); - } + } out << label; } - out << '}'; -} - -template <> + out << '}'; +} + +template <> void Out<NMonitoring::ILabelsPtr>(IOutputStream& out, const NMonitoring::ILabelsPtr& labels) { OutputLabels(out, *labels); } @@ -26,57 +26,57 @@ void Out<NMonitoring::ILabels>(IOutputStream& out, const NMonitoring::ILabels& l } template <> -void Out<NMonitoring::ILabel>(IOutputStream& out, const NMonitoring::ILabel& labels) { - out << labels.Name() << "=" << labels.Value(); -} - -Y_MONLIB_DEFINE_LABELS_OUT(NMonitoring::TLabels); -Y_MONLIB_DEFINE_LABEL_OUT(NMonitoring::TLabel); - -namespace NMonitoring { - bool TryLoadLabelsFromString(TStringBuf sb, ILabels& labels) { - if (sb.Empty()) { - return false; - } - - if (!sb.StartsWith('{') || !sb.EndsWith('}')) { - return false; - } - - sb.Skip(1); - sb.Chop(1); - - if (sb.Empty()) { - return true; - } - - bool ok = true; - TVector<std::pair<TStringBuf, TStringBuf>> rawLabels; - StringSplitter(sb).SplitBySet(" ,").SkipEmpty().Consume([&] (TStringBuf label) { - TStringBuf key, value; - ok &= label.TrySplit('=', key, value); - - if (!ok) { - return; - } - - rawLabels.emplace_back(key, value); - }); - - if (!ok) { - return false; - } - - for (auto&& [k, v] : rawLabels) { - labels.Add(k, v); - } - - return true; - } - - bool TryLoadLabelsFromString(IInputStream& is, ILabels& labels) { - TString str = is.ReadAll(); - return TryLoadLabelsFromString(str, labels); - } - -} // namespace NMonitoring +void Out<NMonitoring::ILabel>(IOutputStream& out, const NMonitoring::ILabel& labels) { + out << labels.Name() << "=" << labels.Value(); +} + +Y_MONLIB_DEFINE_LABELS_OUT(NMonitoring::TLabels); +Y_MONLIB_DEFINE_LABEL_OUT(NMonitoring::TLabel); + +namespace NMonitoring { + bool TryLoadLabelsFromString(TStringBuf sb, ILabels& labels) { + if (sb.Empty()) { + return false; + } + + if (!sb.StartsWith('{') || !sb.EndsWith('}')) { + return false; + } + + sb.Skip(1); + sb.Chop(1); + + if (sb.Empty()) { + return true; + } + + bool ok = true; + TVector<std::pair<TStringBuf, TStringBuf>> rawLabels; + StringSplitter(sb).SplitBySet(" ,").SkipEmpty().Consume([&] (TStringBuf label) { + TStringBuf key, value; + ok &= label.TrySplit('=', key, value); + + if (!ok) { + return; + } + + rawLabels.emplace_back(key, value); + }); + + if (!ok) { + return false; + } + + for (auto&& [k, v] : rawLabels) { + labels.Add(k, v); + } + + return true; + } + + bool TryLoadLabelsFromString(IInputStream& is, ILabels& labels) { + TString str = is.ReadAll(); + return TryLoadLabelsFromString(str, labels); + } + +} // namespace NMonitoring diff --git a/library/cpp/monlib/metrics/labels.h b/library/cpp/monlib/metrics/labels.h index f374148544..63dc997c28 100644 --- a/library/cpp/monlib/metrics/labels.h +++ b/library/cpp/monlib/metrics/labels.h @@ -6,26 +6,26 @@ #include <util/generic/maybe.h> #include <util/generic/string.h> #include <util/generic/vector.h> -#include <util/stream/output.h> +#include <util/stream/output.h> #include <util/string/builder.h> #include <util/string/strip.h> -#include <optional> -#include <type_traits> - +#include <optional> +#include <type_traits> + namespace NMonitoring { - struct ILabel { - virtual ~ILabel() = default; - - virtual TStringBuf Name() const noexcept = 0; - virtual TStringBuf Value() const noexcept = 0; - }; - + struct ILabel { + virtual ~ILabel() = default; + + virtual TStringBuf Name() const noexcept = 0; + virtual TStringBuf Value() const noexcept = 0; + }; + /////////////////////////////////////////////////////////////////////////// // TLabel /////////////////////////////////////////////////////////////////////////// template <typename TStringBackend> - class TLabelImpl: public ILabel { + class TLabelImpl: public ILabel { public: using TStringType = TStringBackend; @@ -45,22 +45,22 @@ namespace NMonitoring { return !(*this == rhs); } - inline TStringBuf Name() const noexcept { + inline TStringBuf Name() const noexcept { + return Name_; + } + + inline TStringBuf Value() const noexcept { + return Value_; + } + + inline const TStringBackend& NameStr() const { return Name_; } - inline TStringBuf Value() const noexcept { + inline const TStringBackend& ValueStr() const { return Value_; } - inline const TStringBackend& NameStr() const { - return Name_; - } - - inline const TStringBackend& ValueStr() const { - return Value_; - } - inline size_t Hash() const noexcept { return MultiHash(Name_, Value_); } @@ -114,80 +114,80 @@ namespace NMonitoring { using TLabel = TLabelImpl<TString>; - struct ILabels { - struct TIterator { - TIterator() = default; - TIterator(const ILabels* labels, size_t idx = 0) - : Labels_{labels} - , Idx_{idx} - { - } - - TIterator& operator++() noexcept { - Idx_++; - return *this; - } - - void operator+=(size_t i) noexcept { - Idx_ += i; - } - - bool operator==(const TIterator& other) const noexcept { - return Idx_ == other.Idx_; - } - - bool operator!=(const TIterator& other) const noexcept { - return !(*this == other); - } - + struct ILabels { + struct TIterator { + TIterator() = default; + TIterator(const ILabels* labels, size_t idx = 0) + : Labels_{labels} + , Idx_{idx} + { + } + + TIterator& operator++() noexcept { + Idx_++; + return *this; + } + + void operator+=(size_t i) noexcept { + Idx_ += i; + } + + bool operator==(const TIterator& other) const noexcept { + return Idx_ == other.Idx_; + } + + bool operator!=(const TIterator& other) const noexcept { + 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_); - } - - - private: - const ILabels* Labels_{nullptr}; - size_t Idx_{0}; - }; - - virtual ~ILabels() = default; - + const ILabel& operator*() const noexcept { + Y_VERIFY_DEBUG(Labels_); + return *Labels_->Get(Idx_); + } + + + private: + const ILabels* Labels_{nullptr}; + size_t Idx_{0}; + }; + + virtual ~ILabels() = default; + virtual bool Add(TStringBuf name, TStringBuf value) noexcept = 0; - virtual bool Add(const ILabel& label) noexcept { - return Add(label.Name(), label.Value()); - } - + virtual bool Add(const ILabel& label) noexcept { + return Add(label.Name(), label.Value()); + } + virtual bool Has(TStringBuf name) const noexcept = 0; - - virtual size_t Size() const noexcept = 0; - virtual bool Empty() const noexcept = 0; - virtual void Clear() noexcept = 0; - - virtual size_t Hash() const noexcept = 0; - + + virtual size_t Size() const noexcept = 0; + virtual bool Empty() const noexcept = 0; + virtual void Clear() noexcept = 0; + + virtual size_t Hash() const noexcept = 0; + virtual std::optional<const ILabel*> Get(TStringBuf name) const = 0; - - // NB: there's no guarantee that indices are preserved after any object modification + + // NB: there's no guarantee that indices are preserved after any object modification virtual const ILabel* Get(size_t idx) const = 0; - - TIterator begin() const { - return TIterator{this}; - } - - TIterator end() const { - return TIterator{this, Size()}; - } - }; - - bool TryLoadLabelsFromString(TStringBuf sb, ILabels& labels); - bool TryLoadLabelsFromString(IInputStream& is, ILabels& labels); - + + TIterator begin() const { + return TIterator{this}; + } + + TIterator end() const { + return TIterator{this, Size()}; + } + }; + + bool TryLoadLabelsFromString(TStringBuf sb, ILabels& labels); + bool TryLoadLabelsFromString(IInputStream& is, ILabels& labels); + /////////////////////////////////////////////////////////////////////////// // TLabels /////////////////////////////////////////////////////////////////////////// @@ -201,15 +201,15 @@ namespace NMonitoring { explicit TLabelsImpl(::NDetail::TReserveTag rt) : Labels_(std::move(rt)) {} - + explicit TLabelsImpl(size_t count) : Labels_(count) {} - + explicit TLabelsImpl(size_t count, const value_type& label) : Labels_(count, label) {} - + TLabelsImpl(std::initializer_list<value_type> il) : Labels_(std::move(il)) {} @@ -237,7 +237,7 @@ namespace NMonitoring { return true; } - using ILabels::Add; + using ILabels::Add; bool Has(TStringBuf name) const noexcept override { auto it = FindIf(Labels_, [name](const TLabelImpl<TStringBackend>& label) { @@ -253,7 +253,7 @@ namespace NMonitoring { return it != Labels_.end(); } - // XXX for backward compatibility + // XXX for backward compatibility TMaybe<TLabelImpl<TStringBackend>> Find(TStringBuf name) const { auto it = FindIf(Labels_, [name](const TLabelImpl<TStringBackend>& label) { return name == TStringBuf{label.Name()}; @@ -266,20 +266,20 @@ namespace NMonitoring { std::optional<const ILabel*> Get(TStringBuf name) const override { auto it = FindIf(Labels_, [name] (auto&& l) { - return name == l.Name(); - }); - + return name == l.Name(); + }); + if (it == Labels_.end()) { - return {}; - } - - return &*it; - } - - const ILabel* Get(size_t idx) const noexcept override { - return &(*this)[idx]; - } - + return {}; + } + + return &*it; + } + + const ILabel* Get(size_t idx) const noexcept override { + return &(*this)[idx]; + } + TMaybe<TLabelImpl<TStringBackend>> Extract(TStringBuf name) { auto it = FindIf(Labels_, [name](const TLabelImpl<TStringBackend>& label) { return name == TStringBuf{label.Name()}; @@ -298,7 +298,7 @@ namespace NMonitoring { }); } - inline size_t Hash() const noexcept override { + inline size_t Hash() const noexcept override { return TSimpleRangeHash()(Labels_); } @@ -306,18 +306,18 @@ namespace NMonitoring { return const_cast<TLabel*>(Labels_.data()); } - inline size_t Size() const noexcept override { + inline size_t Size() const noexcept override { return Labels_.size(); } - inline bool Empty() const noexcept override { + inline bool Empty() const noexcept override { return Labels_.empty(); } - inline void Clear() noexcept override { + inline void Clear() noexcept override { Labels_.clear(); }; - + TLabelImpl<TStringBackend>& front() { return Labels_.front(); } @@ -383,7 +383,7 @@ namespace NMonitoring { protected: TVector<TLabelImpl<TStringBackend>>& AsVector() { return Labels_; - } + } const TVector<TLabelImpl<TStringBackend>>& AsVector() const { return Labels_; @@ -394,7 +394,7 @@ namespace NMonitoring { }; using TLabels = TLabelsImpl<TString>; - using ILabelsPtr = THolder<ILabels>; + using ILabelsPtr = THolder<ILabels>; template <typename T> ILabelsPtr MakeLabels() { @@ -411,73 +411,73 @@ namespace NMonitoring { } } -template<> -struct THash<NMonitoring::ILabelsPtr> { - size_t operator()(const NMonitoring::ILabelsPtr& labels) const noexcept { - return labels->Hash(); - } - - size_t operator()(const NMonitoring::ILabels& labels) const noexcept { - return labels.Hash(); - } -}; - -template<typename TStringBackend> -struct THash<NMonitoring::TLabelsImpl<TStringBackend>> { +template<> +struct THash<NMonitoring::ILabelsPtr> { + size_t operator()(const NMonitoring::ILabelsPtr& labels) const noexcept { + return labels->Hash(); + } + + size_t operator()(const NMonitoring::ILabels& labels) const noexcept { + return labels.Hash(); + } +}; + +template<typename TStringBackend> +struct THash<NMonitoring::TLabelsImpl<TStringBackend>> { size_t operator()(const NMonitoring::TLabelsImpl<TStringBackend>& labels) const noexcept { - return labels.Hash(); - } -}; - + return labels.Hash(); + } +}; + template <typename TStringBackend> struct THash<NMonitoring::TLabelImpl<TStringBackend>> { - inline size_t operator()(const NMonitoring::TLabelImpl<TStringBackend>& label) const noexcept { + inline size_t operator()(const NMonitoring::TLabelImpl<TStringBackend>& label) const noexcept { return label.Hash(); } }; -inline bool operator==(const NMonitoring::ILabels& lhs, const NMonitoring::ILabels& rhs) { - if (lhs.Size() != rhs.Size()) { - return false; +inline bool operator==(const NMonitoring::ILabels& lhs, const NMonitoring::ILabels& rhs) { + if (lhs.Size() != rhs.Size()) { + return false; + } + + for (auto&& l : lhs) { + auto rl = rhs.Get(l.Name()); + if (!rl || (*rl)->Value() != l.Value()) { + return false; + } } - - for (auto&& l : lhs) { - auto rl = rhs.Get(l.Name()); - if (!rl || (*rl)->Value() != l.Value()) { - return false; - } - } - - return true; -} - + + return true; +} + bool operator==(const NMonitoring::ILabelsPtr& lhs, const NMonitoring::ILabelsPtr& rhs) = delete; bool operator==(const NMonitoring::ILabels& lhs, const NMonitoring::ILabelsPtr& rhs) = delete; bool operator==(const NMonitoring::ILabelsPtr& lhs, const NMonitoring::ILabels& rhs) = delete; - -template<> -struct TEqualTo<NMonitoring::ILabelsPtr> { - bool operator()(const NMonitoring::ILabelsPtr& lhs, const NMonitoring::ILabelsPtr& rhs) { + +template<> +struct TEqualTo<NMonitoring::ILabelsPtr> { + bool operator()(const NMonitoring::ILabelsPtr& lhs, const NMonitoring::ILabelsPtr& rhs) { return *lhs == *rhs; - } - - bool operator()(const NMonitoring::ILabelsPtr& lhs, const NMonitoring::ILabels& rhs) { + } + + bool operator()(const NMonitoring::ILabelsPtr& lhs, const NMonitoring::ILabels& rhs) { return *lhs == rhs; - } - - bool operator()(const NMonitoring::ILabels& lhs, const NMonitoring::ILabelsPtr& rhs) { + } + + bool operator()(const NMonitoring::ILabels& lhs, const NMonitoring::ILabelsPtr& rhs) { return lhs == *rhs; - } + } }; -#define Y_MONLIB_DEFINE_LABELS_OUT(T) \ +#define Y_MONLIB_DEFINE_LABELS_OUT(T) \ template <> \ -void Out<T>(IOutputStream& out, const T& labels) { \ - Out<NMonitoring::ILabels>(out, labels); \ +void Out<T>(IOutputStream& out, const T& labels) { \ + Out<NMonitoring::ILabels>(out, labels); \ } -#define Y_MONLIB_DEFINE_LABEL_OUT(T) \ +#define Y_MONLIB_DEFINE_LABEL_OUT(T) \ template <> \ -void Out<T>(IOutputStream& out, const T& label) { \ - Out<NMonitoring::ILabel>(out, label); \ +void Out<T>(IOutputStream& out, const T& label) { \ + Out<NMonitoring::ILabel>(out, label); \ } diff --git a/library/cpp/monlib/metrics/labels_ut.cpp b/library/cpp/monlib/metrics/labels_ut.cpp index 5fa4f9132c..f0e4f532ab 100644 --- a/library/cpp/monlib/metrics/labels_ut.cpp +++ b/library/cpp/monlib/metrics/labels_ut.cpp @@ -125,8 +125,8 @@ Y_UNIT_TEST_SUITE(TLabelsTest) { UNIT_ASSERT_EQUAL(labels[1], TLabel("name2", "value2")); TVector<TLabel> labelsCopy; - for (auto&& label : labels) { - labelsCopy.emplace_back(label.Name(), label.Value()); + for (auto&& label : labels) { + labelsCopy.emplace_back(label.Name(), label.Value()); } UNIT_ASSERT_EQUAL(labelsCopy, TVector<TLabel>({ diff --git a/library/cpp/monlib/metrics/log_histogram_snapshot.cpp b/library/cpp/monlib/metrics/log_histogram_snapshot.cpp index 1f444f2157..21cf2ca2bb 100644 --- a/library/cpp/monlib/metrics/log_histogram_snapshot.cpp +++ b/library/cpp/monlib/metrics/log_histogram_snapshot.cpp @@ -1,9 +1,9 @@ -#include "log_histogram_snapshot.h" - -#include <util/stream/output.h> - +#include "log_histogram_snapshot.h" + +#include <util/stream/output.h> + #include <iostream> - + namespace { @@ -14,14 +14,14 @@ auto& Output(TStream& o, const NMonitoring::TLogHistogramSnapshot& hist) { for (auto i = 0u; i < hist.Count(); ++i) { o << hist.UpperBound(i) << TStringBuf(": ") << hist.Bucket(i); o << TStringBuf(", "); - } - + } + o << TStringBuf("zeros: ") << hist.ZerosCount(); - + o << TStringBuf("}"); return o; -} +} } // namespace diff --git a/library/cpp/monlib/metrics/metric.h b/library/cpp/monlib/metrics/metric.h index 95c9332e0a..b8ce12d753 100644 --- a/library/cpp/monlib/metrics/metric.h +++ b/library/cpp/monlib/metrics/metric.h @@ -20,19 +20,19 @@ namespace NMonitoring { using IMetricPtr = THolder<IMetric>; class IGauge: public IMetric { - public: + public: EMetricType Type() const noexcept final { return EMetricType::GAUGE; - } - - virtual double Add(double n) noexcept = 0; - virtual void Set(double n) noexcept = 0; - virtual double Get() const noexcept = 0; - virtual void Reset() noexcept { - Set(0); - } - }; - + } + + virtual double Add(double n) noexcept = 0; + virtual void Set(double n) noexcept = 0; + virtual double Get() const noexcept = 0; + virtual void Reset() noexcept { + Set(0); + } + }; + class ILazyGauge: public IMetric { public: EMetricType Type() const noexcept final { @@ -42,27 +42,27 @@ namespace NMonitoring { }; class IIntGauge: public IMetric { - public: + public: EMetricType Type() const noexcept final { return EMetricType::IGAUGE; - } - - virtual i64 Add(i64 n) noexcept = 0; - virtual i64 Inc() noexcept { - return Add(1); - } - - virtual i64 Dec() noexcept { - return Add(-1); - } - - virtual void Set(i64 value) noexcept = 0; - virtual i64 Get() const noexcept = 0; - virtual void Reset() noexcept { - Set(0); - } - }; - + } + + virtual i64 Add(i64 n) noexcept = 0; + virtual i64 Inc() noexcept { + return Add(1); + } + + virtual i64 Dec() noexcept { + return Add(-1); + } + + virtual void Set(i64 value) noexcept = 0; + virtual i64 Get() const noexcept = 0; + virtual void Reset() noexcept { + Set(0); + } + }; + class ILazyIntGauge: public IMetric { public: EMetricType Type() const noexcept final { @@ -73,20 +73,20 @@ namespace NMonitoring { }; class ICounter: public IMetric { - public: + public: EMetricType Type() const noexcept final { return EMetricType::COUNTER; - } - - virtual ui64 Inc() noexcept { - return Add(1); - } - - virtual ui64 Add(ui64 n) noexcept = 0; - virtual ui64 Get() const noexcept = 0; - virtual void Reset() noexcept = 0; - }; - + } + + virtual ui64 Inc() noexcept { + return Add(1); + } + + virtual ui64 Add(ui64 n) noexcept = 0; + virtual ui64 Get() const noexcept = 0; + virtual void Reset() noexcept = 0; + }; + class ILazyCounter: public IMetric { public: EMetricType Type() const noexcept final { @@ -97,20 +97,20 @@ namespace NMonitoring { }; class IRate: public IMetric { - public: + public: EMetricType Type() const noexcept final { return EMetricType::RATE; - } - - virtual ui64 Inc() noexcept { - return Add(1); - } - - virtual ui64 Add(ui64 n) noexcept = 0; - virtual ui64 Get() const noexcept = 0; + } + + virtual ui64 Inc() noexcept { + return Add(1); + } + + virtual ui64 Add(ui64 n) noexcept = 0; + virtual ui64 Get() const noexcept = 0; virtual void Reset() noexcept = 0; - }; - + }; + class ILazyRate: public IMetric { public: EMetricType Type() const noexcept final { @@ -121,51 +121,51 @@ namespace NMonitoring { }; class IHistogram: public IMetric { - public: - explicit IHistogram(bool isRate) - : IsRate_{isRate} - { - } - + public: + explicit IHistogram(bool isRate) + : IsRate_{isRate} + { + } + EMetricType Type() const noexcept final { return IsRate_ ? EMetricType::HIST_RATE : EMetricType::HIST; - } - + } + virtual void Record(double value) = 0; virtual void Record(double value, ui32 count) = 0; - virtual IHistogramSnapshotPtr TakeSnapshot() const = 0; - virtual void Reset() = 0; - - protected: - const bool IsRate_; - }; - + virtual IHistogramSnapshotPtr TakeSnapshot() const = 0; + virtual void Reset() = 0; + + protected: + const bool IsRate_; + }; + /////////////////////////////////////////////////////////////////////////////// // TGauge /////////////////////////////////////////////////////////////////////////////// - class TGauge final: public IGauge { + class TGauge final: public IGauge { public: explicit TGauge(double value = 0.0) { Set(value); } - double Add(double n) noexcept override { - double newValue; - double oldValue = Get(); + double Add(double n) noexcept override { + double newValue; + double oldValue = Get(); do { - newValue = oldValue + n; - } while (!Value_.compare_exchange_weak(oldValue, newValue, std::memory_order_release, std::memory_order_consume)); + newValue = oldValue + n; + } while (!Value_.compare_exchange_weak(oldValue, newValue, std::memory_order_release, std::memory_order_consume)); - return newValue; + return newValue; } - void Set(double n) noexcept override { - Value_.store(n, std::memory_order_relaxed); + void Set(double n) noexcept override { + Value_.store(n, std::memory_order_relaxed); } - double Get() const noexcept override { - return Value_.load(std::memory_order_relaxed); + double Get() const noexcept override { + return Value_.load(std::memory_order_relaxed); } void Accept(TInstant time, IMetricConsumer* consumer) const override { @@ -173,7 +173,7 @@ namespace NMonitoring { } private: - std::atomic<double> Value_; + std::atomic<double> Value_; }; /////////////////////////////////////////////////////////////////////////////// @@ -201,22 +201,22 @@ namespace NMonitoring { /////////////////////////////////////////////////////////////////////////////// // TIntGauge /////////////////////////////////////////////////////////////////////////////// - class TIntGauge final: public IIntGauge { + class TIntGauge final: public IIntGauge { public: explicit TIntGauge(i64 value = 0) { Set(value); } - i64 Add(i64 n) noexcept override { - return Value_.fetch_add(n, std::memory_order_relaxed) + n; + 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); + void Set(i64 value) noexcept override { + Value_.store(value, std::memory_order_relaxed); } - i64 Get() const noexcept override { - return Value_.load(std::memory_order_relaxed); + i64 Get() const noexcept override { + return Value_.load(std::memory_order_relaxed); } void Accept(TInstant time, IMetricConsumer* consumer) const override { @@ -224,7 +224,7 @@ namespace NMonitoring { } private: - std::atomic_int64_t Value_; + std::atomic_int64_t Value_; }; /////////////////////////////////////////////////////////////////////////////// @@ -255,19 +255,19 @@ namespace NMonitoring { class TCounter final: public ICounter { public: explicit TCounter(ui64 value = 0) { - Value_.store(value, std::memory_order_relaxed); + Value_.store(value, std::memory_order_relaxed); } - ui64 Add(ui64 n) noexcept override { - return Value_.fetch_add(n, std::memory_order_relaxed) + n; + ui64 Add(ui64 n) noexcept override { + return Value_.fetch_add(n, std::memory_order_relaxed) + n; } - ui64 Get() const noexcept override { - return Value_.load(std::memory_order_relaxed); + ui64 Get() const noexcept override { + return Value_.load(std::memory_order_relaxed); } - void Reset() noexcept override { - Value_.store(0, std::memory_order_relaxed); + void Reset() noexcept override { + Value_.store(0, std::memory_order_relaxed); } void Accept(TInstant time, IMetricConsumer* consumer) const override { @@ -275,7 +275,7 @@ namespace NMonitoring { } private: - std::atomic_uint64_t Value_; + std::atomic_uint64_t Value_; }; /////////////////////////////////////////////////////////////////////////////// @@ -303,18 +303,18 @@ namespace NMonitoring { /////////////////////////////////////////////////////////////////////////////// // TRate /////////////////////////////////////////////////////////////////////////////// - class TRate final: public IRate { + class TRate final: public IRate { public: explicit TRate(ui64 value = 0) { - Value_.store(value, std::memory_order_relaxed); + Value_.store(value, std::memory_order_relaxed); } - ui64 Add(ui64 n) noexcept override { - return Value_.fetch_add(n, std::memory_order_relaxed) + n; + ui64 Add(ui64 n) noexcept override { + return Value_.fetch_add(n, std::memory_order_relaxed) + n; } - ui64 Get() const noexcept override { - return Value_.load(std::memory_order_relaxed); + ui64 Get() const noexcept override { + return Value_.load(std::memory_order_relaxed); } void Reset() noexcept override { @@ -326,7 +326,7 @@ namespace NMonitoring { } private: - std::atomic_uint64_t Value_; + std::atomic_uint64_t Value_; }; /////////////////////////////////////////////////////////////////////////////// @@ -354,11 +354,11 @@ namespace NMonitoring { /////////////////////////////////////////////////////////////////////////////// // THistogram /////////////////////////////////////////////////////////////////////////////// - class THistogram final: public IHistogram { + class THistogram final: public IHistogram { public: THistogram(IHistogramCollectorPtr collector, bool isRate) - : IHistogram(isRate) - , Collector_(std::move(collector)) + : IHistogram(isRate) + , Collector_(std::move(collector)) { } @@ -374,14 +374,14 @@ namespace NMonitoring { consumer->OnHistogram(time, TakeSnapshot()); } - IHistogramSnapshotPtr TakeSnapshot() const override { + IHistogramSnapshotPtr TakeSnapshot() const override { return Collector_->Snapshot(); } - void Reset() override { + void Reset() override { Collector_->Reset(); } - + private: IHistogramCollectorPtr Collector_; }; diff --git a/library/cpp/monlib/metrics/metric_registry.cpp b/library/cpp/monlib/metrics/metric_registry.cpp index 6701bc61a0..b083163a7b 100644 --- a/library/cpp/monlib/metrics/metric_registry.cpp +++ b/library/cpp/monlib/metrics/metric_registry.cpp @@ -1,7 +1,7 @@ #include "metric_registry.h" -#include <memory> - +#include <memory> + namespace NMonitoring { namespace { void ConsumeLabels(IMetricConsumer* consumer, const ILabels& labels) { @@ -25,21 +25,21 @@ namespace NMonitoring { } } - void WriteLabels(IMetricConsumer* consumer, const ILabels& labels) { - consumer->OnLabelsBegin(); + void WriteLabels(IMetricConsumer* consumer, const ILabels& labels) { + consumer->OnLabelsBegin(); ConsumeLabels(consumer, labels); - consumer->OnLabelsEnd(); - } - + consumer->OnLabelsEnd(); + } + TMetricRegistry::TMetricRegistry() = default; TMetricRegistry::~TMetricRegistry() = default; - + TMetricRegistry::TMetricRegistry(const TLabels& commonLabels) : TMetricRegistry{} - { - CommonLabels_ = commonLabels; - } - + { + CommonLabels_ = commonLabels; + } + TMetricRegistry* TMetricRegistry::Instance() { return Singleton<TMetricRegistry>(); } @@ -78,12 +78,12 @@ namespace NMonitoring { TCounter* TMetricRegistry::Counter(TLabels labels) { return Metric<TCounter, EMetricType::COUNTER>(std::move(labels)); - } - + } + TCounter* TMetricRegistry::Counter(ILabelsPtr labels) { return Metric<TCounter, EMetricType::COUNTER>(std::move(labels)); - } - + } + TLazyCounter* TMetricRegistry::LazyCounter(TLabels labels, std::function<ui64()> supplier) { return Metric<TLazyCounter, EMetricType::COUNTER>(std::move(labels), std::move(supplier)); } @@ -94,12 +94,12 @@ namespace NMonitoring { TRate* TMetricRegistry::Rate(TLabels labels) { return Metric<TRate, EMetricType::RATE>(std::move(labels)); - } - + } + TRate* TMetricRegistry::Rate(ILabelsPtr labels) { return Metric<TRate, EMetricType::RATE>(std::move(labels)); - } - + } + TLazyRate* TMetricRegistry::LazyRate(TLabels labels, std::function<ui64()> supplier) { return Metric<TLazyRate, EMetricType::RATE>(std::move(labels), std::move(supplier)); } @@ -114,16 +114,16 @@ namespace NMonitoring { THistogram* TMetricRegistry::HistogramCounter(ILabelsPtr labels, IHistogramCollectorPtr collector) { return Metric<THistogram, EMetricType::HIST>(std::move(labels), std::move(collector), false); - } - + } + THistogram* TMetricRegistry::HistogramRate(TLabels labels, IHistogramCollectorPtr collector) { return Metric<THistogram, EMetricType::HIST_RATE>(std::move(labels), std::move(collector), true); } THistogram* TMetricRegistry::HistogramRate(ILabelsPtr labels, IHistogramCollectorPtr collector) { return Metric<THistogram, EMetricType::HIST_RATE>(std::move(labels), std::move(collector), true); - } - + } + void TMetricRegistry::Reset() { TWriteGuard g{Lock_}; for (auto& [label, metric] : Metrics_) { @@ -177,12 +177,12 @@ namespace NMonitoring { TWriteGuard g{Lock_}; // decltype(Metrics_)::iterator breaks build on windows THashMap<ILabelsPtr, IMetricPtr>::iterator it; - if constexpr (!std::is_convertible_v<TLabelsType, ILabelsPtr>) { + if constexpr (!std::is_convertible_v<TLabelsType, ILabelsPtr>) { it = Metrics_.emplace(new TLabels{std::forward<TLabelsType>(labels)}, std::move(metric)).first; - } else { + } else { it = Metrics_.emplace(std::forward<TLabelsType>(labels), std::move(metric)).first; - } - + } + return static_cast<TMetric*>(it->second.Get()); } } @@ -193,14 +193,14 @@ namespace NMonitoring { } void TMetricRegistry::Accept(TInstant time, IMetricConsumer* consumer) const { - consumer->OnStreamBegin(); - - if (!CommonLabels_.Empty()) { + consumer->OnStreamBegin(); + + if (!CommonLabels_.Empty()) { consumer->OnLabelsBegin(); ConsumeLabels(consumer, CommonLabels_); consumer->OnLabelsEnd(); - } - + } + { TReadGuard g{Lock_}; for (const auto& it: Metrics_) { @@ -212,12 +212,12 @@ namespace NMonitoring { } } - consumer->OnStreamEnd(); - } + consumer->OnStreamEnd(); + } void TMetricRegistry::Append(TInstant time, IMetricConsumer* consumer) const { TReadGuard g{Lock_}; - + for (const auto& it: Metrics_) { ILabels* labels = it.first.Get(); IMetric* metric = it.second.Get(); diff --git a/library/cpp/monlib/metrics/metric_registry.h b/library/cpp/monlib/metrics/metric_registry.h index 8c92c1500d..670cf8651e 100644 --- a/library/cpp/monlib/metrics/metric_registry.h +++ b/library/cpp/monlib/metrics/metric_registry.h @@ -6,47 +6,47 @@ #include <util/system/rwlock.h> #include <library/cpp/threading/light_rw_lock/lightrwlock.h> - - + + namespace NMonitoring { class IMetricFactory { - public: + public: virtual ~IMetricFactory() = default; - - virtual IGauge* Gauge(ILabelsPtr labels) = 0; + + virtual IGauge* Gauge(ILabelsPtr labels) = 0; virtual ILazyGauge* LazyGauge(ILabelsPtr labels, std::function<double()> supplier) = 0; - virtual IIntGauge* IntGauge(ILabelsPtr labels) = 0; + virtual IIntGauge* IntGauge(ILabelsPtr labels) = 0; virtual ILazyIntGauge* LazyIntGauge(ILabelsPtr labels, std::function<i64()> supplier) = 0; - virtual ICounter* Counter(ILabelsPtr labels) = 0; + virtual ICounter* Counter(ILabelsPtr labels) = 0; virtual ILazyCounter* LazyCounter(ILabelsPtr labels, std::function<ui64()> supplier) = 0; - virtual IRate* Rate(ILabelsPtr labels) = 0; + virtual IRate* Rate(ILabelsPtr labels) = 0; virtual ILazyRate* LazyRate(ILabelsPtr labels, std::function<ui64()> supplier) = 0; - - virtual IHistogram* HistogramCounter( - ILabelsPtr labels, - IHistogramCollectorPtr collector) = 0; - - virtual IHistogram* HistogramRate( - ILabelsPtr labels, - IHistogramCollectorPtr collector) = 0; - }; - - class IMetricSupplier { - public: - virtual ~IMetricSupplier() = default; - + + virtual IHistogram* HistogramCounter( + ILabelsPtr labels, + IHistogramCollectorPtr collector) = 0; + + virtual IHistogram* HistogramRate( + ILabelsPtr labels, + IHistogramCollectorPtr collector) = 0; + }; + + class IMetricSupplier { + public: + virtual ~IMetricSupplier() = default; + virtual void Accept(TInstant time, IMetricConsumer* consumer) const = 0; virtual void Append(TInstant time, IMetricConsumer* consumer) const = 0; - }; - - class IMetricRegistry: public IMetricSupplier, public IMetricFactory { - public: - virtual const TLabels& CommonLabels() const noexcept = 0; + }; + + class IMetricRegistry: public IMetricSupplier, public IMetricFactory { + public: + virtual const TLabels& CommonLabels() const noexcept = 0; virtual void RemoveMetric(const ILabels& labels) noexcept = 0; - }; - - + }; + + /////////////////////////////////////////////////////////////////////////////// // TMetricRegistry /////////////////////////////////////////////////////////////////////////////// @@ -62,21 +62,21 @@ namespace NMonitoring { */ static TMetricRegistry* Instance(); - TGauge* Gauge(TLabels labels); + TGauge* Gauge(TLabels labels); TLazyGauge* LazyGauge(TLabels labels, std::function<double()> supplier); - TIntGauge* IntGauge(TLabels labels); + TIntGauge* IntGauge(TLabels labels); TLazyIntGauge* LazyIntGauge(TLabels labels, std::function<i64()> supplier); TCounter* Counter(TLabels labels); TLazyCounter* LazyCounter(TLabels labels, std::function<ui64()> supplier); - TRate* Rate(TLabels labels); + TRate* Rate(TLabels labels); TLazyRate* LazyRate(TLabels labels, std::function<ui64()> supplier); THistogram* HistogramCounter( - TLabels labels, + TLabels labels, IHistogramCollectorPtr collector); THistogram* HistogramRate( - TLabels labels, + TLabels labels, IHistogramCollectorPtr collector); /** @@ -91,39 +91,39 @@ namespace NMonitoring { void Accept(TInstant time, IMetricConsumer* consumer) const override; void Append(TInstant time, IMetricConsumer* consumer) const override; - const TLabels& CommonLabels() const noexcept override { + const TLabels& CommonLabels() const noexcept override { return CommonLabels_; } void RemoveMetric(const ILabels& labels) noexcept override; private: - TGauge* Gauge(ILabelsPtr labels) override; + TGauge* Gauge(ILabelsPtr labels) override; TLazyGauge* LazyGauge(ILabelsPtr labels, std::function<double()> supplier) override; - TIntGauge* IntGauge(ILabelsPtr labels) override; + TIntGauge* IntGauge(ILabelsPtr labels) override; TLazyIntGauge* LazyIntGauge(ILabelsPtr labels, std::function<i64()> supplier) override; TCounter* Counter(ILabelsPtr labels) override; TLazyCounter* LazyCounter(ILabelsPtr labels, std::function<ui64()> supplier) override; - TRate* Rate(ILabelsPtr labels) override; + TRate* Rate(ILabelsPtr labels) override; TLazyRate* LazyRate(ILabelsPtr labels, std::function<ui64()> supplier) override; - - THistogram* HistogramCounter( - ILabelsPtr labels, - IHistogramCollectorPtr collector) override; - - THistogram* HistogramRate( - ILabelsPtr labels, - IHistogramCollectorPtr collector) override; - - private: + + THistogram* HistogramCounter( + ILabelsPtr labels, + IHistogramCollectorPtr collector) override; + + THistogram* HistogramRate( + ILabelsPtr labels, + IHistogramCollectorPtr collector) override; + + private: TRWMutex Lock_; THashMap<ILabelsPtr, IMetricPtr> Metrics_; - + template <typename TMetric, EMetricType type, typename TLabelsType, typename... Args> TMetric* Metric(TLabelsType&& labels, Args&&... args); TLabels CommonLabels_; }; - void WriteLabels(IMetricConsumer* consumer, const ILabels& labels); + void WriteLabels(IMetricConsumer* consumer, const ILabels& labels); } diff --git a/library/cpp/monlib/metrics/metric_registry_ut.cpp b/library/cpp/monlib/metrics/metric_registry_ut.cpp index 6eb3e14100..86d9a52ec0 100644 --- a/library/cpp/monlib/metrics/metric_registry_ut.cpp +++ b/library/cpp/monlib/metrics/metric_registry_ut.cpp @@ -10,33 +10,33 @@ using namespace NMonitoring; -template<> -void Out<NMonitoring::NProto::TSingleSample::ValueCase>(IOutputStream& os, NMonitoring::NProto::TSingleSample::ValueCase val) { - switch (val) { - case NMonitoring::NProto::TSingleSample::ValueCase::kInt64: - os << "Int64"; - break; - case NMonitoring::NProto::TSingleSample::ValueCase::kUint64: - os << "Uint64"; - break; - case NMonitoring::NProto::TSingleSample::ValueCase::kHistogram: - os << "Histogram"; - break; - case NMonitoring::NProto::TSingleSample::ValueCase::kFloat64: - os << "Float64"; - break; +template<> +void Out<NMonitoring::NProto::TSingleSample::ValueCase>(IOutputStream& os, NMonitoring::NProto::TSingleSample::ValueCase val) { + switch (val) { + case NMonitoring::NProto::TSingleSample::ValueCase::kInt64: + os << "Int64"; + break; + case NMonitoring::NProto::TSingleSample::ValueCase::kUint64: + os << "Uint64"; + break; + case NMonitoring::NProto::TSingleSample::ValueCase::kHistogram: + os << "Histogram"; + break; + case NMonitoring::NProto::TSingleSample::ValueCase::kFloat64: + os << "Float64"; + break; case NMonitoring::NProto::TSingleSample::ValueCase::kSummaryDouble: os << "DSummary"; break; case NMonitoring::NProto::TSingleSample::ValueCase::kLogHistogram: os << "LogHistogram"; break; - case NMonitoring::NProto::TSingleSample::ValueCase::VALUE_NOT_SET: - os << "NOT SET"; - break; - } -} - + case NMonitoring::NProto::TSingleSample::ValueCase::VALUE_NOT_SET: + os << "NOT SET"; + break; + } +} + Y_UNIT_TEST_SUITE(TMetricRegistryTest) { Y_UNIT_TEST(Gauge) { TMetricRegistry registry(TLabels{{"common", "label"}}); @@ -79,29 +79,29 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) { TMetricRegistry registry(TLabels{{"common", "label"}}); TIntGauge* g = registry.IntGauge({{"my", "gauge"}}); - UNIT_ASSERT_VALUES_EQUAL(g->Get(), 0); + UNIT_ASSERT_VALUES_EQUAL(g->Get(), 0); i64 val; val = g->Inc(); - UNIT_ASSERT_VALUES_EQUAL(g->Get(), 1); - UNIT_ASSERT_VALUES_EQUAL(g->Get(), val); + UNIT_ASSERT_VALUES_EQUAL(g->Get(), 1); + UNIT_ASSERT_VALUES_EQUAL(g->Get(), val); val = g->Dec(); - UNIT_ASSERT_VALUES_EQUAL(g->Get(), 0); - UNIT_ASSERT_VALUES_EQUAL(g->Get(), val); + UNIT_ASSERT_VALUES_EQUAL(g->Get(), 0); + UNIT_ASSERT_VALUES_EQUAL(g->Get(), val); val = g->Add(1); - UNIT_ASSERT_VALUES_EQUAL(g->Get(), 1); - UNIT_ASSERT_VALUES_EQUAL(g->Get(), val); + UNIT_ASSERT_VALUES_EQUAL(g->Get(), 1); + UNIT_ASSERT_VALUES_EQUAL(g->Get(), val); val = g->Add(2); - UNIT_ASSERT_VALUES_EQUAL(g->Get(), 3); - UNIT_ASSERT_VALUES_EQUAL(g->Get(), val); + UNIT_ASSERT_VALUES_EQUAL(g->Get(), 3); + UNIT_ASSERT_VALUES_EQUAL(g->Get(), val); val = g->Add(-5); - UNIT_ASSERT_VALUES_EQUAL(g->Get(), -2); - UNIT_ASSERT_VALUES_EQUAL(g->Get(), val); + UNIT_ASSERT_VALUES_EQUAL(g->Get(), -2); + UNIT_ASSERT_VALUES_EQUAL(g->Get(), val); } Y_UNIT_TEST(LazyIntGauge) { @@ -126,11 +126,11 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) { TMetricRegistry registry(TLabels{{"common", "label"}}); TCounter* c = registry.Counter({{"my", "counter"}}); - UNIT_ASSERT_VALUES_EQUAL(c->Get(), 0); - UNIT_ASSERT_VALUES_EQUAL(c->Inc(), 1); - UNIT_ASSERT_VALUES_EQUAL(c->Get(), 1); - UNIT_ASSERT_VALUES_EQUAL(c->Add(10), 11); - UNIT_ASSERT_VALUES_EQUAL(c->Get(), 11); + UNIT_ASSERT_VALUES_EQUAL(c->Get(), 0); + UNIT_ASSERT_VALUES_EQUAL(c->Inc(), 1); + UNIT_ASSERT_VALUES_EQUAL(c->Get(), 1); + UNIT_ASSERT_VALUES_EQUAL(c->Add(10), 11); + UNIT_ASSERT_VALUES_EQUAL(c->Get(), 11); } Y_UNIT_TEST(LazyCounter) { @@ -159,11 +159,11 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) { TMetricRegistry registry(TLabels{{"common", "label"}}); TCounter* c = registry.Counter({{"my", "counter"}}); - UNIT_ASSERT_VALUES_EQUAL(c->Get(), 0); + UNIT_ASSERT_VALUES_EQUAL(c->Get(), 0); c->Add(10); c = registry.Counter({{"my", "counter"}}); - UNIT_ASSERT_VALUES_EQUAL(c->Get(), 10); + UNIT_ASSERT_VALUES_EQUAL(c->Get(), 10); } Y_UNIT_TEST(Sample) { @@ -180,31 +180,31 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) { auto now = TInstant::Now(); registry.Accept(now, encoder.Get()); - UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 2); - UNIT_ASSERT_VALUES_EQUAL(samples.CommonLabelsSize(), 1); - { - const NProto::TLabel& label = samples.GetCommonLabels(0); - UNIT_ASSERT_STRINGS_EQUAL(label.GetName(), "common"); - UNIT_ASSERT_STRINGS_EQUAL(label.GetValue(), "label"); - } + UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 2); + UNIT_ASSERT_VALUES_EQUAL(samples.CommonLabelsSize(), 1); + { + const NProto::TLabel& label = samples.GetCommonLabels(0); + UNIT_ASSERT_STRINGS_EQUAL(label.GetName(), "common"); + UNIT_ASSERT_STRINGS_EQUAL(label.GetValue(), "label"); + } + - for (const NProto::TSingleSample& sample : samples.GetSamples()) { - UNIT_ASSERT_VALUES_EQUAL(sample.LabelsSize(), 1); - UNIT_ASSERT_VALUES_EQUAL(sample.GetTime(), now.MilliSeconds()); + UNIT_ASSERT_VALUES_EQUAL(sample.LabelsSize(), 1); + UNIT_ASSERT_VALUES_EQUAL(sample.GetTime(), now.MilliSeconds()); if (sample.GetMetricType() == NProto::GAUGE) { - UNIT_ASSERT_VALUES_EQUAL(sample.GetValueCase(), NProto::TSingleSample::kFloat64); + UNIT_ASSERT_VALUES_EQUAL(sample.GetValueCase(), NProto::TSingleSample::kFloat64); UNIT_ASSERT_DOUBLES_EQUAL(sample.GetFloat64(), 12.34, 1E-6); - const NProto::TLabel& label = sample.GetLabels(0); + const NProto::TLabel& label = sample.GetLabels(0); UNIT_ASSERT_STRINGS_EQUAL(label.GetName(), "my"); UNIT_ASSERT_STRINGS_EQUAL(label.GetValue(), "gauge"); } else if (sample.GetMetricType() == NProto::COUNTER) { - UNIT_ASSERT_VALUES_EQUAL(sample.GetValueCase(), NProto::TSingleSample::kUint64); - UNIT_ASSERT_VALUES_EQUAL(sample.GetUint64(), 10); + UNIT_ASSERT_VALUES_EQUAL(sample.GetValueCase(), NProto::TSingleSample::kUint64); + UNIT_ASSERT_VALUES_EQUAL(sample.GetUint64(), 10); - const NProto::TLabel& label = sample.GetLabels(0); + const NProto::TLabel& label = sample.GetLabels(0); UNIT_ASSERT_STRINGS_EQUAL(label.GetName(), "my"); UNIT_ASSERT_STRINGS_EQUAL(label.GetValue(), "counter"); } else { @@ -212,7 +212,7 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) { } } } - + Y_UNIT_TEST(Histograms) { TMetricRegistry registry(TLabels{{"common", "label"}}); @@ -239,32 +239,32 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) { UNIT_ASSERT_NO_DIFF(ss.Str(), NResource::Find("/histograms.json")); } - Y_UNIT_TEST(StreamingEncoderTest) { - const TString expected { - "{\"commonLabels\":{\"common\":\"label\"}," - "\"sensors\":[{\"kind\":\"GAUGE\",\"labels\":{\"my\":\"gauge\"},\"value\":12.34}]}" - }; - + Y_UNIT_TEST(StreamingEncoderTest) { + const TString expected { + "{\"commonLabels\":{\"common\":\"label\"}," + "\"sensors\":[{\"kind\":\"GAUGE\",\"labels\":{\"my\":\"gauge\"},\"value\":12.34}]}" + }; + TMetricRegistry registry(TLabels{{"common", "label"}}); - - TGauge* g = registry.Gauge({{"my", "gauge"}}); - g->Set(12.34); - - TStringStream os; - auto encoder = EncoderJson(&os); - registry.Accept(TInstant::Zero(), encoder.Get()); - - UNIT_ASSERT_STRINGS_EQUAL(os.Str(), expected); - } - + + TGauge* g = registry.Gauge({{"my", "gauge"}}); + g->Set(12.34); + + TStringStream os; + auto encoder = EncoderJson(&os); + registry.Accept(TInstant::Zero(), encoder.Get()); + + UNIT_ASSERT_STRINGS_EQUAL(os.Str(), expected); + } + Y_UNIT_TEST(CreatingSameMetricWithDifferentTypesShouldThrow) { TMetricRegistry registry; - registry.Gauge({{"foo", "bar"}}); - UNIT_ASSERT_EXCEPTION(registry.Counter({{"foo", "bar"}}), yexception); + registry.Gauge({{"foo", "bar"}}); + UNIT_ASSERT_EXCEPTION(registry.Counter({{"foo", "bar"}}), yexception); - registry.HistogramCounter({{"bar", "baz"}}, nullptr); - UNIT_ASSERT_EXCEPTION(registry.HistogramRate({{"bar", "baz"}}, nullptr), yexception); + registry.HistogramCounter({{"bar", "baz"}}, nullptr); + UNIT_ASSERT_EXCEPTION(registry.HistogramRate({{"bar", "baz"}}, nullptr), yexception); } Y_UNIT_TEST(EncodeRegistryWithCommonLabels) { diff --git a/library/cpp/monlib/metrics/metric_type.cpp b/library/cpp/monlib/metrics/metric_type.cpp index 647d7b2020..a8a546e843 100644 --- a/library/cpp/monlib/metrics/metric_type.cpp +++ b/library/cpp/monlib/metrics/metric_type.cpp @@ -21,7 +21,7 @@ namespace NMonitoring { return TStringBuf("HIST_RATE"); case EMetricType::DSUMMARY: return TStringBuf("DSUMMARY"); - case EMetricType::LOGHIST: + case EMetricType::LOGHIST: return TStringBuf("LOGHIST"); default: return TStringBuf("UNKNOWN"); @@ -44,7 +44,7 @@ namespace NMonitoring { } else if (str == TStringBuf("DSUMMARY")) { return EMetricType::DSUMMARY; } else if (str == TStringBuf("LOGHIST")) { - return EMetricType::LOGHIST; + return EMetricType::LOGHIST; } else { ythrow yexception() << "unknown metric type: " << str; } diff --git a/library/cpp/monlib/metrics/metric_value.h b/library/cpp/monlib/metrics/metric_value.h index 18f8dc6fdb..607fcc8602 100644 --- a/library/cpp/monlib/metrics/metric_value.h +++ b/library/cpp/monlib/metrics/metric_value.h @@ -8,23 +8,23 @@ #include <util/datetime/base.h> #include <util/generic/algorithm.h> #include <util/generic/vector.h> -#include <util/generic/cast.h> -#include <util/generic/ymath.h> +#include <util/generic/cast.h> +#include <util/generic/ymath.h> namespace NMonitoring { - namespace NPrivate { - template <typename T> - T FromFloatSafe(double d) { - static_assert(std::is_integral<T>::value, "this function only converts floats to integers"); + namespace NPrivate { + template <typename T> + T FromFloatSafe(double d) { + static_assert(std::is_integral<T>::value, "this function only converts floats to integers"); Y_ENSURE(::IsValidFloat(d) && d >= Min<T>() && d <= MaxFloor<T>(), "Cannot convert " << d << " to an integer value"); - return static_cast<T>(d); - } + return static_cast<T>(d); + } inline auto POINT_KEY_FN = [](auto& p) { return p.GetTime(); }; - } // namespace NPrivate - + } // namespace NPrivate + template <typename T, typename Enable = void> struct TValueType; @@ -132,9 +132,9 @@ namespace NMonitoring { ui64 AsUint64(EMetricValueType type) const { switch (type) { case EMetricValueType::DOUBLE: - return NPrivate::FromFloatSafe<ui64>(Value_.Double); + return NPrivate::FromFloatSafe<ui64>(Value_.Double); case EMetricValueType::INT64: - return SafeIntegerCast<ui64>(Value_.Int64); + return SafeIntegerCast<ui64>(Value_.Int64); case EMetricValueType::UINT64: return Value_.Uint64; case EMetricValueType::HISTOGRAM: @@ -158,11 +158,11 @@ namespace NMonitoring { i64 AsInt64(EMetricValueType type) const { switch (type) { case EMetricValueType::DOUBLE: - return NPrivate::FromFloatSafe<i64>(Value_.Double); + return NPrivate::FromFloatSafe<i64>(Value_.Double); case EMetricValueType::INT64: return Value_.Int64; case EMetricValueType::UINT64: - return SafeIntegerCast<i64>(Value_.Uint64); + return SafeIntegerCast<i64>(Value_.Uint64); case EMetricValueType::HISTOGRAM: ythrow yexception() << "histogram cannot be casted to Int64"; case EMetricValueType::SUMMARY: @@ -434,8 +434,8 @@ namespace NMonitoring { point.GetValue().AsLogHistogram()->Ref(); } } - } - + } + template <typename TConsumer> void ForEach(TConsumer c) const { for (const auto& point : Points_) { @@ -470,8 +470,8 @@ namespace NMonitoring { private: static void CheckTypes(EMetricValueType t1, EMetricValueType t2) { Y_ENSURE(t1 == t2, - "Series type mismatch: expected " << t1 << - ", but got " << t2); + "Series type mismatch: expected " << t1 << + ", but got " << t2); } private: diff --git a/library/cpp/monlib/metrics/metric_value_ut.cpp b/library/cpp/monlib/metrics/metric_value_ut.cpp index d37492d5da..49b47c4057 100644 --- a/library/cpp/monlib/metrics/metric_value_ut.cpp +++ b/library/cpp/monlib/metrics/metric_value_ut.cpp @@ -169,7 +169,7 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { UNIT_ASSERT_VALUES_EQUAL(1, h2->RefCount()); UNIT_ASSERT_VALUES_EQUAL(1, h3->RefCount()); } - + Y_UNIT_TEST(LogHistogramsUnique) { auto ts1 = TInstant::Now(); auto ts2 = ts1 + TDuration::Seconds(1); @@ -220,7 +220,7 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { auto ts1 = TInstant::Now(); auto ts2 = ts1 + TDuration::Seconds(1); auto ts3 = ts2 + TDuration::Seconds(1); - + auto h1 = MakeSummarySnapshot(); auto h2 = MakeSummarySnapshot(); auto h3 = MakeSummarySnapshot(); @@ -262,13 +262,13 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { UNIT_ASSERT_VALUES_EQUAL(1, h3->RefCount()); } - Y_UNIT_TEST(HistogramsUnique2) { - auto ts1 = TInstant::Now(); - auto ts2 = ts1 + TDuration::Seconds(1); - auto ts3 = ts2 + TDuration::Seconds(1); - auto ts4 = ts3 + TDuration::Seconds(1); - auto ts5 = ts4 + TDuration::Seconds(1); - + Y_UNIT_TEST(HistogramsUnique2) { + auto ts1 = TInstant::Now(); + auto ts2 = ts1 + TDuration::Seconds(1); + auto ts3 = ts2 + TDuration::Seconds(1); + 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); @@ -276,30 +276,30 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { 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(ts1, h1.Get()); + timeSeries.Add(ts1, h2.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); + 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); - } - } + } + } Y_UNIT_TEST(LogHistogramsUnique2) { auto ts1 = TInstant::Now(); diff --git a/library/cpp/monlib/metrics/timer.h b/library/cpp/monlib/metrics/timer.h index 78f82dd448..5c4e26e37b 100644 --- a/library/cpp/monlib/metrics/timer.h +++ b/library/cpp/monlib/metrics/timer.h @@ -26,25 +26,25 @@ namespace NMonitoring { TMetricTimerScope(TMetricTimerScope&) = delete; TMetricTimerScope& operator=(const TMetricTimerScope&) = delete; - + TMetricTimerScope(TMetricTimerScope&& other) { - *this = std::move(other); - } - + *this = std::move(other); + } + TMetricTimerScope& operator=(TMetricTimerScope&& other) { Metric_ = other.Metric_; other.Metric_ = nullptr; - StartTime_ = std::move(other.StartTime_); - - return *this; - } - - void Record() { + StartTime_ = std::move(other.StartTime_); + + return *this; + } + + void Record() { Y_VERIFY_DEBUG(Metric_); if (Metric_ == nullptr) { - return; - } - + return; + } + auto duration = std::chrono::duration_cast<Resolution>(Clock::now() - StartTime_).count(); if constexpr (std::is_same<TMetric, TGauge>::value) { Metric_->Set(duration); @@ -59,69 +59,69 @@ namespace NMonitoring { } else { static_assert(TDependentFalse<TMetric>, "Not supported metric type"); } - + Metric_ = nullptr; } ~TMetricTimerScope() { if (Metric_ == nullptr) { - return; - } - - Record(); - } - + return; + } + + Record(); + } + private: TMetric* Metric_{nullptr}; typename Clock::time_point StartTime_; }; - /** - * @brief A class that is supposed to use to measure execution time of an asynchronuous operation. - * - * In order to be able to capture an object into a lambda which is then passed to TFuture::Subscribe/Apply, + /** + * @brief A class that is supposed to use to measure execution time of an asynchronuous operation. + * + * In order to be able to capture an object into a lambda which is then passed to TFuture::Subscribe/Apply, * the object must be copy constructible (limitation of the std::function class). So, we cannot use the TMetricTimerScope - * with the abovementioned functions without storing it in a shared pointer or somewhere else. This class works around this - * issue with wrapping the timer with a auto_ptr-like hack Also, Record is const so that one doesn't need to make every lambda mutable - * just to record time measurement. - */ + * with the abovementioned functions without storing it in a shared pointer or somewhere else. This class works around this + * issue with wrapping the timer with a auto_ptr-like hack Also, Record is const so that one doesn't need to make every lambda mutable + * just to record time measurement. + */ template <typename TMetric, - typename Resolution = std::chrono::milliseconds, - typename Clock = std::chrono::high_resolution_clock> - class TFutureFriendlyTimer { - public: + typename Resolution = std::chrono::milliseconds, + typename Clock = std::chrono::high_resolution_clock> + class TFutureFriendlyTimer { + public: explicit TFutureFriendlyTimer(TMetric* metric) : Impl_{metric} - { - } - - TFutureFriendlyTimer(const TFutureFriendlyTimer& other) - : Impl_{std::move(other.Impl_)} - { - } - - TFutureFriendlyTimer& operator=(const TFutureFriendlyTimer& other) { - Impl_ = std::move(other.Impl_); - } - - TFutureFriendlyTimer(TFutureFriendlyTimer&&) = default; - TFutureFriendlyTimer& operator=(TFutureFriendlyTimer&& other) = default; - - void Record() const { - Impl_.Record(); - } - - private: + { + } + + TFutureFriendlyTimer(const TFutureFriendlyTimer& other) + : Impl_{std::move(other.Impl_)} + { + } + + TFutureFriendlyTimer& operator=(const TFutureFriendlyTimer& other) { + Impl_ = std::move(other.Impl_); + } + + TFutureFriendlyTimer(TFutureFriendlyTimer&&) = default; + TFutureFriendlyTimer& operator=(TFutureFriendlyTimer&& other) = default; + + void Record() const { + Impl_.Record(); + } + + private: mutable TMetricTimerScope<TMetric, Resolution, Clock> Impl_; - }; - + }; + template <typename TMetric> TMetricTimerScope<TMetric> ScopeTimer(TMetric* metric) { return TMetricTimerScope<TMetric>{metric}; - } - + } + template <typename TMetric> TFutureFriendlyTimer<TMetric> FutureTimer(TMetric* metric) { return TFutureFriendlyTimer<TMetric>{metric}; - } + } } diff --git a/library/cpp/monlib/metrics/timer_ut.cpp b/library/cpp/monlib/metrics/timer_ut.cpp index 1786f12d17..c244a8c9e1 100644 --- a/library/cpp/monlib/metrics/timer_ut.cpp +++ b/library/cpp/monlib/metrics/timer_ut.cpp @@ -5,7 +5,7 @@ #include <library/cpp/threading/future/future.h> using namespace NMonitoring; -using namespace NThreading; +using namespace NThreading; Y_UNIT_TEST_SUITE(TTimerTest) { @@ -115,43 +115,43 @@ Y_UNIT_TEST_SUITE(TTimerTest) { } assertHistogram({1, 1, 0, 0}, histogram.TakeSnapshot()); } - - Y_UNIT_TEST(Moving) { - TTestClock::TimePoint = TTestClock::time_point::min(); - + + Y_UNIT_TEST(Moving) { + TTestClock::TimePoint = TTestClock::time_point::min(); + TCounter counter(0); - { + { TMetricTimerScope<TCounter, milliseconds, TTestClock> t{&counter}; - [tt = std::move(t)] { - TTestClock::TimePoint += milliseconds(5); - Y_UNUSED(tt); - }(); - - TTestClock::TimePoint += milliseconds(10); - } - - UNIT_ASSERT_EQUAL(counter.Get(), 5); - } - - Y_UNIT_TEST(MovingIntoApply) { - TTestClock::TimePoint = TTestClock::time_point::min(); - auto pool = CreateThreadPool(1); - + [tt = std::move(t)] { + TTestClock::TimePoint += milliseconds(5); + Y_UNUSED(tt); + }(); + + TTestClock::TimePoint += milliseconds(10); + } + + UNIT_ASSERT_EQUAL(counter.Get(), 5); + } + + Y_UNIT_TEST(MovingIntoApply) { + TTestClock::TimePoint = TTestClock::time_point::min(); + auto pool = CreateThreadPool(1); + TCounter counter(0); - { + { TFutureFriendlyTimer<TCounter, milliseconds, TTestClock> t{&counter}; - - auto f = Async([=] { - return; - }, *pool).Apply([tt = t] (auto) { - TTestClock::TimePoint += milliseconds(5); - tt.Record(); - }); - - f.Wait(); - TTestClock::TimePoint += milliseconds(10); - } - - UNIT_ASSERT_EQUAL(counter.Get(), 5); - } + + auto f = Async([=] { + return; + }, *pool).Apply([tt = t] (auto) { + TTestClock::TimePoint += milliseconds(5); + tt.Record(); + }); + + f.Wait(); + TTestClock::TimePoint += milliseconds(10); + } + + UNIT_ASSERT_EQUAL(counter.Get(), 5); + } } diff --git a/library/cpp/monlib/metrics/ya.make b/library/cpp/monlib/metrics/ya.make index 9f95309125..0e1fa143f9 100644 --- a/library/cpp/monlib/metrics/ya.make +++ b/library/cpp/monlib/metrics/ya.make @@ -14,7 +14,7 @@ SRCS( histogram_collector_exponential.cpp histogram_collector_linear.cpp histogram_snapshot.cpp - log_histogram_snapshot.cpp + log_histogram_snapshot.cpp labels.cpp metric_registry.cpp metric_consumer.cpp |