metric_registry.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #pragma once
  2. #include "labels.h"
  3. #include "metric.h"
  4. #include <util/system/rwlock.h>
  5. #include <library/cpp/threading/light_rw_lock/lightrwlock.h>
  6. namespace NMonitoring {
  7. class IMetricFactory {
  8. public:
  9. virtual ~IMetricFactory() = default;
  10. virtual IGauge* Gauge(ILabelsPtr labels) = 0;
  11. virtual ILazyGauge* LazyGauge(ILabelsPtr labels, std::function<double()> supplier) = 0;
  12. virtual IIntGauge* IntGauge(ILabelsPtr labels) = 0;
  13. virtual ILazyIntGauge* LazyIntGauge(ILabelsPtr labels, std::function<i64()> supplier) = 0;
  14. virtual ICounter* Counter(ILabelsPtr labels) = 0;
  15. virtual ILazyCounter* LazyCounter(ILabelsPtr labels, std::function<ui64()> supplier) = 0;
  16. virtual IRate* Rate(ILabelsPtr labels) = 0;
  17. virtual ILazyRate* LazyRate(ILabelsPtr labels, std::function<ui64()> supplier) = 0;
  18. virtual IHistogram* HistogramCounter(
  19. ILabelsPtr labels,
  20. IHistogramCollectorPtr collector) = 0;
  21. virtual IHistogram* HistogramRate(
  22. ILabelsPtr labels,
  23. IHistogramCollectorPtr collector) = 0;
  24. };
  25. class IMetricSupplier {
  26. public:
  27. virtual ~IMetricSupplier() = default;
  28. virtual void Accept(TInstant time, IMetricConsumer* consumer) const = 0;
  29. virtual void Append(TInstant time, IMetricConsumer* consumer) const = 0;
  30. };
  31. class IMetricRegistry: public IMetricSupplier, public IMetricFactory {
  32. public:
  33. virtual const TLabels& CommonLabels() const noexcept = 0;
  34. virtual void RemoveMetric(const ILabels& labels) noexcept = 0;
  35. };
  36. ///////////////////////////////////////////////////////////////////////////////
  37. // TMetricRegistry
  38. ///////////////////////////////////////////////////////////////////////////////
  39. class TMetricRegistry: public IMetricRegistry {
  40. public:
  41. TMetricRegistry();
  42. ~TMetricRegistry();
  43. explicit TMetricRegistry(const TLabels& commonLabels);
  44. /**
  45. * Get a global metrics registry instance.
  46. */
  47. static TMetricRegistry* Instance();
  48. TGauge* Gauge(TLabels labels);
  49. TLazyGauge* LazyGauge(TLabels labels, std::function<double()> supplier);
  50. TIntGauge* IntGauge(TLabels labels);
  51. TLazyIntGauge* LazyIntGauge(TLabels labels, std::function<i64()> supplier);
  52. TCounter* Counter(TLabels labels);
  53. TLazyCounter* LazyCounter(TLabels labels, std::function<ui64()> supplier);
  54. TRate* Rate(TLabels labels);
  55. TLazyRate* LazyRate(TLabels labels, std::function<ui64()> supplier);
  56. THistogram* HistogramCounter(
  57. TLabels labels,
  58. IHistogramCollectorPtr collector);
  59. THistogram* HistogramRate(
  60. TLabels labels,
  61. IHistogramCollectorPtr collector);
  62. /**
  63. * Set all registered metrics to zero
  64. */
  65. void Reset();
  66. /**
  67. * Remove all registered metrics from registry
  68. */
  69. void Clear();
  70. void Accept(TInstant time, IMetricConsumer* consumer) const override;
  71. void Append(TInstant time, IMetricConsumer* consumer) const override;
  72. const TLabels& CommonLabels() const noexcept override {
  73. return CommonLabels_;
  74. }
  75. void RemoveMetric(const ILabels& labels) noexcept override;
  76. private:
  77. TGauge* Gauge(ILabelsPtr labels) override;
  78. TLazyGauge* LazyGauge(ILabelsPtr labels, std::function<double()> supplier) override;
  79. TIntGauge* IntGauge(ILabelsPtr labels) override;
  80. TLazyIntGauge* LazyIntGauge(ILabelsPtr labels, std::function<i64()> supplier) override;
  81. TCounter* Counter(ILabelsPtr labels) override;
  82. TLazyCounter* LazyCounter(ILabelsPtr labels, std::function<ui64()> supplier) override;
  83. TRate* Rate(ILabelsPtr labels) override;
  84. TLazyRate* LazyRate(ILabelsPtr labels, std::function<ui64()> supplier) override;
  85. THistogram* HistogramCounter(
  86. ILabelsPtr labels,
  87. IHistogramCollectorPtr collector) override;
  88. THistogram* HistogramRate(
  89. ILabelsPtr labels,
  90. IHistogramCollectorPtr collector) override;
  91. private:
  92. TRWMutex Lock_;
  93. THashMap<ILabelsPtr, IMetricPtr> Metrics_;
  94. template <typename TMetric, EMetricType type, typename TLabelsType, typename... Args>
  95. TMetric* Metric(TLabelsType&& labels, Args&&... args);
  96. TLabels CommonLabels_;
  97. };
  98. void WriteLabels(IMetricConsumer* consumer, const ILabels& labels);
  99. }