diff --git a/include/cinatra/ylt/coro_io/load_blancer.hpp b/include/cinatra/ylt/coro_io/load_blancer.hpp index 15e7fa13..68e07e2e 100644 --- a/include/cinatra/ylt/coro_io/load_blancer.hpp +++ b/include/cinatra/ylt/coro_io/load_blancer.hpp @@ -158,6 +158,7 @@ class load_blancer { this->config_ = std::move(o.config_); this->lb_worker = std::move(o.lb_worker); this->client_pools_ = std::move(o.client_pools_); + return *this; } load_blancer(const load_blancer& o) = delete; load_blancer& operator=(const load_blancer& o) = delete; diff --git a/include/cinatra/ylt/metric/dynamic_metric.hpp b/include/cinatra/ylt/metric/dynamic_metric.hpp index db1f6509..dd8a9d42 100644 --- a/include/cinatra/ylt/metric/dynamic_metric.hpp +++ b/include/cinatra/ylt/metric/dynamic_metric.hpp @@ -96,6 +96,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, @@ -111,14 +120,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/cinatra/ylt/metric/histogram.hpp b/include/cinatra/ylt/metric/histogram.hpp index bc27fabe..ce897a45 100644 --- a/include/cinatra/ylt/metric/histogram.hpp +++ b/include/cinatra/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/include/cinatra/ylt/metric/summary.hpp b/include/cinatra/ylt/metric/summary.hpp index 7d36c86e..9590bb3e 100644 --- a/include/cinatra/ylt/metric/summary.hpp +++ b/include/cinatra/ylt/metric/summary.hpp @@ -37,7 +37,7 @@ YLT_REFL(json_summary_t, name, help, type, labels_name, quantiles_key, metrics); class summary_t : public static_metric { public: summary_t(std::string name, std::string help, std::vector quantiles, - std::chrono::seconds max_age = std::chrono::seconds{60}) + std::chrono::seconds max_age = std::chrono::seconds{36000}) : static_metric(MetricType::Summary, std::move(name), std::move(help)), quantiles_(std::move(quantiles)), impl_(quantiles_, @@ -48,7 +48,7 @@ class summary_t : public static_metric { summary_t(std::string name, std::string help, std::vector quantiles, std::map static_labels, - std::chrono::seconds max_age = std::chrono::seconds{60}) + std::chrono::seconds max_age = std::chrono::seconds{36000}) : static_metric(MetricType::Summary, std::move(name), std::move(help), std::move(static_labels)), quantiles_(std::move(quantiles)), @@ -146,7 +146,7 @@ class basic_dynamic_summary basic_dynamic_summary( std::string name, std::string help, std::vector quantiles, std::array labels_name, - std::chrono::milliseconds max_age = std::chrono::seconds{60}) + std::chrono::milliseconds max_age = std::chrono::seconds{36000}) : Base(MetricType::Summary, std::move(name), std::move(help), std::move(labels_name)), quantiles_(std::move(quantiles)), diff --git a/tests/test_metric.cpp b/tests/test_metric.cpp index 15552c9c..004eac61 100644 --- a/tests/test_metric.cpp +++ b/tests/test_metric.cpp @@ -1971,14 +1971,32 @@ 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"}); + 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") {