diff options
author | k-vukolov <k-vukolov@yandex-team.ru> | 2022-03-17 17:29:32 +0300 |
---|---|---|
committer | k-vukolov <k-vukolov@yandex-team.ru> | 2022-03-17 17:29:32 +0300 |
commit | 26d5a0024c9e3a3c14756599c5dd0ddda71c672b (patch) | |
tree | 39127a6a02de8e14843149118c3789f3e788dc22 | |
parent | b94d94d54d13127426fd034d04c428edd9d2e40a (diff) | |
download | ydb-26d5a0024c9e3a3c14756599c5dd0ddda71c672b.tar.gz |
Do not create buckets on each metric access
ref:23778046ec41d633f28df9e49ac776b31db22afe
-rw-r--r-- | library/cpp/monlib/metrics/fake.cpp | 5 | ||||
-rw-r--r-- | library/cpp/monlib/metrics/fake.h | 1 | ||||
-rw-r--r-- | library/cpp/monlib/metrics/metric_registry.cpp | 6 | ||||
-rw-r--r-- | library/cpp/monlib/metrics/metric_registry.h | 2 | ||||
-rw-r--r-- | library/cpp/monlib/metrics/metric_registry_ut.cpp | 10 | ||||
-rw-r--r-- | library/cpp/monlib/metrics/metric_sub_registry.h | 9 |
6 files changed, 33 insertions, 0 deletions
diff --git a/library/cpp/monlib/metrics/fake.cpp b/library/cpp/monlib/metrics/fake.cpp index b6f5e37af8..d7b21339c3 100644 --- a/library/cpp/monlib/metrics/fake.cpp +++ b/library/cpp/monlib/metrics/fake.cpp @@ -48,6 +48,11 @@ namespace NMonitoring { Metrics_.erase(labels); } + bool TFakeMetricRegistry::HasMetric(const ILabels &labels) noexcept { + TReadGuard g{Lock_}; + return Metrics_.contains(labels); + } + void TFakeMetricRegistry::Accept(TInstant time, IMetricConsumer* consumer) const { Y_UNUSED(time); consumer->OnStreamBegin(); diff --git a/library/cpp/monlib/metrics/fake.h b/library/cpp/monlib/metrics/fake.h index 61ba4f2bd4..a75117dece 100644 --- a/library/cpp/monlib/metrics/fake.h +++ b/library/cpp/monlib/metrics/fake.h @@ -37,6 +37,7 @@ namespace NMonitoring { const TLabels& CommonLabels() const noexcept override; void RemoveMetric(const ILabels& labels) noexcept override; + bool HasMetric(const ILabels &labels) noexcept override; private: TRWMutex Lock_; diff --git a/library/cpp/monlib/metrics/metric_registry.cpp b/library/cpp/monlib/metrics/metric_registry.cpp index 213cab1aca..74260e3069 100644 --- a/library/cpp/monlib/metrics/metric_registry.cpp +++ b/library/cpp/monlib/metrics/metric_registry.cpp @@ -194,6 +194,12 @@ namespace NMonitoring { Metrics_.erase(labels); } + bool TMetricRegistry::HasMetric(const ILabels &labels) noexcept { + TReadGuard g{*Lock_}; + + return Metrics_.contains(labels); + } + void TMetricRegistry::Accept(TInstant time, IMetricConsumer* consumer) const { consumer->OnStreamBegin(); diff --git a/library/cpp/monlib/metrics/metric_registry.h b/library/cpp/monlib/metrics/metric_registry.h index 6d5a59f04d..06e61d9991 100644 --- a/library/cpp/monlib/metrics/metric_registry.h +++ b/library/cpp/monlib/metrics/metric_registry.h @@ -44,6 +44,7 @@ namespace NMonitoring { public: virtual const TLabels& CommonLabels() const noexcept = 0; virtual void RemoveMetric(const ILabels& labels) noexcept = 0; + virtual bool HasMetric(const ILabels& labels) noexcept = 0; }; @@ -101,6 +102,7 @@ namespace NMonitoring { } void RemoveMetric(const ILabels& labels) noexcept override; + bool HasMetric(const ILabels &labels) noexcept override; private: TGauge* Gauge(ILabelsPtr labels) override; diff --git a/library/cpp/monlib/metrics/metric_registry_ut.cpp b/library/cpp/monlib/metrics/metric_registry_ut.cpp index 4436fd4943..65fb2b3b38 100644 --- a/library/cpp/monlib/metrics/metric_registry_ut.cpp +++ b/library/cpp/monlib/metrics/metric_registry_ut.cpp @@ -341,4 +341,14 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) { UNIT_ASSERT(commonLabels[0].GetName() == "common"); UNIT_ASSERT(commonLabels[0].GetValue() == "label"); } + + Y_UNIT_TEST(HasMetricTest) { + TMetricRegistry registry; + TLabels labels{{"some", "labels"}}; + TLabels anotherLabels{{"another", "label"}}; + + registry.Gauge(labels); + UNIT_ASSERT_EQUAL(registry.HasMetric(labels), true); + UNIT_ASSERT_EQUAL(registry.HasMetric(anotherLabels), false); + } } diff --git a/library/cpp/monlib/metrics/metric_sub_registry.h b/library/cpp/monlib/metrics/metric_sub_registry.h index e83eeeafb2..36f8e0e639 100644 --- a/library/cpp/monlib/metrics/metric_sub_registry.h +++ b/library/cpp/monlib/metrics/metric_sub_registry.h @@ -100,6 +100,15 @@ public: DelegatePtr_->RemoveMetric(toRemove); } + bool HasMetric(const ILabels &labels) noexcept override { + TLabelsImpl<TStringBuf> toCheck; + for (auto& l: labels) { + toCheck.Add(l); + } + AddCommonLabels(&toCheck); + return DelegatePtr_->HasMetric(toCheck); + } + private: void AddCommonLabels(ILabels* labels) const { for (auto& label: CommonLabels_) { |