Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serialize metric to json #598

Merged
merged 21 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmake/develop.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ if(ENABLE_SANITIZER AND NOT MSVC)
endif()
endif()

option(ENABLE_METRIC_JSON "Enable serialize metric to json" OFF)
if(ENABLE_METRIC_JSON)
add_definitions(-DCINATRA_ENABLE_METRIC_JSON)
message(STATUS "Enable serialize metric to json")
endif()

SET(ENABLE_GZIP OFF)
SET(ENABLE_SSL OFF)
SET(ENABLE_CLIENT_SSL OFF)
Expand Down
3 changes: 3 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ set(CINATRA_EXAMPLE

include_directories(../include)
include_directories(../include/cinatra)
if(ENABLE_METRIC_JSON)
include_directories(../iguana)
endif()

add_executable(${project_name} ${CINATRA_EXAMPLE})
target_compile_definitions(${project_name} PRIVATE ASYNC_SIMPLE_HAS_NOT_AIO)
Expand Down
6 changes: 3 additions & 3 deletions example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "metric_conf.hpp"

using namespace cinatra;
using namespace ylt;
using namespace ylt::metric;
using namespace std::chrono_literals;

void create_file(std::string filename, size_t file_size = 64) {
Expand Down Expand Up @@ -429,7 +429,7 @@ async_simple::coro::Lazy<void> basic_usage() {
}

void use_metric() {
using namespace ylt;
using namespace ylt::metric;
auto c =
std::make_shared<counter_t>("request_count", "request count",
std::vector<std::string>{"method", "url"});
Expand Down Expand Up @@ -537,7 +537,7 @@ void metrics_example() {
"/", [&](coro_http_request &req, coro_http_response &resp) {
resp.set_status_and_content(status_type::ok, "hello world");
});
server.use_metrics();
server.use_metrics(true, "/metrics");
server.sync_start();
}

Expand Down
21 changes: 16 additions & 5 deletions include/cinatra/coro_http_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,23 @@ class coro_http_server {
}
}

void use_metrics(std::string url_path = "/metrics") {
void use_metrics(bool enable_json = false,
std::string url_path = "/metrics") {
init_metrics();
set_http_handler<http_method::GET>(
url_path, [](coro_http_request &req, coro_http_response &res) {
std::string str = async_simple::coro::syncAwait(
ylt::default_metric_manager::serialize_static());
url_path,
[enable_json](coro_http_request &req, coro_http_response &res) {
std::string str;
#ifdef CINATRA_ENABLE_METRIC_JSON
if (enable_json) {
str =
ylt::metric::default_metric_manager::serialize_to_json_static();
res.set_content_type<resp_content_type::json>();
}
else
#endif
str = ylt::metric::default_metric_manager::serialize_static();

res.set_status_and_content(status_type::ok, std::move(str));
});
}
Expand Down Expand Up @@ -900,7 +911,7 @@ class coro_http_server {

private:
void init_metrics() {
using namespace ylt;
using namespace ylt::metric;

cinatra_metric_conf::enable_metric = true;
default_metric_manager::create_metric_static<counter_t>(
Expand Down
40 changes: 16 additions & 24 deletions include/cinatra/metric_conf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ struct cinatra_metric_conf {
return;
}

static auto m =
ylt::default_metric_manager::get_metric_static<ylt::counter_t>(
server_total_req);
static auto m = ylt::metric::default_metric_manager::get_metric_static<
ylt::metric::counter_t>(server_total_req);
if (m == nullptr) {
return;
}
Expand All @@ -38,9 +37,8 @@ struct cinatra_metric_conf {
if (!enable_metric) {
return;
}
static auto m =
ylt::default_metric_manager::get_metric_static<ylt::counter_t>(
server_failed_req);
static auto m = ylt::metric::default_metric_manager::get_metric_static<
ylt::metric::counter_t>(server_failed_req);
if (m == nullptr) {
return;
}
Expand All @@ -51,9 +49,8 @@ struct cinatra_metric_conf {
if (!enable_metric) {
return;
}
static auto m =
ylt::default_metric_manager::get_metric_static<ylt::gauge_t>(
server_total_fd);
static auto m = ylt::metric::default_metric_manager::get_metric_static<
ylt::metric::gauge_t>(server_total_fd);
if (m == nullptr) {
return;
}
Expand All @@ -64,9 +61,8 @@ struct cinatra_metric_conf {
if (!enable_metric) {
return;
}
static auto m =
ylt::default_metric_manager::get_metric_static<ylt::gauge_t>(
server_total_fd);
static auto m = ylt::metric::default_metric_manager::get_metric_static<
ylt::metric::gauge_t>(server_total_fd);
if (m == nullptr) {
return;
}
Expand All @@ -77,9 +73,8 @@ struct cinatra_metric_conf {
if (!enable_metric) {
return;
}
static auto m =
ylt::default_metric_manager::get_metric_static<ylt::counter_t>(
server_total_recv_bytes);
static auto m = ylt::metric::default_metric_manager::get_metric_static<
ylt::metric::counter_t>(server_total_recv_bytes);
if (m == nullptr) {
return;
}
Expand All @@ -90,9 +85,8 @@ struct cinatra_metric_conf {
if (!enable_metric) {
return;
}
static auto m =
ylt::default_metric_manager::get_metric_static<ylt::counter_t>(
server_total_send_bytes);
static auto m = ylt::metric::default_metric_manager::get_metric_static<
ylt::metric::counter_t>(server_total_send_bytes);
if (m == nullptr) {
return;
}
Expand All @@ -103,9 +97,8 @@ struct cinatra_metric_conf {
if (!enable_metric) {
return;
}
static auto m =
ylt::default_metric_manager::get_metric_static<ylt::histogram_t>(
server_req_latency);
static auto m = ylt::metric::default_metric_manager::get_metric_static<
ylt::metric::histogram_t>(server_req_latency);
if (m == nullptr) {
return;
}
Expand All @@ -116,9 +109,8 @@ struct cinatra_metric_conf {
if (!enable_metric) {
return;
}
static auto m =
ylt::default_metric_manager::get_metric_static<ylt::histogram_t>(
server_read_latency);
static auto m = ylt::metric::default_metric_manager::get_metric_static<
ylt::metric::histogram_t>(server_read_latency);
if (m == nullptr) {
return;
}
Expand Down
70 changes: 57 additions & 13 deletions include/cinatra/ylt/metric/counter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@

#include "metric.hpp"

namespace ylt {
namespace ylt::metric {
enum class op_type_t { INC, DEC, SET };
struct counter_sample {
op_type_t op_type;
std::vector<std::string> labels_value;
double value;

#ifdef CINATRA_ENABLE_METRIC_JSON
struct json_counter_metric_t {
std::unordered_multimap<std::string, std::string> labels;
int64_t value;
};
REFLECTION(json_counter_metric_t, labels, value);
struct json_counter_t {
std::string name;
std::string help;
std::string type;
std::vector<json_counter_metric_t> metrics;
};
REFLECTION(json_counter_t, name, help, type, metrics);
#endif

class counter_t : public metric_t {
public:
Expand All @@ -23,12 +33,8 @@ class counter_t : public metric_t {
// static labels value, contains a map with atomic value.
counter_t(std::string name, std::string help,
std::map<std::string, std::string> labels)
: metric_t(MetricType::Counter, std::move(name), std::move(help)) {
for (auto &[k, v] : labels) {
labels_name_.push_back(k);
labels_value_.push_back(v);
}

: metric_t(MetricType::Counter, std::move(name), std::move(help),
std::move(labels)) {
atomic_value_map_.emplace(labels_value_, 0);
use_atomic_ = true;
}
Expand Down Expand Up @@ -56,7 +62,7 @@ class counter_t : public metric_t {

std::map<std::vector<std::string>, double,
std::less<std::vector<std::string>>>
value_map() {
value_map() override {
std::map<std::vector<std::string>, double,
std::less<std::vector<std::string>>>
map;
Expand Down Expand Up @@ -97,6 +103,44 @@ class counter_t : public metric_t {
}
}

#ifdef CINATRA_ENABLE_METRIC_JSON
void serialize_to_json(std::string &str) override {
std::string s;
if (labels_name_.empty()) {
if (default_lable_value_ == 0) {
return;
}
json_counter_t counter{name_, help_, std::string(metric_name())};
int64_t value = default_lable_value_;
counter.metrics.push_back({{}, value});
iguana::to_json(counter, str);
return;
}

json_counter_t counter{name_, help_, std::string(metric_name())};
if (use_atomic_) {
to_json(counter, atomic_value_map_, str);
}
else {
to_json(counter, value_map_, str);
}
}

template <typename T>
void to_json(json_counter_t &counter, T &map, std::string &str) {
for (auto &[k, v] : map) {
json_counter_metric_t metric;
size_t index = 0;
for (auto &label_value : k) {
metric.labels.emplace(labels_name_[index++], label_value);
}
metric.value = (int64_t)v;
counter.metrics.push_back(std::move(metric));
}
iguana::to_json(counter, str);
}
#endif

void inc(double val = 1) {
if (val < 0) {
throw std::invalid_argument("the value is less than zero");
Expand Down Expand Up @@ -255,4 +299,4 @@ class counter_t : public metric_t {
std::less<std::vector<std::string>>>
value_map_;
};
} // namespace ylt
} // namespace ylt::metric
4 changes: 2 additions & 2 deletions include/cinatra/ylt/metric/detail/ckms_quantiles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

// https://github.com/jupp0r/prometheus-cpp/blob/master/core/include/prometheus/detail/ckms_quantiles.h

namespace ylt {
namespace ylt::metric {
class CKMSQuantiles {
public:
struct Quantile {
Expand Down Expand Up @@ -172,4 +172,4 @@ class CKMSQuantiles {
std::array<double, 500> buffer_;
std::size_t buffer_count_;
};
} // namespace ylt
} // namespace ylt::metric
4 changes: 2 additions & 2 deletions include/cinatra/ylt/metric/detail/time_window_quantiles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "ckms_quantiles.hpp"
// https://github.com/jupp0r/prometheus-cpp/blob/master/core/include/prometheus/detail/time_window_quantiles.h

namespace ylt {
namespace ylt::metric {
class TimeWindowQuantiles {
using Clock = std::chrono::steady_clock;

Expand Down Expand Up @@ -49,4 +49,4 @@ class TimeWindowQuantiles {
mutable Clock::time_point last_rotation_;
const Clock::duration rotation_interval_;
};
} // namespace ylt
} // namespace ylt::metric
4 changes: 2 additions & 2 deletions include/cinatra/ylt/metric/gauge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "counter.hpp"

namespace ylt {
namespace ylt::metric {
class gauge_t : public counter_t {
public:
gauge_t(std::string name, std::string help)
Expand Down Expand Up @@ -49,4 +49,4 @@ class gauge_t : public counter_t {
}
}
};
} // namespace ylt
} // namespace ylt::metric
Loading
Loading