fake.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. void Accept(TInstant time, IMetricConsumer* consumer) const override;
  30. void Append(TInstant time, IMetricConsumer* consumer) const override;
  31. const TLabels& CommonLabels() const noexcept override;
  32. void RemoveMetric(const ILabels& labels) noexcept override;
  33. private:
  34. TRWMutex Lock_;
  35. THashMap<ILabelsPtr, IMetricPtr> Metrics_;
  36. template <typename TMetric, EMetricType type, typename TLabelsType, typename... Args>
  37. TMetric* Metric(TLabelsType&& labels, Args&&... args);
  38. const TLabels CommonLabels_;
  39. };
  40. template <typename TBase>
  41. struct TFakeAcceptor: TBase {
  42. void Accept(TInstant time, IMetricConsumer* consumer) const override {
  43. Y_UNUSED(time, consumer);
  44. }
  45. };
  46. struct TFakeIntGauge final: public TFakeAcceptor<IIntGauge> {
  47. i64 Add(i64 n) noexcept override {
  48. Y_UNUSED(n);
  49. return 0;
  50. }
  51. void Set(i64 n) noexcept override {
  52. Y_UNUSED(n);
  53. }
  54. i64 Get() const noexcept override {
  55. return 0;
  56. }
  57. };
  58. struct TFakeLazyIntGauge final: public TFakeAcceptor<ILazyIntGauge> {
  59. i64 Get() const noexcept override {
  60. return 0;
  61. }
  62. };
  63. struct TFakeRate final: public TFakeAcceptor<IRate> {
  64. ui64 Add(ui64 n) noexcept override {
  65. Y_UNUSED(n);
  66. return 0;
  67. }
  68. ui64 Get() const noexcept override {
  69. return 0;
  70. }
  71. void Reset() noexcept override {
  72. }
  73. };
  74. struct TFakeLazyRate final: public TFakeAcceptor<ILazyRate> {
  75. ui64 Get() const noexcept override {
  76. return 0;
  77. }
  78. };
  79. struct TFakeGauge final: public TFakeAcceptor<IGauge> {
  80. double Add(double n) noexcept override {
  81. Y_UNUSED(n);
  82. return 0;
  83. }
  84. void Set(double n) noexcept override {
  85. Y_UNUSED(n);
  86. }
  87. double Get() const noexcept override {
  88. return 0;
  89. }
  90. };
  91. struct TFakeLazyGauge final: public TFakeAcceptor<ILazyGauge> {
  92. double Get() const noexcept override {
  93. return 0;
  94. }
  95. };
  96. struct TFakeHistogram final: public IHistogram {
  97. TFakeHistogram(bool isRate = false)
  98. : IHistogram{isRate}
  99. {
  100. }
  101. void Record(double value) override {
  102. Y_UNUSED(value);
  103. }
  104. void Record(double value, ui32 count) override {
  105. Y_UNUSED(value, count);
  106. }
  107. IHistogramSnapshotPtr TakeSnapshot() const override {
  108. return nullptr;
  109. }
  110. void Accept(TInstant time, IMetricConsumer* consumer) const override {
  111. Y_UNUSED(time, consumer);
  112. }
  113. void Reset() override {
  114. }
  115. };
  116. struct TFakeCounter final: public TFakeAcceptor<ICounter> {
  117. ui64 Add(ui64 n) noexcept override {
  118. Y_UNUSED(n);
  119. return 0;
  120. }
  121. ui64 Get() const noexcept override {
  122. return 0;
  123. }
  124. void Reset() noexcept override {
  125. }
  126. };
  127. struct TFakeLazyCounter final: public TFakeAcceptor<ILazyCounter> {
  128. ui64 Get() const noexcept override {
  129. return 0;
  130. }
  131. };
  132. } // namespace NMonitoring