Skip to content

Commit

Permalink
clean metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Jun 18, 2024
1 parent 4daf5f8 commit 13493ce
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
68 changes: 67 additions & 1 deletion include/cinatra/ylt/metric/metric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "async_simple/coro/Lazy.h"
#include "async_simple/coro/SyncAwait.h"
#include "cinatra/cinatra_log_wrapper.hpp"
#include "ylt/coro_io/coro_io.hpp"

#ifdef CINATRA_ENABLE_METRIC_JSON
namespace iguana {
Expand Down Expand Up @@ -46,7 +47,10 @@ class metric_t {
public:
metric_t() = default;
metric_t(MetricType type, std::string name, std::string help)
: type_(type), name_(std::move(name)), help_(std::move(help)) {}
: type_(type),
name_(std::move(name)),
help_(std::move(help)),
metric_created_time_(std::chrono::system_clock::now()) {}
metric_t(MetricType type, std::string name, std::string help,
std::vector<std::string> labels_name)
: metric_t(type, std::move(name), std::move(help)) {
Expand All @@ -70,6 +74,8 @@ class metric_t {

MetricType metric_type() { return type_; }

auto get_created_time() { return metric_created_time_; }

std::string_view metric_name() {
switch (type_) {
case MetricType::Counter:
Expand Down Expand Up @@ -173,6 +179,7 @@ class metric_t {
std::vector<std::string> labels_name_; // read only
std::vector<std::string> labels_value_; // read only
bool use_atomic_ = false;
std::chrono::system_clock::time_point metric_created_time_{};
};

template <size_t ID = 0>
Expand Down Expand Up @@ -237,6 +244,14 @@ struct metric_manager_t {
return r;
}

static void set_metric_max_age(std::chrono::steady_clock::duration max_age,
std::chrono::steady_clock::duration
check_duration = std::chrono::minutes(5)) {
metric_max_age_ = max_age;
metric_check_duration_ = check_duration;
start_check();
}

static auto metric_map_static() { return metric_map_impl<false>(); }
static auto metric_map_dynamic() { return metric_map_impl<true>(); }

Expand Down Expand Up @@ -546,12 +561,63 @@ struct metric_manager_t {
return filtered_metrics;
}

static void check_impl() {
check_timer_->expires_after(metric_check_duration_);
check_timer_->async_wait([](std::error_code ec) {
if (ec) {
return;
}

check_clean_metrics();
check_impl();
});
}

static void start_check() {
if (has_start_check_metric_) {
return;
}

has_start_check_metric_ = true;

executor_ = coro_io::create_io_context_pool(1);

check_timer_ =
std::make_shared<coro_io::period_timer>(executor_->get_executor());

check_impl();
}

static void check_clean_metrics() {
auto cur_time = std::chrono::system_clock::now();
{
auto lock = get_lock<true>();
for (auto it = metric_map_.begin(); it != metric_map_.end();) {
if (cur_time - it->second->get_created_time() > metric_max_age_) {
metric_map_.erase(it++);
}
else {
++it;
}
}
}
}

static inline bool has_start_check_metric_ = false;
static inline std::shared_ptr<coro_io::period_timer> check_timer_ = nullptr;
static inline std::shared_ptr<coro_io::io_context_pool> executor_ = nullptr;

static inline std::mutex mtx_;
static inline std::map<std::string, std::shared_ptr<metric_t>> metric_map_;

static inline null_mutex_t null_mtx_;
static inline std::atomic_bool need_lock_ = true;
static inline std::once_flag flag_;

static inline std::chrono::steady_clock::duration metric_max_age_{
std::chrono::hours(24)};
static inline std::chrono::steady_clock::duration metric_check_duration_{
std::chrono::minutes(5)};
};

using default_metric_manager = metric_manager_t<0>;
Expand Down
24 changes: 24 additions & 0 deletions tests/test_metric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,30 @@ TEST_CASE("test summary with static labels") {
#endif
}

TEST_CASE("check clean metrics") {
using metric_mgr = metric_manager_t<11>;
metric_mgr::create_metric_dynamic<counter_t>("test_counter", "");
metric_mgr::create_metric_dynamic<counter_t>("test_counter2", "");
metric_mgr::create_metric_dynamic<histogram_t>(
"http_req_static_hist", "help",
std::vector<double>{5.23, 10.54, 20.0, 50.0, 100.0},
std::vector<std::string>{"method", "url"});

metric_mgr::create_metric_dynamic<summary_t>(
"http_req_static_summary", "help",
summary_t::Quantiles{
{0.5, 0.05}, {0.9, 0.01}, {0.95, 0.005}, {0.99, 0.001}},
std::vector<std::string>{"method", "url"});

CHECK(metric_mgr::metric_count_dynamic() == 4);
metric_mgr::set_metric_max_age(std::chrono::milliseconds(10),
std::chrono::milliseconds(10));

std::this_thread::sleep_for(std::chrono::milliseconds(100));

CHECK(metric_mgr::metric_count_dynamic() == 0);
}

DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4007)
int main(int argc, char** argv) { return doctest::Context(argc, argv).run(); }
DOCTEST_MSVC_SUPPRESS_WARNING_POP

0 comments on commit 13493ce

Please sign in to comment.