Skip to content

Commit

Permalink
use thread local
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Aug 12, 2024
1 parent c465b6c commit 3af0f87
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
5 changes: 4 additions & 1 deletion include/ylt/metric/counter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class basic_dynamic_counter : public dynamic_metric {
auto [it, r] = value_map_.try_emplace(
labels_value, thread_local_value<value_type>(dupli_count_));
if (r) {
g_user_metric_label_count++; // TODO: use thread local
g_user_metric_label_count.local_value()++;
}
set_value(it->second.local_value(), value, op_type_t::INC);
}
Expand All @@ -230,6 +230,9 @@ class basic_dynamic_counter : public dynamic_metric {
}
auto [it, r] = value_map_.try_emplace(
labels_value, thread_local_value<value_type>(dupli_count_));
if (r) {
g_user_metric_label_count.local_value()++;
}
return it->second.update(value);
}

Expand Down
2 changes: 1 addition & 1 deletion include/ylt/metric/gauge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class basic_dynamic_gauge : public basic_dynamic_counter<value_type, N> {
auto [it, r] = value_map_.try_emplace(
labels_value, thread_local_value<value_type>(dupli_count_));
if (r) {
g_user_metric_label_count++; // TODO: use thread local
g_user_metric_label_count.local_value()++;
}

set_value(it->second.local_value(), value, op_type_t::DEC);
Expand Down
3 changes: 2 additions & 1 deletion include/ylt/metric/metric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "async_simple/coro/Lazy.h"
#include "async_simple/coro/SyncAwait.h"
#include "cinatra/cinatra_log_wrapper.hpp"
#include "thread_local_value.hpp"
#if __has_include("ylt/coro_io/coro_io.hpp")
#include "ylt/coro_io/coro_io.hpp"
#else
Expand Down Expand Up @@ -221,7 +222,7 @@ class dynamic_metric : public metric_t {
using metric_t::metric_t;
};

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

Expand Down
2 changes: 1 addition & 1 deletion 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);
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 Down

0 comments on commit 3af0f87

Please sign in to comment.