diff options
author | Alexander Smirnov <alex@ydb.tech> | 2025-04-08 00:51:51 +0000 |
---|---|---|
committer | Alexander Smirnov <alex@ydb.tech> | 2025-04-08 00:51:51 +0000 |
commit | d6fbba6d466eb79119a5b4738589639f5c0fb012 (patch) | |
tree | a2e37baac203cba2dd3365964e8d788a36bd80be /library/cpp/monlib | |
parent | d39b14f0a603ac8070f831528fd9fefd3221d9a1 (diff) | |
parent | 624c1038fe57d045d946d9ab8f197e13b33ca15b (diff) | |
download | ydb-d6fbba6d466eb79119a5b4738589639f5c0fb012.tar.gz |
Merge branch 'rightlib' into merge-libs-250408-0050
Diffstat (limited to 'library/cpp/monlib')
-rw-r--r-- | library/cpp/monlib/metrics/fake.h | 10 | ||||
-rw-r--r-- | library/cpp/monlib/metrics/metric.h | 16 | ||||
-rw-r--r-- | library/cpp/monlib/metrics/metric_registry.cpp | 58 | ||||
-rw-r--r-- | library/cpp/monlib/metrics/metric_registry.h | 38 |
4 files changed, 57 insertions, 65 deletions
diff --git a/library/cpp/monlib/metrics/fake.h b/library/cpp/monlib/metrics/fake.h index b01ff2505ad..a058e1d99a1 100644 --- a/library/cpp/monlib/metrics/fake.h +++ b/library/cpp/monlib/metrics/fake.h @@ -82,6 +82,8 @@ namespace NMonitoring { i64 Get() const noexcept override { return 0; } + + void Reset() noexcept override {} }; struct TFakeRate final: public TFakeAcceptor<IRate> { @@ -102,6 +104,8 @@ namespace NMonitoring { ui64 Get() const noexcept override { return 0; } + + void Reset() noexcept override {} }; struct TFakeGauge final: public TFakeAcceptor<IGauge> { @@ -117,12 +121,16 @@ namespace NMonitoring { double Get() const noexcept override { return 0; } + + void Reset() noexcept override {} }; struct TFakeLazyGauge final: public TFakeAcceptor<ILazyGauge> { double Get() const noexcept override { return 0; } + + void Reset() noexcept override {} }; struct TFakeHistogram final: public IHistogram { @@ -169,5 +177,7 @@ namespace NMonitoring { ui64 Get() const noexcept override { return 0; } + + void Reset() noexcept override {} }; } // namespace NMonitoring diff --git a/library/cpp/monlib/metrics/metric.h b/library/cpp/monlib/metrics/metric.h index 2f7d9de687f..bb5fda322ea 100644 --- a/library/cpp/monlib/metrics/metric.h +++ b/library/cpp/monlib/metrics/metric.h @@ -15,6 +15,7 @@ namespace NMonitoring { virtual EMetricType Type() const noexcept = 0; virtual void Accept(TInstant time, IMetricConsumer* consumer) const = 0; + virtual void Reset() noexcept = 0; }; using IMetricPtr = TIntrusivePtr<IMetric>; @@ -28,7 +29,7 @@ namespace NMonitoring { virtual double Add(double n) noexcept = 0; virtual void Set(double n) noexcept = 0; virtual double Get() const noexcept = 0; - virtual void Reset() noexcept { + void Reset() noexcept override { Set(0); } }; @@ -58,7 +59,7 @@ namespace NMonitoring { virtual void Set(i64 value) noexcept = 0; virtual i64 Get() const noexcept = 0; - virtual void Reset() noexcept { + void Reset() noexcept override { Set(0); } }; @@ -84,7 +85,6 @@ namespace NMonitoring { virtual ui64 Add(ui64 n) noexcept = 0; virtual ui64 Get() const noexcept = 0; - virtual void Reset() noexcept = 0; }; class ILazyCounter: public IMetric { @@ -108,7 +108,6 @@ namespace NMonitoring { virtual ui64 Add(ui64 n) noexcept = 0; virtual ui64 Get() const noexcept = 0; - virtual void Reset() noexcept = 0; }; class ILazyRate: public IMetric { @@ -134,7 +133,6 @@ namespace NMonitoring { virtual void Record(double value) noexcept = 0; virtual void Record(double value, ui32 count) noexcept = 0; virtual IHistogramSnapshotPtr TakeSnapshot() const = 0; - virtual void Reset() noexcept = 0; protected: const bool IsRate_; @@ -194,6 +192,8 @@ namespace NMonitoring { consumer->OnDouble(time, Get()); } + void Reset() noexcept override {} + private: std::function<double()> Supplier_; }; @@ -245,6 +245,8 @@ namespace NMonitoring { consumer->OnInt64(time, Get()); } + void Reset() noexcept override {} + private: std::function<i64()> Supplier_; }; @@ -296,6 +298,8 @@ namespace NMonitoring { consumer->OnUint64(time, Get()); } + void Reset() noexcept override {} + private: std::function<ui64()> Supplier_; }; @@ -347,6 +351,8 @@ namespace NMonitoring { consumer->OnUint64(time, Get()); } + void Reset() noexcept override {} + private: std::function<ui64()> Supplier_; }; diff --git a/library/cpp/monlib/metrics/metric_registry.cpp b/library/cpp/monlib/metrics/metric_registry.cpp index 245f65702d1..dbbea603c16 100644 --- a/library/cpp/monlib/metrics/metric_registry.cpp +++ b/library/cpp/monlib/metrics/metric_registry.cpp @@ -226,29 +226,7 @@ namespace NMonitoring { void TMetricRegistry::Reset() { TWriteGuard g{*Lock_}; for (auto& [label, metricValue] : Metrics_) { - auto metric = metricValue.Metric; - switch (metric->Type()) { - case EMetricType::GAUGE: - static_cast<TGauge*>(metric.Get())->Set(.0); - break; - case EMetricType::IGAUGE: - static_cast<TIntGauge*>(metric.Get())->Set(0); - break; - case EMetricType::COUNTER: - static_cast<TCounter*>(metric.Get())->Reset(); - break; - case EMetricType::RATE: - static_cast<TRate*>(metric.Get())->Reset(); - break; - case EMetricType::HIST: - case EMetricType::HIST_RATE: - static_cast<THistogram*>(metric.Get())->Reset(); - break; - case EMetricType::UNKNOWN: - case EMetricType::DSUMMARY: - case EMetricType::LOGHIST: - break; - } + metricValue.Metric->Reset(); } } @@ -257,40 +235,6 @@ namespace NMonitoring { Metrics_.clear(); } - template <typename TMetric, EMetricType type, typename TLabelsType, typename... Args> - TMetric* TMetricRegistry::Metric(TLabelsType&& labels, TMetricOpts&& opts, Args&&... args) { - { - TReadGuard g{*Lock_}; - - auto it = Metrics_.find(labels); - if (it != Metrics_.end()) { - Y_ENSURE(it->second.Metric->Type() == type, "cannot create metric " << labels - << " with type " << MetricTypeToStr(type) - << ", because registry already has same metric with type " << MetricTypeToStr(it->second.Metric->Type())); - Y_ENSURE(it->second.Opts.MemOnly == opts.MemOnly,"cannot create metric " << labels - << " with memOnly=" << opts.MemOnly - << ", because registry already has same metric with memOnly=" << it->second.Opts.MemOnly); - return static_cast<TMetric*>(it->second.Metric.Get()); - } - } - - { - IMetricPtr metric = MakeIntrusive<TMetric>(std::forward<Args>(args)...); - - TWriteGuard g{*Lock_}; - // decltype(Metrics_)::iterator breaks build on windows - THashMap<ILabelsPtr, TMetricValue>::iterator it; - TMetricValue metricValue = {metric, opts}; - if constexpr (!std::is_convertible_v<TLabelsType, ILabelsPtr>) { - it = Metrics_.emplace(new TLabels{std::forward<TLabelsType>(labels)}, std::move(metricValue)).first; - } else { - it = Metrics_.emplace(std::forward<TLabelsType>(labels), std::move(metricValue)).first; - } - - return static_cast<TMetric*>(it->second.Metric.Get()); - } - } - void TMetricRegistry::RemoveMetric(const ILabels& labels) noexcept { TWriteGuard g{*Lock_}; Metrics_.erase(labels); diff --git a/library/cpp/monlib/metrics/metric_registry.h b/library/cpp/monlib/metrics/metric_registry.h index f60467cf912..7669a8c0886 100644 --- a/library/cpp/monlib/metrics/metric_registry.h +++ b/library/cpp/monlib/metrics/metric_registry.h @@ -274,13 +274,45 @@ namespace NMonitoring { TMetricOpts Opts; }; + protected: + template <typename TMetric, EMetricType type, typename TLabelsType, typename... Args> + TMetric* Metric(TLabelsType&& labels, TMetricOpts&& opts, Args&&... args) { + { + TReadGuard g{*Lock_}; + + auto it = Metrics_.find(labels); + if (it != Metrics_.end()) { + Y_ENSURE(it->second.Metric->Type() == type, "cannot create metric " << labels + << " with type " << MetricTypeToStr(type) + << ", because registry already has same metric with type " << MetricTypeToStr(it->second.Metric->Type())); + Y_ENSURE(it->second.Opts.MemOnly == opts.MemOnly,"cannot create metric " << labels + << " with memOnly=" << opts.MemOnly + << ", because registry already has same metric with memOnly=" << it->second.Opts.MemOnly); + return static_cast<TMetric*>(it->second.Metric.Get()); + } + } + + { + IMetricPtr metric = MakeIntrusive<TMetric>(std::forward<Args>(args)...); + + TWriteGuard g{*Lock_}; + // decltype(Metrics_)::iterator breaks build on windows + THashMap<ILabelsPtr, TMetricValue>::iterator it; + TMetricValue metricValue = {metric, opts}; + if constexpr (!std::is_convertible_v<TLabelsType, ILabelsPtr>) { + it = Metrics_.emplace(new TLabels{std::forward<TLabelsType>(labels)}, std::move(metricValue)).first; + } else { + it = Metrics_.emplace(std::forward<TLabelsType>(labels), std::move(metricValue)).first; + } + + return static_cast<TMetric*>(it->second.Metric.Get()); + } + } + private: THolder<TRWMutex> Lock_ = MakeHolder<TRWMutex>(); THashMap<ILabelsPtr, TMetricValue> Metrics_; - template <typename TMetric, EMetricType type, typename TLabelsType, typename... Args> - TMetric* Metric(TLabelsType&& labels, TMetricOpts&& opts, Args&&... args); - TLabels CommonLabels_; }; |