diff options
author | Sergey Polovko <sergey@polovko.me> | 2022-02-10 16:47:03 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:47:03 +0300 |
commit | 2e714b5ebd40a1f4cc31c27f1ad6e49ca6d895f5 (patch) | |
tree | b83306b6e37edeea782e9eed673d89286c4fef35 /library/cpp/monlib/metrics | |
parent | 3e0b762a82514bac89c1dd6ea7211e381d8aa248 (diff) | |
download | ydb-2e714b5ebd40a1f4cc31c27f1ad6e49ca6d895f5.tar.gz |
Restoring authorship annotation for Sergey Polovko <sergey@polovko.me>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/monlib/metrics')
37 files changed, 2234 insertions, 2234 deletions
diff --git a/library/cpp/monlib/metrics/atomics_array.h b/library/cpp/monlib/metrics/atomics_array.h index c9193f0a33..f19aebf291 100644 --- a/library/cpp/monlib/metrics/atomics_array.h +++ b/library/cpp/monlib/metrics/atomics_array.h @@ -1,52 +1,52 @@ -#pragma once - -#include <util/generic/ptr.h> -#include <util/generic/vector.h> - +#pragma once + +#include <util/generic/ptr.h> +#include <util/generic/vector.h> + #include <atomic> -namespace NMonitoring { - class TAtomicsArray { - public: - explicit TAtomicsArray(size_t size) +namespace NMonitoring { + class TAtomicsArray { + public: + explicit TAtomicsArray(size_t size) : Values_(new std::atomic<ui64>[size]) - , Size_(size) - { - for (size_t i = 0; i < Size_; i++) { + , Size_(size) + { + for (size_t i = 0; i < Size_; i++) { Values_[i].store(0, std::memory_order_relaxed); - } - } - - ui64 operator[](size_t index) const noexcept { - Y_VERIFY_DEBUG(index < Size_); + } + } + + ui64 operator[](size_t index) const noexcept { + Y_VERIFY_DEBUG(index < Size_); return Values_[index].load(std::memory_order_relaxed); - } - - size_t Size() const noexcept { - return Size_; - } - - void Add(size_t index, ui32 count) noexcept { - Y_VERIFY_DEBUG(index < Size_); + } + + size_t Size() const noexcept { + return Size_; + } + + void Add(size_t index, ui32 count) noexcept { + Y_VERIFY_DEBUG(index < Size_); Values_[index].fetch_add(count, std::memory_order_relaxed); - } - + } + void Reset() noexcept { for (size_t i = 0; i < Size_; i++) { Values_[i].store(0, std::memory_order_relaxed); } } - TVector<ui64> Copy() const { - TVector<ui64> copy(Reserve(Size_)); - for (size_t i = 0; i < Size_; i++) { + 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)); - } - return copy; - } - - private: + } + return copy; + } + + private: TArrayHolder<std::atomic<ui64>> Values_; - size_t Size_; - }; -} + size_t Size_; + }; +} diff --git a/library/cpp/monlib/metrics/ewma.cpp b/library/cpp/monlib/metrics/ewma.cpp index 4ee78c0c3d..8a296c3225 100644 --- a/library/cpp/monlib/metrics/ewma.cpp +++ b/library/cpp/monlib/metrics/ewma.cpp @@ -1,5 +1,5 @@ #include "ewma.h" -#include "metric.h" +#include "metric.h" #include <atomic> #include <cmath> @@ -14,12 +14,12 @@ namespace { class TExpMovingAverage final: public IExpMovingAverage { public: - explicit TExpMovingAverage(IGauge* metric, double alpha, TDuration interval) - : Metric_{metric} + explicit TExpMovingAverage(IGauge* metric, double alpha, TDuration interval) + : Metric_{metric} , Alpha_{alpha} , Interval_{interval.Seconds()} { - Y_VERIFY(metric != nullptr, "Passing nullptr metric is not allowed"); + Y_VERIFY(metric != nullptr, "Passing nullptr metric is not allowed"); } ~TExpMovingAverage() override = default; @@ -30,11 +30,11 @@ namespace { const double instantRate = double(current) / Interval_; if (Y_UNLIKELY(!IsInitialized())) { - Metric_->Set(instantRate); + Metric_->Set(instantRate); Init_ = true; } else { - const double currentRate = Metric_->Get(); - Metric_->Set(Alpha_ * (instantRate - currentRate) + currentRate); + const double currentRate = Metric_->Get(); + Metric_->Set(Alpha_ * (instantRate - currentRate) + currentRate); } } @@ -44,7 +44,7 @@ namespace { } double Rate() const override { - return Metric_->Get(); + return Metric_->Get(); } void Reset() override { @@ -61,7 +61,7 @@ namespace { std::atomic<i64> Uncounted_{0}; std::atomic<bool> Init_{false}; - IGauge* Metric_{nullptr}; + IGauge* Metric_{nullptr}; double Alpha_; ui64 Interval_; }; @@ -132,19 +132,19 @@ namespace { return Ewma_->Rate(); } - IExpMovingAveragePtr OneMinuteEwma(IGauge* metric) { - return MakeHolder<TExpMovingAverage>(metric, ALPHA1, DEFAULT_INTERVAL); + IExpMovingAveragePtr OneMinuteEwma(IGauge* metric) { + return MakeHolder<TExpMovingAverage>(metric, ALPHA1, DEFAULT_INTERVAL); } - IExpMovingAveragePtr FiveMinuteEwma(IGauge* metric) { - return MakeHolder<TExpMovingAverage>(metric, ALPHA5, 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 FiveteenMinuteEwma(IGauge* metric) { + return MakeHolder<TExpMovingAverage>(metric, ALPHA15, DEFAULT_INTERVAL); } - IExpMovingAveragePtr CreateEwma(IGauge* metric, double alpha, TDuration interval) { - return MakeHolder<TExpMovingAverage>(metric, alpha, interval); + IExpMovingAveragePtr CreateEwma(IGauge* metric, double alpha, TDuration interval) { + return MakeHolder<TExpMovingAverage>(metric, alpha, interval); } } // namespace NMonitoring diff --git a/library/cpp/monlib/metrics/ewma_ut.cpp b/library/cpp/monlib/metrics/ewma_ut.cpp index 56e292f9fd..01ef2478f7 100644 --- a/library/cpp/monlib/metrics/ewma_ut.cpp +++ b/library/cpp/monlib/metrics/ewma_ut.cpp @@ -1,5 +1,5 @@ #include "ewma.h" -#include "metric.h" +#include "metric.h" #include <library/cpp/testing/unittest/registar.h> @@ -15,9 +15,9 @@ void ElapseMinute(IExpMovingAverage& ewma) { Y_UNIT_TEST_SUITE(TEwmaTest) { Y_UNIT_TEST(OneMinute) { - TGauge gauge; + TGauge gauge; - auto ewma = OneMinuteEwma(&gauge); + auto ewma = OneMinuteEwma(&gauge); ewma->Update(3); ewma->Tick(); @@ -47,9 +47,9 @@ Y_UNIT_TEST_SUITE(TEwmaTest) { } Y_UNIT_TEST(FiveMinutes) { - TGauge gauge; + TGauge gauge; - auto ewma = FiveMinuteEwma(&gauge); + auto ewma = FiveMinuteEwma(&gauge); ewma->Update(3); ewma->Tick(); @@ -79,9 +79,9 @@ Y_UNIT_TEST_SUITE(TEwmaTest) { } Y_UNIT_TEST(FiveteenMinutes) { - TGauge gauge; + TGauge gauge; - auto ewma = FiveteenMinuteEwma(&gauge); + auto ewma = FiveteenMinuteEwma(&gauge); ewma->Update(3); ewma->Tick(); diff --git a/library/cpp/monlib/metrics/fake.cpp b/library/cpp/monlib/metrics/fake.cpp index 41522314ca..b6f5e37af8 100644 --- a/library/cpp/monlib/metrics/fake.cpp +++ b/library/cpp/monlib/metrics/fake.cpp @@ -1,9 +1,9 @@ #include "fake.h" namespace NMonitoring { - - IGauge* TFakeMetricRegistry::Gauge(ILabelsPtr labels) { - return Metric<TFakeGauge, EMetricType::GAUGE>(std::move(labels)); + + IGauge* TFakeMetricRegistry::Gauge(ILabelsPtr labels) { + return Metric<TFakeGauge, EMetricType::GAUGE>(std::move(labels)); } ILazyGauge* TFakeMetricRegistry::LazyGauge(ILabelsPtr labels, std::function<double()> supplier) { @@ -11,8 +11,8 @@ namespace NMonitoring { return Metric<TFakeLazyGauge, EMetricType::GAUGE>(std::move(labels)); } - IIntGauge* TFakeMetricRegistry::IntGauge(ILabelsPtr labels) { - return Metric<TFakeIntGauge, EMetricType::IGAUGE>(std::move(labels)); + IIntGauge* TFakeMetricRegistry::IntGauge(ILabelsPtr labels) { + return Metric<TFakeIntGauge, EMetricType::IGAUGE>(std::move(labels)); } ILazyIntGauge* TFakeMetricRegistry::LazyIntGauge(ILabelsPtr labels, std::function<i64()> supplier) { @@ -20,8 +20,8 @@ namespace NMonitoring { return Metric<TFakeLazyIntGauge, EMetricType::IGAUGE>(std::move(labels)); } - ICounter* TFakeMetricRegistry::Counter(ILabelsPtr labels) { - return Metric<TFakeCounter, EMetricType::COUNTER>(std::move(labels)); + ICounter* TFakeMetricRegistry::Counter(ILabelsPtr labels) { + return Metric<TFakeCounter, EMetricType::COUNTER>(std::move(labels)); } ILazyCounter* TFakeMetricRegistry::LazyCounter(ILabelsPtr labels, std::function<ui64()> supplier) { @@ -29,8 +29,8 @@ namespace NMonitoring { return Metric<TFakeLazyCounter, EMetricType::COUNTER>(std::move(labels)); } - IRate* TFakeMetricRegistry::Rate(ILabelsPtr labels) { - return Metric<TFakeRate, EMetricType::RATE>(std::move(labels)); + IRate* TFakeMetricRegistry::Rate(ILabelsPtr labels) { + return Metric<TFakeRate, EMetricType::RATE>(std::move(labels)); } ILazyRate* TFakeMetricRegistry::LazyRate(ILabelsPtr labels, std::function<ui64()> supplier) { @@ -38,46 +38,46 @@ namespace NMonitoring { return Metric<TFakeLazyRate, EMetricType::RATE>(std::move(labels)); } - IHistogram* TFakeMetricRegistry::HistogramCounter(ILabelsPtr labels, IHistogramCollectorPtr collector) { + IHistogram* TFakeMetricRegistry::HistogramCounter(ILabelsPtr labels, IHistogramCollectorPtr collector) { Y_UNUSED(collector); - return Metric<TFakeHistogram, EMetricType::HIST>(std::move(labels), false); + return Metric<TFakeHistogram, EMetricType::HIST>(std::move(labels), false); } - void TFakeMetricRegistry::RemoveMetric(const ILabels& labels) noexcept { + void TFakeMetricRegistry::RemoveMetric(const ILabels& labels) noexcept { TWriteGuard g{Lock_}; - Metrics_.erase(labels); + Metrics_.erase(labels); } - void TFakeMetricRegistry::Accept(TInstant time, IMetricConsumer* consumer) const { + void TFakeMetricRegistry::Accept(TInstant time, IMetricConsumer* consumer) const { Y_UNUSED(time); consumer->OnStreamBegin(); consumer->OnStreamEnd(); } - IHistogram* TFakeMetricRegistry::HistogramRate(ILabelsPtr labels, IHistogramCollectorPtr collector) { + IHistogram* TFakeMetricRegistry::HistogramRate(ILabelsPtr labels, IHistogramCollectorPtr collector) { Y_UNUSED(collector); - return Metric<TFakeHistogram, EMetricType::HIST_RATE>(std::move(labels), true); + return Metric<TFakeHistogram, EMetricType::HIST_RATE>(std::move(labels), true); } - void TFakeMetricRegistry::Append(TInstant time, IMetricConsumer* consumer) const { + void TFakeMetricRegistry::Append(TInstant time, IMetricConsumer* consumer) const { Y_UNUSED(time, consumer); } - const TLabels& TFakeMetricRegistry::CommonLabels() const noexcept { + const TLabels& TFakeMetricRegistry::CommonLabels() const noexcept { return CommonLabels_; } - template <typename TMetric, EMetricType type, typename TLabelsType, typename... Args> - TMetric* TFakeMetricRegistry::Metric(TLabelsType&& labels, Args&&... args) { + template <typename TMetric, EMetricType type, typename TLabelsType, typename... Args> + TMetric* TFakeMetricRegistry::Metric(TLabelsType&& labels, Args&&... args) { { 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()); + 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()); } } @@ -86,15 +86,15 @@ namespace NMonitoring { IMetricPtr metric = MakeHolder<TMetric>(std::forward<Args>(args)...); - // decltype(Metrics_)::iterator breaks build on windows - THashMap<ILabelsPtr, IMetricPtr>::iterator it; + // decltype(Metrics_)::iterator breaks build on windows + THashMap<ILabelsPtr, IMetricPtr>::iterator it; if constexpr (!std::is_convertible_v<TLabelsType, ILabelsPtr>) { - it = Metrics_.emplace(new TLabels{std::forward<TLabelsType>(labels)}, std::move(metric)).first; + it = Metrics_.emplace(new TLabels{std::forward<TLabelsType>(labels)}, std::move(metric)).first; } else { - it = Metrics_.emplace(std::forward<TLabelsType>(labels), std::move(metric)).first; + it = Metrics_.emplace(std::forward<TLabelsType>(labels), std::move(metric)).first; } - return static_cast<TMetric*>(it->second.Get()); + return static_cast<TMetric*>(it->second.Get()); } } } // namespace NMonitoring diff --git a/library/cpp/monlib/metrics/fake.h b/library/cpp/monlib/metrics/fake.h index c2e40b267b..61ba4f2bd4 100644 --- a/library/cpp/monlib/metrics/fake.h +++ b/library/cpp/monlib/metrics/fake.h @@ -1,21 +1,21 @@ #pragma once -#include "metric.h" -#include "metric_registry.h" +#include "metric.h" +#include "metric_registry.h" namespace NMonitoring { - class TFakeMetricRegistry: public IMetricRegistry { + class TFakeMetricRegistry: public IMetricRegistry { public: - TFakeMetricRegistry() noexcept - : CommonLabels_{0} - { - } - - explicit TFakeMetricRegistry(TLabels commonLabels) noexcept - : CommonLabels_{std::move(commonLabels)} - { - } - + TFakeMetricRegistry() noexcept + : CommonLabels_{0} + { + } + + explicit TFakeMetricRegistry(TLabels commonLabels) noexcept + : CommonLabels_{std::move(commonLabels)} + { + } + IGauge* Gauge(ILabelsPtr labels) override; ILazyGauge* LazyGauge(ILabelsPtr labels, std::function<double()> supplier) override; IIntGauge* IntGauge(ILabelsPtr labels) override; @@ -32,25 +32,25 @@ namespace NMonitoring { IHistogram* HistogramRate( ILabelsPtr labels, IHistogramCollectorPtr collector) override; - void Accept(TInstant time, IMetricConsumer* consumer) const override; - void Append(TInstant time, IMetricConsumer* consumer) const override; + void Accept(TInstant time, IMetricConsumer* consumer) const override; + void Append(TInstant time, IMetricConsumer* consumer) const override; const TLabels& CommonLabels() const noexcept override; - void RemoveMetric(const ILabels& labels) noexcept override; + void RemoveMetric(const ILabels& labels) noexcept override; private: TRWMutex Lock_; - THashMap<ILabelsPtr, IMetricPtr> Metrics_; + THashMap<ILabelsPtr, IMetricPtr> Metrics_; - template <typename TMetric, EMetricType type, typename TLabelsType, typename... Args> - TMetric* Metric(TLabelsType&& labels, Args&&... args); + template <typename TMetric, EMetricType type, typename TLabelsType, typename... Args> + TMetric* Metric(TLabelsType&& labels, Args&&... args); const TLabels CommonLabels_; }; template <typename TBase> struct TFakeAcceptor: TBase { - void Accept(TInstant time, IMetricConsumer* consumer) const override { + void Accept(TInstant time, IMetricConsumer* consumer) const override { Y_UNUSED(time, consumer); } }; @@ -85,9 +85,9 @@ namespace NMonitoring { ui64 Get() const noexcept override { return 0; } - - void Reset() noexcept override { - } + + void Reset() noexcept override { + } }; struct TFakeLazyRate final: public TFakeAcceptor<ILazyRate> { @@ -123,11 +123,11 @@ namespace NMonitoring { { } - void Record(double value) override { + void Record(double value) override { Y_UNUSED(value); } - void Record(double value, ui32 count) override { + void Record(double value, ui32 count) override { Y_UNUSED(value, count); } @@ -135,7 +135,7 @@ namespace NMonitoring { return nullptr; } - void Accept(TInstant time, IMetricConsumer* consumer) const override { + void Accept(TInstant time, IMetricConsumer* consumer) const override { Y_UNUSED(time, consumer); } @@ -148,7 +148,7 @@ namespace NMonitoring { Y_UNUSED(n); return 0; } - + ui64 Get() const noexcept override { return 0; } diff --git a/library/cpp/monlib/metrics/fake_ut.cpp b/library/cpp/monlib/metrics/fake_ut.cpp index af164f4628..c3368ca302 100644 --- a/library/cpp/monlib/metrics/fake_ut.cpp +++ b/library/cpp/monlib/metrics/fake_ut.cpp @@ -1,34 +1,34 @@ -#include "fake.h" - -#include <library/cpp/testing/unittest/registar.h> - -#include <util/generic/ptr.h> - -using namespace NMonitoring; - -Y_UNIT_TEST_SUITE(TFakeTest) { - - Y_UNIT_TEST(CreateOnStack) { - TFakeMetricRegistry registry; - } - - Y_UNIT_TEST(CreateOnHeap) { - auto registry = MakeAtomicShared<TFakeMetricRegistry>(); - UNIT_ASSERT(registry); - } - - Y_UNIT_TEST(Gauge) { - TFakeMetricRegistry registry(TLabels{{"common", "label"}}); - - IGauge* g = registry.Gauge(MakeLabels({{"my", "gauge"}})); - UNIT_ASSERT(g); - - UNIT_ASSERT_DOUBLES_EQUAL(g->Get(), 0.0, 1E-6); - g->Set(12.34); - UNIT_ASSERT_DOUBLES_EQUAL(g->Get(), 0.0, 1E-6); // no changes - - double val = g->Add(1.2); - UNIT_ASSERT_DOUBLES_EQUAL(g->Get(), 0.0, 1E-6); - UNIT_ASSERT_DOUBLES_EQUAL(val, 0.0, 1E-6); - } -} +#include "fake.h" + +#include <library/cpp/testing/unittest/registar.h> + +#include <util/generic/ptr.h> + +using namespace NMonitoring; + +Y_UNIT_TEST_SUITE(TFakeTest) { + + Y_UNIT_TEST(CreateOnStack) { + TFakeMetricRegistry registry; + } + + Y_UNIT_TEST(CreateOnHeap) { + auto registry = MakeAtomicShared<TFakeMetricRegistry>(); + UNIT_ASSERT(registry); + } + + Y_UNIT_TEST(Gauge) { + TFakeMetricRegistry registry(TLabels{{"common", "label"}}); + + IGauge* g = registry.Gauge(MakeLabels({{"my", "gauge"}})); + UNIT_ASSERT(g); + + UNIT_ASSERT_DOUBLES_EQUAL(g->Get(), 0.0, 1E-6); + g->Set(12.34); + UNIT_ASSERT_DOUBLES_EQUAL(g->Get(), 0.0, 1E-6); // no changes + + double val = g->Add(1.2); + UNIT_ASSERT_DOUBLES_EQUAL(g->Get(), 0.0, 1E-6); + UNIT_ASSERT_DOUBLES_EQUAL(val, 0.0, 1E-6); + } +} diff --git a/library/cpp/monlib/metrics/fwd.h b/library/cpp/monlib/metrics/fwd.h index 91b0ad9fcb..b4327ee5d5 100644 --- a/library/cpp/monlib/metrics/fwd.h +++ b/library/cpp/monlib/metrics/fwd.h @@ -1,40 +1,40 @@ -#pragma once - -namespace NMonitoring { - - struct ILabel; - struct ILabels; - - class ICounter; - class IGauge; - class IHistogram; - class IIntGauge; - class ILazyCounter; - class ILazyGauge; - class ILazyIntGauge; - class ILazyRate; - class IMetric; - class IRate; - class TCounter; - class TGauge; - class THistogram; - class TIntGauge; - class TLazyCounter; - class TLazyGauge; - class TLazyIntGauge; - class TLazyRate; - class TRate; - - class IMetricSupplier; - class IMetricFactory; - class IMetricConsumer; - - class IMetricRegistry; - class TMetricRegistry; - - class IHistogramCollector; - class IHistogramSnapshot; - - class IExpMovingAverage; - -} // namespace NMonitoring +#pragma once + +namespace NMonitoring { + + struct ILabel; + struct ILabels; + + class ICounter; + class IGauge; + class IHistogram; + class IIntGauge; + class ILazyCounter; + class ILazyGauge; + class ILazyIntGauge; + class ILazyRate; + class IMetric; + class IRate; + class TCounter; + class TGauge; + class THistogram; + class TIntGauge; + class TLazyCounter; + class TLazyGauge; + class TLazyIntGauge; + class TLazyRate; + class TRate; + + class IMetricSupplier; + class IMetricFactory; + class IMetricConsumer; + + class IMetricRegistry; + class TMetricRegistry; + + class IHistogramCollector; + class IHistogramSnapshot; + + class IExpMovingAverage; + +} // namespace NMonitoring diff --git a/library/cpp/monlib/metrics/histogram_collector.h b/library/cpp/monlib/metrics/histogram_collector.h index b1d628ce9e..9f6bbbdfb7 100644 --- a/library/cpp/monlib/metrics/histogram_collector.h +++ b/library/cpp/monlib/metrics/histogram_collector.h @@ -1,119 +1,119 @@ -#pragma once - -#include "histogram_snapshot.h" - -namespace NMonitoring { - - /////////////////////////////////////////////////////////////////////////// - // IHistogramCollector - /////////////////////////////////////////////////////////////////////////// - class IHistogramCollector { - public: - virtual ~IHistogramCollector() = default; - - /** - * Store {@code count} times given {@code value} in this collector. - */ +#pragma once + +#include "histogram_snapshot.h" + +namespace NMonitoring { + + /////////////////////////////////////////////////////////////////////////// + // IHistogramCollector + /////////////////////////////////////////////////////////////////////////// + class IHistogramCollector { + public: + virtual ~IHistogramCollector() = default; + + /** + * Store {@code count} times given {@code value} in this collector. + */ virtual void Collect(double value, ui32 count) = 0; - - /** - * Store given {@code value} in this collector. - */ - void Collect(double value) { - Collect(value, 1); - } - - /** - * Add counts from snapshot into this collector - */ - void Collect(const IHistogramSnapshot& snapshot) { - for (ui32 i = 0; i < snapshot.Count(); i++) { - Collect(snapshot.UpperBound(i), snapshot.Value(i)); - } - } - - /** + + /** + * Store given {@code value} in this collector. + */ + void Collect(double value) { + Collect(value, 1); + } + + /** + * Add counts from snapshot into this collector + */ + void Collect(const IHistogramSnapshot& snapshot) { + for (ui32 i = 0; i < snapshot.Count(); i++) { + Collect(snapshot.UpperBound(i), snapshot.Value(i)); + } + } + + /** * Reset collector values */ virtual void Reset() = 0; /** - * @return snapshot of the state of this collector. - */ - virtual IHistogramSnapshotPtr Snapshot() const = 0; - }; - - using IHistogramCollectorPtr = THolder<IHistogramCollector>; - - /////////////////////////////////////////////////////////////////////////// - // free functions - /////////////////////////////////////////////////////////////////////////// - - /** - * <p>Creates histogram collector for a set of buckets with arbitrary - * bounds.</p> - * - * <p>Defines {@code bounds.size() + 1} buckets with these boundaries for - * bucket i:</p> - * <ul> - * <li>Upper bound (0 <= i < N-1): {@code bounds[i]}</li> - * <li>Lower bound (1 <= i < N): {@code bounds[i - 1]}</li> - * </ul> - * - * <p>For example, if the list of boundaries is:</p> - * <pre>0, 1, 2, 5, 10, 20</pre> - * - * <p>then there are five finite buckets with the following ranges:</p> - * <pre>(-INF, 0], (0, 1], (1, 2], (2, 5], (5, 10], (10, 20], (20, +INF)</pre> - * - * @param bounds array of upper bounds for buckets. Values must be sorted. - */ + * @return snapshot of the state of this collector. + */ + virtual IHistogramSnapshotPtr Snapshot() const = 0; + }; + + using IHistogramCollectorPtr = THolder<IHistogramCollector>; + + /////////////////////////////////////////////////////////////////////////// + // free functions + /////////////////////////////////////////////////////////////////////////// + + /** + * <p>Creates histogram collector for a set of buckets with arbitrary + * bounds.</p> + * + * <p>Defines {@code bounds.size() + 1} buckets with these boundaries for + * bucket i:</p> + * <ul> + * <li>Upper bound (0 <= i < N-1): {@code bounds[i]}</li> + * <li>Lower bound (1 <= i < N): {@code bounds[i - 1]}</li> + * </ul> + * + * <p>For example, if the list of boundaries is:</p> + * <pre>0, 1, 2, 5, 10, 20</pre> + * + * <p>then there are five finite buckets with the following ranges:</p> + * <pre>(-INF, 0], (0, 1], (1, 2], (2, 5], (5, 10], (10, 20], (20, +INF)</pre> + * + * @param bounds array of upper bounds for buckets. Values must be sorted. + */ IHistogramCollectorPtr ExplicitHistogram(TBucketBounds bounds); - - /** - * <p>Creates histogram collector for a sequence of buckets that have a - * width proportional to the value of the lower bound.</p> - * - * <p>Defines {@code bucketsCount} buckets with these boundaries for bucket i:</p> - * <ul> - * <li>Upper bound (0 <= i < N-1): {@code scale * (base ^ i)}</li> - * <li>Lower bound (1 <= i < N): {@code scale * (base ^ (i - 1))}</li> - * </ul> - * - * <p>For example, if {@code bucketsCount=6}, {@code base=2}, and {@code scale=3}, - * then the bucket ranges are as follows:</p> - * - * <pre>(-INF, 3], (3, 6], (6, 12], (12, 24], (24, 48], (48, +INF)</pre> - * - * @param bucketsCount the total number of buckets. The value must be >= 2. - * @param base the exponential growth factor for the buckets width. - * The value must be >= 1.0. - * @param scale the linear scale for the buckets. The value must be >= 1.0. - */ - IHistogramCollectorPtr ExponentialHistogram( - ui32 bucketsCount, double base, double scale = 1.0); - - /** - * <p>Creates histogram collector for a sequence of buckets that all have - * the same width (except overflow and underflow).</p> - * - * <p>Defines {@code bucketsCount} buckets with these boundaries for bucket i:</p> - * <ul> - * <li>Upper bound (0 <= i < N-1): {@code startValue + bucketWidth * i}</li> - * <li>Lower bound (1 <= i < N): {@code startValue + bucketWidth * (i - 1)}</li> - * </ul> - * - * <p>For example, if {@code bucketsCount=6}, {@code startValue=5}, and - * {@code bucketWidth=15}, then the bucket ranges are as follows:</p> - * - * <pre>(-INF, 5], (5, 20], (20, 35], (35, 50], (50, 65], (65, +INF)</pre> - * - * @param bucketsCount the total number of buckets. The value must be >= 2. - * @param startValue the upper boundary of the first bucket. - * @param bucketWidth the difference between the upper and lower bounds for - * each bucket. The value must be >= 1. - */ - IHistogramCollectorPtr LinearHistogram( + + /** + * <p>Creates histogram collector for a sequence of buckets that have a + * width proportional to the value of the lower bound.</p> + * + * <p>Defines {@code bucketsCount} buckets with these boundaries for bucket i:</p> + * <ul> + * <li>Upper bound (0 <= i < N-1): {@code scale * (base ^ i)}</li> + * <li>Lower bound (1 <= i < N): {@code scale * (base ^ (i - 1))}</li> + * </ul> + * + * <p>For example, if {@code bucketsCount=6}, {@code base=2}, and {@code scale=3}, + * then the bucket ranges are as follows:</p> + * + * <pre>(-INF, 3], (3, 6], (6, 12], (12, 24], (24, 48], (48, +INF)</pre> + * + * @param bucketsCount the total number of buckets. The value must be >= 2. + * @param base the exponential growth factor for the buckets width. + * The value must be >= 1.0. + * @param scale the linear scale for the buckets. The value must be >= 1.0. + */ + IHistogramCollectorPtr ExponentialHistogram( + ui32 bucketsCount, double base, double scale = 1.0); + + /** + * <p>Creates histogram collector for a sequence of buckets that all have + * the same width (except overflow and underflow).</p> + * + * <p>Defines {@code bucketsCount} buckets with these boundaries for bucket i:</p> + * <ul> + * <li>Upper bound (0 <= i < N-1): {@code startValue + bucketWidth * i}</li> + * <li>Lower bound (1 <= i < N): {@code startValue + bucketWidth * (i - 1)}</li> + * </ul> + * + * <p>For example, if {@code bucketsCount=6}, {@code startValue=5}, and + * {@code bucketWidth=15}, then the bucket ranges are as follows:</p> + * + * <pre>(-INF, 5], (5, 20], (20, 35], (35, 50], (50, 65], (65, +INF)</pre> + * + * @param bucketsCount the total number of buckets. The value must be >= 2. + * @param startValue the upper boundary of the first bucket. + * @param bucketWidth the difference between the upper and lower bounds for + * each bucket. The value must be >= 1. + */ + IHistogramCollectorPtr LinearHistogram( ui32 bucketsCount, TBucketBound startValue, TBucketBound bucketWidth); - + } // namespace NMonitoring diff --git a/library/cpp/monlib/metrics/histogram_collector_explicit.cpp b/library/cpp/monlib/metrics/histogram_collector_explicit.cpp index 90c41a235a..377fc233ef 100644 --- a/library/cpp/monlib/metrics/histogram_collector_explicit.cpp +++ b/library/cpp/monlib/metrics/histogram_collector_explicit.cpp @@ -1,55 +1,55 @@ -#include "histogram_collector.h" -#include "atomics_array.h" - -#include <util/generic/algorithm.h> -#include <util/generic/vector.h> -#include <util/generic/yexception.h> -#include <util/generic/ylimits.h> - -namespace NMonitoring { - - /////////////////////////////////////////////////////////////////////////// - // TExplicitHistogramCollector - /////////////////////////////////////////////////////////////////////////// - class TExplicitHistogramCollector: public IHistogramCollector { - public: +#include "histogram_collector.h" +#include "atomics_array.h" + +#include <util/generic/algorithm.h> +#include <util/generic/vector.h> +#include <util/generic/yexception.h> +#include <util/generic/ylimits.h> + +namespace NMonitoring { + + /////////////////////////////////////////////////////////////////////////// + // TExplicitHistogramCollector + /////////////////////////////////////////////////////////////////////////// + class TExplicitHistogramCollector: public IHistogramCollector { + public: TExplicitHistogramCollector(TBucketBounds bounds) : Values_(bounds.size() + 1) , Bounds_(std::move(bounds)) - { - // add one bucket as +INF - Bounds_.push_back(Max<TBucketBound>()); - } - + { + // 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); - auto index = std::distance(Bounds_.begin(), it); - Values_.Add(index, count); - } - + auto index = std::distance(Bounds_.begin(), it); + Values_.Add(index, count); + } + void Reset() override { Values_.Reset(); } - IHistogramSnapshotPtr Snapshot() const override { + IHistogramSnapshotPtr Snapshot() const override { auto values = Values_.Copy(); return ExplicitHistogramSnapshot(Bounds_, values); - } - - private: + } + + private: TAtomicsArray Values_; - TBucketBounds Bounds_; - }; - - IHistogramCollectorPtr ExplicitHistogram(TBucketBounds bounds) { - Y_ENSURE(bounds.size() >= 1, - "explicit histogram must contain at least one bucket"); + 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 - << ", but got: " << bounds.size()); - Y_ENSURE(IsSorted(bounds.begin(), bounds.end()), - "bounds for explicit histogram must be sorted"); - + << ", but got: " << bounds.size()); + Y_ENSURE(IsSorted(bounds.begin(), bounds.end()), + "bounds for explicit histogram must be sorted"); + return MakeHolder<TExplicitHistogramCollector>(bounds); - } -} + } +} diff --git a/library/cpp/monlib/metrics/histogram_collector_exponential.cpp b/library/cpp/monlib/metrics/histogram_collector_exponential.cpp index a6b929e8f3..2f8a50a5f9 100644 --- a/library/cpp/monlib/metrics/histogram_collector_exponential.cpp +++ b/library/cpp/monlib/metrics/histogram_collector_exponential.cpp @@ -1,68 +1,68 @@ -#include "histogram_collector.h" -#include "atomics_array.h" - -#include <util/generic/algorithm.h> -#include <util/generic/vector.h> -#include <util/generic/yexception.h> -#include <util/generic/ylimits.h> - -namespace NMonitoring { - /////////////////////////////////////////////////////////////////////////// - // TExponentialHistogramCollector - /////////////////////////////////////////////////////////////////////////// - class TExponentialHistogramCollector: public IHistogramCollector { - public: - TExponentialHistogramCollector(ui32 bucketsCount, double base, double scale) - : Values_(bucketsCount) - , Base_(base) - , Scale_(scale) +#include "histogram_collector.h" +#include "atomics_array.h" + +#include <util/generic/algorithm.h> +#include <util/generic/vector.h> +#include <util/generic/yexception.h> +#include <util/generic/ylimits.h> + +namespace NMonitoring { + /////////////////////////////////////////////////////////////////////////// + // TExponentialHistogramCollector + /////////////////////////////////////////////////////////////////////////// + class TExponentialHistogramCollector: public IHistogramCollector { + public: + TExponentialHistogramCollector(ui32 bucketsCount, double base, double scale) + : Values_(bucketsCount) + , Base_(base) + , Scale_(scale) , MinValue_(scale) , MaxValue_(scale * std::pow(base, bucketsCount - 2)) - , LogOfBase_(std::log(base)) - { - } - + , LogOfBase_(std::log(base)) + { + } + void Collect(double value, ui32 count) override { - ui32 index = Max<ui32>(); - if (value <= MinValue_) { - index = 0; - } else if (value > MaxValue_) { - index = Values_.Size() - 1; - } else { - double logBase = std::log(value / Scale_) / LogOfBase_; - index = static_cast<ui32>(std::ceil(logBase)); - } - Values_.Add(index, count); - } - + ui32 index = Max<ui32>(); + if (value <= MinValue_) { + index = 0; + } else if (value > MaxValue_) { + index = Values_.Size() - 1; + } else { + double logBase = std::log(value / Scale_) / LogOfBase_; + index = static_cast<ui32>(std::ceil(logBase)); + } + Values_.Add(index, count); + } + void Reset() override { Values_.Reset(); } - IHistogramSnapshotPtr Snapshot() const override { - return new TExponentialHistogramSnapshot(Base_, Scale_, Values_.Copy()); - } - - private: - TAtomicsArray Values_; - double Base_; - double Scale_; + IHistogramSnapshotPtr Snapshot() const override { + return new TExponentialHistogramSnapshot(Base_, Scale_, Values_.Copy()); + } + + private: + TAtomicsArray Values_; + double Base_; + double Scale_; TBucketBound MinValue_; TBucketBound MaxValue_; - double LogOfBase_; - }; - - IHistogramCollectorPtr ExponentialHistogram( - ui32 bucketsCount, double base, double scale) - { - Y_ENSURE(bucketsCount >= 2, - "exponential histogram must contain at least two buckets"); + double LogOfBase_; + }; + + IHistogramCollectorPtr ExponentialHistogram( + ui32 bucketsCount, double base, double scale) + { + 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 - << ", 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); - - return MakeHolder<TExponentialHistogramCollector>(bucketsCount, base, scale); - } -} + << ", 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); + + return MakeHolder<TExponentialHistogramCollector>(bucketsCount, base, scale); + } +} diff --git a/library/cpp/monlib/metrics/histogram_collector_linear.cpp b/library/cpp/monlib/metrics/histogram_collector_linear.cpp index c0afa29241..f8ad86f3a4 100644 --- a/library/cpp/monlib/metrics/histogram_collector_linear.cpp +++ b/library/cpp/monlib/metrics/histogram_collector_linear.cpp @@ -1,67 +1,67 @@ -#include "histogram_collector.h" -#include "atomics_array.h" - -#include <util/generic/algorithm.h> -#include <util/generic/vector.h> -#include <util/generic/yexception.h> -#include <util/generic/ylimits.h> - -#include <cmath> - -namespace NMonitoring { - /////////////////////////////////////////////////////////////////////////// - // TLinearHistogramCollector - /////////////////////////////////////////////////////////////////////////// - class TLinearHistogramCollector: public IHistogramCollector { - public: - TLinearHistogramCollector( +#include "histogram_collector.h" +#include "atomics_array.h" + +#include <util/generic/algorithm.h> +#include <util/generic/vector.h> +#include <util/generic/yexception.h> +#include <util/generic/ylimits.h> + +#include <cmath> + +namespace NMonitoring { + /////////////////////////////////////////////////////////////////////////// + // TLinearHistogramCollector + /////////////////////////////////////////////////////////////////////////// + class TLinearHistogramCollector: public IHistogramCollector { + public: + TLinearHistogramCollector( ui32 bucketsCount, TBucketBound startValue, TBucketBound bucketWidth) - : Values_(bucketsCount) - , StartValue_(startValue) - , BucketWidth_(bucketWidth) - , MaxValue_(startValue + bucketWidth * (bucketsCount - 2)) - { - } - + : Values_(bucketsCount) + , StartValue_(startValue) + , BucketWidth_(bucketWidth) + , MaxValue_(startValue + bucketWidth * (bucketsCount - 2)) + { + } + 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 { + ui32 index = Max<ui32>(); + if (value <= StartValue_) { + index = 0; + } else if (value > MaxValue_) { + index = Values_.Size() - 1; + } else { double buckets = (value - StartValue_) / BucketWidth_; - index = static_cast<ui32>(std::ceil(buckets)); - } - Values_.Add(index, count); - } - + index = static_cast<ui32>(std::ceil(buckets)); + } + Values_.Add(index, count); + } + void Reset() override { Values_.Reset(); } - IHistogramSnapshotPtr Snapshot() const override { - return new TLinearHistogramSnapshot( - StartValue_, BucketWidth_, Values_.Copy()); - } - - private: - TAtomicsArray Values_; + IHistogramSnapshotPtr Snapshot() const override { + return new TLinearHistogramSnapshot( + StartValue_, BucketWidth_, Values_.Copy()); + } + + private: + TAtomicsArray Values_; TBucketBound StartValue_; double BucketWidth_; TBucketBound MaxValue_; - }; - - IHistogramCollectorPtr LinearHistogram( + }; + + IHistogramCollectorPtr LinearHistogram( ui32 bucketsCount, TBucketBound startValue, TBucketBound bucketWidth) - { - Y_ENSURE(bucketsCount >= 2, - "linear histogram must contain at least two buckets"); + { + 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 - << ", but got: " << bucketsCount); - Y_ENSURE(bucketWidth >= 1, "bucketWidth must be >= 1, got: " << bucketWidth); - + << ", but got: " << bucketsCount); + Y_ENSURE(bucketWidth >= 1, "bucketWidth must be >= 1, got: " << bucketWidth); + return MakeHolder<TLinearHistogramCollector>(bucketsCount, startValue, bucketWidth); - } -} + } +} diff --git a/library/cpp/monlib/metrics/histogram_collector_ut.cpp b/library/cpp/monlib/metrics/histogram_collector_ut.cpp index bfbc1a5f2e..1cf66507fa 100644 --- a/library/cpp/monlib/metrics/histogram_collector_ut.cpp +++ b/library/cpp/monlib/metrics/histogram_collector_ut.cpp @@ -1,114 +1,114 @@ -#include "histogram_collector.h" - +#include "histogram_collector.h" + #include <library/cpp/testing/unittest/registar.h> - -using namespace NMonitoring; - -Y_UNIT_TEST_SUITE(THistogramCollectorTest) { - void CheckSnapshot( - const IHistogramSnapshot& s, - const TBucketBounds& bounds, - const TBucketValues& values) { - UNIT_ASSERT_VALUES_EQUAL(bounds.size(), values.size()); - UNIT_ASSERT_VALUES_EQUAL(bounds.size(), s.Count()); - - double epsilon = std::numeric_limits<double>::epsilon(); - for (ui32 i = 0; i < s.Count(); i++) { - UNIT_ASSERT_DOUBLES_EQUAL(bounds[i], s.UpperBound(i), epsilon); - UNIT_ASSERT_VALUES_EQUAL(values[i], s.Value(i)); - } - } - - Y_UNIT_TEST(Explicit) { - auto histogram = ExplicitHistogram({0, 1, 2, 5, 10, 20}); - histogram->Collect(-2); - histogram->Collect(-1); - histogram->Collect(0); - histogram->Collect(1); - histogram->Collect(20); - histogram->Collect(21); - histogram->Collect(1000); - - TBucketBounds expectedBounds = {0, 1, 2, 5, 10, 20, Max<TBucketBound>()}; - TBucketValues expectedValues = {3, 1, 0, 0, 0, 1, 2}; - - CheckSnapshot(*histogram->Snapshot(), expectedBounds, expectedValues); - } - - Y_UNIT_TEST(ExplicitWithFloadBounds) { - auto histogram = ExplicitHistogram({0.1, 0.5, 1, 1.7, 10}); - histogram->Collect(0.3, 2); - histogram->Collect(0.01); - histogram->Collect(0.9); - histogram->Collect(0.6); - histogram->Collect(1.1); - histogram->Collect(0.7); - histogram->Collect(2.71); - - TBucketBounds expectedBounds = {0.1, 0.5, 1, 1.7, 10, Max<TBucketBound>()}; - TBucketValues expectedValues = {1, 2, 3, 1, 1, 0}; - - CheckSnapshot(*histogram->Snapshot(), expectedBounds, expectedValues); - } - - Y_UNIT_TEST(Exponential) { - auto histogram = ExponentialHistogram(6, 2.0, 3.0); - histogram->Collect(-1); - histogram->Collect(0); - histogram->Collect(1); - histogram->Collect(3); - histogram->Collect(4); - histogram->Collect(5); - histogram->Collect(22); - histogram->Collect(23); - histogram->Collect(24); - histogram->Collect(50); - histogram->Collect(100); - histogram->Collect(1000); - - TBucketBounds expectedBounds = {3, 6, 12, 24, 48, Max<TBucketBound>()}; - TBucketValues expectedValues = {4, 2, 0, 3, 0, 3}; - - CheckSnapshot(*histogram->Snapshot(), expectedBounds, expectedValues); - } - - Y_UNIT_TEST(Linear) { - auto histogram = LinearHistogram(6, 5, 15); - histogram->Collect(-1); - histogram->Collect(0); - histogram->Collect(1); - histogram->Collect(4); - histogram->Collect(5); - histogram->Collect(6); - histogram->Collect(64); - histogram->Collect(65); - histogram->Collect(66); - histogram->Collect(100); - histogram->Collect(1000); - - TBucketBounds expectedBounds = {5, 20, 35, 50, 65, Max<TBucketBound>()}; - TBucketValues expectedValues = {5, 1, 0, 0, 2, 3}; - - CheckSnapshot(*histogram->Snapshot(), expectedBounds, expectedValues); - } - - Y_UNIT_TEST(SnapshotOutput) { - auto histogram = ExplicitHistogram({0, 1, 2, 5, 10, 20}); - histogram->Collect(-2); - histogram->Collect(-1); - histogram->Collect(0); - histogram->Collect(1); - histogram->Collect(20); - histogram->Collect(21); - histogram->Collect(1000); - - auto snapshot = histogram->Snapshot(); - - TStringStream ss; - ss << *snapshot; - - UNIT_ASSERT_STRINGS_EQUAL( - "{0: 3, 1: 1, 2: 0, 5: 0, 10: 0, 20: 1, inf: 2}", - ss.Str()); - } -} + +using namespace NMonitoring; + +Y_UNIT_TEST_SUITE(THistogramCollectorTest) { + void CheckSnapshot( + const IHistogramSnapshot& s, + const TBucketBounds& bounds, + const TBucketValues& values) { + UNIT_ASSERT_VALUES_EQUAL(bounds.size(), values.size()); + UNIT_ASSERT_VALUES_EQUAL(bounds.size(), s.Count()); + + double epsilon = std::numeric_limits<double>::epsilon(); + for (ui32 i = 0; i < s.Count(); i++) { + UNIT_ASSERT_DOUBLES_EQUAL(bounds[i], s.UpperBound(i), epsilon); + UNIT_ASSERT_VALUES_EQUAL(values[i], s.Value(i)); + } + } + + Y_UNIT_TEST(Explicit) { + auto histogram = ExplicitHistogram({0, 1, 2, 5, 10, 20}); + histogram->Collect(-2); + histogram->Collect(-1); + histogram->Collect(0); + histogram->Collect(1); + histogram->Collect(20); + histogram->Collect(21); + histogram->Collect(1000); + + TBucketBounds expectedBounds = {0, 1, 2, 5, 10, 20, Max<TBucketBound>()}; + TBucketValues expectedValues = {3, 1, 0, 0, 0, 1, 2}; + + CheckSnapshot(*histogram->Snapshot(), expectedBounds, expectedValues); + } + + Y_UNIT_TEST(ExplicitWithFloadBounds) { + auto histogram = ExplicitHistogram({0.1, 0.5, 1, 1.7, 10}); + histogram->Collect(0.3, 2); + histogram->Collect(0.01); + histogram->Collect(0.9); + histogram->Collect(0.6); + histogram->Collect(1.1); + histogram->Collect(0.7); + histogram->Collect(2.71); + + TBucketBounds expectedBounds = {0.1, 0.5, 1, 1.7, 10, Max<TBucketBound>()}; + TBucketValues expectedValues = {1, 2, 3, 1, 1, 0}; + + CheckSnapshot(*histogram->Snapshot(), expectedBounds, expectedValues); + } + + Y_UNIT_TEST(Exponential) { + auto histogram = ExponentialHistogram(6, 2.0, 3.0); + histogram->Collect(-1); + histogram->Collect(0); + histogram->Collect(1); + histogram->Collect(3); + histogram->Collect(4); + histogram->Collect(5); + histogram->Collect(22); + histogram->Collect(23); + histogram->Collect(24); + histogram->Collect(50); + histogram->Collect(100); + histogram->Collect(1000); + + TBucketBounds expectedBounds = {3, 6, 12, 24, 48, Max<TBucketBound>()}; + TBucketValues expectedValues = {4, 2, 0, 3, 0, 3}; + + CheckSnapshot(*histogram->Snapshot(), expectedBounds, expectedValues); + } + + Y_UNIT_TEST(Linear) { + auto histogram = LinearHistogram(6, 5, 15); + histogram->Collect(-1); + histogram->Collect(0); + histogram->Collect(1); + histogram->Collect(4); + histogram->Collect(5); + histogram->Collect(6); + histogram->Collect(64); + histogram->Collect(65); + histogram->Collect(66); + histogram->Collect(100); + histogram->Collect(1000); + + TBucketBounds expectedBounds = {5, 20, 35, 50, 65, Max<TBucketBound>()}; + TBucketValues expectedValues = {5, 1, 0, 0, 2, 3}; + + CheckSnapshot(*histogram->Snapshot(), expectedBounds, expectedValues); + } + + Y_UNIT_TEST(SnapshotOutput) { + auto histogram = ExplicitHistogram({0, 1, 2, 5, 10, 20}); + histogram->Collect(-2); + histogram->Collect(-1); + histogram->Collect(0); + histogram->Collect(1); + histogram->Collect(20); + histogram->Collect(21); + histogram->Collect(1000); + + auto snapshot = histogram->Snapshot(); + + TStringStream ss; + ss << *snapshot; + + UNIT_ASSERT_STRINGS_EQUAL( + "{0: 3, 1: 1, 2: 0, 5: 0, 10: 0, 20: 1, inf: 2}", + ss.Str()); + } +} diff --git a/library/cpp/monlib/metrics/histogram_snapshot.cpp b/library/cpp/monlib/metrics/histogram_snapshot.cpp index df787bedaf..75b5811546 100644 --- a/library/cpp/monlib/metrics/histogram_snapshot.cpp +++ b/library/cpp/monlib/metrics/histogram_snapshot.cpp @@ -1,27 +1,27 @@ -#include "histogram_snapshot.h" - +#include "histogram_snapshot.h" + #include <util/stream/output.h> #include <iostream> -namespace NMonitoring { - +namespace NMonitoring { + IHistogramSnapshotPtr ExplicitHistogramSnapshot(TConstArrayRef<TBucketBound> bounds, TConstArrayRef<TBucketValue> values) { Y_ENSURE(bounds.size() == values.size(), "mismatched sizes: bounds(" << bounds.size() << ") != buckets(" << values.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]; } - + return snapshot; - } - + } + } // namespace NMonitoring namespace { diff --git a/library/cpp/monlib/metrics/histogram_snapshot.h b/library/cpp/monlib/metrics/histogram_snapshot.h index 1f092a60d5..e8acf6ac2b 100644 --- a/library/cpp/monlib/metrics/histogram_snapshot.h +++ b/library/cpp/monlib/metrics/histogram_snapshot.h @@ -1,50 +1,50 @@ -#pragma once - +#pragma once + #include <util/generic/array_ref.h> -#include <util/generic/ptr.h> -#include <util/generic/vector.h> +#include <util/generic/ptr.h> +#include <util/generic/vector.h> #include <util/generic/yexception.h> - + #include <cmath> -#include <limits> - - -namespace NMonitoring { - - using TBucketBound = double; - using TBucketValue = ui64; - - using TBucketBounds = TVector<TBucketBound>; - using TBucketValues = TVector<TBucketValue>; - - constexpr ui32 HISTOGRAM_MAX_BUCKETS_COUNT = 51; - constexpr TBucketBound HISTOGRAM_INF_BOUND = std::numeric_limits<TBucketBound>::max(); - - /////////////////////////////////////////////////////////////////////////// - // IHistogramSnapshot - /////////////////////////////////////////////////////////////////////////// - class IHistogramSnapshot: public TAtomicRefCount<IHistogramSnapshot> { - public: - virtual ~IHistogramSnapshot() = default; - - /** - * @return buckets count. - */ - virtual ui32 Count() const = 0; - - /** - * @return upper bound for the bucket with particular index. - */ - virtual TBucketBound UpperBound(ui32 index) const = 0; - - /** - * @return value stored in the bucket with particular index. - */ - virtual TBucketValue Value(ui32 index) const = 0; - }; - - using IHistogramSnapshotPtr = TIntrusivePtr<IHistogramSnapshot>; - +#include <limits> + + +namespace NMonitoring { + + using TBucketBound = double; + using TBucketValue = ui64; + + using TBucketBounds = TVector<TBucketBound>; + using TBucketValues = TVector<TBucketValue>; + + constexpr ui32 HISTOGRAM_MAX_BUCKETS_COUNT = 51; + constexpr TBucketBound HISTOGRAM_INF_BOUND = std::numeric_limits<TBucketBound>::max(); + + /////////////////////////////////////////////////////////////////////////// + // IHistogramSnapshot + /////////////////////////////////////////////////////////////////////////// + class IHistogramSnapshot: public TAtomicRefCount<IHistogramSnapshot> { + public: + virtual ~IHistogramSnapshot() = default; + + /** + * @return buckets count. + */ + virtual ui32 Count() const = 0; + + /** + * @return upper bound for the bucket with particular index. + */ + virtual TBucketBound UpperBound(ui32 index) const = 0; + + /** + * @return value stored in the bucket with particular index. + */ + virtual TBucketValue Value(ui32 index) const = 0; + }; + + using IHistogramSnapshotPtr = TIntrusivePtr<IHistogramSnapshot>; + /////////////////////////////////////////////////////////////////////////////// // TLinearHistogramSnapshot /////////////////////////////////////////////////////////////////////////////// @@ -85,9 +85,9 @@ namespace NMonitoring { TBucketValues Values_; }; - /////////////////////////////////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////////// // TExponentialHistogramSnapshot - /////////////////////////////////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////////// class TExponentialHistogramSnapshot: public IHistogramSnapshot { public: TExponentialHistogramSnapshot( diff --git a/library/cpp/monlib/metrics/labels.cpp b/library/cpp/monlib/metrics/labels.cpp index 226bf5df26..1eaadb7cba 100644 --- a/library/cpp/monlib/metrics/labels.cpp +++ b/library/cpp/monlib/metrics/labels.cpp @@ -1,31 +1,31 @@ -#include "labels.h" - +#include "labels.h" + #include <util/stream/output.h> #include <util/string/split.h> - -static void OutputLabels(IOutputStream& out, const NMonitoring::ILabels& labels) { - size_t i = 0; + +static void OutputLabels(IOutputStream& out, const NMonitoring::ILabels& labels) { + size_t i = 0; out << '{'; - for (const auto& label: labels) { - if (i++ > 0) { + for (const auto& label: labels) { + if (i++ > 0) { out << TStringBuf(", "); } - out << label; - } + out << label; + } out << '}'; } template <> -void Out<NMonitoring::ILabelsPtr>(IOutputStream& out, const NMonitoring::ILabelsPtr& labels) { - OutputLabels(out, *labels); -} - -template <> -void Out<NMonitoring::ILabels>(IOutputStream& out, const NMonitoring::ILabels& labels) { - OutputLabels(out, labels); -} - -template <> +void Out<NMonitoring::ILabelsPtr>(IOutputStream& out, const NMonitoring::ILabelsPtr& labels) { + OutputLabels(out, *labels); +} + +template <> +void Out<NMonitoring::ILabels>(IOutputStream& out, const NMonitoring::ILabels& labels) { + OutputLabels(out, labels); +} + +template <> void Out<NMonitoring::ILabel>(IOutputStream& out, const NMonitoring::ILabel& labels) { out << labels.Name() << "=" << labels.Value(); } diff --git a/library/cpp/monlib/metrics/labels.h b/library/cpp/monlib/metrics/labels.h index 5afb7e332e..63dc997c28 100644 --- a/library/cpp/monlib/metrics/labels.h +++ b/library/cpp/monlib/metrics/labels.h @@ -1,19 +1,19 @@ -#pragma once - -#include <util/digest/multi.h> -#include <util/digest/sequence.h> -#include <util/generic/algorithm.h> -#include <util/generic/maybe.h> -#include <util/generic/string.h> -#include <util/generic/vector.h> +#pragma once + +#include <util/digest/multi.h> +#include <util/digest/sequence.h> +#include <util/generic/algorithm.h> +#include <util/generic/maybe.h> +#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 <optional> #include <type_traits> -namespace NMonitoring { +namespace NMonitoring { struct ILabel { virtual ~ILabel() = default; @@ -21,38 +21,38 @@ namespace NMonitoring { virtual TStringBuf Value() const noexcept = 0; }; - /////////////////////////////////////////////////////////////////////////// - // TLabel - /////////////////////////////////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////////// + // TLabel + /////////////////////////////////////////////////////////////////////////// template <typename TStringBackend> class TLabelImpl: public ILabel { - public: + public: using TStringType = TStringBackend; TLabelImpl() = default; - + inline TLabelImpl(TStringBuf name, TStringBuf value) : Name_{name} , Value_{value} - { - } - + { + } + inline bool operator==(const TLabelImpl& rhs) const noexcept { - return Name_ == rhs.Name_ && Value_ == rhs.Value_; - } - + return Name_ == rhs.Name_ && Value_ == rhs.Value_; + } + inline bool operator!=(const TLabelImpl& rhs) const noexcept { - return !(*this == rhs); - } - + return !(*this == rhs); + } + inline TStringBuf Name() const noexcept { - return Name_; - } - + return Name_; + } + inline TStringBuf Value() const noexcept { - return Value_; - } - + return Value_; + } + inline const TStringBackend& NameStr() const { return Name_; } @@ -61,18 +61,18 @@ namespace NMonitoring { return Value_; } - inline size_t Hash() const noexcept { - return MultiHash(Name_, Value_); - } - + inline size_t Hash() const noexcept { + 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), @@ -107,11 +107,11 @@ namespace NMonitoring { return true; } - private: + private: TStringBackend Name_; TStringBackend Value_; - }; - + }; + using TLabel = TLabelImpl<TString>; struct ILabels { @@ -158,12 +158,12 @@ namespace NMonitoring { virtual ~ILabels() = default; - virtual bool Add(TStringBuf name, TStringBuf value) noexcept = 0; + virtual bool Add(TStringBuf name, TStringBuf value) noexcept = 0; virtual bool Add(const ILabel& label) noexcept { return Add(label.Name(), label.Value()); } - virtual bool Has(TStringBuf name) const noexcept = 0; + virtual bool Has(TStringBuf name) const noexcept = 0; virtual size_t Size() const noexcept = 0; virtual bool Empty() const noexcept = 0; @@ -171,7 +171,7 @@ namespace NMonitoring { virtual size_t Hash() const noexcept = 0; - virtual std::optional<const ILabel*> Get(TStringBuf name) const = 0; + 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; @@ -188,16 +188,16 @@ namespace NMonitoring { bool TryLoadLabelsFromString(TStringBuf sb, ILabels& labels); bool TryLoadLabelsFromString(IInputStream& is, ILabels& labels); - /////////////////////////////////////////////////////////////////////////// - // TLabels - /////////////////////////////////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////////// + // TLabels + /////////////////////////////////////////////////////////////////////////// template <typename TStringBackend> class TLabelsImpl: public ILabels { - public: + public: using value_type = TLabelImpl<TStringBackend>; - + TLabelsImpl() = default; - + explicit TLabelsImpl(::NDetail::TReserveTag rt) : Labels_(std::move(rt)) {} @@ -222,13 +222,13 @@ namespace NMonitoring { inline bool operator==(const TLabelsImpl& rhs) const { return Labels_ == rhs.Labels_; - } - + } + inline bool operator!=(const TLabelsImpl& rhs) const { return Labels_ != rhs.Labels_; - } - - bool Add(TStringBuf name, TStringBuf value) noexcept override { + } + + bool Add(TStringBuf name, TStringBuf value) noexcept override { if (Has(name)) { return false; } @@ -239,7 +239,7 @@ namespace NMonitoring { using ILabels::Add; - bool Has(TStringBuf name) const noexcept override { + bool Has(TStringBuf name) const noexcept override { auto it = FindIf(Labels_, [name](const TLabelImpl<TStringBackend>& label) { return name == TStringBuf{label.Name()}; }); @@ -264,7 +264,7 @@ namespace NMonitoring { return *it; } - std::optional<const ILabel*> Get(TStringBuf name) const override { + std::optional<const ILabel*> Get(TStringBuf name) const override { auto it = FindIf(Labels_, [name] (auto&& l) { return name == l.Name(); }); @@ -300,20 +300,20 @@ namespace NMonitoring { inline size_t Hash() const noexcept override { return TSimpleRangeHash()(Labels_); - } - - inline TLabel* Data() const noexcept { + } + + inline TLabel* Data() const noexcept { return const_cast<TLabel*>(Labels_.data()); - } - + } + inline size_t Size() const noexcept override { return Labels_.size(); - } - + } + inline bool Empty() const noexcept override { return Labels_.empty(); - } - + } + inline void Clear() noexcept override { Labels_.clear(); }; @@ -391,26 +391,26 @@ namespace NMonitoring { private: TVector<TLabelImpl<TStringBackend>> Labels_; - }; + }; using TLabels = TLabelsImpl<TString>; using ILabelsPtr = THolder<ILabels>; - - template <typename T> - ILabelsPtr MakeLabels() { - return MakeHolder<TLabelsImpl<T>>(); - } - - template <typename T> - ILabelsPtr MakeLabels(std::initializer_list<TLabelImpl<T>> labels) { - return MakeHolder<TLabelsImpl<T>>(labels); - } - - inline ILabelsPtr MakeLabels(TLabels&& labels) { - return MakeHolder<TLabels>(std::move(labels)); - } -} - + + template <typename T> + ILabelsPtr MakeLabels() { + return MakeHolder<TLabelsImpl<T>>(); + } + + template <typename T> + ILabelsPtr MakeLabels(std::initializer_list<TLabelImpl<T>> labels) { + return MakeHolder<TLabelsImpl<T>>(labels); + } + + inline ILabelsPtr MakeLabels(TLabels&& labels) { + return MakeHolder<TLabels>(std::move(labels)); + } +} + template<> struct THash<NMonitoring::ILabelsPtr> { size_t operator()(const NMonitoring::ILabelsPtr& labels) const noexcept { @@ -432,14 +432,14 @@ struct THash<NMonitoring::TLabelsImpl<TStringBackend>> { template <typename TStringBackend> struct THash<NMonitoring::TLabelImpl<TStringBackend>> { inline size_t operator()(const NMonitoring::TLabelImpl<TStringBackend>& label) const noexcept { - return label.Hash(); - } -}; - + return label.Hash(); + } +}; + 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()); @@ -468,7 +468,7 @@ struct TEqualTo<NMonitoring::ILabelsPtr> { bool operator()(const NMonitoring::ILabels& lhs, const NMonitoring::ILabelsPtr& rhs) { return lhs == *rhs; } -}; +}; #define Y_MONLIB_DEFINE_LABELS_OUT(T) \ template <> \ diff --git a/library/cpp/monlib/metrics/labels_ut.cpp b/library/cpp/monlib/metrics/labels_ut.cpp index 8c96c23bfb..f0e4f532ab 100644 --- a/library/cpp/monlib/metrics/labels_ut.cpp +++ b/library/cpp/monlib/metrics/labels_ut.cpp @@ -1,194 +1,194 @@ -#include "labels.h" - +#include "labels.h" + #include <library/cpp/testing/unittest/registar.h> - -using namespace NMonitoring; - + +using namespace NMonitoring; + Y_UNIT_TEST_SUITE(TLabelsTest) { - TLabel pSolomon("project", "solomon"); - TLabel pKikimr("project", "kikimr"); - + TLabel pSolomon("project", "solomon"); + TLabel pKikimr("project", "kikimr"); + Y_UNIT_TEST(Equals) { - UNIT_ASSERT(pSolomon == TLabel("project", "solomon")); - - UNIT_ASSERT_STRINGS_EQUAL(pSolomon.Name(), "project"); - UNIT_ASSERT_STRINGS_EQUAL(pSolomon.Value(), "solomon"); - - UNIT_ASSERT(pSolomon != pKikimr); - } - + UNIT_ASSERT(pSolomon == TLabel("project", "solomon")); + + UNIT_ASSERT_STRINGS_EQUAL(pSolomon.Name(), "project"); + UNIT_ASSERT_STRINGS_EQUAL(pSolomon.Value(), "solomon"); + + UNIT_ASSERT(pSolomon != pKikimr); + } + Y_UNIT_TEST(ToString) { - UNIT_ASSERT_STRINGS_EQUAL(pSolomon.ToString(), "project=solomon"); - UNIT_ASSERT_STRINGS_EQUAL(pKikimr.ToString(), "project=kikimr"); - } - + UNIT_ASSERT_STRINGS_EQUAL(pSolomon.ToString(), "project=solomon"); + UNIT_ASSERT_STRINGS_EQUAL(pKikimr.ToString(), "project=kikimr"); + } + Y_UNIT_TEST(FromString) { - auto pYql = TLabel::FromString("project=yql"); - UNIT_ASSERT_EQUAL(pYql, TLabel("project", "yql")); - - UNIT_ASSERT_EQUAL(TLabel::FromString("k=v"), TLabel("k", "v")); - UNIT_ASSERT_EQUAL(TLabel::FromString("k=v "), TLabel("k", "v")); - UNIT_ASSERT_EQUAL(TLabel::FromString("k= v"), TLabel("k", "v")); - UNIT_ASSERT_EQUAL(TLabel::FromString("k =v"), TLabel("k", "v")); - UNIT_ASSERT_EQUAL(TLabel::FromString(" k=v"), TLabel("k", "v")); - UNIT_ASSERT_EQUAL(TLabel::FromString(" k = v "), TLabel("k", "v")); - - UNIT_ASSERT_EXCEPTION_CONTAINS( - TLabel::FromString(""), - yexception, - "invalid label string format"); - - UNIT_ASSERT_EXCEPTION_CONTAINS( - TLabel::FromString("k v"), - yexception, - "invalid label string format"); - - UNIT_ASSERT_EXCEPTION_CONTAINS( - TLabel::FromString(" =v"), - yexception, - "label name cannot be empty"); - - UNIT_ASSERT_EXCEPTION_CONTAINS( - TLabel::FromString("k= "), - yexception, - "label value cannot be empty"); - } - + auto pYql = TLabel::FromString("project=yql"); + UNIT_ASSERT_EQUAL(pYql, TLabel("project", "yql")); + + UNIT_ASSERT_EQUAL(TLabel::FromString("k=v"), TLabel("k", "v")); + UNIT_ASSERT_EQUAL(TLabel::FromString("k=v "), TLabel("k", "v")); + UNIT_ASSERT_EQUAL(TLabel::FromString("k= v"), TLabel("k", "v")); + UNIT_ASSERT_EQUAL(TLabel::FromString("k =v"), TLabel("k", "v")); + UNIT_ASSERT_EQUAL(TLabel::FromString(" k=v"), TLabel("k", "v")); + UNIT_ASSERT_EQUAL(TLabel::FromString(" k = v "), TLabel("k", "v")); + + UNIT_ASSERT_EXCEPTION_CONTAINS( + TLabel::FromString(""), + yexception, + "invalid label string format"); + + UNIT_ASSERT_EXCEPTION_CONTAINS( + TLabel::FromString("k v"), + yexception, + "invalid label string format"); + + UNIT_ASSERT_EXCEPTION_CONTAINS( + TLabel::FromString(" =v"), + yexception, + "label name cannot be empty"); + + UNIT_ASSERT_EXCEPTION_CONTAINS( + TLabel::FromString("k= "), + yexception, + "label value cannot be empty"); + } + Y_UNIT_TEST(TryFromString) { - TLabel pYql; - UNIT_ASSERT(TLabel::TryFromString("project=yql", pYql)); - UNIT_ASSERT_EQUAL(pYql, TLabel("project", "yql")); - - { - TLabel label; - UNIT_ASSERT(TLabel::TryFromString("k=v", label)); - UNIT_ASSERT_EQUAL(label, TLabel("k", "v")); - } - { - TLabel label; - UNIT_ASSERT(TLabel::TryFromString("k=v ", label)); - UNIT_ASSERT_EQUAL(label, TLabel("k", "v")); - } - { - TLabel label; - UNIT_ASSERT(TLabel::TryFromString("k= v", label)); - UNIT_ASSERT_EQUAL(label, TLabel("k", "v")); - } - { - TLabel label; - UNIT_ASSERT(TLabel::TryFromString("k =v", label)); - UNIT_ASSERT_EQUAL(label, TLabel("k", "v")); - } - { - TLabel label; - UNIT_ASSERT(TLabel::TryFromString(" k=v", label)); - UNIT_ASSERT_EQUAL(label, TLabel("k", "v")); - } - { - TLabel label; - UNIT_ASSERT(TLabel::TryFromString(" k = v ", label)); - UNIT_ASSERT_EQUAL(label, TLabel("k", "v")); - } - } - + TLabel pYql; + UNIT_ASSERT(TLabel::TryFromString("project=yql", pYql)); + UNIT_ASSERT_EQUAL(pYql, TLabel("project", "yql")); + + { + TLabel label; + UNIT_ASSERT(TLabel::TryFromString("k=v", label)); + UNIT_ASSERT_EQUAL(label, TLabel("k", "v")); + } + { + TLabel label; + UNIT_ASSERT(TLabel::TryFromString("k=v ", label)); + UNIT_ASSERT_EQUAL(label, TLabel("k", "v")); + } + { + TLabel label; + UNIT_ASSERT(TLabel::TryFromString("k= v", label)); + UNIT_ASSERT_EQUAL(label, TLabel("k", "v")); + } + { + TLabel label; + UNIT_ASSERT(TLabel::TryFromString("k =v", label)); + UNIT_ASSERT_EQUAL(label, TLabel("k", "v")); + } + { + TLabel label; + UNIT_ASSERT(TLabel::TryFromString(" k=v", label)); + UNIT_ASSERT_EQUAL(label, TLabel("k", "v")); + } + { + TLabel label; + UNIT_ASSERT(TLabel::TryFromString(" k = v ", label)); + UNIT_ASSERT_EQUAL(label, TLabel("k", "v")); + } + } + Y_UNIT_TEST(Labels) { - TLabels labels; + TLabels labels; UNIT_ASSERT(labels.Add(TStringBuf("name1"), TStringBuf("value1"))); - UNIT_ASSERT(labels.Size() == 1); + UNIT_ASSERT(labels.Size() == 1); UNIT_ASSERT(labels.Has(TStringBuf("name1"))); - { - auto l = labels.Find("name1"); - UNIT_ASSERT(l.Defined()); - UNIT_ASSERT_STRINGS_EQUAL(l->Name(), "name1"); - UNIT_ASSERT_STRINGS_EQUAL(l->Value(), "value1"); - } - { - auto l = labels.Find("name2"); - UNIT_ASSERT(!l.Defined()); - } - - // duplicated name + { + auto l = labels.Find("name1"); + UNIT_ASSERT(l.Defined()); + UNIT_ASSERT_STRINGS_EQUAL(l->Name(), "name1"); + UNIT_ASSERT_STRINGS_EQUAL(l->Value(), "value1"); + } + { + auto l = labels.Find("name2"); + UNIT_ASSERT(!l.Defined()); + } + + // duplicated name UNIT_ASSERT(!labels.Add(TStringBuf("name1"), TStringBuf("value2"))); - UNIT_ASSERT(labels.Size() == 1); - + UNIT_ASSERT(labels.Size() == 1); + UNIT_ASSERT(labels.Add(TStringBuf("name2"), TStringBuf("value2"))); - UNIT_ASSERT(labels.Size() == 2); + UNIT_ASSERT(labels.Size() == 2); UNIT_ASSERT(labels.Has(TStringBuf("name2"))); - { - auto l = labels.Find("name2"); - UNIT_ASSERT(l.Defined()); - UNIT_ASSERT_STRINGS_EQUAL(l->Name(), "name2"); - UNIT_ASSERT_STRINGS_EQUAL(l->Value(), "value2"); - } - - UNIT_ASSERT_EQUAL(labels[0], TLabel("name1", "value1")); - UNIT_ASSERT_EQUAL(labels[1], TLabel("name2", "value2")); - + { + auto l = labels.Find("name2"); + UNIT_ASSERT(l.Defined()); + UNIT_ASSERT_STRINGS_EQUAL(l->Name(), "name2"); + UNIT_ASSERT_STRINGS_EQUAL(l->Value(), "value2"); + } + + UNIT_ASSERT_EQUAL(labels[0], TLabel("name1", "value1")); + UNIT_ASSERT_EQUAL(labels[1], TLabel("name2", "value2")); + TVector<TLabel> labelsCopy; for (auto&& label : labels) { labelsCopy.emplace_back(label.Name(), label.Value()); - } - + } + UNIT_ASSERT_EQUAL(labelsCopy, TVector<TLabel>({ - {"name1", "value1"}, - {"name2", "value2"}, - })); - } - + {"name1", "value1"}, + {"name2", "value2"}, + })); + } + Y_UNIT_TEST(Hash) { - TLabel label("name", "value"); - UNIT_ASSERT_EQUAL(ULL(2378153472115172159), label.Hash()); - - { - TLabels labels = {{"name", "value"}}; - UNIT_ASSERT_EQUAL(ULL(5420514431458887014), labels.Hash()); - } - { - TLabels labels = {{"name1", "value1"}, {"name2", "value2"}}; - UNIT_ASSERT_EQUAL(ULL(2226975250396609813), labels.Hash()); - } - } - - Y_UNIT_TEST(MakeEmptyLabels) { - { - auto labels = MakeLabels<TString>(); - UNIT_ASSERT(labels); - UNIT_ASSERT(labels->Empty()); - UNIT_ASSERT_VALUES_EQUAL(labels->Size(), 0); - } - { - auto labels = MakeLabels<TStringBuf>(); - UNIT_ASSERT(labels); - UNIT_ASSERT(labels->Empty()); - UNIT_ASSERT_VALUES_EQUAL(labels->Size(), 0); - } - } - - Y_UNIT_TEST(MakeLabelsFromInitializerList) { - auto labels = MakeLabels<TString>({{"my", "label"}}); - UNIT_ASSERT(labels); - UNIT_ASSERT(!labels->Empty()); - UNIT_ASSERT_VALUES_EQUAL(labels->Size(), 1); - - UNIT_ASSERT(labels->Has("my")); - - auto label = labels->Get("my"); - UNIT_ASSERT(label.has_value()); - UNIT_ASSERT_STRINGS_EQUAL((*label)->Name(), "my"); - UNIT_ASSERT_STRINGS_EQUAL((*label)->Value(), "label"); - } - - Y_UNIT_TEST(MakeLabelsFromOtherLabel) { - auto labels = MakeLabels({{"my", "label"}}); - UNIT_ASSERT(labels); - UNIT_ASSERT(!labels->Empty()); - UNIT_ASSERT_VALUES_EQUAL(labels->Size(), 1); - - UNIT_ASSERT(labels->Has("my")); - - auto label = labels->Get("my"); - UNIT_ASSERT(label.has_value()); - UNIT_ASSERT_STRINGS_EQUAL((*label)->Name(), "my"); - UNIT_ASSERT_STRINGS_EQUAL((*label)->Value(), "label"); - } -} + TLabel label("name", "value"); + UNIT_ASSERT_EQUAL(ULL(2378153472115172159), label.Hash()); + + { + TLabels labels = {{"name", "value"}}; + UNIT_ASSERT_EQUAL(ULL(5420514431458887014), labels.Hash()); + } + { + TLabels labels = {{"name1", "value1"}, {"name2", "value2"}}; + UNIT_ASSERT_EQUAL(ULL(2226975250396609813), labels.Hash()); + } + } + + Y_UNIT_TEST(MakeEmptyLabels) { + { + auto labels = MakeLabels<TString>(); + UNIT_ASSERT(labels); + UNIT_ASSERT(labels->Empty()); + UNIT_ASSERT_VALUES_EQUAL(labels->Size(), 0); + } + { + auto labels = MakeLabels<TStringBuf>(); + UNIT_ASSERT(labels); + UNIT_ASSERT(labels->Empty()); + UNIT_ASSERT_VALUES_EQUAL(labels->Size(), 0); + } + } + + Y_UNIT_TEST(MakeLabelsFromInitializerList) { + auto labels = MakeLabels<TString>({{"my", "label"}}); + UNIT_ASSERT(labels); + UNIT_ASSERT(!labels->Empty()); + UNIT_ASSERT_VALUES_EQUAL(labels->Size(), 1); + + UNIT_ASSERT(labels->Has("my")); + + auto label = labels->Get("my"); + UNIT_ASSERT(label.has_value()); + UNIT_ASSERT_STRINGS_EQUAL((*label)->Name(), "my"); + UNIT_ASSERT_STRINGS_EQUAL((*label)->Value(), "label"); + } + + Y_UNIT_TEST(MakeLabelsFromOtherLabel) { + auto labels = MakeLabels({{"my", "label"}}); + UNIT_ASSERT(labels); + UNIT_ASSERT(!labels->Empty()); + UNIT_ASSERT_VALUES_EQUAL(labels->Size(), 1); + + UNIT_ASSERT(labels->Has("my")); + + auto label = labels->Get("my"); + UNIT_ASSERT(label.has_value()); + UNIT_ASSERT_STRINGS_EQUAL((*label)->Name(), "my"); + UNIT_ASSERT_STRINGS_EQUAL((*label)->Value(), "label"); + } +} diff --git a/library/cpp/monlib/metrics/log_histogram_collector.h b/library/cpp/monlib/metrics/log_histogram_collector.h index 13b656af35..b81f84ebf3 100644 --- a/library/cpp/monlib/metrics/log_histogram_collector.h +++ b/library/cpp/monlib/metrics/log_histogram_collector.h @@ -25,9 +25,9 @@ namespace NMonitoring { Merge(logHist); } - bool Collect(double value) { + bool Collect(double value) { std::lock_guard guard(Mutex_); - return CollectDouble(value); + return CollectDouble(value); } TLogHistogramSnapshotPtr Snapshot() const { @@ -64,16 +64,16 @@ namespace NMonitoring { ++Buckets_[idx]; } - bool CollectDouble(double value) { - if (Y_UNLIKELY(std::isnan(value) || std::isinf(value))) { - return false; - } + bool CollectDouble(double value) { + if (Y_UNLIKELY(std::isnan(value) || std::isinf(value))) { + return false; + } if (value <= 0.0) { ++CountZero_; } else { CollectPositiveDouble(value); } - return true; + return true; } void Merge(TLogHistogramSnapshot* logHist) { diff --git a/library/cpp/monlib/metrics/metric.h b/library/cpp/monlib/metrics/metric.h index f4c7dc37f2..b8ce12d753 100644 --- a/library/cpp/monlib/metrics/metric.h +++ b/library/cpp/monlib/metrics/metric.h @@ -1,28 +1,28 @@ -#pragma once - -#include "metric_consumer.h" - -#include <util/datetime/base.h> -#include <util/generic/ptr.h> - -namespace NMonitoring { - /////////////////////////////////////////////////////////////////////////////// - // IMetric - /////////////////////////////////////////////////////////////////////////////// - class IMetric { - public: - virtual ~IMetric() = default; - - virtual EMetricType Type() const noexcept = 0; - virtual void Accept(TInstant time, IMetricConsumer* consumer) const = 0; - }; - - using IMetricPtr = THolder<IMetric>; - - class IGauge: public IMetric { +#pragma once + +#include "metric_consumer.h" + +#include <util/datetime/base.h> +#include <util/generic/ptr.h> + +namespace NMonitoring { + /////////////////////////////////////////////////////////////////////////////// + // IMetric + /////////////////////////////////////////////////////////////////////////////// + class IMetric { + public: + virtual ~IMetric() = default; + + virtual EMetricType Type() const noexcept = 0; + virtual void Accept(TInstant time, IMetricConsumer* consumer) const = 0; + }; + + using IMetricPtr = THolder<IMetric>; + + class IGauge: public IMetric { public: - EMetricType Type() const noexcept final { - return EMetricType::GAUGE; + EMetricType Type() const noexcept final { + return EMetricType::GAUGE; } virtual double Add(double n) noexcept = 0; @@ -41,10 +41,10 @@ namespace NMonitoring { virtual double Get() const noexcept = 0; }; - class IIntGauge: public IMetric { + class IIntGauge: public IMetric { public: - EMetricType Type() const noexcept final { - return EMetricType::IGAUGE; + EMetricType Type() const noexcept final { + return EMetricType::IGAUGE; } virtual i64 Add(i64 n) noexcept = 0; @@ -72,10 +72,10 @@ namespace NMonitoring { virtual i64 Get() const noexcept = 0; }; - class ICounter: public IMetric { + class ICounter: public IMetric { public: - EMetricType Type() const noexcept final { - return EMetricType::COUNTER; + EMetricType Type() const noexcept final { + return EMetricType::COUNTER; } virtual ui64 Inc() noexcept { @@ -96,10 +96,10 @@ namespace NMonitoring { virtual ui64 Get() const noexcept = 0; }; - class IRate: public IMetric { + class IRate: public IMetric { public: - EMetricType Type() const noexcept final { - return EMetricType::RATE; + EMetricType Type() const noexcept final { + return EMetricType::RATE; } virtual ui64 Inc() noexcept { @@ -108,7 +108,7 @@ namespace NMonitoring { virtual ui64 Add(ui64 n) noexcept = 0; virtual ui64 Get() const noexcept = 0; - virtual void Reset() noexcept = 0; + virtual void Reset() noexcept = 0; }; class ILazyRate: public IMetric { @@ -120,19 +120,19 @@ namespace NMonitoring { virtual ui64 Get() const noexcept = 0; }; - class IHistogram: public IMetric { + class IHistogram: public IMetric { public: explicit IHistogram(bool isRate) : IsRate_{isRate} { } - EMetricType Type() const noexcept final { - return IsRate_ ? EMetricType::HIST_RATE : EMetricType::HIST; + 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 void Record(double value) = 0; + virtual void Record(double value, ui32 count) = 0; virtual IHistogramSnapshotPtr TakeSnapshot() const = 0; virtual void Reset() = 0; @@ -140,15 +140,15 @@ namespace NMonitoring { const bool IsRate_; }; - /////////////////////////////////////////////////////////////////////////////// - // TGauge - /////////////////////////////////////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////////////// + // TGauge + /////////////////////////////////////////////////////////////////////////////// class TGauge final: public IGauge { - public: - explicit TGauge(double value = 0.0) { - Set(value); - } - + public: + explicit TGauge(double value = 0.0) { + Set(value); + } + double Add(double n) noexcept override { double newValue; double oldValue = Get(); @@ -162,21 +162,21 @@ namespace NMonitoring { 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); - } - - void Accept(TInstant time, IMetricConsumer* consumer) const override { - consumer->OnDouble(time, Get()); - } - - private: + } + + void Accept(TInstant time, IMetricConsumer* consumer) const override { + consumer->OnDouble(time, Get()); + } + + private: std::atomic<double> Value_; - }; - - /////////////////////////////////////////////////////////////////////////////// + }; + + /////////////////////////////////////////////////////////////////////////////// // TLazyGauge /////////////////////////////////////////////////////////////////////////////// class TLazyGauge final: public ILazyGauge { @@ -199,35 +199,35 @@ namespace NMonitoring { }; /////////////////////////////////////////////////////////////////////////////// - // TIntGauge - /////////////////////////////////////////////////////////////////////////////// + // TIntGauge + /////////////////////////////////////////////////////////////////////////////// class TIntGauge final: public IIntGauge { - public: - explicit TIntGauge(i64 value = 0) { - Set(value); - } - + public: + explicit TIntGauge(i64 value = 0) { + Set(value); + } + 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); - } - + } + i64 Get() const noexcept override { return Value_.load(std::memory_order_relaxed); - } - - void Accept(TInstant time, IMetricConsumer* consumer) const override { - consumer->OnInt64(time, Get()); - } - - private: + } + + void Accept(TInstant time, IMetricConsumer* consumer) const override { + consumer->OnInt64(time, Get()); + } + + private: std::atomic_int64_t Value_; - }; - - /////////////////////////////////////////////////////////////////////////////// + }; + + /////////////////////////////////////////////////////////////////////////////// // TLazyIntGauge /////////////////////////////////////////////////////////////////////////////// class TLazyIntGauge final: public ILazyIntGauge { @@ -250,35 +250,35 @@ namespace NMonitoring { }; /////////////////////////////////////////////////////////////////////////////// - // TCounter - /////////////////////////////////////////////////////////////////////////////// - class TCounter final: public ICounter { - public: - explicit TCounter(ui64 value = 0) { + // TCounter + /////////////////////////////////////////////////////////////////////////////// + class TCounter final: public ICounter { + public: + explicit TCounter(ui64 value = 0) { Value_.store(value, std::memory_order_relaxed); - } - + } + 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); - } - + } + void Reset() noexcept override { Value_.store(0, std::memory_order_relaxed); - } - - void Accept(TInstant time, IMetricConsumer* consumer) const override { - consumer->OnUint64(time, Get()); - } - - private: + } + + void Accept(TInstant time, IMetricConsumer* consumer) const override { + consumer->OnUint64(time, Get()); + } + + private: std::atomic_uint64_t Value_; - }; - - /////////////////////////////////////////////////////////////////////////////// + }; + + /////////////////////////////////////////////////////////////////////////////// // TLazyCounter /////////////////////////////////////////////////////////////////////////////// class TLazyCounter final: public ILazyCounter { @@ -301,35 +301,35 @@ namespace NMonitoring { }; /////////////////////////////////////////////////////////////////////////////// - // TRate - /////////////////////////////////////////////////////////////////////////////// + // TRate + /////////////////////////////////////////////////////////////////////////////// class TRate final: public IRate { - public: - explicit TRate(ui64 value = 0) { + public: + explicit TRate(ui64 value = 0) { Value_.store(value, std::memory_order_relaxed); - } - + } + 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); - } - - void Reset() noexcept override { - Value_.store(0, std::memory_order_relaxed); - } - - void Accept(TInstant time, IMetricConsumer* consumer) const override { - consumer->OnUint64(time, Get()); - } - - private: + } + + void Reset() noexcept override { + Value_.store(0, std::memory_order_relaxed); + } + + void Accept(TInstant time, IMetricConsumer* consumer) const override { + consumer->OnUint64(time, Get()); + } + + private: std::atomic_uint64_t Value_; - }; - - /////////////////////////////////////////////////////////////////////////////// + }; + + /////////////////////////////////////////////////////////////////////////////// // TLazyRate /////////////////////////////////////////////////////////////////////////////// class TLazyRate final: public ILazyRate { @@ -352,28 +352,28 @@ namespace NMonitoring { }; /////////////////////////////////////////////////////////////////////////////// - // THistogram - /////////////////////////////////////////////////////////////////////////////// + // THistogram + /////////////////////////////////////////////////////////////////////////////// class THistogram final: public IHistogram { - public: - THistogram(IHistogramCollectorPtr collector, bool isRate) + public: + THistogram(IHistogramCollectorPtr collector, bool isRate) : IHistogram(isRate) , Collector_(std::move(collector)) - { - } - - void Record(double value) override { - Collector_->Collect(value); - } - - void Record(double value, ui32 count) override { - Collector_->Collect(value, count); - } - - void Accept(TInstant time, IMetricConsumer* consumer) const override { + { + } + + void Record(double value) override { + Collector_->Collect(value); + } + + void Record(double value, ui32 count) override { + Collector_->Collect(value, count); + } + + void Accept(TInstant time, IMetricConsumer* consumer) const override { consumer->OnHistogram(time, TakeSnapshot()); - } - + } + IHistogramSnapshotPtr TakeSnapshot() const override { return Collector_->Snapshot(); } @@ -382,7 +382,7 @@ namespace NMonitoring { Collector_->Reset(); } - private: - IHistogramCollectorPtr Collector_; - }; -} + private: + IHistogramCollectorPtr Collector_; + }; +} diff --git a/library/cpp/monlib/metrics/metric_consumer.cpp b/library/cpp/monlib/metrics/metric_consumer.cpp index 4289efdad9..121ee368f0 100644 --- a/library/cpp/monlib/metrics/metric_consumer.cpp +++ b/library/cpp/monlib/metrics/metric_consumer.cpp @@ -8,7 +8,7 @@ namespace NMonitoring { Y_ENSURE(false, "Not implemented"); } - std::pair<ui32, ui32> IMetricConsumer::PrepareLabel(TStringBuf name, TStringBuf value) { + std::pair<ui32, ui32> IMetricConsumer::PrepareLabel(TStringBuf name, TStringBuf value) { Y_UNUSED(name, value); Y_ENSURE(false, "Not implemented"); } diff --git a/library/cpp/monlib/metrics/metric_consumer.h b/library/cpp/monlib/metrics/metric_consumer.h index 87deed785d..f7a727585a 100644 --- a/library/cpp/monlib/metrics/metric_consumer.h +++ b/library/cpp/monlib/metrics/metric_consumer.h @@ -1,40 +1,40 @@ -#pragma once - -#include "metric_type.h" -#include "histogram_collector.h" +#pragma once + +#include "metric_type.h" +#include "histogram_collector.h" #include "summary_collector.h" #include "log_histogram_snapshot.h" - -class TInstant; - -namespace NMonitoring { - class IMetricConsumer { - public: - virtual ~IMetricConsumer() = default; - - virtual void OnStreamBegin() = 0; - virtual void OnStreamEnd() = 0; - - virtual void OnCommonTime(TInstant time) = 0; - - virtual void OnMetricBegin(EMetricType type) = 0; - virtual void OnMetricEnd() = 0; - - virtual void OnLabelsBegin() = 0; - virtual void OnLabelsEnd() = 0; - virtual void OnLabel(TStringBuf name, TStringBuf value) = 0; + +class TInstant; + +namespace NMonitoring { + class IMetricConsumer { + public: + virtual ~IMetricConsumer() = default; + + virtual void OnStreamBegin() = 0; + virtual void OnStreamEnd() = 0; + + virtual void OnCommonTime(TInstant time) = 0; + + virtual void OnMetricBegin(EMetricType type) = 0; + virtual void OnMetricEnd() = 0; + + virtual void OnLabelsBegin() = 0; + virtual void OnLabelsEnd() = 0; + virtual void OnLabel(TStringBuf name, TStringBuf value) = 0; virtual void OnLabel(ui32 name, ui32 value); - virtual std::pair<ui32, ui32> PrepareLabel(TStringBuf name, TStringBuf value); - - virtual void OnDouble(TInstant time, double value) = 0; - virtual void OnInt64(TInstant time, i64 value) = 0; - virtual void OnUint64(TInstant time, ui64 value) = 0; - - virtual void OnHistogram(TInstant time, IHistogramSnapshotPtr snapshot) = 0; + virtual std::pair<ui32, ui32> PrepareLabel(TStringBuf name, TStringBuf value); + + virtual void OnDouble(TInstant time, double value) = 0; + virtual void OnInt64(TInstant time, i64 value) = 0; + virtual void OnUint64(TInstant time, ui64 value) = 0; + + virtual void OnHistogram(TInstant time, IHistogramSnapshotPtr snapshot) = 0; virtual void OnLogHistogram(TInstant time, TLogHistogramSnapshotPtr snapshot) = 0; - virtual void OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr snapshot) = 0; - }; - - using IMetricConsumerPtr = THolder<IMetricConsumer>; - -} + virtual void OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr snapshot) = 0; + }; + + using IMetricConsumerPtr = THolder<IMetricConsumer>; + +} diff --git a/library/cpp/monlib/metrics/metric_registry.cpp b/library/cpp/monlib/metrics/metric_registry.cpp index 091a7b5282..b083163a7b 100644 --- a/library/cpp/monlib/metrics/metric_registry.cpp +++ b/library/cpp/monlib/metrics/metric_registry.cpp @@ -1,41 +1,41 @@ -#include "metric_registry.h" - +#include "metric_registry.h" + #include <memory> -namespace NMonitoring { - namespace { - void ConsumeLabels(IMetricConsumer* consumer, const ILabels& labels) { - for (auto&& label: labels) { - consumer->OnLabel(label.Name(), label.Value()); - } - } - - template <typename TLabelsConsumer> - void ConsumeMetric(TInstant time, IMetricConsumer* consumer, IMetric* metric, TLabelsConsumer&& labelsConsumer) { - consumer->OnMetricBegin(metric->Type()); - - // (1) add labels - consumer->OnLabelsBegin(); - labelsConsumer(); - consumer->OnLabelsEnd(); - - // (2) add time and value - metric->Accept(time, consumer); - consumer->OnMetricEnd(); - } - } - +namespace NMonitoring { + namespace { + void ConsumeLabels(IMetricConsumer* consumer, const ILabels& labels) { + for (auto&& label: labels) { + consumer->OnLabel(label.Name(), label.Value()); + } + } + + template <typename TLabelsConsumer> + void ConsumeMetric(TInstant time, IMetricConsumer* consumer, IMetric* metric, TLabelsConsumer&& labelsConsumer) { + consumer->OnMetricBegin(metric->Type()); + + // (1) add labels + consumer->OnLabelsBegin(); + labelsConsumer(); + consumer->OnLabelsEnd(); + + // (2) add time and value + metric->Accept(time, consumer); + consumer->OnMetricEnd(); + } + } + void WriteLabels(IMetricConsumer* consumer, const ILabels& labels) { consumer->OnLabelsBegin(); - ConsumeLabels(consumer, labels); + ConsumeLabels(consumer, labels); consumer->OnLabelsEnd(); } - TMetricRegistry::TMetricRegistry() = default; - TMetricRegistry::~TMetricRegistry() = default; + TMetricRegistry::TMetricRegistry() = default; + TMetricRegistry::~TMetricRegistry() = default; - TMetricRegistry::TMetricRegistry(const TLabels& commonLabels) - : TMetricRegistry{} + TMetricRegistry::TMetricRegistry(const TLabels& commonLabels) + : TMetricRegistry{} { CommonLabels_ = commonLabels; } @@ -44,14 +44,14 @@ namespace NMonitoring { return Singleton<TMetricRegistry>(); } - TGauge* TMetricRegistry::Gauge(TLabels labels) { - return Metric<TGauge, EMetricType::GAUGE>(std::move(labels)); - } - - TGauge* TMetricRegistry::Gauge(ILabelsPtr labels) { - return Metric<TGauge, EMetricType::GAUGE>(std::move(labels)); - } - + TGauge* TMetricRegistry::Gauge(TLabels labels) { + return Metric<TGauge, EMetricType::GAUGE>(std::move(labels)); + } + + TGauge* TMetricRegistry::Gauge(ILabelsPtr labels) { + return Metric<TGauge, EMetricType::GAUGE>(std::move(labels)); + } + TLazyGauge* TMetricRegistry::LazyGauge(TLabels labels, std::function<double()> supplier) { return Metric<TLazyGauge, EMetricType::GAUGE>(std::move(labels), std::move(supplier)); } @@ -60,14 +60,14 @@ namespace NMonitoring { return Metric<TLazyGauge, EMetricType::GAUGE>(std::move(labels), std::move(supplier)); } - TIntGauge* TMetricRegistry::IntGauge(TLabels labels) { - return Metric<TIntGauge, EMetricType::IGAUGE>(std::move(labels)); - } - - TIntGauge* TMetricRegistry::IntGauge(ILabelsPtr labels) { - return Metric<TIntGauge, EMetricType::IGAUGE>(std::move(labels)); - } - + TIntGauge* TMetricRegistry::IntGauge(TLabels labels) { + return Metric<TIntGauge, EMetricType::IGAUGE>(std::move(labels)); + } + + TIntGauge* TMetricRegistry::IntGauge(ILabelsPtr labels) { + return Metric<TIntGauge, EMetricType::IGAUGE>(std::move(labels)); + } + TLazyIntGauge* TMetricRegistry::LazyIntGauge(TLabels labels, std::function<i64()> supplier) { return Metric<TLazyIntGauge, EMetricType::GAUGE>(std::move(labels), std::move(supplier)); } @@ -76,12 +76,12 @@ namespace NMonitoring { return Metric<TLazyIntGauge, EMetricType::GAUGE>(std::move(labels), std::move(supplier)); } - TCounter* TMetricRegistry::Counter(TLabels labels) { - return Metric<TCounter, EMetricType::COUNTER>(std::move(labels)); + 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)); + TCounter* TMetricRegistry::Counter(ILabelsPtr labels) { + return Metric<TCounter, EMetricType::COUNTER>(std::move(labels)); } TLazyCounter* TMetricRegistry::LazyCounter(TLabels labels, std::function<ui64()> supplier) { @@ -92,12 +92,12 @@ namespace NMonitoring { return Metric<TLazyCounter, EMetricType::COUNTER>(std::move(labels), std::move(supplier)); } - TRate* TMetricRegistry::Rate(TLabels labels) { - return Metric<TRate, EMetricType::RATE>(std::move(labels)); + 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)); + TRate* TMetricRegistry::Rate(ILabelsPtr labels) { + return Metric<TRate, EMetricType::RATE>(std::move(labels)); } TLazyRate* TMetricRegistry::LazyRate(TLabels labels, std::function<ui64()> supplier) { @@ -108,20 +108,20 @@ namespace NMonitoring { return Metric<TLazyRate, EMetricType::RATE>(std::move(labels), std::move(supplier)); } - THistogram* TMetricRegistry::HistogramCounter(TLabels labels, IHistogramCollectorPtr collector) { - return Metric<THistogram, EMetricType::HIST>(std::move(labels), std::move(collector), false); - } - - THistogram* TMetricRegistry::HistogramCounter(ILabelsPtr labels, IHistogramCollectorPtr collector) { - return Metric<THistogram, EMetricType::HIST>(std::move(labels), std::move(collector), false); + THistogram* TMetricRegistry::HistogramCounter(TLabels 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); + 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() { @@ -157,74 +157,74 @@ namespace NMonitoring { Metrics_.clear(); } - template <typename TMetric, EMetricType type, typename TLabelsType, typename... Args> - TMetric* TMetricRegistry::Metric(TLabelsType&& labels, Args&&... args) { - { + template <typename TMetric, EMetricType type, typename TLabelsType, typename... Args> + TMetric* TMetricRegistry::Metric(TLabelsType&& labels, Args&&... args) { + { 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()); - } - } - - { - IMetricPtr metric = MakeHolder<TMetric>(std::forward<Args>(args)...); - + 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()); + } + } + + { + IMetricPtr metric = MakeHolder<TMetric>(std::forward<Args>(args)...); + TWriteGuard g{Lock_}; - // decltype(Metrics_)::iterator breaks build on windows - THashMap<ILabelsPtr, IMetricPtr>::iterator it; + // decltype(Metrics_)::iterator breaks build on windows + THashMap<ILabelsPtr, IMetricPtr>::iterator it; if constexpr (!std::is_convertible_v<TLabelsType, ILabelsPtr>) { - it = Metrics_.emplace(new TLabels{std::forward<TLabelsType>(labels)}, std::move(metric)).first; + it = Metrics_.emplace(new TLabels{std::forward<TLabelsType>(labels)}, std::move(metric)).first; } else { - it = Metrics_.emplace(std::forward<TLabelsType>(labels), std::move(metric)).first; + it = Metrics_.emplace(std::forward<TLabelsType>(labels), std::move(metric)).first; } - return static_cast<TMetric*>(it->second.Get()); - } - } + return static_cast<TMetric*>(it->second.Get()); + } + } - void TMetricRegistry::RemoveMetric(const ILabels& labels) noexcept { + void TMetricRegistry::RemoveMetric(const ILabels& labels) noexcept { TWriteGuard g{Lock_}; - Metrics_.erase(labels); + Metrics_.erase(labels); } - void TMetricRegistry::Accept(TInstant time, IMetricConsumer* consumer) const { + void TMetricRegistry::Accept(TInstant time, IMetricConsumer* consumer) const { consumer->OnStreamBegin(); if (!CommonLabels_.Empty()) { - consumer->OnLabelsBegin(); - ConsumeLabels(consumer, CommonLabels_); - consumer->OnLabelsEnd(); + consumer->OnLabelsBegin(); + ConsumeLabels(consumer, CommonLabels_); + consumer->OnLabelsEnd(); + } + + { + TReadGuard g{Lock_}; + for (const auto& it: Metrics_) { + ILabels* labels = it.first.Get(); + IMetric* metric = it.second.Get(); + ConsumeMetric(time, consumer, metric, [&]() { + ConsumeLabels(consumer, *labels); + }); + } } - { - TReadGuard g{Lock_}; - for (const auto& it: Metrics_) { - ILabels* labels = it.first.Get(); - IMetric* metric = it.second.Get(); - ConsumeMetric(time, consumer, metric, [&]() { - ConsumeLabels(consumer, *labels); - }); - } - } - consumer->OnStreamEnd(); } - void TMetricRegistry::Append(TInstant time, IMetricConsumer* consumer) const { + 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(); - ConsumeMetric(time, consumer, metric, [&]() { - ConsumeLabels(consumer, CommonLabels_); - ConsumeLabels(consumer, *labels); - }); - } - } -} + for (const auto& it: Metrics_) { + ILabels* labels = it.first.Get(); + IMetric* metric = it.second.Get(); + ConsumeMetric(time, consumer, metric, [&]() { + ConsumeLabels(consumer, CommonLabels_); + ConsumeLabels(consumer, *labels); + }); + } + } +} diff --git a/library/cpp/monlib/metrics/metric_registry.h b/library/cpp/monlib/metrics/metric_registry.h index e308065da9..670cf8651e 100644 --- a/library/cpp/monlib/metrics/metric_registry.h +++ b/library/cpp/monlib/metrics/metric_registry.h @@ -1,17 +1,17 @@ -#pragma once - -#include "labels.h" -#include "metric.h" - +#pragma once + +#include "labels.h" +#include "metric.h" + #include <util/system/rwlock.h> - + #include <library/cpp/threading/light_rw_lock/lightrwlock.h> -namespace NMonitoring { - class IMetricFactory { +namespace NMonitoring { + class IMetricFactory { public: - virtual ~IMetricFactory() = default; + virtual ~IMetricFactory() = default; virtual IGauge* Gauge(ILabelsPtr labels) = 0; virtual ILazyGauge* LazyGauge(ILabelsPtr labels, std::function<double()> supplier) = 0; @@ -36,27 +36,27 @@ namespace NMonitoring { public: virtual ~IMetricSupplier() = default; - virtual void Accept(TInstant time, IMetricConsumer* consumer) const = 0; - virtual void Append(TInstant time, IMetricConsumer* consumer) const = 0; + 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; - virtual void RemoveMetric(const ILabels& labels) noexcept = 0; + virtual void RemoveMetric(const ILabels& labels) noexcept = 0; }; - /////////////////////////////////////////////////////////////////////////////// - // TMetricRegistry - /////////////////////////////////////////////////////////////////////////////// - class TMetricRegistry: public IMetricRegistry { - public: - TMetricRegistry(); - ~TMetricRegistry(); - - explicit TMetricRegistry(const TLabels& commonLabels); - + /////////////////////////////////////////////////////////////////////////////// + // TMetricRegistry + /////////////////////////////////////////////////////////////////////////////// + class TMetricRegistry: public IMetricRegistry { + public: + TMetricRegistry(); + ~TMetricRegistry(); + + explicit TMetricRegistry(const TLabels& commonLabels); + /** * Get a global metrics registry instance. */ @@ -66,19 +66,19 @@ namespace NMonitoring { TLazyGauge* LazyGauge(TLabels labels, std::function<double()> supplier); TIntGauge* IntGauge(TLabels labels); TLazyIntGauge* LazyIntGauge(TLabels labels, std::function<i64()> supplier); - TCounter* Counter(TLabels labels); + TCounter* Counter(TLabels labels); TLazyCounter* LazyCounter(TLabels labels, std::function<ui64()> supplier); TRate* Rate(TLabels labels); TLazyRate* LazyRate(TLabels labels, std::function<ui64()> supplier); - - THistogram* HistogramCounter( + + THistogram* HistogramCounter( TLabels labels, - IHistogramCollectorPtr collector); - - THistogram* HistogramRate( + IHistogramCollectorPtr collector); + + THistogram* HistogramRate( TLabels labels, - IHistogramCollectorPtr collector); - + IHistogramCollectorPtr collector); + /** * Set all registered metrics to zero */ @@ -88,21 +88,21 @@ namespace NMonitoring { */ void Clear(); - void Accept(TInstant time, IMetricConsumer* consumer) const override; - void Append(TInstant time, IMetricConsumer* consumer) const override; - + void Accept(TInstant time, IMetricConsumer* consumer) const override; + void Append(TInstant time, IMetricConsumer* consumer) const override; + const TLabels& CommonLabels() const noexcept override { - return CommonLabels_; - } - - void RemoveMetric(const ILabels& labels) noexcept override; + return CommonLabels_; + } - private: + void RemoveMetric(const ILabels& labels) noexcept override; + + private: TGauge* Gauge(ILabelsPtr labels) override; TLazyGauge* LazyGauge(ILabelsPtr labels, std::function<double()> supplier) override; TIntGauge* IntGauge(ILabelsPtr labels) override; TLazyIntGauge* LazyIntGauge(ILabelsPtr labels, std::function<i64()> supplier) override; - TCounter* Counter(ILabelsPtr labels) override; + TCounter* Counter(ILabelsPtr labels) override; TLazyCounter* LazyCounter(ILabelsPtr labels, std::function<ui64()> supplier) override; TRate* Rate(ILabelsPtr labels) override; TLazyRate* LazyRate(ILabelsPtr labels, std::function<ui64()> supplier) override; @@ -117,13 +117,13 @@ namespace NMonitoring { private: TRWMutex Lock_; - THashMap<ILabelsPtr, IMetricPtr> Metrics_; + THashMap<ILabelsPtr, IMetricPtr> Metrics_; - template <typename TMetric, EMetricType type, typename TLabelsType, typename... Args> - TMetric* Metric(TLabelsType&& labels, Args&&... args); - - TLabels CommonLabels_; - }; + template <typename TMetric, EMetricType type, typename TLabelsType, typename... Args> + TMetric* Metric(TLabelsType&& labels, Args&&... args); + + TLabels CommonLabels_; + }; 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 e10d2efc02..86d9a52ec0 100644 --- a/library/cpp/monlib/metrics/metric_registry_ut.cpp +++ b/library/cpp/monlib/metrics/metric_registry_ut.cpp @@ -1,15 +1,15 @@ -#include "metric_registry.h" - -#include <library/cpp/monlib/encode/protobuf/protobuf.h> -#include <library/cpp/monlib/encode/json/json.h> +#include "metric_registry.h" + +#include <library/cpp/monlib/encode/protobuf/protobuf.h> +#include <library/cpp/monlib/encode/json/json.h> #include <library/cpp/resource/resource.h> - + #include <library/cpp/testing/unittest/registar.h> - -#include <util/stream/str.h> - -using namespace NMonitoring; - + +#include <util/stream/str.h> + +using namespace NMonitoring; + template<> void Out<NMonitoring::NProto::TSingleSample::ValueCase>(IOutputStream& os, NMonitoring::NProto::TSingleSample::ValueCase val) { switch (val) { @@ -37,14 +37,14 @@ void Out<NMonitoring::NProto::TSingleSample::ValueCase>(IOutputStream& os, NMoni } } -Y_UNIT_TEST_SUITE(TMetricRegistryTest) { +Y_UNIT_TEST_SUITE(TMetricRegistryTest) { Y_UNIT_TEST(Gauge) { - TMetricRegistry registry(TLabels{{"common", "label"}}); - TGauge* g = registry.Gauge({{"my", "gauge"}}); - - UNIT_ASSERT_DOUBLES_EQUAL(g->Get(), 0.0, 1E-6); - g->Set(12.34); - UNIT_ASSERT_DOUBLES_EQUAL(g->Get(), 12.34, 1E-6); + TMetricRegistry registry(TLabels{{"common", "label"}}); + TGauge* g = registry.Gauge({{"my", "gauge"}}); + + 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; @@ -55,8 +55,8 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) { 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) { TMetricRegistry registry(TLabels{{"common", "label"}}); double val = 0.0; @@ -76,7 +76,7 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) { } Y_UNIT_TEST(IntGauge) { - TMetricRegistry registry(TLabels{{"common", "label"}}); + TMetricRegistry registry(TLabels{{"common", "label"}}); TIntGauge* g = registry.IntGauge({{"my", "gauge"}}); UNIT_ASSERT_VALUES_EQUAL(g->Get(), 0); @@ -123,16 +123,16 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) { } Y_UNIT_TEST(Counter) { - TMetricRegistry registry(TLabels{{"common", "label"}}); - TCounter* c = registry.Counter({{"my", "counter"}}); - + 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); - } - + } + Y_UNIT_TEST(LazyCounter) { TMetricRegistry registry(TLabels{{"common", "label"}}); ui64 val = 0; @@ -156,9 +156,9 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) { } Y_UNIT_TEST(DoubleCounter) { - TMetricRegistry registry(TLabels{{"common", "label"}}); + TMetricRegistry registry(TLabels{{"common", "label"}}); - TCounter* c = registry.Counter({{"my", "counter"}}); + TCounter* c = registry.Counter({{"my", "counter"}}); UNIT_ASSERT_VALUES_EQUAL(c->Get(), 0); c->Add(10); @@ -167,19 +167,19 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) { } Y_UNIT_TEST(Sample) { - TMetricRegistry registry(TLabels{{"common", "label"}}); - - TGauge* g = registry.Gauge({{"my", "gauge"}}); - g->Set(12.34); - - TCounter* c = registry.Counter({{"my", "counter"}}); - c->Add(10); - - NProto::TSingleSamplesList samples; - auto encoder = EncoderProtobuf(&samples); - auto now = TInstant::Now(); - registry.Accept(now, encoder.Get()); - + TMetricRegistry registry(TLabels{{"common", "label"}}); + + TGauge* g = registry.Gauge({{"my", "gauge"}}); + g->Set(12.34); + + TCounter* c = registry.Counter({{"my", "counter"}}); + c->Add(10); + + NProto::TSingleSamplesList samples; + auto encoder = EncoderProtobuf(&samples); + auto now = TInstant::Now(); + registry.Accept(now, encoder.Get()); + UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 2); UNIT_ASSERT_VALUES_EQUAL(samples.CommonLabelsSize(), 1); { @@ -187,65 +187,65 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) { UNIT_ASSERT_STRINGS_EQUAL(label.GetName(), "common"); UNIT_ASSERT_STRINGS_EQUAL(label.GetValue(), "label"); } - - for (const NProto::TSingleSample& sample : samples.GetSamples()) { + + for (const NProto::TSingleSample& sample : samples.GetSamples()) { UNIT_ASSERT_VALUES_EQUAL(sample.LabelsSize(), 1); UNIT_ASSERT_VALUES_EQUAL(sample.GetTime(), now.MilliSeconds()); - - if (sample.GetMetricType() == NProto::GAUGE) { + + if (sample.GetMetricType() == NProto::GAUGE) { UNIT_ASSERT_VALUES_EQUAL(sample.GetValueCase(), NProto::TSingleSample::kFloat64); - UNIT_ASSERT_DOUBLES_EQUAL(sample.GetFloat64(), 12.34, 1E-6); - + UNIT_ASSERT_DOUBLES_EQUAL(sample.GetFloat64(), 12.34, 1E-6); + 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_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); - + const NProto::TLabel& label = sample.GetLabels(0); - UNIT_ASSERT_STRINGS_EQUAL(label.GetName(), "my"); - UNIT_ASSERT_STRINGS_EQUAL(label.GetValue(), "counter"); - } else { - UNIT_FAIL("unexpected sample type"); - } - } - } - - Y_UNIT_TEST(Histograms) { - TMetricRegistry registry(TLabels{{"common", "label"}}); - - THistogram* h1 = registry.HistogramCounter( - {{"sensor", "readTimeMillis"}}, - ExponentialHistogram(5, 2)); - - THistogram* h2 = registry.HistogramRate( - {{"sensor", "writeTimeMillis"}}, - ExplicitHistogram({1, 5, 15, 20, 25})); - - for (i64 i = 0; i < 100; i++) { - h1->Record(i); - h2->Record(i); - } - - TStringStream ss; - { - auto encoder = EncoderJson(&ss, 2); - registry.Accept(TInstant::Zero(), encoder.Get()); - } - ss << '\n'; - - UNIT_ASSERT_NO_DIFF(ss.Str(), NResource::Find("/histograms.json")); - } - + UNIT_ASSERT_STRINGS_EQUAL(label.GetName(), "my"); + UNIT_ASSERT_STRINGS_EQUAL(label.GetValue(), "counter"); + } else { + UNIT_FAIL("unexpected sample type"); + } + } + } + + Y_UNIT_TEST(Histograms) { + TMetricRegistry registry(TLabels{{"common", "label"}}); + + THistogram* h1 = registry.HistogramCounter( + {{"sensor", "readTimeMillis"}}, + ExponentialHistogram(5, 2)); + + THistogram* h2 = registry.HistogramRate( + {{"sensor", "writeTimeMillis"}}, + ExplicitHistogram({1, 5, 15, 20, 25})); + + for (i64 i = 0; i < 100; i++) { + h1->Record(i); + h2->Record(i); + } + + TStringStream ss; + { + auto encoder = EncoderJson(&ss, 2); + registry.Accept(TInstant::Zero(), encoder.Get()); + } + ss << '\n'; + + 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}]}" }; - TMetricRegistry registry(TLabels{{"common", "label"}}); + TMetricRegistry registry(TLabels{{"common", "label"}}); TGauge* g = registry.Gauge({{"my", "gauge"}}); g->Set(12.34); @@ -257,48 +257,48 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) { UNIT_ASSERT_STRINGS_EQUAL(os.Str(), expected); } - Y_UNIT_TEST(CreatingSameMetricWithDifferentTypesShouldThrow) { - TMetricRegistry registry; - + Y_UNIT_TEST(CreatingSameMetricWithDifferentTypesShouldThrow) { + TMetricRegistry registry; + 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); - } - - Y_UNIT_TEST(EncodeRegistryWithCommonLabels) { - TMetricRegistry registry(TLabels{{"common", "label"}}); - - TGauge* g = registry.Gauge({{"my", "gauge"}}); - g->Set(12.34); - - // Append() adds common labels to each metric, allowing to combine - // several metric registries in one resulting blob - { - TStringStream os; - auto encoder = EncoderJson(&os); - encoder->OnStreamBegin(); - registry.Append(TInstant::Zero(), encoder.Get()); - encoder->OnStreamEnd(); - - UNIT_ASSERT_STRINGS_EQUAL( - os.Str(), - "{\"sensors\":[{\"kind\":\"GAUGE\",\"labels\":{\"common\":\"label\",\"my\":\"gauge\"},\"value\":12.34}]}"); - } - - // Accept() adds common labels to the beginning of the blob - { - TStringStream os; - auto encoder = EncoderJson(&os); - registry.Accept(TInstant::Zero(), encoder.Get()); - - UNIT_ASSERT_STRINGS_EQUAL( - os.Str(), - "{\"commonLabels\":{\"common\":\"label\"}," - "\"sensors\":[{\"kind\":\"GAUGE\",\"labels\":{\"my\":\"gauge\"},\"value\":12.34}]}"); - } - } + } + + Y_UNIT_TEST(EncodeRegistryWithCommonLabels) { + TMetricRegistry registry(TLabels{{"common", "label"}}); + + TGauge* g = registry.Gauge({{"my", "gauge"}}); + g->Set(12.34); + + // Append() adds common labels to each metric, allowing to combine + // several metric registries in one resulting blob + { + TStringStream os; + auto encoder = EncoderJson(&os); + encoder->OnStreamBegin(); + registry.Append(TInstant::Zero(), encoder.Get()); + encoder->OnStreamEnd(); + + UNIT_ASSERT_STRINGS_EQUAL( + os.Str(), + "{\"sensors\":[{\"kind\":\"GAUGE\",\"labels\":{\"common\":\"label\",\"my\":\"gauge\"},\"value\":12.34}]}"); + } + + // Accept() adds common labels to the beginning of the blob + { + TStringStream os; + auto encoder = EncoderJson(&os); + registry.Accept(TInstant::Zero(), encoder.Get()); + + UNIT_ASSERT_STRINGS_EQUAL( + os.Str(), + "{\"commonLabels\":{\"common\":\"label\"}," + "\"sensors\":[{\"kind\":\"GAUGE\",\"labels\":{\"my\":\"gauge\"},\"value\":12.34}]}"); + } + } Y_UNIT_TEST(MetricsRegistryClear) { TMetricRegistry registry; @@ -316,4 +316,4 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) { UNIT_ASSERT(samples.SamplesSize() == 0); } -} +} diff --git a/library/cpp/monlib/metrics/metric_sub_registry.h b/library/cpp/monlib/metrics/metric_sub_registry.h index 7039762336..e83eeeafb2 100644 --- a/library/cpp/monlib/metrics/metric_sub_registry.h +++ b/library/cpp/monlib/metrics/metric_sub_registry.h @@ -1,116 +1,116 @@ -#pragma once - -#include "metric_registry.h" - -namespace NMonitoring { - -/** - * This registry is wrapping given delegate registry to add common labels - * to all created metrics through this sub registry. - */ -class TMetricSubRegistry final: public IMetricRegistry { -public: - /** - * Do not keep ownership of the given delegate. - */ - TMetricSubRegistry(TLabels commonLabels, IMetricRegistry* delegate) noexcept - : CommonLabels_{std::move(commonLabels)} - , DelegatePtr_{delegate} - { - } - - /** - * Keeps ownership of the given delegate. - */ - TMetricSubRegistry(TLabels commonLabels, std::shared_ptr<IMetricRegistry> delegate) noexcept - : CommonLabels_{std::move(commonLabels)} - , Delegate_{std::move(delegate)} - , DelegatePtr_{Delegate_.get()} - { - } - - IGauge* Gauge(ILabelsPtr labels) override { - AddCommonLabels(labels.Get()); - return DelegatePtr_->Gauge(std::move(labels)); - } - - ILazyGauge* LazyGauge(ILabelsPtr labels, std::function<double()> supplier) override { - AddCommonLabels(labels.Get()); - return DelegatePtr_->LazyGauge(std::move(labels), std::move(supplier)); - } - - IIntGauge* IntGauge(ILabelsPtr labels) override { - AddCommonLabels(labels.Get()); - return DelegatePtr_->IntGauge(std::move(labels)); - } - - ILazyIntGauge* LazyIntGauge(ILabelsPtr labels, std::function<i64()> supplier) override { - AddCommonLabels(labels.Get()); - return DelegatePtr_->LazyIntGauge(std::move(labels), std::move(supplier)); - } - - ICounter* Counter(ILabelsPtr labels) override { - AddCommonLabels(labels.Get()); - return DelegatePtr_->Counter(std::move(labels)); - } - - ILazyCounter* LazyCounter(ILabelsPtr labels, std::function<ui64()> supplier) override { - AddCommonLabels(labels.Get()); - return DelegatePtr_->LazyCounter(std::move(labels), std::move(supplier)); - } - - IRate* Rate(ILabelsPtr labels) override { - AddCommonLabels(labels.Get()); - return DelegatePtr_->Rate(std::move(labels)); - } - - ILazyRate* LazyRate(ILabelsPtr labels, std::function<ui64()> supplier) override { - AddCommonLabels(labels.Get()); - return DelegatePtr_->LazyRate(std::move(labels), std::move(supplier)); - } - - IHistogram* HistogramCounter(ILabelsPtr labels, IHistogramCollectorPtr collector) override { - AddCommonLabels(labels.Get()); - return DelegatePtr_->HistogramCounter(std::move(labels), std::move(collector)); - } - - IHistogram* HistogramRate(ILabelsPtr labels, IHistogramCollectorPtr collector) override { - AddCommonLabels(labels.Get()); - return DelegatePtr_->HistogramRate(std::move(labels), std::move(collector)); - } - - void Accept(TInstant time, IMetricConsumer* consumer) const override { - DelegatePtr_->Accept(time, consumer); - } - - void Append(TInstant time, IMetricConsumer* consumer) const override { - DelegatePtr_->Append(time, consumer); - } - - const TLabels& CommonLabels() const noexcept override { - return CommonLabels_; - } - - void RemoveMetric(const ILabels& labels) noexcept override { - TLabelsImpl<TStringBuf> toRemove; - for (auto& l: labels) { - toRemove.Add(l); - } - AddCommonLabels(&toRemove); - DelegatePtr_->RemoveMetric(toRemove); - } - -private: - void AddCommonLabels(ILabels* labels) const { - for (auto& label: CommonLabels_) { - labels->Add(label); - } - } - -private: - const TLabels CommonLabels_; - std::shared_ptr<IMetricRegistry> Delegate_; - IMetricRegistry* DelegatePtr_; -}; - -} // namespace NMonitoring +#pragma once + +#include "metric_registry.h" + +namespace NMonitoring { + +/** + * This registry is wrapping given delegate registry to add common labels + * to all created metrics through this sub registry. + */ +class TMetricSubRegistry final: public IMetricRegistry { +public: + /** + * Do not keep ownership of the given delegate. + */ + TMetricSubRegistry(TLabels commonLabels, IMetricRegistry* delegate) noexcept + : CommonLabels_{std::move(commonLabels)} + , DelegatePtr_{delegate} + { + } + + /** + * Keeps ownership of the given delegate. + */ + TMetricSubRegistry(TLabels commonLabels, std::shared_ptr<IMetricRegistry> delegate) noexcept + : CommonLabels_{std::move(commonLabels)} + , Delegate_{std::move(delegate)} + , DelegatePtr_{Delegate_.get()} + { + } + + IGauge* Gauge(ILabelsPtr labels) override { + AddCommonLabels(labels.Get()); + return DelegatePtr_->Gauge(std::move(labels)); + } + + ILazyGauge* LazyGauge(ILabelsPtr labels, std::function<double()> supplier) override { + AddCommonLabels(labels.Get()); + return DelegatePtr_->LazyGauge(std::move(labels), std::move(supplier)); + } + + IIntGauge* IntGauge(ILabelsPtr labels) override { + AddCommonLabels(labels.Get()); + return DelegatePtr_->IntGauge(std::move(labels)); + } + + ILazyIntGauge* LazyIntGauge(ILabelsPtr labels, std::function<i64()> supplier) override { + AddCommonLabels(labels.Get()); + return DelegatePtr_->LazyIntGauge(std::move(labels), std::move(supplier)); + } + + ICounter* Counter(ILabelsPtr labels) override { + AddCommonLabels(labels.Get()); + return DelegatePtr_->Counter(std::move(labels)); + } + + ILazyCounter* LazyCounter(ILabelsPtr labels, std::function<ui64()> supplier) override { + AddCommonLabels(labels.Get()); + return DelegatePtr_->LazyCounter(std::move(labels), std::move(supplier)); + } + + IRate* Rate(ILabelsPtr labels) override { + AddCommonLabels(labels.Get()); + return DelegatePtr_->Rate(std::move(labels)); + } + + ILazyRate* LazyRate(ILabelsPtr labels, std::function<ui64()> supplier) override { + AddCommonLabels(labels.Get()); + return DelegatePtr_->LazyRate(std::move(labels), std::move(supplier)); + } + + IHistogram* HistogramCounter(ILabelsPtr labels, IHistogramCollectorPtr collector) override { + AddCommonLabels(labels.Get()); + return DelegatePtr_->HistogramCounter(std::move(labels), std::move(collector)); + } + + IHistogram* HistogramRate(ILabelsPtr labels, IHistogramCollectorPtr collector) override { + AddCommonLabels(labels.Get()); + return DelegatePtr_->HistogramRate(std::move(labels), std::move(collector)); + } + + void Accept(TInstant time, IMetricConsumer* consumer) const override { + DelegatePtr_->Accept(time, consumer); + } + + void Append(TInstant time, IMetricConsumer* consumer) const override { + DelegatePtr_->Append(time, consumer); + } + + const TLabels& CommonLabels() const noexcept override { + return CommonLabels_; + } + + void RemoveMetric(const ILabels& labels) noexcept override { + TLabelsImpl<TStringBuf> toRemove; + for (auto& l: labels) { + toRemove.Add(l); + } + AddCommonLabels(&toRemove); + DelegatePtr_->RemoveMetric(toRemove); + } + +private: + void AddCommonLabels(ILabels* labels) const { + for (auto& label: CommonLabels_) { + labels->Add(label); + } + } + +private: + const TLabels CommonLabels_; + std::shared_ptr<IMetricRegistry> Delegate_; + IMetricRegistry* DelegatePtr_; +}; + +} // namespace NMonitoring diff --git a/library/cpp/monlib/metrics/metric_sub_registry_ut.cpp b/library/cpp/monlib/metrics/metric_sub_registry_ut.cpp index af6d4fa462..0c5d48b876 100644 --- a/library/cpp/monlib/metrics/metric_sub_registry_ut.cpp +++ b/library/cpp/monlib/metrics/metric_sub_registry_ut.cpp @@ -1,65 +1,65 @@ -#include "metric_sub_registry.h" - -#include <library/cpp/testing/unittest/registar.h> - -using namespace NMonitoring; - -Y_UNIT_TEST_SUITE(TMetricSubRegistryTest) { - Y_UNIT_TEST(WrapRegistry) { - TMetricRegistry registry; - - { - TMetricSubRegistry subRegistry{{{"common", "label"}}, ®istry}; - IIntGauge* g = subRegistry.IntGauge(MakeLabels({{"my", "gauge"}})); - UNIT_ASSERT(g); - g->Set(42); - } - - TIntGauge* g = registry.IntGauge({{"my", "gauge"}, {"common", "label"}}); - UNIT_ASSERT(g); - UNIT_ASSERT_VALUES_EQUAL(g->Get(), 42); - } - - Y_UNIT_TEST(CommonLabelsDoNotOverrideGeneralLabel) { - TMetricRegistry registry; - - { - TMetricSubRegistry subRegistry{{{"common", "label"}, {"my", "notOverride"}}, ®istry}; - IIntGauge* g = subRegistry.IntGauge(MakeLabels({{"my", "gauge"}})); - UNIT_ASSERT(g); - g->Set(1234); - } - - TIntGauge* knownGauge = registry.IntGauge({{"my", "gauge"}, {"common", "label"}}); - UNIT_ASSERT(knownGauge); - UNIT_ASSERT_VALUES_EQUAL(knownGauge->Get(), 1234); - - TIntGauge* newGauge = registry.IntGauge({{"common", "label"}, {"my", "notOverride"}}); - UNIT_ASSERT(newGauge); - UNIT_ASSERT_VALUES_EQUAL(newGauge->Get(), 0); - } - - Y_UNIT_TEST(RemoveMetric) { - TMetricRegistry registry; - - { - TMetricSubRegistry subRegistry{{{"common", "label"}}, ®istry}; - IIntGauge* g = subRegistry.IntGauge(MakeLabels({{"my", "gauge"}})); - UNIT_ASSERT(g); - g->Set(1234); - } - - IIntGauge* g1 = registry.IntGauge({{"my", "gauge"}, {"common", "label"}}); - UNIT_ASSERT(g1); - UNIT_ASSERT_VALUES_EQUAL(g1->Get(), 1234); - - { - TMetricSubRegistry subRegistry{{{"common", "label"}}, ®istry}; - subRegistry.RemoveMetric(TLabels{{"my", "gauge"}}); - } - - IIntGauge* g2 = registry.IntGauge({{"my", "gauge"}, {"common", "label"}}); - UNIT_ASSERT(g2); - UNIT_ASSERT_VALUES_EQUAL(g2->Get(), 0); - } -} +#include "metric_sub_registry.h" + +#include <library/cpp/testing/unittest/registar.h> + +using namespace NMonitoring; + +Y_UNIT_TEST_SUITE(TMetricSubRegistryTest) { + Y_UNIT_TEST(WrapRegistry) { + TMetricRegistry registry; + + { + TMetricSubRegistry subRegistry{{{"common", "label"}}, ®istry}; + IIntGauge* g = subRegistry.IntGauge(MakeLabels({{"my", "gauge"}})); + UNIT_ASSERT(g); + g->Set(42); + } + + TIntGauge* g = registry.IntGauge({{"my", "gauge"}, {"common", "label"}}); + UNIT_ASSERT(g); + UNIT_ASSERT_VALUES_EQUAL(g->Get(), 42); + } + + Y_UNIT_TEST(CommonLabelsDoNotOverrideGeneralLabel) { + TMetricRegistry registry; + + { + TMetricSubRegistry subRegistry{{{"common", "label"}, {"my", "notOverride"}}, ®istry}; + IIntGauge* g = subRegistry.IntGauge(MakeLabels({{"my", "gauge"}})); + UNIT_ASSERT(g); + g->Set(1234); + } + + TIntGauge* knownGauge = registry.IntGauge({{"my", "gauge"}, {"common", "label"}}); + UNIT_ASSERT(knownGauge); + UNIT_ASSERT_VALUES_EQUAL(knownGauge->Get(), 1234); + + TIntGauge* newGauge = registry.IntGauge({{"common", "label"}, {"my", "notOverride"}}); + UNIT_ASSERT(newGauge); + UNIT_ASSERT_VALUES_EQUAL(newGauge->Get(), 0); + } + + Y_UNIT_TEST(RemoveMetric) { + TMetricRegistry registry; + + { + TMetricSubRegistry subRegistry{{{"common", "label"}}, ®istry}; + IIntGauge* g = subRegistry.IntGauge(MakeLabels({{"my", "gauge"}})); + UNIT_ASSERT(g); + g->Set(1234); + } + + IIntGauge* g1 = registry.IntGauge({{"my", "gauge"}, {"common", "label"}}); + UNIT_ASSERT(g1); + UNIT_ASSERT_VALUES_EQUAL(g1->Get(), 1234); + + { + TMetricSubRegistry subRegistry{{{"common", "label"}}, ®istry}; + subRegistry.RemoveMetric(TLabels{{"my", "gauge"}}); + } + + IIntGauge* g2 = registry.IntGauge({{"my", "gauge"}, {"common", "label"}}); + UNIT_ASSERT(g2); + UNIT_ASSERT_VALUES_EQUAL(g2->Get(), 0); + } +} diff --git a/library/cpp/monlib/metrics/metric_type.cpp b/library/cpp/monlib/metrics/metric_type.cpp index c903ae3dea..a8a546e843 100644 --- a/library/cpp/monlib/metrics/metric_type.cpp +++ b/library/cpp/monlib/metrics/metric_type.cpp @@ -1,57 +1,57 @@ -#include "metric_type.h" - -#include <util/generic/strbuf.h> -#include <util/generic/yexception.h> -#include <util/stream/output.h> - -namespace NMonitoring { - TStringBuf MetricTypeToStr(EMetricType type) { - switch (type) { - case EMetricType::GAUGE: +#include "metric_type.h" + +#include <util/generic/strbuf.h> +#include <util/generic/yexception.h> +#include <util/stream/output.h> + +namespace NMonitoring { + TStringBuf MetricTypeToStr(EMetricType type) { + switch (type) { + case EMetricType::GAUGE: return TStringBuf("GAUGE"); - case EMetricType::COUNTER: + case EMetricType::COUNTER: return TStringBuf("COUNTER"); - case EMetricType::RATE: + case EMetricType::RATE: return TStringBuf("RATE"); - case EMetricType::IGAUGE: + case EMetricType::IGAUGE: return TStringBuf("IGAUGE"); - case EMetricType::HIST: + case EMetricType::HIST: return TStringBuf("HIST"); - case EMetricType::HIST_RATE: + case EMetricType::HIST_RATE: return TStringBuf("HIST_RATE"); - case EMetricType::DSUMMARY: + case EMetricType::DSUMMARY: return TStringBuf("DSUMMARY"); case EMetricType::LOGHIST: return TStringBuf("LOGHIST"); - default: + default: return TStringBuf("UNKNOWN"); - } - } - - EMetricType MetricTypeFromStr(TStringBuf str) { + } + } + + EMetricType MetricTypeFromStr(TStringBuf str) { if (str == TStringBuf("GAUGE") || str == TStringBuf("DGAUGE")) { - return EMetricType::GAUGE; + return EMetricType::GAUGE; } else if (str == TStringBuf("COUNTER")) { - return EMetricType::COUNTER; + return EMetricType::COUNTER; } else if (str == TStringBuf("RATE")) { - return EMetricType::RATE; + return EMetricType::RATE; } else if (str == TStringBuf("IGAUGE")) { - return EMetricType::IGAUGE; + return EMetricType::IGAUGE; } else if (str == TStringBuf("HIST")) { - return EMetricType::HIST; + return EMetricType::HIST; } else if (str == TStringBuf("HIST_RATE")) { - return EMetricType::HIST_RATE; + return EMetricType::HIST_RATE; } else if (str == TStringBuf("DSUMMARY")) { - return EMetricType::DSUMMARY; + return EMetricType::DSUMMARY; } else if (str == TStringBuf("LOGHIST")) { return EMetricType::LOGHIST; - } else { - ythrow yexception() << "unknown metric type: " << str; - } - } -} - -template <> -void Out<NMonitoring::EMetricType>(IOutputStream& o, NMonitoring::EMetricType t) { - o << NMonitoring::MetricTypeToStr(t); -} + } else { + ythrow yexception() << "unknown metric type: " << str; + } + } +} + +template <> +void Out<NMonitoring::EMetricType>(IOutputStream& o, NMonitoring::EMetricType t) { + o << NMonitoring::MetricTypeToStr(t); +} diff --git a/library/cpp/monlib/metrics/metric_type.h b/library/cpp/monlib/metrics/metric_type.h index 00a8187af1..1984c42c1e 100644 --- a/library/cpp/monlib/metrics/metric_type.h +++ b/library/cpp/monlib/metrics/metric_type.h @@ -1,25 +1,25 @@ -#pragma once - -#include <util/generic/fwd.h> - -namespace NMonitoring { - - constexpr ui32 MaxMetricTypeNameLength = 9; - - enum class EMetricType { - UNKNOWN = 0, - GAUGE = 1, - COUNTER = 2, - RATE = 3, - IGAUGE = 4, - HIST = 5, - HIST_RATE = 6, +#pragma once + +#include <util/generic/fwd.h> + +namespace NMonitoring { + + constexpr ui32 MaxMetricTypeNameLength = 9; + + enum class EMetricType { + UNKNOWN = 0, + GAUGE = 1, + COUNTER = 2, + RATE = 3, + IGAUGE = 4, + HIST = 5, + HIST_RATE = 6, DSUMMARY = 7, // ISUMMARY = 8, reserved LOGHIST = 9, - }; - - TStringBuf MetricTypeToStr(EMetricType type); - EMetricType MetricTypeFromStr(TStringBuf str); - -} + }; + + TStringBuf MetricTypeToStr(EMetricType type); + EMetricType MetricTypeFromStr(TStringBuf str); + +} diff --git a/library/cpp/monlib/metrics/metric_value.cpp b/library/cpp/monlib/metrics/metric_value.cpp index 1ea2ed4142..b95d7011c6 100644 --- a/library/cpp/monlib/metrics/metric_value.cpp +++ b/library/cpp/monlib/metrics/metric_value.cpp @@ -1,27 +1,27 @@ -#include "metric_value.h" - - -namespace NMonitoring { - void TMetricTimeSeries::SortByTs() { +#include "metric_value.h" + + +namespace NMonitoring { + void TMetricTimeSeries::SortByTs() { SortPointsByTs(ValueType_, Points_); - } - - void TMetricTimeSeries::Clear() noexcept { - if (ValueType_ == EMetricValueType::HISTOGRAM) { - for (TPoint& p: Points_) { - SnapshotUnRef<EMetricValueType::HISTOGRAM>(p); - } - } else if (ValueType_ == EMetricValueType::SUMMARY) { + } + + void TMetricTimeSeries::Clear() noexcept { + if (ValueType_ == EMetricValueType::HISTOGRAM) { + for (TPoint& p: Points_) { + SnapshotUnRef<EMetricValueType::HISTOGRAM>(p); + } + } else if (ValueType_ == EMetricValueType::SUMMARY) { for (TPoint& p: Points_) { - SnapshotUnRef<EMetricValueType::SUMMARY>(p); + SnapshotUnRef<EMetricValueType::SUMMARY>(p); } } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { for (TPoint& p: Points_) { SnapshotUnRef<EMetricValueType::LOGHISTOGRAM>(p); } - } + } - Points_.clear(); - ValueType_ = EMetricValueType::UNKNOWN; - } -} + Points_.clear(); + ValueType_ = EMetricValueType::UNKNOWN; + } +} diff --git a/library/cpp/monlib/metrics/metric_value.h b/library/cpp/monlib/metrics/metric_value.h index d7b2f6a680..607fcc8602 100644 --- a/library/cpp/monlib/metrics/metric_value.h +++ b/library/cpp/monlib/metrics/metric_value.h @@ -1,17 +1,17 @@ -#pragma once - -#include "histogram_collector.h" -#include "metric_value_type.h" +#pragma once + +#include "histogram_collector.h" +#include "metric_value_type.h" #include "summary_collector.h" #include "log_histogram_snapshot.h" - -#include <util/datetime/base.h> -#include <util/generic/algorithm.h> -#include <util/generic/vector.h> + +#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> - -namespace NMonitoring { + +namespace NMonitoring { namespace NPrivate { template <typename T> T FromFloatSafe(double d) { @@ -25,71 +25,71 @@ namespace NMonitoring { }; } // namespace NPrivate - template <typename T, typename Enable = void> - struct TValueType; - - template <> - struct TValueType<double> { - static constexpr auto Type = EMetricValueType::DOUBLE; - }; - - template <> - struct TValueType<i64> { - static constexpr auto Type = EMetricValueType::INT64; - }; - - template <> - struct TValueType<ui64> { - static constexpr auto Type = EMetricValueType::UINT64; - }; - + template <typename T, typename Enable = void> + struct TValueType; + + template <> + struct TValueType<double> { + static constexpr auto Type = EMetricValueType::DOUBLE; + }; + + template <> + struct TValueType<i64> { + static constexpr auto Type = EMetricValueType::INT64; + }; + + template <> + struct TValueType<ui64> { + static constexpr auto Type = EMetricValueType::UINT64; + }; + template <> struct TValueType<TLogHistogramSnapshot*> { static constexpr auto Type = EMetricValueType::LOGHISTOGRAM; }; - template <typename T> - struct TValueType<T*, typename std::enable_if_t<std::is_base_of<IHistogramSnapshot, T>::value>> { - static constexpr auto Type = EMetricValueType::HISTOGRAM; - }; - + template <typename T> + struct TValueType<T*, typename std::enable_if_t<std::is_base_of<IHistogramSnapshot, T>::value>> { + static constexpr auto Type = EMetricValueType::HISTOGRAM; + }; + template <typename T> struct TValueType<T*, typename std::enable_if_t<std::is_base_of<ISummaryDoubleSnapshot, T>::value>> { - static constexpr auto Type = EMetricValueType::SUMMARY; + static constexpr auto Type = EMetricValueType::SUMMARY; }; - - /////////////////////////////////////////////////////////////////////////// - // TMetricValue - /////////////////////////////////////////////////////////////////////////// - // TMetricValue represents a generic value. It does not contain type + + /////////////////////////////////////////////////////////////////////////// + // 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. - class TMetricValue { - public: - TMetricValue() noexcept { - Value_.Uint64 = 0; - } - - explicit TMetricValue(double value) noexcept { - Value_.Double = value; - } - - explicit TMetricValue(i64 value) noexcept { - Value_.Int64 = value; - } - - explicit TMetricValue(ui64 value) noexcept { - Value_.Uint64 = value; - } - - explicit TMetricValue(IHistogramSnapshot* histogram) noexcept { - Value_.Histogram = histogram; - } - - explicit TMetricValue(ISummaryDoubleSnapshot* summary) noexcept { + class TMetricValue { + public: + TMetricValue() noexcept { + Value_.Uint64 = 0; + } + + explicit TMetricValue(double value) noexcept { + Value_.Double = value; + } + + explicit TMetricValue(i64 value) noexcept { + Value_.Int64 = value; + } + + explicit TMetricValue(ui64 value) noexcept { + Value_.Uint64 = value; + } + + explicit TMetricValue(IHistogramSnapshot* histogram) noexcept { + Value_.Histogram = histogram; + } + + explicit TMetricValue(ISummaryDoubleSnapshot* summary) noexcept { Value_.Summary = summary; } @@ -97,90 +97,90 @@ namespace NMonitoring { Value_.LogHistogram = logHist; } - double AsDouble() const noexcept { - return Value_.Double; - } - - // will cast value into double, current value type is determined by - // the given type argument - double AsDouble(EMetricValueType type) const { - switch (type) { - case EMetricValueType::DOUBLE: - return Value_.Double; - case EMetricValueType::INT64: - return static_cast<double>(Value_.Int64); - case EMetricValueType::UINT64: - return static_cast<double>(Value_.Uint64); - case EMetricValueType::HISTOGRAM: - ythrow yexception() << "histogram cannot be casted to Double"; - case EMetricValueType::SUMMARY: + double AsDouble() const noexcept { + return Value_.Double; + } + + // will cast value into double, current value type is determined by + // the given type argument + double AsDouble(EMetricValueType type) const { + switch (type) { + case EMetricValueType::DOUBLE: + return Value_.Double; + case EMetricValueType::INT64: + return static_cast<double>(Value_.Int64); + case EMetricValueType::UINT64: + return static_cast<double>(Value_.Uint64); + case EMetricValueType::HISTOGRAM: + ythrow yexception() << "histogram cannot be casted to Double"; + case EMetricValueType::SUMMARY: ythrow yexception() << "summary cannot be casted to Double"; case EMetricValueType::LOGHISTOGRAM: ythrow yexception() << "loghistogram cannot be casted to Double"; - case EMetricValueType::UNKNOWN: - ythrow yexception() << "unknown value type"; - } - Y_FAIL(); // for GCC - } - - ui64 AsUint64() const noexcept { - return Value_.Uint64; - } - - // will cast value into uint64, current value's type is determined by - // the given type argument - ui64 AsUint64(EMetricValueType type) const { - switch (type) { - case EMetricValueType::DOUBLE: + case EMetricValueType::UNKNOWN: + ythrow yexception() << "unknown value type"; + } + Y_FAIL(); // for GCC + } + + ui64 AsUint64() const noexcept { + return Value_.Uint64; + } + + // will cast value into uint64, current value's type is determined by + // the given type argument + ui64 AsUint64(EMetricValueType type) const { + switch (type) { + case EMetricValueType::DOUBLE: return NPrivate::FromFloatSafe<ui64>(Value_.Double); - case EMetricValueType::INT64: + case EMetricValueType::INT64: return SafeIntegerCast<ui64>(Value_.Int64); - case EMetricValueType::UINT64: - return Value_.Uint64; - case EMetricValueType::HISTOGRAM: - ythrow yexception() << "histogram cannot be casted to Uint64"; - case EMetricValueType::SUMMARY: + case EMetricValueType::UINT64: + return Value_.Uint64; + case EMetricValueType::HISTOGRAM: + ythrow yexception() << "histogram cannot be casted to Uint64"; + case EMetricValueType::SUMMARY: ythrow yexception() << "summary cannot be casted to Uint64"; case EMetricValueType::LOGHISTOGRAM: ythrow yexception() << "loghistogram cannot be casted to Uint64"; - case EMetricValueType::UNKNOWN: - ythrow yexception() << "unknown value type"; - } - Y_FAIL(); // for GCC - } - - i64 AsInt64() const noexcept { - return Value_.Int64; - } - - // will cast value into int64, current value's type is determined by - // the given type argument - i64 AsInt64(EMetricValueType type) const { - switch (type) { - case EMetricValueType::DOUBLE: + case EMetricValueType::UNKNOWN: + ythrow yexception() << "unknown value type"; + } + Y_FAIL(); // for GCC + } + + i64 AsInt64() const noexcept { + return Value_.Int64; + } + + // will cast value into int64, current value's type is determined by + // the given type argument + i64 AsInt64(EMetricValueType type) const { + switch (type) { + case EMetricValueType::DOUBLE: return NPrivate::FromFloatSafe<i64>(Value_.Double); - case EMetricValueType::INT64: - return Value_.Int64; - case EMetricValueType::UINT64: + case EMetricValueType::INT64: + return Value_.Int64; + case EMetricValueType::UINT64: return SafeIntegerCast<i64>(Value_.Uint64); - case EMetricValueType::HISTOGRAM: - ythrow yexception() << "histogram cannot be casted to Int64"; - case EMetricValueType::SUMMARY: + case EMetricValueType::HISTOGRAM: + ythrow yexception() << "histogram cannot be casted to Int64"; + case EMetricValueType::SUMMARY: ythrow yexception() << "summary cannot be casted to Int64"; case EMetricValueType::LOGHISTOGRAM: ythrow yexception() << "loghistogram cannot be casted to Int64"; - case EMetricValueType::UNKNOWN: - ythrow yexception() << "unknown value type"; - } - Y_FAIL(); // for GCC - } - - IHistogramSnapshot* AsHistogram() const noexcept { - return Value_.Histogram; - } - - IHistogramSnapshot* AsHistogram(EMetricValueType type) const { - if (type != EMetricValueType::HISTOGRAM) { + case EMetricValueType::UNKNOWN: + ythrow yexception() << "unknown value type"; + } + Y_FAIL(); // for GCC + } + + IHistogramSnapshot* AsHistogram() const noexcept { + return Value_.Histogram; + } + + IHistogramSnapshot* AsHistogram(EMetricValueType type) const { + if (type != EMetricValueType::HISTOGRAM) { ythrow yexception() << type << " cannot be casted to Histogram"; } @@ -191,8 +191,8 @@ namespace NMonitoring { return Value_.Summary; } - ISummaryDoubleSnapshot* AsSummaryDouble(EMetricValueType type) const { - if (type != EMetricValueType::SUMMARY) { + ISummaryDoubleSnapshot* AsSummaryDouble(EMetricValueType type) const { + if (type != EMetricValueType::SUMMARY) { ythrow yexception() << type << " cannot be casted to SummaryDouble"; } @@ -211,35 +211,35 @@ namespace NMonitoring { return Value_.LogHistogram; } - protected: - union { - double Double; - i64 Int64; - ui64 Uint64; - IHistogramSnapshot* Histogram; + protected: + union { + double Double; + i64 Int64; + ui64 Uint64; + IHistogramSnapshot* Histogram; ISummaryDoubleSnapshot* Summary; TLogHistogramSnapshot* LogHistogram; - } Value_; - }; - + } Value_; + }; + /////////////////////////////////////////////////////////////////////////// - // TMetricValueWithType + // TMetricValueWithType /////////////////////////////////////////////////////////////////////////// - // Same as TMetricValue, but this type holds an ownership of + // Same as TMetricValue, but this type holds an ownership of // snapshots and contains value type information. - class TMetricValueWithType: private TMetricValue, public TMoveOnly { + class TMetricValueWithType: private TMetricValue, public TMoveOnly { public: - using TBase = TMetricValue; + using TBase = TMetricValue; template <typename T> - explicit TMetricValueWithType(T value) + explicit TMetricValueWithType(T value) : TBase(value) , ValueType_{TValueType<T>::Type} { Ref(); } - TMetricValueWithType(TMetricValueWithType&& other) + TMetricValueWithType(TMetricValueWithType&& other) : TBase(std::move(other)) , ValueType_{other.ValueType_} { @@ -247,7 +247,7 @@ namespace NMonitoring { other.Clear(); } - TMetricValueWithType& operator=(TMetricValueWithType&& other) { + TMetricValueWithType& operator=(TMetricValueWithType&& other) { TBase::operator=(other); ValueType_ = other.ValueType_; @@ -257,16 +257,16 @@ namespace NMonitoring { return *this; } - ~TMetricValueWithType() { + ~TMetricValueWithType() { UnRef(); } void Clear() { UnRef(); - ValueType_ = EMetricValueType::UNKNOWN; + ValueType_ = EMetricValueType::UNKNOWN; } - EMetricValueType GetType() const noexcept { + EMetricValueType GetType() const noexcept { return ValueType_; } @@ -296,9 +296,9 @@ namespace NMonitoring { private: void Ref() { - if (ValueType_ == EMetricValueType::SUMMARY) { + if (ValueType_ == EMetricValueType::SUMMARY) { TBase::AsSummaryDouble()->Ref(); - } else if (ValueType_ == EMetricValueType::HISTOGRAM) { + } else if (ValueType_ == EMetricValueType::HISTOGRAM) { TBase::AsHistogram()->Ref(); } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { TBase::AsLogHistogram()->Ref(); @@ -306,9 +306,9 @@ namespace NMonitoring { } void UnRef() { - if (ValueType_ == EMetricValueType::SUMMARY) { + if (ValueType_ == EMetricValueType::SUMMARY) { TBase::AsSummaryDouble()->UnRef(); - } else if (ValueType_ == EMetricValueType::HISTOGRAM) { + } else if (ValueType_ == EMetricValueType::HISTOGRAM) { TBase::AsHistogram()->UnRef(); } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { TBase::AsLogHistogram()->UnRef(); @@ -316,114 +316,114 @@ namespace NMonitoring { } private: - EMetricValueType ValueType_ = EMetricValueType::UNKNOWN; + EMetricValueType ValueType_ = EMetricValueType::UNKNOWN; }; - static_assert(sizeof(TMetricValue) == sizeof(ui64), - "expected size of TMetricValue is one machine word"); - - /////////////////////////////////////////////////////////////////////////// - // TMetricTimeSeries - /////////////////////////////////////////////////////////////////////////// - class TMetricTimeSeries: private TMoveOnly { - public: - class TPoint { - public: - TPoint() - : Time_(TInstant::Zero()) - { - } - - template <typename T> - TPoint(TInstant time, T value) - : Time_(time) - , Value_(value) - { - } - - TInstant GetTime() const noexcept { - return Time_; - } - - TMetricValue GetValue() const noexcept { - return Value_; - } - - void ClearValue() { - Value_ = {}; - } - - private: - TInstant Time_; - TMetricValue Value_; - }; - - public: - TMetricTimeSeries() = default; - - TMetricTimeSeries(TMetricTimeSeries&& rhs) noexcept - : ValueType_(rhs.ValueType_) - , Points_(std::move(rhs.Points_)) - { - rhs.ValueType_ = EMetricValueType::UNKNOWN; - } - - TMetricTimeSeries& operator=(TMetricTimeSeries&& rhs) noexcept { - Clear(); - - ValueType_ = rhs.ValueType_; - rhs.ValueType_ = EMetricValueType::UNKNOWN; - - Points_ = std::move(rhs.Points_); - return *this; - } - - ~TMetricTimeSeries() { - Clear(); - } - - template <typename T> - void Add(TInstant time, T value) { + static_assert(sizeof(TMetricValue) == sizeof(ui64), + "expected size of TMetricValue is one machine word"); + + /////////////////////////////////////////////////////////////////////////// + // TMetricTimeSeries + /////////////////////////////////////////////////////////////////////////// + class TMetricTimeSeries: private TMoveOnly { + public: + class TPoint { + public: + TPoint() + : Time_(TInstant::Zero()) + { + } + + template <typename T> + TPoint(TInstant time, T value) + : Time_(time) + , Value_(value) + { + } + + TInstant GetTime() const noexcept { + return Time_; + } + + TMetricValue GetValue() const noexcept { + return Value_; + } + + void ClearValue() { + Value_ = {}; + } + + private: + TInstant Time_; + TMetricValue Value_; + }; + + public: + TMetricTimeSeries() = default; + + TMetricTimeSeries(TMetricTimeSeries&& rhs) noexcept + : ValueType_(rhs.ValueType_) + , Points_(std::move(rhs.Points_)) + { + rhs.ValueType_ = EMetricValueType::UNKNOWN; + } + + TMetricTimeSeries& operator=(TMetricTimeSeries&& rhs) noexcept { + Clear(); + + ValueType_ = rhs.ValueType_; + rhs.ValueType_ = EMetricValueType::UNKNOWN; + + Points_ = std::move(rhs.Points_); + return *this; + } + + ~TMetricTimeSeries() { + Clear(); + } + + template <typename T> + void Add(TInstant time, T value) { Add(TPoint(time, value), TValueType<T>::Type); } void Add(TPoint point, EMetricValueType valueType) { - if (Empty()) { + if (Empty()) { ValueType_ = valueType; - } else { + } else { CheckTypes(ValueType_, valueType); - } + } Points_.push_back(point); - - if (ValueType_ == EMetricValueType::SUMMARY) { + + if (ValueType_ == EMetricValueType::SUMMARY) { TPoint& p = Points_.back(); p.GetValue().AsSummaryDouble()->Ref(); - } else if (ValueType_ == EMetricValueType::HISTOGRAM) { + } else if (ValueType_ == EMetricValueType::HISTOGRAM) { TPoint& p = Points_.back(); p.GetValue().AsHistogram()->Ref(); } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { TPoint& p = Points_.back(); p.GetValue().AsLogHistogram()->Ref(); - } - } - - void CopyFrom(const TMetricTimeSeries& other) { + } + } + + void CopyFrom(const TMetricTimeSeries& other) { if (Empty()) { ValueType_ = other.ValueType_; } else { CheckTypes(GetValueType(), other.GetValueType()); } - - size_t prevSize = Points_.size(); - Copy(std::begin(other.Points_), std::end(other.Points_), - std::back_inserter(Points_)); - - if (ValueType_ == EMetricValueType::HISTOGRAM) { - for (size_t i = prevSize; i < Points_.size(); i++) { - TPoint& point = Points_[i]; - point.GetValue().AsHistogram()->Ref(); - } - } else if (ValueType_ == EMetricValueType::SUMMARY) { + + size_t prevSize = Points_.size(); + Copy(std::begin(other.Points_), std::end(other.Points_), + std::back_inserter(Points_)); + + if (ValueType_ == EMetricValueType::HISTOGRAM) { + for (size_t i = prevSize; i < Points_.size(); i++) { + TPoint& point = Points_[i]; + point.GetValue().AsHistogram()->Ref(); + } + } else if (ValueType_ == EMetricValueType::SUMMARY) { for (size_t i = prevSize; i < Points_.size(); ++i) { TPoint& point = Points_[i]; point.GetValue().AsSummaryDouble()->Ref(); @@ -436,56 +436,56 @@ namespace NMonitoring { } } - template <typename TConsumer> - void ForEach(TConsumer c) const { - for (const auto& point : Points_) { - c(point.GetTime(), ValueType_, point.GetValue()); - } - } - - bool Empty() const noexcept { - return Points_.empty(); - } - - size_t Size() const noexcept { - return Points_.size(); - } - + template <typename TConsumer> + void ForEach(TConsumer c) const { + for (const auto& point : Points_) { + c(point.GetTime(), ValueType_, point.GetValue()); + } + } + + bool Empty() const noexcept { + return Points_.empty(); + } + + size_t Size() const noexcept { + return Points_.size(); + } + size_t Capacity() const noexcept { return Points_.capacity(); } - const TPoint& operator[](size_t index) const noexcept { - return Points_[index]; - } - - void SortByTs(); - - void Clear() noexcept; - - EMetricValueType GetValueType() const noexcept { - return ValueType_; - } - - private: - static void CheckTypes(EMetricValueType t1, EMetricValueType t2) { - Y_ENSURE(t1 == t2, + const TPoint& operator[](size_t index) const noexcept { + return Points_[index]; + } + + void SortByTs(); + + void Clear() noexcept; + + EMetricValueType GetValueType() const noexcept { + return ValueType_; + } + + private: + static void CheckTypes(EMetricValueType t1, EMetricValueType t2) { + Y_ENSURE(t1 == t2, "Series type mismatch: expected " << t1 << ", but got " << t2); - } - - private: - EMetricValueType ValueType_ = EMetricValueType::UNKNOWN; - TVector<TPoint> Points_; - }; - - template <EMetricValueType valueType, typename TPoint> + } + + private: + EMetricValueType ValueType_ = EMetricValueType::UNKNOWN; + TVector<TPoint> Points_; + }; + + template <EMetricValueType valueType, typename TPoint> static inline void SnapshotUnRef(TPoint& point) { - if constexpr (valueType == EMetricValueType::HISTOGRAM) { + if constexpr (valueType == EMetricValueType::HISTOGRAM) { if (auto* hist = point.GetValue().AsHistogram()) { hist->UnRef(); } - } else if constexpr (valueType == EMetricValueType::SUMMARY) { + } else if constexpr (valueType == EMetricValueType::SUMMARY) { if (auto* summary = point.GetValue().AsSummaryDouble()) { summary->UnRef(); } @@ -496,14 +496,14 @@ namespace NMonitoring { } } - template <EMetricValueType valueType, typename TPoint> + template <EMetricValueType valueType, typename TPoint> static void EraseDuplicates(TVector<TPoint>& points) { // we have to manually clean reference to a snapshot from point // while removing duplicates auto result = points.rbegin(); for (auto it = result + 1; it != points.rend(); ++it) { if (result->GetTime() != it->GetTime() && ++result != it) { - SnapshotUnRef<valueType>(*result); + SnapshotUnRef<valueType>(*result); *result = *it; // (2) copy it->ClearValue(); // (3) clean pointer in the source } @@ -511,13 +511,13 @@ namespace NMonitoring { // erase tail points for (auto it = result + 1; it != points.rend(); ++it) { - SnapshotUnRef<valueType>(*it); + SnapshotUnRef<valueType>(*it); } points.erase(points.begin(), (result + 1).base()); } template <typename TPoint> - void SortPointsByTs(EMetricValueType valueType, TVector<TPoint>& points) { + void SortPointsByTs(EMetricValueType valueType, TVector<TPoint>& points) { if (points.size() < 2) { return; } @@ -530,13 +530,13 @@ namespace NMonitoring { points.erase(points.begin(), it.base()); } else { StableSortBy(points, NPrivate::POINT_KEY_FN); - if (valueType == EMetricValueType::HISTOGRAM) { - EraseDuplicates<EMetricValueType::HISTOGRAM>(points); + if (valueType == EMetricValueType::HISTOGRAM) { + EraseDuplicates<EMetricValueType::HISTOGRAM>(points); } else if (valueType == EMetricValueType::LOGHISTOGRAM) { EraseDuplicates<EMetricValueType::LOGHISTOGRAM>(points); } else { - EraseDuplicates<EMetricValueType::SUMMARY>(points); + 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 b17d1ca04a..ab30a958c2 100644 --- a/library/cpp/monlib/metrics/metric_value_type.h +++ b/library/cpp/monlib/metrics/metric_value_type.h @@ -3,7 +3,7 @@ namespace NMonitoring { -enum class EMetricValueType { +enum class EMetricValueType { UNKNOWN, DOUBLE, INT64, diff --git a/library/cpp/monlib/metrics/metric_value_ut.cpp b/library/cpp/monlib/metrics/metric_value_ut.cpp index 6db9c583ad..49b47c4057 100644 --- a/library/cpp/monlib/metrics/metric_value_ut.cpp +++ b/library/cpp/monlib/metrics/metric_value_ut.cpp @@ -1,33 +1,33 @@ -#include "metric_value.h" - +#include "metric_value.h" + #include <library/cpp/testing/unittest/registar.h> - -using namespace NMonitoring; - -Y_UNIT_TEST_SUITE(TMetricValueTest) { - - class TTestHistogram: public IHistogramSnapshot { + +using namespace NMonitoring; + +Y_UNIT_TEST_SUITE(TMetricValueTest) { + + class TTestHistogram: public IHistogramSnapshot { public: TTestHistogram(ui32 count = 1) : Count_{count} {} private: - ui32 Count() const override { + ui32 Count() const override { return Count_; - } - - TBucketBound UpperBound(ui32 /*index*/) const override { - return 1234.56; - } - - TBucketValue Value(ui32 /*index*/) const override { - return 42; - } + } + + TBucketBound UpperBound(ui32 /*index*/) const override { + return 1234.56; + } + + TBucketValue Value(ui32 /*index*/) const override { + return 42; + } ui32 Count_{0}; - }; - + }; + IHistogramSnapshotPtr MakeHistogramSnapshot() { return MakeIntrusive<TTestHistogram>(); } @@ -44,46 +44,46 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { return MakeIntrusive<TLogHistogramSnapshot>(1.5, 0u, 0, buckets); } - Y_UNIT_TEST(Sorted) { - auto ts1 = TInstant::Now(); - auto ts2 = ts1 + TDuration::Seconds(1); - - TMetricTimeSeries timeSeries; - timeSeries.Add(ts1, 3.14159); - timeSeries.Add(ts1, 6.28318); - timeSeries.Add(ts2, 2.71828); - - UNIT_ASSERT_EQUAL(timeSeries.Size(), 3); - - timeSeries.SortByTs(); - UNIT_ASSERT_EQUAL(timeSeries.Size(), 2); - - UNIT_ASSERT_EQUAL(ts1, timeSeries[0].GetTime()); + Y_UNIT_TEST(Sorted) { + auto ts1 = TInstant::Now(); + auto ts2 = ts1 + TDuration::Seconds(1); + + TMetricTimeSeries timeSeries; + timeSeries.Add(ts1, 3.14159); + timeSeries.Add(ts1, 6.28318); + timeSeries.Add(ts2, 2.71828); + + UNIT_ASSERT_EQUAL(timeSeries.Size(), 3); + + timeSeries.SortByTs(); + 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_EQUAL(ts2, timeSeries[1].GetTime()); - UNIT_ASSERT_DOUBLES_EQUAL(2.71828, timeSeries[1].GetValue().AsDouble(), Min<double>()); - } - - Y_UNIT_TEST(Histograms) { - auto ts = TInstant::Now(); - auto histogram = MakeIntrusive<TTestHistogram>(); - - UNIT_ASSERT_VALUES_EQUAL(1, histogram->RefCount()); - { - TMetricTimeSeries timeSeries; - timeSeries.Add(ts, histogram.Get()); - UNIT_ASSERT_VALUES_EQUAL(2, histogram->RefCount()); - } - UNIT_ASSERT_VALUES_EQUAL(1, histogram->RefCount()); - } - + + UNIT_ASSERT_EQUAL(ts2, timeSeries[1].GetTime()); + UNIT_ASSERT_DOUBLES_EQUAL(2.71828, timeSeries[1].GetValue().AsDouble(), Min<double>()); + } + + Y_UNIT_TEST(Histograms) { + auto ts = TInstant::Now(); + auto histogram = MakeIntrusive<TTestHistogram>(); + + UNIT_ASSERT_VALUES_EQUAL(1, histogram->RefCount()); + { + TMetricTimeSeries timeSeries; + timeSeries.Add(ts, histogram.Get()); + UNIT_ASSERT_VALUES_EQUAL(2, histogram->RefCount()); + } + UNIT_ASSERT_VALUES_EQUAL(1, histogram->RefCount()); + } + Y_UNIT_TEST(Summary) { auto ts = TInstant::Now(); auto summary = MakeSummarySnapshot(); UNIT_ASSERT_VALUES_EQUAL(1, summary->RefCount()); { - TMetricTimeSeries timeSeries; + TMetricTimeSeries timeSeries; timeSeries.Add(ts, summary.Get()); UNIT_ASSERT_VALUES_EQUAL(2, summary->RefCount()); } @@ -102,73 +102,73 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { UNIT_ASSERT_VALUES_EQUAL(1, logHist->RefCount()); } - Y_UNIT_TEST(TimeSeriesMovable) { - auto ts = TInstant::Now(); - auto histogram = MakeIntrusive<TTestHistogram>(); - - UNIT_ASSERT_VALUES_EQUAL(1, histogram->RefCount()); - { - TMetricTimeSeries timeSeriesA; - timeSeriesA.Add(ts, histogram.Get()); - UNIT_ASSERT_VALUES_EQUAL(2, histogram->RefCount()); - - TMetricTimeSeries timeSeriesB = std::move(timeSeriesA); - UNIT_ASSERT_VALUES_EQUAL(2, histogram->RefCount()); - - UNIT_ASSERT_VALUES_EQUAL(1, timeSeriesB.Size()); - UNIT_ASSERT_EQUAL(EMetricValueType::HISTOGRAM, timeSeriesB.GetValueType()); - - UNIT_ASSERT_VALUES_EQUAL(0, timeSeriesA.Size()); - UNIT_ASSERT_EQUAL(EMetricValueType::UNKNOWN, timeSeriesA.GetValueType()); - } - UNIT_ASSERT_VALUES_EQUAL(1, histogram->RefCount()); - } - - Y_UNIT_TEST(HistogramsUnique) { - auto ts1 = TInstant::Now(); - auto ts2 = ts1 + TDuration::Seconds(1); - auto ts3 = ts2 + TDuration::Seconds(1); - - auto h1 = MakeIntrusive<TTestHistogram>(); - auto h2 = MakeIntrusive<TTestHistogram>(); - auto h3 = MakeIntrusive<TTestHistogram>(); - - UNIT_ASSERT_VALUES_EQUAL(1, h1->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(1, h2->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(1, h3->RefCount()); - - { - TMetricTimeSeries timeSeries; - timeSeries.Add(ts1, h1.Get()); // drop at the head - timeSeries.Add(ts1, h1.Get()); - timeSeries.Add(ts1, h1.Get()); - - timeSeries.Add(ts2, h2.Get()); // drop in the middle - timeSeries.Add(ts2, h2.Get()); - timeSeries.Add(ts2, h2.Get()); - - timeSeries.Add(ts3, h3.Get()); // drop at the end - timeSeries.Add(ts3, h3.Get()); - timeSeries.Add(ts3, h3.Get()); - - UNIT_ASSERT_EQUAL(timeSeries.Size(), 9); - - UNIT_ASSERT_VALUES_EQUAL(4, h1->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(4, h2->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(4, h3->RefCount()); - - timeSeries.SortByTs(); - UNIT_ASSERT_EQUAL(timeSeries.Size(), 3); - - UNIT_ASSERT_VALUES_EQUAL(2, h1->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(2, h2->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(2, h3->RefCount()); - } - - UNIT_ASSERT_VALUES_EQUAL(1, h1->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(1, h2->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(1, h3->RefCount()); - } + Y_UNIT_TEST(TimeSeriesMovable) { + auto ts = TInstant::Now(); + auto histogram = MakeIntrusive<TTestHistogram>(); + + UNIT_ASSERT_VALUES_EQUAL(1, histogram->RefCount()); + { + TMetricTimeSeries timeSeriesA; + timeSeriesA.Add(ts, histogram.Get()); + UNIT_ASSERT_VALUES_EQUAL(2, histogram->RefCount()); + + TMetricTimeSeries timeSeriesB = std::move(timeSeriesA); + UNIT_ASSERT_VALUES_EQUAL(2, histogram->RefCount()); + + UNIT_ASSERT_VALUES_EQUAL(1, timeSeriesB.Size()); + UNIT_ASSERT_EQUAL(EMetricValueType::HISTOGRAM, timeSeriesB.GetValueType()); + + UNIT_ASSERT_VALUES_EQUAL(0, timeSeriesA.Size()); + UNIT_ASSERT_EQUAL(EMetricValueType::UNKNOWN, timeSeriesA.GetValueType()); + } + UNIT_ASSERT_VALUES_EQUAL(1, histogram->RefCount()); + } + + Y_UNIT_TEST(HistogramsUnique) { + auto ts1 = TInstant::Now(); + auto ts2 = ts1 + TDuration::Seconds(1); + auto ts3 = ts2 + TDuration::Seconds(1); + + auto h1 = MakeIntrusive<TTestHistogram>(); + auto h2 = MakeIntrusive<TTestHistogram>(); + auto h3 = MakeIntrusive<TTestHistogram>(); + + UNIT_ASSERT_VALUES_EQUAL(1, h1->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(1, h2->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(1, h3->RefCount()); + + { + TMetricTimeSeries timeSeries; + timeSeries.Add(ts1, h1.Get()); // drop at the head + timeSeries.Add(ts1, h1.Get()); + timeSeries.Add(ts1, h1.Get()); + + timeSeries.Add(ts2, h2.Get()); // drop in the middle + timeSeries.Add(ts2, h2.Get()); + timeSeries.Add(ts2, h2.Get()); + + timeSeries.Add(ts3, h3.Get()); // drop at the end + timeSeries.Add(ts3, h3.Get()); + timeSeries.Add(ts3, h3.Get()); + + UNIT_ASSERT_EQUAL(timeSeries.Size(), 9); + + UNIT_ASSERT_VALUES_EQUAL(4, h1->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(4, h2->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(4, h3->RefCount()); + + timeSeries.SortByTs(); + UNIT_ASSERT_EQUAL(timeSeries.Size(), 3); + + UNIT_ASSERT_VALUES_EQUAL(2, h1->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(2, h2->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(2, h3->RefCount()); + } + + UNIT_ASSERT_VALUES_EQUAL(1, h1->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(1, h2->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(1, h3->RefCount()); + } Y_UNIT_TEST(LogHistogramsUnique) { auto ts1 = TInstant::Now(); @@ -230,7 +230,7 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { UNIT_ASSERT_VALUES_EQUAL(1, h3->RefCount()); { - TMetricTimeSeries timeSeries; + TMetricTimeSeries timeSeries; timeSeries.Add(ts1, h1.Get()); // drop at the head timeSeries.Add(ts1, h1.Get()); timeSeries.Add(ts1, h1.Get()); @@ -278,7 +278,7 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { auto h7 = MakeIntrusive<TTestHistogram>(7u); { - TMetricTimeSeries timeSeries; + TMetricTimeSeries timeSeries; timeSeries.Add(ts1, h1.Get()); timeSeries.Add(ts1, h2.Get()); @@ -356,7 +356,7 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { auto h7 = MakeSummarySnapshot(7u); { - TMetricTimeSeries timeSeries; + TMetricTimeSeries timeSeries; timeSeries.Add(ts1, h1.Get()); timeSeries.Add(ts1, h2.Get()); @@ -379,27 +379,27 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { } } - Y_UNIT_TEST(TMetricValueWithType) { + Y_UNIT_TEST(TMetricValueWithType) { // correct usage { double value = 1.23; - TMetricValueWithType v{value}; + TMetricValueWithType v{value}; - UNIT_ASSERT_VALUES_EQUAL(v.GetType(), EMetricValueType::DOUBLE); + UNIT_ASSERT_VALUES_EQUAL(v.GetType(), EMetricValueType::DOUBLE); UNIT_ASSERT_VALUES_EQUAL(v.AsDouble(), value); } { ui64 value = 12; - TMetricValueWithType v{value}; + TMetricValueWithType v{value}; - UNIT_ASSERT_VALUES_EQUAL(v.GetType(), EMetricValueType::UINT64); + UNIT_ASSERT_VALUES_EQUAL(v.GetType(), EMetricValueType::UINT64); UNIT_ASSERT_VALUES_EQUAL(v.AsUint64(), value); } { i64 value = i64(-12); - TMetricValueWithType v{value}; + TMetricValueWithType v{value}; - UNIT_ASSERT_VALUES_EQUAL(v.GetType(), EMetricValueType::INT64); + UNIT_ASSERT_VALUES_EQUAL(v.GetType(), EMetricValueType::INT64); UNIT_ASSERT_VALUES_EQUAL(v.AsInt64(), value); } { @@ -408,11 +408,11 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { { auto value = h.Get(); - TMetricValueWithType v{value}; + TMetricValueWithType v{value}; UNIT_ASSERT_VALUES_EQUAL(h.RefCount(), 2); - UNIT_ASSERT_VALUES_EQUAL(v.GetType(), EMetricValueType::HISTOGRAM); + UNIT_ASSERT_VALUES_EQUAL(v.GetType(), EMetricValueType::HISTOGRAM); UNIT_ASSERT_VALUES_EQUAL(v.AsHistogram(), value); } @@ -425,11 +425,11 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 1); { - TMetricValueWithType v{value}; + TMetricValueWithType v{value}; UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 2); - UNIT_ASSERT_VALUES_EQUAL(v.GetType(), EMetricValueType::SUMMARY); + UNIT_ASSERT_VALUES_EQUAL(v.GetType(), EMetricValueType::SUMMARY); UNIT_ASSERT_VALUES_EQUAL(v.AsSummaryDouble(), value); } @@ -442,7 +442,7 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 1); { - TMetricValueWithType v{value}; + TMetricValueWithType v{value}; UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 2); @@ -457,19 +457,19 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { auto value = s.Get(); { - TMetricValueWithType v1{ui64{1}}; + TMetricValueWithType v1{ui64{1}}; UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 1); { - TMetricValueWithType v2{value}; + 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(v1.GetType(), EMetricValueType::SUMMARY); - UNIT_ASSERT_VALUES_EQUAL(v2.GetType(), EMetricValueType::UNKNOWN); + UNIT_ASSERT_VALUES_EQUAL(v1.GetType(), EMetricValueType::SUMMARY); + UNIT_ASSERT_VALUES_EQUAL(v2.GetType(), EMetricValueType::UNKNOWN); } UNIT_ASSERT_VALUES_EQUAL(s.RefCount(), 2); @@ -480,14 +480,14 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { // incorrect usage { - TMetricValueWithType v{1.23}; + TMetricValueWithType v{1.23}; UNIT_ASSERT_EXCEPTION(v.AsHistogram(), yexception); UNIT_ASSERT_EXCEPTION(v.AsSummaryDouble(), yexception); } { auto h = MakeHistogramSnapshot(); - TMetricValueWithType v{h.Get()}; + TMetricValueWithType v{h.Get()}; UNIT_ASSERT_EXCEPTION(v.AsUint64(), yexception); UNIT_ASSERT_EXCEPTION(v.AsInt64(), yexception); @@ -496,7 +496,7 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { } { auto s = MakeSummarySnapshot(); - TMetricValueWithType v{s.Get()}; + TMetricValueWithType v{s.Get()}; UNIT_ASSERT_EXCEPTION(v.AsUint64(), yexception); UNIT_ASSERT_EXCEPTION(v.AsInt64(), yexception); @@ -504,4 +504,4 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { UNIT_ASSERT_EXCEPTION(v.AsHistogram(), yexception); } } -} +} diff --git a/library/cpp/monlib/metrics/timer.h b/library/cpp/monlib/metrics/timer.h index 35d7824fac..5c4e26e37b 100644 --- a/library/cpp/monlib/metrics/timer.h +++ b/library/cpp/monlib/metrics/timer.h @@ -1,6 +1,6 @@ #pragma once -#include "metric.h" +#include "metric.h" #include <util/generic/typetraits.h> @@ -12,59 +12,59 @@ namespace NMonitoring { /** * A timing scope to record elapsed time since creation. */ - template <typename TMetric, + template <typename TMetric, typename Resolution = std::chrono::milliseconds, typename Clock = std::chrono::high_resolution_clock> - class TMetricTimerScope { + class TMetricTimerScope { public: - explicit TMetricTimerScope(TMetric* metric) - : Metric_(metric) + explicit TMetricTimerScope(TMetric* metric) + : Metric_(metric) , StartTime_(Clock::now()) { - Y_ENSURE(Metric_); + Y_ENSURE(Metric_); } - TMetricTimerScope(TMetricTimerScope&) = delete; - TMetricTimerScope& operator=(const TMetricTimerScope&) = delete; + TMetricTimerScope(TMetricTimerScope&) = delete; + TMetricTimerScope& operator=(const TMetricTimerScope&) = delete; - TMetricTimerScope(TMetricTimerScope&& other) { + TMetricTimerScope(TMetricTimerScope&& other) { *this = std::move(other); } - TMetricTimerScope& operator=(TMetricTimerScope&& other) { - Metric_ = other.Metric_; - other.Metric_ = nullptr; + TMetricTimerScope& operator=(TMetricTimerScope&& other) { + Metric_ = other.Metric_; + other.Metric_ = nullptr; StartTime_ = std::move(other.StartTime_); return *this; } void Record() { - Y_VERIFY_DEBUG(Metric_); - if (Metric_ == nullptr) { + Y_VERIFY_DEBUG(Metric_); + if (Metric_ == nullptr) { return; } auto duration = std::chrono::duration_cast<Resolution>(Clock::now() - StartTime_).count(); - if constexpr (std::is_same<TMetric, TGauge>::value) { - Metric_->Set(duration); - } else if constexpr (std::is_same<TMetric, TIntGauge>::value) { - Metric_->Set(duration); - } else if constexpr (std::is_same<TMetric, TCounter>::value) { - Metric_->Add(duration); - } else if constexpr (std::is_same<TMetric, TRate>::value) { - Metric_->Add(duration); - } else if constexpr (std::is_same<TMetric, THistogram>::value) { - Metric_->Record(duration); + if constexpr (std::is_same<TMetric, TGauge>::value) { + Metric_->Set(duration); + } else if constexpr (std::is_same<TMetric, TIntGauge>::value) { + Metric_->Set(duration); + } else if constexpr (std::is_same<TMetric, TCounter>::value) { + Metric_->Add(duration); + } else if constexpr (std::is_same<TMetric, TRate>::value) { + Metric_->Add(duration); + } else if constexpr (std::is_same<TMetric, THistogram>::value) { + Metric_->Record(duration); } else { static_assert(TDependentFalse<TMetric>, "Not supported metric type"); } - Metric_ = nullptr; + Metric_ = nullptr; } - ~TMetricTimerScope() { - if (Metric_ == nullptr) { + ~TMetricTimerScope() { + if (Metric_ == nullptr) { return; } @@ -72,7 +72,7 @@ namespace NMonitoring { } private: - TMetric* Metric_{nullptr}; + TMetric* Metric_{nullptr}; typename Clock::time_point StartTime_; }; @@ -80,18 +80,18 @@ namespace NMonitoring { * @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 + * 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. */ - template <typename TMetric, + template <typename TMetric, typename Resolution = std::chrono::milliseconds, typename Clock = std::chrono::high_resolution_clock> class TFutureFriendlyTimer { public: - explicit TFutureFriendlyTimer(TMetric* metric) - : Impl_{metric} + explicit TFutureFriendlyTimer(TMetric* metric) + : Impl_{metric} { } @@ -112,16 +112,16 @@ namespace NMonitoring { } private: - mutable TMetricTimerScope<TMetric, Resolution, Clock> Impl_; + mutable TMetricTimerScope<TMetric, Resolution, Clock> Impl_; }; - template <typename TMetric> - TMetricTimerScope<TMetric> ScopeTimer(TMetric* metric) { - return TMetricTimerScope<TMetric>{metric}; + template <typename TMetric> + TMetricTimerScope<TMetric> ScopeTimer(TMetric* metric) { + return TMetricTimerScope<TMetric>{metric}; } - template <typename TMetric> - TFutureFriendlyTimer<TMetric> FutureTimer(TMetric* metric) { - return TFutureFriendlyTimer<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 f72c965fee..c244a8c9e1 100644 --- a/library/cpp/monlib/metrics/timer_ut.cpp +++ b/library/cpp/monlib/metrics/timer_ut.cpp @@ -29,13 +29,13 @@ Y_UNIT_TEST_SUITE(TTimerTest) { TGauge gauge(0); { - TMetricTimerScope<TGauge, milliseconds, TTestClock> t{&gauge}; + TMetricTimerScope<TGauge, milliseconds, TTestClock> t{&gauge}; TTestClock::TimePoint += milliseconds(10); } UNIT_ASSERT_EQUAL(10, gauge.Get()); { - TMetricTimerScope<TGauge, milliseconds, TTestClock> t{&gauge}; + TMetricTimerScope<TGauge, milliseconds, TTestClock> t{&gauge}; TTestClock::TimePoint += milliseconds(20); } UNIT_ASSERT_EQUAL(20, gauge.Get()); @@ -46,13 +46,13 @@ Y_UNIT_TEST_SUITE(TTimerTest) { TIntGauge gauge(0); { - TMetricTimerScope<TIntGauge, milliseconds, TTestClock> t{&gauge}; + TMetricTimerScope<TIntGauge, milliseconds, TTestClock> t{&gauge}; TTestClock::TimePoint += milliseconds(10); } UNIT_ASSERT_EQUAL(10, gauge.Get()); { - TMetricTimerScope<TIntGauge, milliseconds, TTestClock> t{&gauge}; + TMetricTimerScope<TIntGauge, milliseconds, TTestClock> t{&gauge}; TTestClock::TimePoint += milliseconds(20); } UNIT_ASSERT_EQUAL(20, gauge.Get()); @@ -61,15 +61,15 @@ Y_UNIT_TEST_SUITE(TTimerTest) { Y_UNIT_TEST(CounterNew) { TTestClock::TimePoint = TTestClock::time_point::min(); - TCounter counter(0); + TCounter counter(0); { - TMetricTimerScope<TCounter, milliseconds, TTestClock> t{&counter}; + TMetricTimerScope<TCounter, milliseconds, TTestClock> t{&counter}; TTestClock::TimePoint += milliseconds(10); } UNIT_ASSERT_EQUAL(10, counter.Get()); { - TMetricTimerScope<TCounter, milliseconds, TTestClock> t{&counter}; + TMetricTimerScope<TCounter, milliseconds, TTestClock> t{&counter}; TTestClock::TimePoint += milliseconds(20); } UNIT_ASSERT_EQUAL(30, counter.Get()); @@ -80,13 +80,13 @@ Y_UNIT_TEST_SUITE(TTimerTest) { TRate rate(0); { - TMetricTimerScope<TRate, milliseconds, TTestClock> t{&rate}; + TMetricTimerScope<TRate, milliseconds, TTestClock> t{&rate}; TTestClock::TimePoint += milliseconds(10); } UNIT_ASSERT_EQUAL(10, rate.Get()); { - TMetricTimerScope<TRate, milliseconds, TTestClock> t{&rate}; + TMetricTimerScope<TRate, milliseconds, TTestClock> t{&rate}; TTestClock::TimePoint += milliseconds(20); } UNIT_ASSERT_EQUAL(30, rate.Get()); @@ -104,13 +104,13 @@ Y_UNIT_TEST_SUITE(TTimerTest) { THistogram histogram(ExplicitHistogram({10, 20, 30}), true); { - TMetricTimerScope<THistogram, milliseconds, TTestClock> t{&histogram}; + TMetricTimerScope<THistogram, milliseconds, TTestClock> t{&histogram}; TTestClock::TimePoint += milliseconds(5); } assertHistogram({1, 0, 0, 0}, histogram.TakeSnapshot()); { - TMetricTimerScope<THistogram, milliseconds, TTestClock> t{&histogram}; + TMetricTimerScope<THistogram, milliseconds, TTestClock> t{&histogram}; TTestClock::TimePoint += milliseconds(15); } assertHistogram({1, 1, 0, 0}, histogram.TakeSnapshot()); @@ -119,9 +119,9 @@ Y_UNIT_TEST_SUITE(TTimerTest) { Y_UNIT_TEST(Moving) { TTestClock::TimePoint = TTestClock::time_point::min(); - TCounter counter(0); + TCounter counter(0); { - TMetricTimerScope<TCounter, milliseconds, TTestClock> t{&counter}; + TMetricTimerScope<TCounter, milliseconds, TTestClock> t{&counter}; [tt = std::move(t)] { TTestClock::TimePoint += milliseconds(5); Y_UNUSED(tt); @@ -137,9 +137,9 @@ Y_UNIT_TEST_SUITE(TTimerTest) { TTestClock::TimePoint = TTestClock::time_point::min(); auto pool = CreateThreadPool(1); - TCounter counter(0); + TCounter counter(0); { - TFutureFriendlyTimer<TCounter, milliseconds, TTestClock> t{&counter}; + TFutureFriendlyTimer<TCounter, milliseconds, TTestClock> t{&counter}; auto f = Async([=] { return; diff --git a/library/cpp/monlib/metrics/ut/histograms.json b/library/cpp/monlib/metrics/ut/histograms.json index 074273cfc4..a6e8b78fea 100644 --- a/library/cpp/monlib/metrics/ut/histograms.json +++ b/library/cpp/monlib/metrics/ut/histograms.json @@ -1,61 +1,61 @@ -{ - "commonLabels": - { - "common":"label" - }, - "sensors": - [ - { - "kind":"HIST", - "labels": - { - "sensor":"readTimeMillis" - }, - "hist": - { - "bounds": - [ - 1, - 2, - 4, - 8 - ], - "buckets": - [ - 2, - 1, - 2, - 4 - ], - "inf":91 - } - }, - { - "kind":"HIST_RATE", - "labels": - { - "sensor":"writeTimeMillis" - }, - "hist": - { - "bounds": - [ - 1, - 5, - 15, - 20, - 25 - ], - "buckets": - [ - 2, - 4, - 10, - 5, - 5 - ], - "inf":74 - } - } - ] -} +{ + "commonLabels": + { + "common":"label" + }, + "sensors": + [ + { + "kind":"HIST", + "labels": + { + "sensor":"readTimeMillis" + }, + "hist": + { + "bounds": + [ + 1, + 2, + 4, + 8 + ], + "buckets": + [ + 2, + 1, + 2, + 4 + ], + "inf":91 + } + }, + { + "kind":"HIST_RATE", + "labels": + { + "sensor":"writeTimeMillis" + }, + "hist": + { + "bounds": + [ + 1, + 5, + 15, + 20, + 25 + ], + "buckets": + [ + 2, + 4, + 10, + 5, + 5 + ], + "inf":74 + } + } + ] +} diff --git a/library/cpp/monlib/metrics/ut/ya.make b/library/cpp/monlib/metrics/ut/ya.make index 2f57822c05..aec9974fbd 100644 --- a/library/cpp/monlib/metrics/ut/ya.make +++ b/library/cpp/monlib/metrics/ut/ya.make @@ -1,32 +1,32 @@ -UNITTEST_FOR(library/cpp/monlib/metrics) - +UNITTEST_FOR(library/cpp/monlib/metrics) + OWNER( jamel g:solomon ) - -SRCS( - ewma_ut.cpp - fake_ut.cpp - histogram_collector_ut.cpp - labels_ut.cpp + +SRCS( + ewma_ut.cpp + fake_ut.cpp + histogram_collector_ut.cpp + labels_ut.cpp log_histogram_collector_ut.cpp - metric_registry_ut.cpp - metric_sub_registry_ut.cpp - metric_value_ut.cpp - summary_collector_ut.cpp + metric_registry_ut.cpp + metric_sub_registry_ut.cpp + metric_value_ut.cpp + summary_collector_ut.cpp timer_ut.cpp -) - -RESOURCE( - histograms.json /histograms.json -) - -PEERDIR( +) + +RESOURCE( + histograms.json /histograms.json +) + +PEERDIR( library/cpp/resource - library/cpp/monlib/encode/protobuf - library/cpp/monlib/encode/json + library/cpp/monlib/encode/protobuf + library/cpp/monlib/encode/json library/cpp/threading/future -) - -END() +) + +END() diff --git a/library/cpp/monlib/metrics/ya.make b/library/cpp/monlib/metrics/ya.make index a4bacb0180..0e1fa143f9 100644 --- a/library/cpp/monlib/metrics/ya.make +++ b/library/cpp/monlib/metrics/ya.make @@ -1,26 +1,26 @@ -LIBRARY() - +LIBRARY() + OWNER( g:solomon jamel ) - -GENERATE_ENUM_SERIALIZATION_WITH_HEADER(metric_value_type.h) -SRCS( - ewma.cpp - fake.cpp - histogram_collector_explicit.cpp - histogram_collector_exponential.cpp - histogram_collector_linear.cpp - histogram_snapshot.cpp +GENERATE_ENUM_SERIALIZATION_WITH_HEADER(metric_value_type.h) + +SRCS( + ewma.cpp + fake.cpp + histogram_collector_explicit.cpp + histogram_collector_exponential.cpp + histogram_collector_linear.cpp + histogram_snapshot.cpp log_histogram_snapshot.cpp - labels.cpp - metric_registry.cpp + labels.cpp + metric_registry.cpp metric_consumer.cpp - metric_type.cpp - metric_value.cpp - summary_snapshot.cpp -) - -END() + metric_type.cpp + metric_value.cpp + summary_snapshot.cpp +) + +END() |