aboutsummaryrefslogtreecommitdiffstats
path: root/library
diff options
context:
space:
mode:
authorborman <borman@yandex-team.com>2022-12-09 12:46:57 +0300
committerborman <borman@yandex-team.com>2022-12-09 12:46:57 +0300
commitaf9f3fc1ae4712171d64ba9ad5855326b05b68df (patch)
treefe252250cd05ed6f4901fdbef8460a28bc49757a /library
parent216e087ea762ae7e4f19e219a18360a93adf8ae6 (diff)
downloadydb-af9f3fc1ae4712171d64ba9ad5855326b05b68df.tar.gz
Fix static type hints for LazyIntGauge: GAUGE -> IGAUGE
This would previously break repeated metric access. Should not affect production code, since the static type hint is only used for checking against an already-present metric at creation time. Also, added idempotency checks to registry unit tests.
Diffstat (limited to 'library')
-rw-r--r--library/cpp/monlib/metrics/metric_registry.cpp4
-rw-r--r--library/cpp/monlib/metrics/metric_registry_ut.cpp32
2 files changed, 25 insertions, 11 deletions
diff --git a/library/cpp/monlib/metrics/metric_registry.cpp b/library/cpp/monlib/metrics/metric_registry.cpp
index b986f40249..5c6cabf2fc 100644
--- a/library/cpp/monlib/metrics/metric_registry.cpp
+++ b/library/cpp/monlib/metrics/metric_registry.cpp
@@ -77,11 +77,11 @@ namespace NMonitoring {
}
TLazyIntGauge* TMetricRegistry::LazyIntGauge(TLabels labels, std::function<i64()> supplier) {
- return Metric<TLazyIntGauge, EMetricType::GAUGE>(std::move(labels), std::move(supplier));
+ return Metric<TLazyIntGauge, EMetricType::IGAUGE>(std::move(labels), std::move(supplier));
}
TLazyIntGauge* TMetricRegistry::LazyIntGauge(ILabelsPtr labels, std::function<i64()> supplier) {
- return Metric<TLazyIntGauge, EMetricType::GAUGE>(std::move(labels), std::move(supplier));
+ return Metric<TLazyIntGauge, EMetricType::IGAUGE>(std::move(labels), std::move(supplier));
}
TCounter* TMetricRegistry::Counter(TLabels labels) {
diff --git a/library/cpp/monlib/metrics/metric_registry_ut.cpp b/library/cpp/monlib/metrics/metric_registry_ut.cpp
index 22bb8f0fe2..802c423264 100644
--- a/library/cpp/monlib/metrics/metric_registry_ut.cpp
+++ b/library/cpp/monlib/metrics/metric_registry_ut.cpp
@@ -37,10 +37,20 @@ void Out<NMonitoring::NProto::TSingleSample::ValueCase>(IOutputStream& os, NMoni
}
}
+namespace {
+ template<typename F>
+ auto EnsureIdempotent(F&& f) {
+ auto firstResult = f();
+ auto secondResult = f();
+ UNIT_ASSERT_VALUES_EQUAL(firstResult, secondResult);
+ return secondResult;
+ }
+}
+
Y_UNIT_TEST_SUITE(TMetricRegistryTest) {
Y_UNIT_TEST(Gauge) {
TMetricRegistry registry(TLabels{{"common", "label"}});
- TGauge* g = registry.Gauge({{"my", "gauge"}});
+ TGauge* g = EnsureIdempotent([&] { return registry.Gauge({{"my", "gauge"}}); });
UNIT_ASSERT_DOUBLES_EQUAL(g->Get(), 0.0, 1E-6);
g->Set(12.34);
@@ -60,7 +70,7 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) {
Y_UNIT_TEST(LazyGauge) {
TMetricRegistry registry(TLabels{{"common", "label"}});
double val = 0.0;
- TLazyGauge* g = registry.LazyGauge({{"my", "lazyGauge"}}, [&val](){return val;});
+ TLazyGauge* g = EnsureIdempotent([&] { return registry.LazyGauge({{"my", "lazyGauge"}}, [&val](){return val;}); });
UNIT_ASSERT_DOUBLES_EQUAL(g->Get(), 0.0, 1E-6);
val = 12.34;
@@ -77,7 +87,7 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) {
Y_UNIT_TEST(IntGauge) {
TMetricRegistry registry(TLabels{{"common", "label"}});
- TIntGauge* g = registry.IntGauge({{"my", "gauge"}});
+ TIntGauge* g = EnsureIdempotent([&] { return registry.IntGauge({{"my", "gauge"}}); });
UNIT_ASSERT_VALUES_EQUAL(g->Get(), 0);
@@ -107,7 +117,7 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) {
Y_UNIT_TEST(LazyIntGauge) {
TMetricRegistry registry(TLabels{{"common", "label"}});
i64 val = 0;
- TLazyIntGauge* g = registry.LazyIntGauge({{"my", "gauge"}}, [&val](){return val;});
+ TLazyIntGauge* g = EnsureIdempotent([&] { return registry.LazyIntGauge({{"my", "gauge"}}, [&val](){return val;}); });
UNIT_ASSERT_VALUES_EQUAL(g->Get(), 0);
val += 1;
@@ -124,7 +134,7 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) {
Y_UNIT_TEST(Counter) {
TMetricRegistry registry(TLabels{{"common", "label"}});
- TCounter* c = registry.Counter({{"my", "counter"}});
+ TCounter* c = EnsureIdempotent([&] { return registry.Counter({{"my", "counter"}}); });
UNIT_ASSERT_VALUES_EQUAL(c->Get(), 0);
UNIT_ASSERT_VALUES_EQUAL(c->Inc(), 1);
@@ -137,7 +147,7 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) {
TMetricRegistry registry(TLabels{{"common", "label"}});
ui64 val = 0;
- TLazyCounter* c = registry.LazyCounter({{"my", "counter"}}, [&val](){return val;});
+ TLazyCounter* c = EnsureIdempotent([&] { return registry.LazyCounter({{"my", "counter"}}, [&val](){return val;}); });
UNIT_ASSERT_VALUES_EQUAL(c->Get(), 0);
val = 42;
@@ -148,7 +158,7 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) {
TMetricRegistry registry(TLabels{{"common", "label"}});
ui64 val = 0;
- TLazyRate* r = registry.LazyRate({{"my", "rate"}}, [&val](){return val;});
+ TLazyRate* r = EnsureIdempotent([&] { return registry.LazyRate({{"my", "rate"}}, [&val](){return val;}); });
UNIT_ASSERT_VALUES_EQUAL(r->Get(), 0);
val = 42;
@@ -216,13 +226,17 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) {
Y_UNIT_TEST(Histograms) {
TMetricRegistry registry(TLabels{{"common", "label"}});
- THistogram* h1 = registry.HistogramCounter(
+ THistogram* h1 = EnsureIdempotent([&] {
+ return registry.HistogramCounter(
{{"sensor", "readTimeMillis"}},
ExponentialHistogram(5, 2));
+ });
- THistogram* h2 = registry.HistogramRate(
+ THistogram* h2 = EnsureIdempotent([&] {
+ return registry.HistogramRate(
{{"sensor", "writeTimeMillis"}},
ExplicitHistogram({1, 5, 15, 20, 25}));
+ });
for (i64 i = 0; i < 100; i++) {
h1->Record(i);