fake.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. void TFakeMetricRegistry::RemoveMetric(const ILabels& labels) noexcept {
  36. TWriteGuard g{Lock_};
  37. Metrics_.erase(labels);
  38. }
  39. void TFakeMetricRegistry::Accept(TInstant time, IMetricConsumer* consumer) const {
  40. Y_UNUSED(time);
  41. consumer->OnStreamBegin();
  42. consumer->OnStreamEnd();
  43. }
  44. IHistogram* TFakeMetricRegistry::HistogramRate(ILabelsPtr labels, IHistogramCollectorPtr collector) {
  45. Y_UNUSED(collector);
  46. return Metric<TFakeHistogram, EMetricType::HIST_RATE>(std::move(labels), true);
  47. }
  48. void TFakeMetricRegistry::Append(TInstant time, IMetricConsumer* consumer) const {
  49. Y_UNUSED(time, consumer);
  50. }
  51. const TLabels& TFakeMetricRegistry::CommonLabels() const noexcept {
  52. return CommonLabels_;
  53. }
  54. template <typename TMetric, EMetricType type, typename TLabelsType, typename... Args>
  55. TMetric* TFakeMetricRegistry::Metric(TLabelsType&& labels, Args&&... args) {
  56. {
  57. TReadGuard g{Lock_};
  58. auto it = Metrics_.find(labels);
  59. if (it != Metrics_.end()) {
  60. Y_ENSURE(it->second->Type() == type, "cannot create metric " << labels
  61. << " with type " << MetricTypeToStr(type)
  62. << ", because registry already has same metric with type " << MetricTypeToStr(it->second->Type()));
  63. return static_cast<TMetric*>(it->second.Get());
  64. }
  65. }
  66. {
  67. TWriteGuard g{Lock_};
  68. IMetricPtr metric = MakeHolder<TMetric>(std::forward<Args>(args)...);
  69. // decltype(Metrics_)::iterator breaks build on windows
  70. THashMap<ILabelsPtr, IMetricPtr>::iterator it;
  71. if constexpr (!std::is_convertible_v<TLabelsType, ILabelsPtr>) {
  72. it = Metrics_.emplace(new TLabels{std::forward<TLabelsType>(labels)}, std::move(metric)).first;
  73. } else {
  74. it = Metrics_.emplace(std::forward<TLabelsType>(labels), std::move(metric)).first;
  75. }
  76. return static_cast<TMetric*>(it->second.Get());
  77. }
  78. }
  79. } // namespace NMonitoring