Skip to content

Commit

Permalink
simplify usage
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Aug 6, 2024
1 parent aa0f32a commit 96f5901
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 3 deletions.
15 changes: 13 additions & 2 deletions include/ylt/metric/counter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,13 @@ class basic_counter : public metric_t {
virtual ~basic_counter() { g_user_metric_count--; }

value_type value() {
if (!labels_value_.empty()) {
value_type val = atomic_value_map_[labels_value_].value();
return val;
}

if (!labels_name_.empty()) {
throw std::invalid_argument("it's not a default value counter!");
throw std::invalid_argument("it's not a dynamic counter!");
}
return default_label_value_.value();
}
Expand Down Expand Up @@ -168,8 +173,14 @@ class basic_counter : public metric_t {
#endif

void inc(value_type val = 1) {
if (!labels_value_.empty()) {
set_value<true>(atomic_value_map_[labels_value_].local_value(), val,
op_type_t::INC);
return;
}

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

if (val < 0) {
Expand Down
8 changes: 7 additions & 1 deletion include/ylt/metric/gauge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@ class basic_gauge : public basic_counter<value_type> {
}

void dec(value_type value = 1) {
if (!labels_value_.empty()) {
set_value_static(atomic_value_map_[labels_value_].local_value(), value,
op_type_t::DEC);
return;
}

if (!labels_name_.empty()) {
throw std::bad_function_call();
throw std::invalid_argument("it's not a dynamic counter!");
}
#ifdef __APPLE__
if constexpr (std::is_floating_point_v<value_type>) {
Expand Down
5 changes: 5 additions & 0 deletions include/ylt/metric/histogram.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ class basic_histogram : public metric_t {
}

void observe(value_type value) {
if (!labels_value_.empty()) {
observe(labels_value_, value);
return;
}

if (!use_atomic_ || !labels_name_.empty()) {
throw std::invalid_argument("not a default label metric");
}
Expand Down
9 changes: 9 additions & 0 deletions include/ylt/metric/summary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ class summary_t : public metric_t {
};

void observe(double value) {
if (!labels_value_.empty()) {
observe(labels_value_, value);
return;
}

if (!labels_name_.empty()) {
throw std::invalid_argument("not a default label metric");
}
Expand Down Expand Up @@ -149,6 +154,10 @@ class summary_t : public metric_t {

async_simple::coro::Lazy<std::vector<double>> get_rates(double &sum,
uint64_t &count) {
if (!labels_value_.empty()) {
co_return co_await get_rates(labels_value_, sum, count);
}

std::vector<double> vec;
if (quantiles_.empty()) {
co_return std::vector<double>{};
Expand Down
50 changes: 50 additions & 0 deletions src/metric/tests/test_metric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@ TEST_CASE("test with atomic") {
std::cout << str1;
CHECK(str.find("} 10") != std::string::npos);
CHECK(str1.find("} 1") != std::string::npos);

{
gauge_t g("get_qps", "get qps",
std::map<std::string, std::string>{{"method", "POST"},
{"url", "/test"}});
g.inc();
g.inc({"POST", "/test"});
CHECK(g.value() == 2);
CHECK(g.value({"POST", "/test"}) == 2);
g.dec();
CHECK(g.value() == 1);
CHECK(g.value({"POST", "/test"}) == 1);
g.dec({"POST", "/test"});
CHECK(g.value() == 0);
CHECK(g.value({"POST", "/test"}) == 0);
}
}

TEST_CASE("test counter with dynamic labels value") {
Expand Down Expand Up @@ -585,6 +601,20 @@ TEST_CASE("test get metric by static labels and label") {
metric_mgr::instance().get_metric_by_label_static({"method", "GET"});
CHECK(vec.size() == 4);

{
using metric_mgr2 = metric_manager_t<test_id_t<19>>;
auto s2 = metric_mgr2::instance().create_metric_static<summary_t>(
"http_req_static_summary2", "help",
summary_t::Quantiles{
{0.5, 0.05}, {0.9, 0.01}, {0.95, 0.005}, {0.99, 0.001}},
std::map<std::string, std::string>{{"method", "GET"}, {"url", "/"}});
s2->observe(23);

auto vec =
metric_mgr2::instance().get_metric_by_label_static({"method", "GET"});
CHECK(vec.size() == 1);
}

vec = metric_mgr::instance().get_metric_by_label_static({"url", "/"});
CHECK(vec.size() == 4);

Expand Down Expand Up @@ -723,6 +753,23 @@ TEST_CASE("test histogram serialize with dynamic labels") {
#endif
}

TEST_CASE("test histogram serialize with static labels default") {
histogram_t h(
"test", "help", {5.23, 10.54, 20.0, 50.0, 100.0},
std::map<std::string, std::string>{{"method", "GET"}, {"url", "/"}});
h.observe(23);
auto counts = h.get_bucket_counts();
CHECK(counts[3]->value({"GET", "/"}) == 1);
h.observe(42);
CHECK(counts[3]->value({"GET", "/"}) == 2);
h.observe({"GET", "/"}, 60);
CHECK(counts[4]->value({"GET", "/"}) == 1);
h.observe({"GET", "/"}, 120);
CHECK(counts[5]->value({"GET", "/"}) == 1);
h.observe(1);
CHECK(counts[0]->value({"GET", "/"}) == 1);
}

TEST_CASE("test histogram serialize with static labels") {
histogram_t h(
"test", "help", {5.23, 10.54, 20.0, 50.0, 100.0},
Expand Down Expand Up @@ -805,6 +852,9 @@ TEST_CASE("test summary with static labels") {
summary.get_rates({"GET", "/"}, sum, count));
std::cout << rates.size() << "\n";

auto rates1 = async_simple::coro::syncAwait(summary.get_rates(sum, count));
CHECK(rates == rates1);

std::string str;
async_simple::coro::syncAwait(summary.serialize_async(str));
std::cout << str;
Expand Down

0 comments on commit 96f5901

Please sign in to comment.