Skip to content

Commit

Permalink
fix stat system metric (#752)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Aug 14, 2024
1 parent dfcb5d5 commit 7b47305
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
10 changes: 6 additions & 4 deletions include/ylt/metric/counter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<value_type>(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());
}
Expand All @@ -227,14 +228,15 @@ class basic_dynamic_counter : public dynamic_metric {

value_type update(const std::array<std::string, N> &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<value_type>(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());
}
Expand Down
5 changes: 3 additions & 2 deletions include/ylt/metric/gauge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@ class basic_dynamic_gauge : public basic_dynamic_counter<value_type, N> {
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<value_type>(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());
}
Expand Down
2 changes: 1 addition & 1 deletion include/ylt/metric/metric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ class dynamic_metric : public metric_t {
using metric_t::metric_t;
};

inline thread_local_value<int64_t> g_user_metric_label_count{2};
inline auto g_user_metric_label_count = new thread_local_value<int64_t>(2);
inline std::atomic<int64_t> g_summary_failed_count = 0;
inline std::atomic<int64_t> g_user_metric_count = 0;

Expand Down
8 changes: 4 additions & 4 deletions include/ylt/metric/metric_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ class static_metric_manager {
static_metric_manager& operator=(static_metric_manager&&) = delete;

static static_metric_manager<Tag>& instance() {
static static_metric_manager<Tag> inst;
return inst;
static auto* inst = new static_metric_manager<Tag>();
return *inst;
}

template <typename T, typename... Args>
Expand Down Expand Up @@ -279,8 +279,8 @@ class dynamic_metric_manager {
dynamic_metric_manager& operator=(dynamic_metric_manager&&) = delete;

static dynamic_metric_manager<Tag>& instance() {
static dynamic_metric_manager<Tag> inst;
return inst;
static auto* inst = new dynamic_metric_manager<Tag>();
return *inst;
}

template <typename T, typename... Args>
Expand Down
9 changes: 7 additions & 2 deletions include/ylt/metric/system_metric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ inline void stat_metric() {
static auto user_metric_label_count =
system_metric_manager::instance().get_metric_static<gauge_t>(
"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<gauge_t>(
Expand All @@ -379,7 +379,12 @@ inline void ylt_stat() {
stat_metric();
}

inline void start_stat(std::shared_ptr<coro_io::period_timer> timer) {
inline void start_stat(std::weak_ptr<coro_io::period_timer> 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) {
Expand Down

0 comments on commit 7b47305

Please sign in to comment.