diff --git a/include/ylt/metric/counter.hpp b/include/ylt/metric/counter.hpp index 95a2f3e41..246b8ed47 100644 --- a/include/ylt/metric/counter.hpp +++ b/include/ylt/metric/counter.hpp @@ -210,14 +210,15 @@ class basic_dynamic_counter : public dynamic_metric { return; } - std::lock_guard lock(mtx_); + std::unique_lock lock(mtx_); if (value_map_.size() > ylt_label_capacity) { return; } auto [it, r] = value_map_.try_emplace( labels_value, thread_local_value(dupli_count_)); + lock.unlock(); if (r) { - g_user_metric_label_count.local_value()++; + g_user_metric_label_count->local_value()++; if (ylt_label_max_age.count()) { it->second.set_created_time(std::chrono::system_clock::now()); } @@ -227,14 +228,15 @@ class basic_dynamic_counter : public dynamic_metric { value_type update(const std::array &labels_value, value_type value) { - std::lock_guard lock(mtx_); + std::unique_lock lock(mtx_); if (value_map_.size() > ylt_label_capacity) { return value_type{}; } auto [it, r] = value_map_.try_emplace( labels_value, thread_local_value(dupli_count_)); + lock.unlock(); if (r) { - g_user_metric_label_count.local_value()++; + g_user_metric_label_count->local_value()++; if (ylt_label_max_age.count()) { it->second.set_created_time(std::chrono::system_clock::now()); } diff --git a/include/ylt/metric/gauge.hpp b/include/ylt/metric/gauge.hpp index cd2ba69a4..c68d46de6 100644 --- a/include/ylt/metric/gauge.hpp +++ b/include/ylt/metric/gauge.hpp @@ -66,14 +66,15 @@ class basic_dynamic_gauge : public basic_dynamic_counter { return; } - std::lock_guard lock(mtx_); + std::unique_lock lock(mtx_); if (value_map_.size() > ylt_label_capacity) { return; } auto [it, r] = value_map_.try_emplace( labels_value, thread_local_value(dupli_count_)); + lock.unlock(); if (r) { - g_user_metric_label_count.local_value()++; + g_user_metric_label_count->local_value()++; if (ylt_label_max_age.count()) { it->second.set_created_time(std::chrono::system_clock::now()); } diff --git a/include/ylt/metric/metric.hpp b/include/ylt/metric/metric.hpp index 1ee5a18eb..f37de8cab 100644 --- a/include/ylt/metric/metric.hpp +++ b/include/ylt/metric/metric.hpp @@ -225,7 +225,7 @@ class dynamic_metric : public metric_t { using metric_t::metric_t; }; -inline thread_local_value g_user_metric_label_count{2}; +inline auto g_user_metric_label_count = new thread_local_value(2); inline std::atomic g_summary_failed_count = 0; inline std::atomic g_user_metric_count = 0; diff --git a/include/ylt/metric/metric_manager.hpp b/include/ylt/metric/metric_manager.hpp index 5ae16c2dd..8a48d5f12 100644 --- a/include/ylt/metric/metric_manager.hpp +++ b/include/ylt/metric/metric_manager.hpp @@ -169,8 +169,8 @@ class static_metric_manager { static_metric_manager& operator=(static_metric_manager&&) = delete; static static_metric_manager& instance() { - static static_metric_manager inst; - return inst; + static auto* inst = new static_metric_manager(); + return *inst; } template @@ -279,8 +279,8 @@ class dynamic_metric_manager { dynamic_metric_manager& operator=(dynamic_metric_manager&&) = delete; static dynamic_metric_manager& instance() { - static dynamic_metric_manager inst; - return inst; + static auto* inst = new dynamic_metric_manager(); + return *inst; } template diff --git a/include/ylt/metric/system_metric.hpp b/include/ylt/metric/system_metric.hpp index dffbab3b3..8cd305f9c 100644 --- a/include/ylt/metric/system_metric.hpp +++ b/include/ylt/metric/system_metric.hpp @@ -362,7 +362,7 @@ inline void stat_metric() { static auto user_metric_label_count = system_metric_manager::instance().get_metric_static( "ylt_user_metric_labels"); - user_metric_label_count->update(g_user_metric_label_count.value()); + user_metric_label_count->update(g_user_metric_label_count->value()); static auto ylt_summary_failed_count = system_metric_manager::instance().get_metric_static( @@ -379,7 +379,12 @@ inline void ylt_stat() { stat_metric(); } -inline void start_stat(std::shared_ptr timer) { +inline void start_stat(std::weak_ptr weak) { + auto timer = weak.lock(); + if (timer == nullptr) { + return; + } + timer->expires_after(std::chrono::seconds(1)); timer->async_wait([timer](std::error_code ec) { if (ec) {