Skip to content

Commit

Permalink
improve metric
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Jun 18, 2024
1 parent 4f4b010 commit ffc7443
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 45 deletions.
12 changes: 12 additions & 0 deletions include/ylt/coro_io/io_context_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,18 @@ inline T &g_io_context_pool(
return *_g_io_context_pool;
}

template <typename T = io_context_pool>
inline std::shared_ptr<T> create_io_context_pool(
unsigned pool_size = std::thread::hardware_concurrency()) {
auto pool = std::make_shared<T>(pool_size);
std::thread thrd{[pool] {
pool->run();
}};
thrd.detach();

return pool;
}

template <typename T = io_context_pool>
inline T &g_block_io_context_pool(
unsigned pool_size = std::thread::hardware_concurrency()) {
Expand Down
15 changes: 12 additions & 3 deletions include/ylt/metric/counter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,12 @@ class counter_t : public metric_t {
label_val += value;
}
#else
label_val += value;
if constexpr (is_atomic) {
label_val.fetch_add(value, std::memory_order_relaxed);
}
else {
label_val += value;
}
#endif
} break;
case op_type_t::DEC:
Expand All @@ -278,9 +283,13 @@ class counter_t : public metric_t {
else {
label_val -= value;
}

#else
label_val -= value;
if constexpr (is_atomic) {
label_val.fetch_sub(value, std::memory_order_relaxed);
}
else {
label_val -= value;
}
#endif
break;
case op_type_t::SET:
Expand Down
72 changes: 71 additions & 1 deletion include/ylt/metric/metric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
#include "async_simple/coro/Lazy.h"
#include "async_simple/coro/SyncAwait.h"
#include "cinatra/cinatra_log_wrapper.hpp"
#if __has_include("ylt/coro_io/coro_io.hpp")
#include "ylt/coro_io/coro_io.hpp"
#else
#include "cinatra/ylt/coro_io/coro_io.hpp"
#endif

#ifdef CINATRA_ENABLE_METRIC_JSON
namespace iguana {
Expand Down Expand Up @@ -46,7 +51,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 +78,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 +183,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 +248,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 +565,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
Loading

0 comments on commit ffc7443

Please sign in to comment.