metric.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. #pragma once
  2. #include "metric_consumer.h"
  3. #include <util/datetime/base.h>
  4. #include <util/generic/ptr.h>
  5. namespace NMonitoring {
  6. ///////////////////////////////////////////////////////////////////////////////
  7. // IMetric
  8. ///////////////////////////////////////////////////////////////////////////////
  9. class IMetric : public TThrRefBase {
  10. public:
  11. virtual ~IMetric() = default;
  12. virtual EMetricType Type() const noexcept = 0;
  13. virtual void Accept(TInstant time, IMetricConsumer* consumer) const = 0;
  14. };
  15. using IMetricPtr = TIntrusivePtr<IMetric>;
  16. class IGauge: public IMetric {
  17. public:
  18. EMetricType Type() const noexcept final {
  19. return EMetricType::GAUGE;
  20. }
  21. virtual double Add(double n) noexcept = 0;
  22. virtual void Set(double n) noexcept = 0;
  23. virtual double Get() const noexcept = 0;
  24. virtual void Reset() noexcept {
  25. Set(0);
  26. }
  27. };
  28. class ILazyGauge: public IMetric {
  29. public:
  30. EMetricType Type() const noexcept final {
  31. return EMetricType::GAUGE;
  32. }
  33. virtual double Get() const noexcept = 0;
  34. };
  35. class IIntGauge: public IMetric {
  36. public:
  37. EMetricType Type() const noexcept final {
  38. return EMetricType::IGAUGE;
  39. }
  40. virtual i64 Add(i64 n) noexcept = 0;
  41. virtual i64 Inc() noexcept {
  42. return Add(1);
  43. }
  44. virtual i64 Dec() noexcept {
  45. return Add(-1);
  46. }
  47. virtual void Set(i64 value) noexcept = 0;
  48. virtual i64 Get() const noexcept = 0;
  49. virtual void Reset() noexcept {
  50. Set(0);
  51. }
  52. };
  53. class ILazyIntGauge: public IMetric {
  54. public:
  55. EMetricType Type() const noexcept final {
  56. return EMetricType::IGAUGE;
  57. }
  58. virtual i64 Get() const noexcept = 0;
  59. };
  60. class ICounter: public IMetric {
  61. public:
  62. EMetricType Type() const noexcept final {
  63. return EMetricType::COUNTER;
  64. }
  65. virtual ui64 Inc() noexcept {
  66. return Add(1);
  67. }
  68. virtual ui64 Add(ui64 n) noexcept = 0;
  69. virtual ui64 Get() const noexcept = 0;
  70. virtual void Reset() noexcept = 0;
  71. };
  72. class ILazyCounter: public IMetric {
  73. public:
  74. EMetricType Type() const noexcept final {
  75. return EMetricType::COUNTER;
  76. }
  77. virtual ui64 Get() const noexcept = 0;
  78. };
  79. class IRate: public IMetric {
  80. public:
  81. EMetricType Type() const noexcept final {
  82. return EMetricType::RATE;
  83. }
  84. virtual ui64 Inc() noexcept {
  85. return Add(1);
  86. }
  87. virtual ui64 Add(ui64 n) noexcept = 0;
  88. virtual ui64 Get() const noexcept = 0;
  89. virtual void Reset() noexcept = 0;
  90. };
  91. class ILazyRate: public IMetric {
  92. public:
  93. EMetricType Type() const noexcept final {
  94. return EMetricType::RATE;
  95. }
  96. virtual ui64 Get() const noexcept = 0;
  97. };
  98. class IHistogram: public IMetric {
  99. public:
  100. explicit IHistogram(bool isRate)
  101. : IsRate_{isRate}
  102. {
  103. }
  104. EMetricType Type() const noexcept final {
  105. return IsRate_ ? EMetricType::HIST_RATE : EMetricType::HIST;
  106. }
  107. virtual void Record(double value) = 0;
  108. virtual void Record(double value, ui32 count) = 0;
  109. virtual IHistogramSnapshotPtr TakeSnapshot() const = 0;
  110. virtual void Reset() = 0;
  111. protected:
  112. const bool IsRate_;
  113. };
  114. ///////////////////////////////////////////////////////////////////////////////
  115. // TGauge
  116. ///////////////////////////////////////////////////////////////////////////////
  117. class TGauge final: public IGauge {
  118. public:
  119. explicit TGauge(double value = 0.0) {
  120. Set(value);
  121. }
  122. double Add(double n) noexcept override {
  123. double newValue;
  124. double oldValue = Get();
  125. do {
  126. newValue = oldValue + n;
  127. } while (!Value_.compare_exchange_weak(oldValue, newValue, std::memory_order_release, std::memory_order_consume));
  128. return newValue;
  129. }
  130. void Set(double n) noexcept override {
  131. Value_.store(n, std::memory_order_relaxed);
  132. }
  133. double Get() const noexcept override {
  134. return Value_.load(std::memory_order_relaxed);
  135. }
  136. void Accept(TInstant time, IMetricConsumer* consumer) const override {
  137. consumer->OnDouble(time, Get());
  138. }
  139. private:
  140. std::atomic<double> Value_;
  141. };
  142. ///////////////////////////////////////////////////////////////////////////////
  143. // TLazyGauge
  144. ///////////////////////////////////////////////////////////////////////////////
  145. class TLazyGauge final: public ILazyGauge {
  146. public:
  147. explicit TLazyGauge(std::function<double()> supplier)
  148. : Supplier_(std::move(supplier))
  149. {
  150. }
  151. double Get() const noexcept override {
  152. return Supplier_();
  153. }
  154. void Accept(TInstant time, IMetricConsumer* consumer) const override {
  155. consumer->OnDouble(time, Get());
  156. }
  157. private:
  158. std::function<double()> Supplier_;
  159. };
  160. ///////////////////////////////////////////////////////////////////////////////
  161. // TIntGauge
  162. ///////////////////////////////////////////////////////////////////////////////
  163. class TIntGauge final: public IIntGauge {
  164. public:
  165. explicit TIntGauge(i64 value = 0) {
  166. Set(value);
  167. }
  168. i64 Add(i64 n) noexcept override {
  169. return Value_.fetch_add(n, std::memory_order_relaxed) + n;
  170. }
  171. void Set(i64 value) noexcept override {
  172. Value_.store(value, std::memory_order_relaxed);
  173. }
  174. i64 Get() const noexcept override {
  175. return Value_.load(std::memory_order_relaxed);
  176. }
  177. void Accept(TInstant time, IMetricConsumer* consumer) const override {
  178. consumer->OnInt64(time, Get());
  179. }
  180. private:
  181. std::atomic_int64_t Value_;
  182. };
  183. ///////////////////////////////////////////////////////////////////////////////
  184. // TLazyIntGauge
  185. ///////////////////////////////////////////////////////////////////////////////
  186. class TLazyIntGauge final: public ILazyIntGauge {
  187. public:
  188. explicit TLazyIntGauge(std::function<i64()> supplier)
  189. : Supplier_(std::move(supplier))
  190. {
  191. }
  192. i64 Get() const noexcept override {
  193. return Supplier_();
  194. }
  195. void Accept(TInstant time, IMetricConsumer* consumer) const override {
  196. consumer->OnInt64(time, Get());
  197. }
  198. private:
  199. std::function<i64()> Supplier_;
  200. };
  201. ///////////////////////////////////////////////////////////////////////////////
  202. // TCounter
  203. ///////////////////////////////////////////////////////////////////////////////
  204. class TCounter final: public ICounter {
  205. public:
  206. explicit TCounter(ui64 value = 0) {
  207. Value_.store(value, std::memory_order_relaxed);
  208. }
  209. ui64 Add(ui64 n) noexcept override {
  210. return Value_.fetch_add(n, std::memory_order_relaxed) + n;
  211. }
  212. ui64 Get() const noexcept override {
  213. return Value_.load(std::memory_order_relaxed);
  214. }
  215. void Reset() noexcept override {
  216. Value_.store(0, std::memory_order_relaxed);
  217. }
  218. void Accept(TInstant time, IMetricConsumer* consumer) const override {
  219. consumer->OnUint64(time, Get());
  220. }
  221. private:
  222. std::atomic_uint64_t Value_;
  223. };
  224. ///////////////////////////////////////////////////////////////////////////////
  225. // TLazyCounter
  226. ///////////////////////////////////////////////////////////////////////////////
  227. class TLazyCounter final: public ILazyCounter {
  228. public:
  229. explicit TLazyCounter(std::function<ui64()> supplier)
  230. : Supplier_(std::move(supplier))
  231. {
  232. }
  233. ui64 Get() const noexcept override {
  234. return Supplier_();
  235. }
  236. void Accept(TInstant time, IMetricConsumer* consumer) const override {
  237. consumer->OnUint64(time, Get());
  238. }
  239. private:
  240. std::function<ui64()> Supplier_;
  241. };
  242. ///////////////////////////////////////////////////////////////////////////////
  243. // TRate
  244. ///////////////////////////////////////////////////////////////////////////////
  245. class TRate final: public IRate {
  246. public:
  247. explicit TRate(ui64 value = 0) {
  248. Value_.store(value, std::memory_order_relaxed);
  249. }
  250. ui64 Add(ui64 n) noexcept override {
  251. return Value_.fetch_add(n, std::memory_order_relaxed) + n;
  252. }
  253. ui64 Get() const noexcept override {
  254. return Value_.load(std::memory_order_relaxed);
  255. }
  256. void Reset() noexcept override {
  257. Value_.store(0, std::memory_order_relaxed);
  258. }
  259. void Accept(TInstant time, IMetricConsumer* consumer) const override {
  260. consumer->OnUint64(time, Get());
  261. }
  262. private:
  263. std::atomic_uint64_t Value_;
  264. };
  265. ///////////////////////////////////////////////////////////////////////////////
  266. // TLazyRate
  267. ///////////////////////////////////////////////////////////////////////////////
  268. class TLazyRate final: public ILazyRate {
  269. public:
  270. explicit TLazyRate(std::function<ui64()> supplier)
  271. : Supplier_(std::move(supplier))
  272. {
  273. }
  274. ui64 Get() const noexcept override {
  275. return Supplier_();
  276. }
  277. void Accept(TInstant time, IMetricConsumer* consumer) const override {
  278. consumer->OnUint64(time, Get());
  279. }
  280. private:
  281. std::function<ui64()> Supplier_;
  282. };
  283. ///////////////////////////////////////////////////////////////////////////////
  284. // THistogram
  285. ///////////////////////////////////////////////////////////////////////////////
  286. class THistogram final: public IHistogram {
  287. public:
  288. THistogram(IHistogramCollectorPtr collector, bool isRate)
  289. : IHistogram(isRate)
  290. , Collector_(std::move(collector))
  291. {
  292. }
  293. THistogram(std::function<IHistogramCollectorPtr()> makeHistogramCollector, bool isRate)
  294. : IHistogram(isRate)
  295. , Collector_(makeHistogramCollector())
  296. {
  297. }
  298. void Record(double value) override {
  299. Collector_->Collect(value);
  300. }
  301. void Record(double value, ui32 count) override {
  302. Collector_->Collect(value, count);
  303. }
  304. void Accept(TInstant time, IMetricConsumer* consumer) const override {
  305. consumer->OnHistogram(time, TakeSnapshot());
  306. }
  307. IHistogramSnapshotPtr TakeSnapshot() const override {
  308. return Collector_->Snapshot();
  309. }
  310. void Reset() override {
  311. Collector_->Reset();
  312. }
  313. private:
  314. IHistogramCollectorPtr Collector_;
  315. };
  316. }