Skip to content

Commit

Permalink
add check
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Aug 5, 2024
1 parent 6d6f3e5 commit 4e96464
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
24 changes: 21 additions & 3 deletions include/ylt/metric/counter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class basic_counter : public metric_t {
// default, no labels, only contains an atomic value.
basic_counter(std::string name, std::string help, size_t dupli_count = 2)
: metric_t(MetricType::Counter, std::move(name), std::move(help)),
dupli_count_(dupli_count) {
dupli_count_(dupli_count),
default_label_value_(dupli_count) {
use_atomic_ = true;
g_user_metric_count++;
}
Expand Down Expand Up @@ -59,9 +60,18 @@ class basic_counter : public metric_t {

virtual ~basic_counter() { g_user_metric_count--; }

value_type value() { return default_label_value_.value(); }
value_type value() {
if (!labels_name_.empty()) {
throw std::invalid_argument("it's not a default value counter!");
}
return default_label_value_.value();
}

value_type value(const std::vector<std::string> &labels_value) {
if (labels_name_.empty()) {
throw std::invalid_argument("it is a default value counter");
}

if (use_atomic_) {
value_type val = atomic_value_map_[labels_value].value();
return val;
Expand Down Expand Up @@ -158,6 +168,10 @@ class basic_counter : public metric_t {
#endif

void inc(value_type val = 1) {
if (!labels_name_.empty()) {
throw std::invalid_argument("it's not a default value counter!");
}

if (val < 0) {
throw std::invalid_argument("the value is less than zero");
}
Expand All @@ -179,6 +193,10 @@ class basic_counter : public metric_t {
return;
}

if (labels_name_.empty()) {
throw std::invalid_argument("it's a default value counter!");
}

validate(labels_value, value);
if (use_atomic_) {
if (labels_value != labels_value_) {
Expand Down Expand Up @@ -359,7 +377,7 @@ class basic_counter : public metric_t {
}

metric_hash_map<thread_local_value<value_type>> atomic_value_map_;
thread_local_value<value_type> default_label_value_ = {10};
thread_local_value<value_type> default_label_value_;

std::mutex mtx_;
metric_hash_map<thread_local_value<value_type>> value_map_;
Expand Down
4 changes: 4 additions & 0 deletions include/ylt/metric/gauge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class basic_gauge : public basic_counter<value_type> {
using basic_counter<value_type>::validate;
using metric_t::use_atomic_;
using basic_counter<value_type>::default_label_value_;
using metric_t::labels_name_;
using metric_t::labels_value_;
using basic_counter<value_type>::atomic_value_map_;
using basic_counter<value_type>::value_map_;
Expand Down Expand Up @@ -40,6 +41,9 @@ class basic_gauge : public basic_counter<value_type> {
}

void dec(value_type value = 1) {
if (!labels_name_.empty()) {
throw std::bad_function_call();
}
#ifdef __APPLE__
if constexpr (std::is_floating_point_v<value_type>) {
mac_os_atomic_fetch_sub(&default_label_value_.local_value(), value);
Expand Down

0 comments on commit 4e96464

Please sign in to comment.