metric_sub_registry.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #pragma once
  2. #include "metric_registry.h"
  3. namespace NMonitoring {
  4. /**
  5. * This registry is wrapping given delegate registry to add common labels
  6. * to all created metrics through this sub registry.
  7. */
  8. class TMetricSubRegistry final: public IMetricRegistry {
  9. public:
  10. /**
  11. * Do not keep ownership of the given delegate.
  12. */
  13. TMetricSubRegistry(TLabels commonLabels, IMetricRegistry* delegate) noexcept
  14. : CommonLabels_{std::move(commonLabels)}
  15. , DelegatePtr_{delegate}
  16. {
  17. }
  18. /**
  19. * Keeps ownership of the given delegate.
  20. */
  21. TMetricSubRegistry(TLabels commonLabels, std::shared_ptr<IMetricRegistry> delegate) noexcept
  22. : CommonLabels_{std::move(commonLabels)}
  23. , Delegate_{std::move(delegate)}
  24. , DelegatePtr_{Delegate_.get()}
  25. {
  26. }
  27. IGauge* Gauge(ILabelsPtr labels) override {
  28. AddCommonLabels(labels.Get());
  29. return DelegatePtr_->Gauge(std::move(labels));
  30. }
  31. ILazyGauge* LazyGauge(ILabelsPtr labels, std::function<double()> supplier) override {
  32. AddCommonLabels(labels.Get());
  33. return DelegatePtr_->LazyGauge(std::move(labels), std::move(supplier));
  34. }
  35. IIntGauge* IntGauge(ILabelsPtr labels) override {
  36. AddCommonLabels(labels.Get());
  37. return DelegatePtr_->IntGauge(std::move(labels));
  38. }
  39. ILazyIntGauge* LazyIntGauge(ILabelsPtr labels, std::function<i64()> supplier) override {
  40. AddCommonLabels(labels.Get());
  41. return DelegatePtr_->LazyIntGauge(std::move(labels), std::move(supplier));
  42. }
  43. ICounter* Counter(ILabelsPtr labels) override {
  44. AddCommonLabels(labels.Get());
  45. return DelegatePtr_->Counter(std::move(labels));
  46. }
  47. ILazyCounter* LazyCounter(ILabelsPtr labels, std::function<ui64()> supplier) override {
  48. AddCommonLabels(labels.Get());
  49. return DelegatePtr_->LazyCounter(std::move(labels), std::move(supplier));
  50. }
  51. IRate* Rate(ILabelsPtr labels) override {
  52. AddCommonLabels(labels.Get());
  53. return DelegatePtr_->Rate(std::move(labels));
  54. }
  55. ILazyRate* LazyRate(ILabelsPtr labels, std::function<ui64()> supplier) override {
  56. AddCommonLabels(labels.Get());
  57. return DelegatePtr_->LazyRate(std::move(labels), std::move(supplier));
  58. }
  59. IHistogram* HistogramCounter(ILabelsPtr labels, IHistogramCollectorPtr collector) override {
  60. AddCommonLabels(labels.Get());
  61. return DelegatePtr_->HistogramCounter(std::move(labels), std::move(collector));
  62. }
  63. IHistogram* HistogramRate(ILabelsPtr labels, IHistogramCollectorPtr collector) override {
  64. AddCommonLabels(labels.Get());
  65. return DelegatePtr_->HistogramRate(std::move(labels), std::move(collector));
  66. }
  67. IHistogram* HistogramCounter(ILabelsPtr labels, std::function<IHistogramCollectorPtr()> collector) override {
  68. AddCommonLabels(labels.Get());
  69. return DelegatePtr_->HistogramCounter(std::move(labels), std::move(collector));
  70. }
  71. IHistogram* HistogramRate(ILabelsPtr labels, std::function<IHistogramCollectorPtr()> collector) override {
  72. AddCommonLabels(labels.Get());
  73. return DelegatePtr_->HistogramRate(std::move(labels), std::move(collector));
  74. }
  75. void Accept(TInstant time, IMetricConsumer* consumer) const override {
  76. DelegatePtr_->Accept(time, consumer);
  77. }
  78. void Append(TInstant time, IMetricConsumer* consumer) const override {
  79. DelegatePtr_->Append(time, consumer);
  80. }
  81. const TLabels& CommonLabels() const noexcept override {
  82. return CommonLabels_;
  83. }
  84. void RemoveMetric(const ILabels& labels) noexcept override {
  85. TLabelsImpl<TStringBuf> toRemove;
  86. for (auto& l: labels) {
  87. toRemove.Add(l);
  88. }
  89. AddCommonLabels(&toRemove);
  90. DelegatePtr_->RemoveMetric(toRemove);
  91. }
  92. private:
  93. void AddCommonLabels(ILabels* labels) const {
  94. for (auto& label: CommonLabels_) {
  95. labels->Add(label);
  96. }
  97. }
  98. private:
  99. const TLabels CommonLabels_;
  100. std::shared_ptr<IMetricRegistry> Delegate_;
  101. IMetricRegistry* DelegatePtr_;
  102. };
  103. } // namespace NMonitoring