Skip to content

Commit

Permalink
fix and update (#581)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored May 20, 2024
1 parent a1ca474 commit ece1c36
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 26 deletions.
8 changes: 1 addition & 7 deletions example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,14 +458,8 @@ void use_metric() {

server.set_http_handler<GET, POST>(
"/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();
}
Expand Down
10 changes: 4 additions & 6 deletions include/cinatra/ylt/metric/counter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,14 @@ class counter_t : public metric_t {

std::map<std::vector<std::string>, sample_t,
std::less<std::vector<std::string>>>
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");
Expand All @@ -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(" ");
Expand Down
13 changes: 7 additions & 6 deletions include/cinatra/ylt/metric/histogram.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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()) {
Expand All @@ -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_)
Expand Down
11 changes: 10 additions & 1 deletion include/cinatra/ylt/metric/metric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class metric_t {

virtual std::map<std::vector<std::string>, sample_t,
std::less<std::vector<std::string>>>
values(bool need_lock = true) {
values() {
return {};
}

Expand Down Expand Up @@ -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_;
Expand Down
26 changes: 21 additions & 5 deletions include/cinatra/ylt/metric/summary.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#pragma once
#include <atomic>

#include "detail/time_window_quantiles.hpp"
#include "metric.hpp"

Expand All @@ -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<std::mutex> lock(mutex_);

count_ += 1;
std::lock_guard<std::mutex> lock(mutex_);
sum_ += value;
quantile_values_.insert(value);
}

auto get_quantile_values() {
std::lock_guard<std::mutex> lock(mutex_);
return quantile_values_;
}

auto get_sum() {
std::lock_guard<std::mutex> 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_)
Expand All @@ -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_))
Expand All @@ -51,7 +67,7 @@ class summary_t : public metric_t {
private:
Quantiles quantiles_;
mutable std::mutex mutex_;
std::uint64_t count_{};
std::atomic<std::uint64_t> count_{};
double sum_{};
TimeWindowQuantiles quantile_values_;
};
Expand Down
10 changes: 9 additions & 1 deletion tests/test_metric.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include <string>

#include "cinatra/ylt/metric/gauge.hpp"
#include "cinatra/ylt/metric/metric.hpp"
#define DOCTEST_CONFIG_IMPLEMENT
#include <random>

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit ece1c36

Please sign in to comment.