123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- #pragma once
- #include "metric_consumer.h"
- #include <util/datetime/base.h>
- #include <util/generic/ptr.h>
- namespace NMonitoring {
- ///////////////////////////////////////////////////////////////////////////////
- // IMetric
- ///////////////////////////////////////////////////////////////////////////////
- class IMetric : public TThrRefBase {
- public:
- virtual ~IMetric() = default;
- virtual EMetricType Type() const noexcept = 0;
- virtual void Accept(TInstant time, IMetricConsumer* consumer) const = 0;
- };
- using IMetricPtr = TIntrusivePtr<IMetric>;
- class IGauge: public IMetric {
- public:
- EMetricType Type() const noexcept final {
- return EMetricType::GAUGE;
- }
- virtual double Add(double n) noexcept = 0;
- virtual void Set(double n) noexcept = 0;
- virtual double Get() const noexcept = 0;
- virtual void Reset() noexcept {
- Set(0);
- }
- };
- class ILazyGauge: public IMetric {
- public:
- EMetricType Type() const noexcept final {
- return EMetricType::GAUGE;
- }
- virtual double Get() const noexcept = 0;
- };
- class IIntGauge: public IMetric {
- public:
- EMetricType Type() const noexcept final {
- return EMetricType::IGAUGE;
- }
- virtual i64 Add(i64 n) noexcept = 0;
- virtual i64 Inc() noexcept {
- return Add(1);
- }
- virtual i64 Dec() noexcept {
- return Add(-1);
- }
- virtual void Set(i64 value) noexcept = 0;
- virtual i64 Get() const noexcept = 0;
- virtual void Reset() noexcept {
- Set(0);
- }
- };
- class ILazyIntGauge: public IMetric {
- public:
- EMetricType Type() const noexcept final {
- return EMetricType::IGAUGE;
- }
- virtual i64 Get() const noexcept = 0;
- };
- class ICounter: public IMetric {
- public:
- EMetricType Type() const noexcept final {
- return EMetricType::COUNTER;
- }
- virtual ui64 Inc() noexcept {
- return Add(1);
- }
- virtual ui64 Add(ui64 n) noexcept = 0;
- virtual ui64 Get() const noexcept = 0;
- virtual void Reset() noexcept = 0;
- };
- class ILazyCounter: public IMetric {
- public:
- EMetricType Type() const noexcept final {
- return EMetricType::COUNTER;
- }
- virtual ui64 Get() const noexcept = 0;
- };
- class IRate: public IMetric {
- public:
- EMetricType Type() const noexcept final {
- return EMetricType::RATE;
- }
- virtual ui64 Inc() noexcept {
- return Add(1);
- }
- virtual ui64 Add(ui64 n) noexcept = 0;
- virtual ui64 Get() const noexcept = 0;
- virtual void Reset() noexcept = 0;
- };
- class ILazyRate: public IMetric {
- public:
- EMetricType Type() const noexcept final {
- return EMetricType::RATE;
- }
- virtual ui64 Get() const noexcept = 0;
- };
- class IHistogram: public IMetric {
- public:
- explicit IHistogram(bool isRate)
- : IsRate_{isRate}
- {
- }
- EMetricType Type() const noexcept final {
- return IsRate_ ? EMetricType::HIST_RATE : EMetricType::HIST;
- }
- virtual void Record(double value) = 0;
- virtual void Record(double value, ui32 count) = 0;
- virtual IHistogramSnapshotPtr TakeSnapshot() const = 0;
- virtual void Reset() = 0;
- protected:
- const bool IsRate_;
- };
- ///////////////////////////////////////////////////////////////////////////////
- // TGauge
- ///////////////////////////////////////////////////////////////////////////////
- class TGauge final: public IGauge {
- public:
- explicit TGauge(double value = 0.0) {
- Set(value);
- }
- double Add(double n) noexcept override {
- double newValue;
- double oldValue = Get();
- do {
- newValue = oldValue + n;
- } while (!Value_.compare_exchange_weak(oldValue, newValue, std::memory_order_release, std::memory_order_consume));
- return newValue;
- }
- void Set(double n) noexcept override {
- Value_.store(n, std::memory_order_relaxed);
- }
- 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:
- std::atomic<double> Value_;
- };
- ///////////////////////////////////////////////////////////////////////////////
- // TLazyGauge
- ///////////////////////////////////////////////////////////////////////////////
- class TLazyGauge final: public ILazyGauge {
- public:
- explicit TLazyGauge(std::function<double()> supplier)
- : Supplier_(std::move(supplier))
- {
- }
- double Get() const noexcept override {
- return Supplier_();
- }
- void Accept(TInstant time, IMetricConsumer* consumer) const override {
- consumer->OnDouble(time, Get());
- }
- private:
- std::function<double()> Supplier_;
- };
- ///////////////////////////////////////////////////////////////////////////////
- // TIntGauge
- ///////////////////////////////////////////////////////////////////////////////
- class TIntGauge final: public IIntGauge {
- public:
- explicit TIntGauge(i64 value = 0) {
- Set(value);
- }
- i64 Add(i64 n) noexcept override {
- return Value_.fetch_add(n, std::memory_order_relaxed) + n;
- }
- 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:
- std::atomic_int64_t Value_;
- };
- ///////////////////////////////////////////////////////////////////////////////
- // TLazyIntGauge
- ///////////////////////////////////////////////////////////////////////////////
- class TLazyIntGauge final: public ILazyIntGauge {
- public:
- explicit TLazyIntGauge(std::function<i64()> supplier)
- : Supplier_(std::move(supplier))
- {
- }
- i64 Get() const noexcept override {
- return Supplier_();
- }
- void Accept(TInstant time, IMetricConsumer* consumer) const override {
- consumer->OnInt64(time, Get());
- }
- private:
- std::function<i64()> Supplier_;
- };
- ///////////////////////////////////////////////////////////////////////////////
- // 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:
- std::atomic_uint64_t Value_;
- };
- ///////////////////////////////////////////////////////////////////////////////
- // TLazyCounter
- ///////////////////////////////////////////////////////////////////////////////
- class TLazyCounter final: public ILazyCounter {
- public:
- explicit TLazyCounter(std::function<ui64()> supplier)
- : Supplier_(std::move(supplier))
- {
- }
- ui64 Get() const noexcept override {
- return Supplier_();
- }
- void Accept(TInstant time, IMetricConsumer* consumer) const override {
- consumer->OnUint64(time, Get());
- }
- private:
- std::function<ui64()> Supplier_;
- };
- ///////////////////////////////////////////////////////////////////////////////
- // TRate
- ///////////////////////////////////////////////////////////////////////////////
- class TRate final: public IRate {
- 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:
- std::atomic_uint64_t Value_;
- };
- ///////////////////////////////////////////////////////////////////////////////
- // TLazyRate
- ///////////////////////////////////////////////////////////////////////////////
- class TLazyRate final: public ILazyRate {
- public:
- explicit TLazyRate(std::function<ui64()> supplier)
- : Supplier_(std::move(supplier))
- {
- }
- ui64 Get() const noexcept override {
- return Supplier_();
- }
- void Accept(TInstant time, IMetricConsumer* consumer) const override {
- consumer->OnUint64(time, Get());
- }
- private:
- std::function<ui64()> Supplier_;
- };
- ///////////////////////////////////////////////////////////////////////////////
- // THistogram
- ///////////////////////////////////////////////////////////////////////////////
- class THistogram final: public IHistogram {
- public:
- THistogram(IHistogramCollectorPtr collector, bool isRate)
- : IHistogram(isRate)
- , Collector_(std::move(collector))
- {
- }
- THistogram(std::function<IHistogramCollectorPtr()> makeHistogramCollector, bool isRate)
- : IHistogram(isRate)
- , Collector_(makeHistogramCollector())
- {
- }
- 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();
- }
- void Reset() override {
- Collector_->Reset();
- }
- private:
- IHistogramCollectorPtr Collector_;
- };
- }
|