Skip to content

Commit

Permalink
improve counter
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Jul 31, 2024
1 parent 48e1f28 commit 5826e94
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
10 changes: 10 additions & 0 deletions include/ylt/metric/counter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,16 @@ class basic_counter : public metric_t {
}
}

template <typename T>
void set_value_static(T &label_val, value_type value, op_type_t type) {
set_value<true>(label_val, value, type);
}

template <typename T>
void set_value_dynamic(T &label_val, value_type value, op_type_t type) {
set_value<false>(label_val, value, type);
}

template <bool is_atomic = false, typename T>
void set_value(T &label_val, value_type value, op_type_t type) {
switch (type) {
Expand Down
10 changes: 6 additions & 4 deletions include/ylt/metric/gauge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class basic_gauge : public basic_counter<value_type> {
using basic_counter<value_type>::value_map_;
using basic_counter<value_type>::mtx_;
using basic_counter<value_type>::stat_metric;
using basic_counter<value_type>::set_value_static;
using basic_counter<value_type>::set_value_dynamic;

public:
basic_gauge(std::string name, std::string help, size_t dupli_count = 2)
Expand Down Expand Up @@ -56,14 +58,14 @@ class basic_gauge : public basic_counter<value_type> {
throw std::invalid_argument(
"the given labels_value is not match with origin labels_value");
}
set_value<true>(atomic_value_map_[labels_value].local_value(), value,
op_type_t::DEC);
set_value_static(atomic_value_map_[labels_value].local_value(), value,
op_type_t::DEC);
}
else {
std::lock_guard lock(mtx_);
stat_metric(labels_value);
set_value<false>(value_map_[labels_value].local_value(), value,
op_type_t::DEC);
set_value_dynamic(value_map_[labels_value].local_value(), value,
op_type_t::DEC);
}
}
};
Expand Down

0 comments on commit 5826e94

Please sign in to comment.