Skip to content

Commit

Permalink
[metric][improve]Histgram label expire (#857)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Dec 18, 2024
1 parent 712f1c7 commit f3e7189
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
18 changes: 10 additions & 8 deletions include/ylt/metric/dynamic_metric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ class dynamic_metric_impl : public dynamic_metric {
return map_.template copy<std::shared_ptr<metric_pair>>();
}

void clean_expired_label() override {
erase_if([now = std::chrono::steady_clock::now()](auto& pair) mutable {
bool r = std::chrono::duration_cast<std::chrono::seconds>(
now - pair.second->get_created_time())
.count() >= ylt_label_max_age.count();
return r;
});
}

protected:
template <typename Key, typename... Args>
std::pair<std::shared_ptr<metric_pair>, bool> try_emplace(Key&& key,
Expand All @@ -107,14 +116,7 @@ class dynamic_metric_impl : public dynamic_metric {
},
std::forward<Key>(key), std::forward<Args>(args)...);
}
void clean_expired_label() override {
erase_if([now = std::chrono::steady_clock::now()](auto& pair) mutable {
bool r = std::chrono::duration_cast<std::chrono::seconds>(
now - pair.second->get_created_time())
.count() >= ylt_label_max_age.count();
return r;
});
}

std::shared_ptr<metric_pair> find(std::span<const std::string, N> key) const {
return map_.find(key);
}
Expand Down
9 changes: 9 additions & 0 deletions include/ylt/metric/histogram.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()) {
Expand Down
18 changes: 18 additions & 0 deletions src/metric/tests/test_metric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1946,14 +1946,32 @@ TEST_CASE("test metric manager clean expired label") {
auto& inst = dynamic_metric_manager<test_tag>::instance();
auto pair = inst.create_metric_dynamic<dynamic_counter_1t>(
std::string("some_counter"), "", std::array<std::string, 1>{"url"});
auto summary = std::make_shared<basic_dynamic_summary<2>>(
std::string("test_summary"), std::string("summary help"),
std::vector<double>{0.5, 0.9, 0.95, 0.99},
std::array<std::string, 2>{"method", "url"});
auto h = std::make_shared<dynamic_histogram_t>(
std::string("test"), std::string("help"),
std::vector<double>{5.23, 10.54, 20.0, 50.0, 100.0},
std::array<std::string, 2>{"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") {
Expand Down

0 comments on commit f3e7189

Please sign in to comment.