fake.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "fake.h"
  2. namespace NMonitoring {
  3. IGauge* TFakeMetricRegistry::Gauge(ILabelsPtr labels) {
  4. return Metric<TFakeGauge, EMetricType::GAUGE>(std::move(labels));
  5. }
  6. ILazyGauge* TFakeMetricRegistry::LazyGauge(ILabelsPtr labels, std::function<double()> supplier) {
  7. Y_UNUSED(supplier);
  8. return Metric<TFakeLazyGauge, EMetricType::GAUGE>(std::move(labels));
  9. }
  10. IIntGauge* TFakeMetricRegistry::IntGauge(ILabelsPtr labels) {
  11. return Metric<TFakeIntGauge, EMetricType::IGAUGE>(std::move(labels));
  12. }
  13. ILazyIntGauge* TFakeMetricRegistry::LazyIntGauge(ILabelsPtr labels, std::function<i64()> supplier) {
  14. Y_UNUSED(supplier);
  15. return Metric<TFakeLazyIntGauge, EMetricType::IGAUGE>(std::move(labels));
  16. }
  17. ICounter* TFakeMetricRegistry::Counter(ILabelsPtr labels) {
  18. return Metric<TFakeCounter, EMetricType::COUNTER>(std::move(labels));
  19. }
  20. ILazyCounter* TFakeMetricRegistry::LazyCounter(ILabelsPtr labels, std::function<ui64()> supplier) {
  21. Y_UNUSED(supplier);
  22. return Metric<TFakeLazyCounter, EMetricType::COUNTER>(std::move(labels));
  23. }
  24. IRate* TFakeMetricRegistry::Rate(ILabelsPtr labels) {
  25. return Metric<TFakeRate, EMetricType::RATE>(std::move(labels));
  26. }
  27. ILazyRate* TFakeMetricRegistry::LazyRate(ILabelsPtr labels, std::function<ui64()> supplier) {
  28. Y_UNUSED(supplier);
  29. return Metric<TFakeLazyRate, EMetricType::RATE>(std::move(labels));
  30. }
  31. IHistogram* TFakeMetricRegistry::HistogramCounter(ILabelsPtr labels, IHistogramCollectorPtr collector) {
  32. Y_UNUSED(collector);
  33. return Metric<TFakeHistogram, EMetricType::HIST>(std::move(labels), false);
  34. }
  35. IHistogram* TFakeMetricRegistry::HistogramCounter(ILabelsPtr labels, std::function<IHistogramCollectorPtr()> collector) {
  36. Y_UNUSED(collector);
  37. return Metric<TFakeHistogram, EMetricType::HIST>(std::move(labels), false);
  38. }
  39. void TFakeMetricRegistry::RemoveMetric(const ILabels& labels) noexcept {
  40. TWriteGuard g{Lock_};
  41. Metrics_.erase(labels);
  42. }
  43. void TFakeMetricRegistry::Accept(TInstant time, IMetricConsumer* consumer) const {
  44. Y_UNUSED(time);
  45. consumer->OnStreamBegin();
  46. consumer->OnStreamEnd();
  47. }
  48. IHistogram* TFakeMetricRegistry::HistogramRate(ILabelsPtr labels, IHistogramCollectorPtr collector) {
  49. Y_UNUSED(collector);
  50. return Metric<TFakeHistogram, EMetricType::HIST_RATE>(std::move(labels), true);
  51. }
  52. IHistogram* TFakeMetricRegistry::HistogramRate(ILabelsPtr labels, std::function<IHistogramCollectorPtr()> collector) {
  53. Y_UNUSED(collector);
  54. return Metric<TFakeHistogram, EMetricType::HIST_RATE>(std::move(labels), true);
  55. }
  56. void TFakeMetricRegistry::Append(TInstant time, IMetricConsumer* consumer) const {
  57. Y_UNUSED(time, consumer);
  58. }
  59. const TLabels& TFakeMetricRegistry::CommonLabels() const noexcept {
  60. return CommonLabels_;
  61. }
  62. template <typename TMetric, EMetricType type, typename TLabelsType, typename... Args>
  63. TMetric* TFakeMetricRegistry::Metric(TLabelsType&& labels, Args&&... args) {
  64. {
  65. TReadGuard g{Lock_};
  66. auto it = Metrics_.find(labels);
  67. if (it != Metrics_.end()) {
  68. Y_ENSURE(it->second->Type() == type, "cannot create metric " << labels
  69. << " with type " << MetricTypeToStr(type)
  70. << ", because registry already has same metric with type " << MetricTypeToStr(it->second->Type()));
  71. return static_cast<TMetric*>(it->second.Get());
  72. }
  73. }
  74. {
  75. TWriteGuard g{Lock_};
  76. IMetricPtr metric = MakeIntrusive<TMetric>(std::forward<Args>(args)...);
  77. // decltype(Metrics_)::iterator breaks build on windows
  78. THashMap<ILabelsPtr, IMetricPtr>::iterator it;
  79. if constexpr (!std::is_convertible_v<TLabelsType, ILabelsPtr>) {
  80. it = Metrics_.emplace(new TLabels{std::forward<TLabelsType>(labels)}, std::move(metric)).first;
  81. } else {
  82. it = Metrics_.emplace(std::forward<TLabelsType>(labels), std::move(metric)).first;
  83. }
  84. return static_cast<TMetric*>(it->second.Get());
  85. }
  86. }
  87. } // namespace NMonitoring