From 94709cd7045414a672dd95c4f4f904a420151997 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Wed, 18 Dec 2024 10:39:17 +0800 Subject: [PATCH 1/3] summary expire test --- src/metric/tests/test_metric.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/metric/tests/test_metric.cpp b/src/metric/tests/test_metric.cpp index 41ec14232..b5e29cbd5 100644 --- a/src/metric/tests/test_metric.cpp +++ b/src/metric/tests/test_metric.cpp @@ -1946,14 +1946,22 @@ TEST_CASE("test metric manager clean expired label") { auto& inst = dynamic_metric_manager::instance(); auto pair = inst.create_metric_dynamic( std::string("some_counter"), "", std::array{"url"}); + auto summary = std::make_shared>( + std::string("test_summary"), std::string("summary help"), + std::vector{0.5, 0.9, 0.95, 0.99}, + std::array{"method", "url"}); + inst.register_metric(summary); auto c = pair.second; c->inc({"/"}); c->inc({"/test"}); + summary->observe({"GET", "test"}, 10); CHECK(c->label_value_count() == 2); std::this_thread::sleep_for(std::chrono::seconds(2)); c->inc({"/index"}); size_t count = c->label_value_count(); CHECK(count == 1); + auto ct1 = summary->label_value_count(); + CHECK(ct1 == 0); } TEST_CASE("test remove label value") { From 28adb8948c220db2374c40e4a4294032bbea936f Mon Sep 17 00:00:00 2001 From: qicosmos Date: Wed, 18 Dec 2024 11:38:28 +0800 Subject: [PATCH 2/3] histogram expired --- include/ylt/metric/dynamic_metric.hpp | 18 ++++++++++-------- include/ylt/metric/histogram.hpp | 9 +++++++++ src/metric/tests/test_metric.cpp | 10 ++++++++++ 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/include/ylt/metric/dynamic_metric.hpp b/include/ylt/metric/dynamic_metric.hpp index 573bf2a91..8858812f3 100644 --- a/include/ylt/metric/dynamic_metric.hpp +++ b/include/ylt/metric/dynamic_metric.hpp @@ -92,6 +92,15 @@ class dynamic_metric_impl : public dynamic_metric { return map_.template copy>(); } + void clean_expired_label() override { + erase_if([now = std::chrono::steady_clock::now()](auto& pair) mutable { + bool r = std::chrono::duration_cast( + now - pair.second->get_created_time()) + .count() >= ylt_label_max_age.count(); + return r; + }); + } + protected: template std::pair, bool> try_emplace(Key&& key, @@ -107,14 +116,7 @@ class dynamic_metric_impl : public dynamic_metric { }, std::forward(key), std::forward(args)...); } - void clean_expired_label() override { - erase_if([now = std::chrono::steady_clock::now()](auto& pair) mutable { - bool r = std::chrono::duration_cast( - now - pair.second->get_created_time()) - .count() >= ylt_label_max_age.count(); - return r; - }); - } + std::shared_ptr find(std::span key) const { return map_.find(key); } diff --git a/include/ylt/metric/histogram.hpp b/include/ylt/metric/histogram.hpp index bc27fabec..6b4bd4ad9 100644 --- a/include/ylt/metric/histogram.hpp +++ b/include/ylt/metric/histogram.hpp @@ -188,6 +188,13 @@ class basic_dynamic_histogram : public dynamic_metric { bucket_counts_[bucket_index]->inc(labels_value); } + void clean_expired_label() override { + sum_->clean_expired_label(); + for (auto m : bucket_counts_) { + m->clean_expired_label(); + } + } + auto get_bucket_counts() { return bucket_counts_; } bool has_label_value(const std::string &label_val) override { @@ -202,6 +209,8 @@ class basic_dynamic_histogram : public dynamic_metric { return sum_->has_label_value(label_value); } + size_t label_value_count() const { return sum_->label_value_count(); } + void serialize(std::string &str) override { auto value_map = sum_->copy(); if (value_map.empty()) { diff --git a/src/metric/tests/test_metric.cpp b/src/metric/tests/test_metric.cpp index b5e29cbd5..b0e2eaeb2 100644 --- a/src/metric/tests/test_metric.cpp +++ b/src/metric/tests/test_metric.cpp @@ -1950,18 +1950,28 @@ TEST_CASE("test metric manager clean expired label") { std::string("test_summary"), std::string("summary help"), std::vector{0.5, 0.9, 0.95, 0.99}, std::array{"method", "url"}); + auto h = std::make_shared( + std::string("test"), std::string("help"), + std::vector{5.23, 10.54, 20.0, 50.0, 100.0}, + std::array{"method", "url"}); inst.register_metric(summary); + inst.register_metric(h); auto c = pair.second; c->inc({"/"}); c->inc({"/test"}); summary->observe({"GET", "test"}, 10); + h->observe({"GET", "test"}, 10); CHECK(c->label_value_count() == 2); + CHECK(summary->label_value_count() == 1); + CHECK(h->label_value_count() == 1); std::this_thread::sleep_for(std::chrono::seconds(2)); c->inc({"/index"}); size_t count = c->label_value_count(); CHECK(count == 1); auto ct1 = summary->label_value_count(); CHECK(ct1 == 0); + auto ct2 = h->label_value_count(); + CHECK(ct2 == 0); } TEST_CASE("test remove label value") { From 52e21622660b76aac379d86973863a5a0161fed6 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Wed, 18 Dec 2024 15:19:41 +0800 Subject: [PATCH 3/3] update --- include/ylt/metric/histogram.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/ylt/metric/histogram.hpp b/include/ylt/metric/histogram.hpp index 6b4bd4ad9..ce897a452 100644 --- a/include/ylt/metric/histogram.hpp +++ b/include/ylt/metric/histogram.hpp @@ -190,7 +190,7 @@ class basic_dynamic_histogram : public dynamic_metric { void clean_expired_label() override { sum_->clean_expired_label(); - for (auto m : bucket_counts_) { + for (auto &m : bucket_counts_) { m->clean_expired_label(); } }