From ece1c3618ddbd3cdd02bb1d4a9be29eb55be8f3f Mon Sep 17 00:00:00 2001 From: qicosmos Date: Mon, 20 May 2024 10:10:03 +0800 Subject: [PATCH] fix and update (#581) --- example/main.cpp | 8 +------- include/cinatra/ylt/metric/counter.hpp | 10 ++++----- include/cinatra/ylt/metric/histogram.hpp | 13 ++++++------ include/cinatra/ylt/metric/metric.hpp | 11 +++++++++- include/cinatra/ylt/metric/summary.hpp | 26 +++++++++++++++++++----- tests/test_metric.cpp | 10 ++++++++- 6 files changed, 52 insertions(+), 26 deletions(-) diff --git a/example/main.cpp b/example/main.cpp index fc922a60..7dd4123b 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -458,14 +458,8 @@ void use_metric() { server.set_http_handler( "/metrics", [](coro_http_request &req, coro_http_response &resp) { - std::string str; - auto metrics = metric_t::collect(); - for (auto &m : metrics) { - m->serialize(str); - } - std::cout << str; resp.need_date_head(false); - resp.set_status_and_content(status_type::ok, std::move(str)); + resp.set_status_and_content(status_type::ok, metric_t::serialize()); }); server.sync_start(); } diff --git a/include/cinatra/ylt/metric/counter.hpp b/include/cinatra/ylt/metric/counter.hpp index ef3b0b50..775a1bcb 100644 --- a/include/cinatra/ylt/metric/counter.hpp +++ b/include/cinatra/ylt/metric/counter.hpp @@ -48,16 +48,14 @@ class counter_t : public metric_t { std::map, sample_t, std::less>> - values(bool need_lock = true) override { - if (need_lock) { - return value_map_; - } + values() override { std::lock_guard guard(mtx_); return value_map_; } void serialize(std::string &str) override { - if (value_map_.empty()) { + auto value_map = values(); + if (value_map.empty()) { return; } str.append("# HELP ").append(name_).append(" ").append(help_).append("\n"); @@ -66,7 +64,7 @@ class counter_t : public metric_t { .append(" ") .append(metric_name()) .append("\n"); - for (auto &[labels_value, sample] : value_map_) { + for (auto &[labels_value, sample] : value_map) { str.append(name_); if (labels_name_.empty()) { str.append(" "); diff --git a/include/cinatra/ylt/metric/histogram.hpp b/include/cinatra/ylt/metric/histogram.hpp index a0d58339..5fcf2802 100644 --- a/include/cinatra/ylt/metric/histogram.hpp +++ b/include/cinatra/ylt/metric/histogram.hpp @@ -57,13 +57,13 @@ class histogram_t : public metric_t { sum_->reset(); } - auto bucket_counts() { + auto get_bucket_counts() { std::lock_guard guard(mtx_); return bucket_counts_; } void serialize(std::string& str) override { - if (sum_->values(false).empty()) { + if (sum_->values().empty()) { return; } str.append("# HELP ").append(name_).append(" ").append(help_).append("\n"); @@ -73,9 +73,10 @@ class histogram_t : public metric_t { .append(metric_name()) .append("\n"); double count = 0; - for (size_t i = 0; i < bucket_counts_.size(); i++) { - auto counter = bucket_counts_[i]; - auto values = counter->values(false); + auto bucket_counts = get_bucket_counts(); + for (size_t i = 0; i < bucket_counts.size(); i++) { + auto counter = bucket_counts[i]; + auto values = counter->values(); for (auto& [labels_value, sample] : values) { str.append(name_).append("_bucket{"); if (i == bucket_boundaries_.size()) { @@ -98,7 +99,7 @@ class histogram_t : public metric_t { str.append(name_) .append("_sum ") - .append(std::to_string((sum_->values(false)[{}].value))) + .append(std::to_string((sum_->values()[{}].value))) .append("\n"); str.append(name_) diff --git a/include/cinatra/ylt/metric/metric.hpp b/include/cinatra/ylt/metric/metric.hpp index 4926c3b7..ceac6e28 100644 --- a/include/cinatra/ylt/metric/metric.hpp +++ b/include/cinatra/ylt/metric/metric.hpp @@ -56,7 +56,7 @@ class metric_t { virtual std::map, sample_t, std::less>> - values(bool need_lock = true) { + values() { return {}; } @@ -87,6 +87,15 @@ class metric_t { return metrics; } + static std::string serialize() { + std::string str; + auto metrics = metric_t::collect(); + for (auto& m : metrics) { + m->serialize(str); + } + return str; + } + static auto metric_map() { std::scoped_lock guard(mtx_); return metric_map_; diff --git a/include/cinatra/ylt/metric/summary.hpp b/include/cinatra/ylt/metric/summary.hpp index 6cdcd84f..2e272193 100644 --- a/include/cinatra/ylt/metric/summary.hpp +++ b/include/cinatra/ylt/metric/summary.hpp @@ -1,4 +1,6 @@ #pragma once +#include + #include "detail/time_window_quantiles.hpp" #include "metric.hpp" @@ -14,18 +16,29 @@ class summary_t : public metric_t { metric_t(MetricType::Summary, std::move(name), std::move(help)) {} void observe(double value) { - std::lock_guard lock(mutex_); - count_ += 1; + std::lock_guard lock(mutex_); sum_ += value; quantile_values_.insert(value); } + auto get_quantile_values() { + std::lock_guard lock(mutex_); + return quantile_values_; + } + + auto get_sum() { + std::lock_guard lock(mutex_); + return sum_; + } + void serialize(std::string& str) override { if (quantiles_.empty()) { return; } + auto quantile_values = get_quantile_values(); + str.append("# HELP ").append(name_).append(" ").append(help_).append("\n"); str.append("# TYPE ") .append(name_) @@ -37,11 +50,14 @@ class summary_t : public metric_t { str.append(name_); str.append("{quantile=\""); str.append(std::to_string(quantile.quantile)).append("\"} "); - str.append(std::to_string(quantile_values_.get(quantile.quantile))) + str.append(std::to_string(quantile_values.get(quantile.quantile))) .append("\n"); } - str.append(name_).append("_sum ").append(std::to_string(sum_)).append("\n"); + str.append(name_) + .append("_sum ") + .append(std::to_string(get_sum())) + .append("\n"); str.append(name_) .append("_count ") .append(std::to_string(count_)) @@ -51,7 +67,7 @@ class summary_t : public metric_t { private: Quantiles quantiles_; mutable std::mutex mutex_; - std::uint64_t count_{}; + std::atomic count_{}; double sum_{}; TimeWindowQuantiles quantile_values_; }; diff --git a/tests/test_metric.cpp b/tests/test_metric.cpp index db0caabb..ae37ec7a 100644 --- a/tests/test_metric.cpp +++ b/tests/test_metric.cpp @@ -1,4 +1,7 @@ +#include + #include "cinatra/ylt/metric/gauge.hpp" +#include "cinatra/ylt/metric/metric.hpp" #define DOCTEST_CONFIG_IMPLEMENT #include @@ -110,7 +113,7 @@ TEST_CASE("test guage") { TEST_CASE("test histogram") { histogram_t h("test", "help", {5.0, 10.0, 20.0, 50.0, 100.0}); h.observe(23); - auto counts = h.bucket_counts(); + auto counts = h.get_bucket_counts(); CHECK(counts[3]->values()[{}].value == 1); h.observe(42); CHECK(counts[3]->values()[{}].value == 2); @@ -169,6 +172,11 @@ TEST_CASE("test register metric") { CHECK(map["get_count"]->values()[{}].value == 1); CHECK(map["get_guage_count"]->values()[{}].value == 1); + auto s = metric_t::serialize(); + std::cout << s << "\n"; + CHECK(s.find("get_count 1") != std::string::npos); + CHECK(s.find("get_guage_count 1") != std::string::npos); + metric_t::remove_metric("get_count"); CHECK(metric_t::metric_count() == 1); }