metric_registry.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. virtual IHistogram* HistogramCounter(
  25. ILabelsPtr labels,
  26. std::function<IHistogramCollectorPtr()> makeHistogramCollector) = 0;
  27. virtual IHistogram* HistogramRate(
  28. ILabelsPtr labels,
  29. std::function<IHistogramCollectorPtr()> makeHistogramCollector) = 0;
  30. };
  31. class IMetricSupplier {
  32. public:
  33. virtual ~IMetricSupplier() = default;
  34. virtual void Accept(TInstant time, IMetricConsumer* consumer) const = 0;
  35. virtual void Append(TInstant time, IMetricConsumer* consumer) const = 0;
  36. };
  37. class IMetricRegistry: public IMetricSupplier, public IMetricFactory {
  38. public:
  39. virtual const TLabels& CommonLabels() const noexcept = 0;
  40. virtual void RemoveMetric(const ILabels& labels) noexcept = 0;
  41. };
  42. ///////////////////////////////////////////////////////////////////////////////
  43. // TMetricRegistry
  44. ///////////////////////////////////////////////////////////////////////////////
  45. class TMetricRegistry: public IMetricRegistry {
  46. public:
  47. TMetricRegistry();
  48. TMetricRegistry(TMetricRegistry&& other);
  49. ~TMetricRegistry();
  50. explicit TMetricRegistry(const TLabels& commonLabels);
  51. /**
  52. * Not thread-safe. There should be no concurrent operations in the registry.
  53. */
  54. TMetricRegistry& operator=(TMetricRegistry&& other);
  55. /**
  56. * Get a global metrics registry instance.
  57. */
  58. static TMetricRegistry* Instance();
  59. static std::shared_ptr<TMetricRegistry> SharedInstance();
  60. TGauge* Gauge(TLabels labels);
  61. TLazyGauge* LazyGauge(TLabels labels, std::function<double()> supplier);
  62. TIntGauge* IntGauge(TLabels labels);
  63. TLazyIntGauge* LazyIntGauge(TLabels labels, std::function<i64()> supplier);
  64. TCounter* Counter(TLabels labels);
  65. TLazyCounter* LazyCounter(TLabels labels, std::function<ui64()> supplier);
  66. TRate* Rate(TLabels labels);
  67. TLazyRate* LazyRate(TLabels labels, std::function<ui64()> supplier);
  68. THistogram* HistogramCounter(
  69. TLabels labels,
  70. IHistogramCollectorPtr collector);
  71. THistogram* HistogramRate(
  72. TLabels labels,
  73. IHistogramCollectorPtr collector);
  74. THistogram* HistogramCounter(
  75. TLabels labels,
  76. std::function<IHistogramCollectorPtr()> makeHistogramCollector);
  77. THistogram* HistogramRate(
  78. TLabels labels,
  79. std::function<IHistogramCollectorPtr()> makeHistogramCollector);
  80. /**
  81. * Set all registered metrics to zero
  82. */
  83. void Reset();
  84. /**
  85. * Remove all registered metrics from registry
  86. */
  87. void Clear();
  88. void Accept(TInstant time, IMetricConsumer* consumer) const override;
  89. void Append(TInstant time, IMetricConsumer* consumer) const override;
  90. const TLabels& CommonLabels() const noexcept override {
  91. return CommonLabels_;
  92. }
  93. void RemoveMetric(const ILabels& labels) noexcept override;
  94. private:
  95. TGauge* Gauge(ILabelsPtr labels) override;
  96. TLazyGauge* LazyGauge(ILabelsPtr labels, std::function<double()> supplier) override;
  97. TIntGauge* IntGauge(ILabelsPtr labels) override;
  98. TLazyIntGauge* LazyIntGauge(ILabelsPtr labels, std::function<i64()> supplier) override;
  99. TCounter* Counter(ILabelsPtr labels) override;
  100. TLazyCounter* LazyCounter(ILabelsPtr labels, std::function<ui64()> supplier) override;
  101. TRate* Rate(ILabelsPtr labels) override;
  102. TLazyRate* LazyRate(ILabelsPtr labels, std::function<ui64()> supplier) override;
  103. THistogram* HistogramCounter(
  104. ILabelsPtr labels,
  105. IHistogramCollectorPtr collector) override;
  106. THistogram* HistogramRate(
  107. ILabelsPtr labels,
  108. IHistogramCollectorPtr collector) override;
  109. THistogram* HistogramCounter(
  110. ILabelsPtr labels,
  111. std::function<IHistogramCollectorPtr()> makeHistogramCollector) override;
  112. THistogram* HistogramRate(
  113. ILabelsPtr labels,
  114. std::function<IHistogramCollectorPtr()> makeHistogramCollector) override;
  115. private:
  116. THolder<TRWMutex> Lock_ = MakeHolder<TRWMutex>();
  117. THashMap<ILabelsPtr, IMetricPtr> Metrics_;
  118. template <typename TMetric, EMetricType type, typename TLabelsType, typename... Args>
  119. TMetric* Metric(TLabelsType&& labels, Args&&... args);
  120. TLabels CommonLabels_;
  121. };
  122. void WriteLabels(IMetricConsumer* consumer, const ILabels& labels);
  123. }