Skip to content

Commit

Permalink
histgram expired (#676)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Dec 18, 2024
1 parent 8eb4847 commit a297836
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
1 change: 1 addition & 0 deletions include/cinatra/ylt/coro_io/load_blancer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 10 additions & 8 deletions include/cinatra/ylt/metric/dynamic_metric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,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 @@ -111,14 +120,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/cinatra/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
6 changes: 3 additions & 3 deletions include/cinatra/ylt/metric/summary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> 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_,
Expand All @@ -48,7 +48,7 @@ class summary_t : public static_metric {

summary_t(std::string name, std::string help, std::vector<double> quantiles,
std::map<std::string, std::string> 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)),
Expand Down Expand Up @@ -146,7 +146,7 @@ class basic_dynamic_summary
basic_dynamic_summary(
std::string name, std::string help, std::vector<double> quantiles,
std::array<std::string, N> 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)),
Expand Down
18 changes: 18 additions & 0 deletions tests/test_metric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1971,14 +1971,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 a297836

Please sign in to comment.