fake.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #pragma once
  2. #include "metric.h"
  3. #include "metric_registry.h"
  4. namespace NMonitoring {
  5. class TFakeMetricRegistry: public IMetricRegistry {
  6. public:
  7. TFakeMetricRegistry() noexcept
  8. : CommonLabels_{0}
  9. {
  10. }
  11. explicit TFakeMetricRegistry(TLabels commonLabels) noexcept
  12. : CommonLabels_{std::move(commonLabels)}
  13. {
  14. }
  15. IGauge* Gauge(ILabelsPtr labels) override;
  16. ILazyGauge* LazyGauge(ILabelsPtr labels, std::function<double()> supplier) override;
  17. IIntGauge* IntGauge(ILabelsPtr labels) override;
  18. ILazyIntGauge* LazyIntGauge(ILabelsPtr labels, std::function<i64()> supplier) override;
  19. ICounter* Counter(ILabelsPtr labels) override;
  20. ILazyCounter* LazyCounter(ILabelsPtr labels, std::function<ui64()> supplier) override;
  21. IRate* Rate(ILabelsPtr labels) override;
  22. ILazyRate* LazyRate(ILabelsPtr labels, std::function<ui64()> supplier) override;
  23. IHistogram* HistogramCounter(
  24. ILabelsPtr labels,
  25. IHistogramCollectorPtr collector) override;
  26. IHistogram* HistogramRate(
  27. ILabelsPtr labels,
  28. IHistogramCollectorPtr collector) override;
  29. IHistogram* HistogramCounter(
  30. ILabelsPtr labels,
  31. std::function<IHistogramCollectorPtr()> collector) override;
  32. IHistogram* HistogramRate(
  33. ILabelsPtr labels,
  34. std::function<IHistogramCollectorPtr()> collector) override;
  35. void Accept(TInstant time, IMetricConsumer* consumer) const override;
  36. void Append(TInstant time, IMetricConsumer* consumer) const override;
  37. const TLabels& CommonLabels() const noexcept override;
  38. void RemoveMetric(const ILabels& labels) noexcept override;
  39. private:
  40. TRWMutex Lock_;
  41. THashMap<ILabelsPtr, IMetricPtr> Metrics_;
  42. template <typename TMetric, EMetricType type, typename TLabelsType, typename... Args>
  43. TMetric* Metric(TLabelsType&& labels, Args&&... args);
  44. const TLabels CommonLabels_;
  45. };
  46. template <typename TBase>
  47. struct TFakeAcceptor: TBase {
  48. void Accept(TInstant time, IMetricConsumer* consumer) const override {
  49. Y_UNUSED(time, consumer);
  50. }
  51. };
  52. struct TFakeIntGauge final: public TFakeAcceptor<IIntGauge> {
  53. i64 Add(i64 n) noexcept override {
  54. Y_UNUSED(n);
  55. return 0;
  56. }
  57. void Set(i64 n) noexcept override {
  58. Y_UNUSED(n);
  59. }
  60. i64 Get() const noexcept override {
  61. return 0;
  62. }
  63. };
  64. struct TFakeLazyIntGauge final: public TFakeAcceptor<ILazyIntGauge> {
  65. i64 Get() const noexcept override {
  66. return 0;
  67. }
  68. };
  69. struct TFakeRate final: public TFakeAcceptor<IRate> {
  70. ui64 Add(ui64 n) noexcept override {
  71. Y_UNUSED(n);
  72. return 0;
  73. }
  74. ui64 Get() const noexcept override {
  75. return 0;
  76. }
  77. void Reset() noexcept override {
  78. }
  79. };
  80. struct TFakeLazyRate final: public TFakeAcceptor<ILazyRate> {
  81. ui64 Get() const noexcept override {
  82. return 0;
  83. }
  84. };
  85. struct TFakeGauge final: public TFakeAcceptor<IGauge> {
  86. double Add(double n) noexcept override {
  87. Y_UNUSED(n);
  88. return 0;
  89. }
  90. void Set(double n) noexcept override {
  91. Y_UNUSED(n);
  92. }
  93. double Get() const noexcept override {
  94. return 0;
  95. }
  96. };
  97. struct TFakeLazyGauge final: public TFakeAcceptor<ILazyGauge> {
  98. double Get() const noexcept override {
  99. return 0;
  100. }
  101. };
  102. struct TFakeHistogram final: public IHistogram {
  103. TFakeHistogram(bool isRate = false)
  104. : IHistogram{isRate}
  105. {
  106. }
  107. void Record(double value) override {
  108. Y_UNUSED(value);
  109. }
  110. void Record(double value, ui32 count) override {
  111. Y_UNUSED(value, count);
  112. }
  113. IHistogramSnapshotPtr TakeSnapshot() const override {
  114. return nullptr;
  115. }
  116. void Accept(TInstant time, IMetricConsumer* consumer) const override {
  117. Y_UNUSED(time, consumer);
  118. }
  119. void Reset() override {
  120. }
  121. };
  122. struct TFakeCounter final: public TFakeAcceptor<ICounter> {
  123. ui64 Add(ui64 n) noexcept override {
  124. Y_UNUSED(n);
  125. return 0;
  126. }
  127. ui64 Get() const noexcept override {
  128. return 0;
  129. }
  130. void Reset() noexcept override {
  131. }
  132. };
  133. struct TFakeLazyCounter final: public TFakeAcceptor<ILazyCounter> {
  134. ui64 Get() const noexcept override {
  135. return 0;
  136. }
  137. };
  138. } // namespace NMonitoring