Skip to content

Commit

Permalink
Merge branch 'master' into feat_add_br_support
Browse files Browse the repository at this point in the history
  • Loading branch information
helintongh authored Jun 20, 2024
2 parents bd1fc40 + 235d3ed commit ad4bee5
Show file tree
Hide file tree
Showing 19 changed files with 1,651 additions and 146 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/linux_llvm_cov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ jobs:
./test_corofile
./test_http_parse
./test_time_util
./test_metric
llvm-profdata merge -sparse test_cinatra-*.profraw -o test_cinatra.profdata
llvm-cov show test_cinatra -object test_corofile -object test_time_util -object test_http_parse -instr-profile=test_cinatra.profdata -format=html -output-dir=../.coverage_llvm_cov -ignore-filename-regex="example|asio|cmdline|async_simple|tests" -show-instantiations=false
llvm-cov show test_cinatra -object test_corofile -object test_time_util -object test_http_parse -object test_metric -instr-profile=test_cinatra.profdata -format=html -output-dir=../.coverage_llvm_cov -ignore-filename-regex="example|asio|cmdline|async_simple|tests" -show-instantiations=false
echo "Done!"
- name: Upload Coverage Results
Expand Down
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(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH}")

SET(ENABLE_GZIP OFF)
Expand Down
4 changes: 3 additions & 1 deletion example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,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
8 changes: 4 additions & 4 deletions example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
#include <vector>

#include "../include/cinatra.hpp"
#include "metric_conf.hpp"
#include "cinatra/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
12 changes: 12 additions & 0 deletions include/cinatra/ylt/coro_io/io_context_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,18 @@ inline T &g_io_context_pool(
return *_g_io_context_pool;
}

template <typename T = io_context_pool>
inline std::shared_ptr<T> create_io_context_pool(
unsigned pool_size = std::thread::hardware_concurrency()) {
auto pool = std::make_shared<T>(pool_size);
std::thread thrd{[pool] {
pool->run();
}};
thrd.detach();

return pool;
}

template <typename T = io_context_pool>
inline T &g_block_io_context_pool(
unsigned pool_size = std::thread::hardware_concurrency()) {
Expand Down
103 changes: 73 additions & 30 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 All @@ -54,12 +60,8 @@ class counter_t : public metric_t {
}
}

std::map<std::vector<std::string>, double,
std::less<std::vector<std::string>>>
value_map() {
std::map<std::vector<std::string>, double,
std::less<std::vector<std::string>>>
map;
metric_hash_map<double> value_map() override {
metric_hash_map<double> map;
if (use_atomic_) {
map = {atomic_value_map_.begin(), atomic_value_map_.end()};
}
Expand Down Expand Up @@ -97,6 +99,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 @@ -148,9 +188,7 @@ class counter_t : public metric_t {
}
}

std::map<std::vector<std::string>, std::atomic<double>,
std::less<std::vector<std::string>>>
&atomic_value_map() {
metric_hash_map<std::atomic<double>> &atomic_value_map() {
return atomic_value_map_;
}

Expand Down Expand Up @@ -223,7 +261,12 @@ class counter_t : public metric_t {
label_val += value;
}
#else
label_val += value;
if constexpr (is_atomic) {
label_val.fetch_add(value, std::memory_order_relaxed);
}
else {
label_val += value;
}
#endif
} break;
case op_type_t::DEC:
Expand All @@ -234,9 +277,13 @@ class counter_t : public metric_t {
else {
label_val -= value;
}

#else
label_val -= value;
if constexpr (is_atomic) {
label_val.fetch_sub(value, std::memory_order_relaxed);
}
else {
label_val -= value;
}
#endif
break;
case op_type_t::SET:
Expand All @@ -245,14 +292,10 @@ class counter_t : public metric_t {
}
}

std::map<std::vector<std::string>, std::atomic<double>,
std::less<std::vector<std::string>>>
atomic_value_map_;
metric_hash_map<std::atomic<double>> atomic_value_map_;
std::atomic<double> default_lable_value_ = 0;

std::mutex mtx_;
std::map<std::vector<std::string>, double,
std::less<std::vector<std::string>>>
value_map_;
metric_hash_map<double> value_map_;
};
} // namespace ylt
} // namespace ylt::metric
Loading

0 comments on commit ad4bee5

Please sign in to comment.