diff options
Diffstat (limited to 'library/cpp/monlib/metrics/metric.h')
-rw-r--r-- | library/cpp/monlib/metrics/metric.h | 286 |
1 files changed, 143 insertions, 143 deletions
diff --git a/library/cpp/monlib/metrics/metric.h b/library/cpp/monlib/metrics/metric.h index b8ce12d753..f4c7dc37f2 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_; + }; +} |