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

fix typo #596

Merged
merged 1 commit into from
Jun 7, 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
14 changes: 7 additions & 7 deletions example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,11 +448,11 @@ void use_metric() {
summary_t::Quantiles{
{0.5, 0.05}, {0.9, 0.01}, {0.95, 0.005}, {0.99, 0.001}});

default_metric_manger::register_metric_dynamic(c);
default_metric_manger::register_metric_dynamic(total);
default_metric_manger::register_metric_dynamic(failed);
default_metric_manger::register_metric_dynamic(h);
default_metric_manger::register_metric_dynamic(summary);
default_metric_manager::register_metric_dynamic(c);
default_metric_manager::register_metric_dynamic(total);
default_metric_manager::register_metric_dynamic(failed);
default_metric_manager::register_metric_dynamic(h);
default_metric_manager::register_metric_dynamic(summary);

std::random_device rd;
std::mt19937 gen(rd());
Expand Down Expand Up @@ -514,7 +514,7 @@ void metrics_example() {
"get_req_count", "get req count",
std::map<std::string, std::string>{{"url", "/get"}});
auto get_req_qps = std::make_shared<gauge_t>("get_req_qps", "get req qps");
// default_metric_manger::register_metric_static(get_req_counter,
// default_metric_manager::register_metric_static(get_req_counter,
// get_req_qps);
int64_t last = 0;
std::thread thd([&] {
Expand Down Expand Up @@ -573,7 +573,7 @@ async_simple::coro::Lazy<void> use_pool() {
server.use_metrics();
server.async_start();

auto map = default_metric_manger::metric_map_static();
auto map = default_metric_manager::metric_map_static();
for (auto &[k, m] : map) {
std::cout << k << ", ";
std::cout << m->help() << "\n";
Expand Down
16 changes: 8 additions & 8 deletions include/cinatra/coro_http_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class coro_http_server {
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_manger::serialize_static());
ylt::default_metric_manager::serialize_static());
res.set_status_and_content(status_type::ok, std::move(str));
});
}
Expand Down Expand Up @@ -903,20 +903,20 @@ class coro_http_server {
using namespace ylt;

cinatra_metric_conf::enable_metric = true;
default_metric_manger::create_metric_static<counter_t>(
default_metric_manager::create_metric_static<counter_t>(
cinatra_metric_conf::server_total_req, "");
default_metric_manger::create_metric_static<counter_t>(
default_metric_manager::create_metric_static<counter_t>(
cinatra_metric_conf::server_failed_req, "");
default_metric_manger::create_metric_static<counter_t>(
default_metric_manager::create_metric_static<counter_t>(
cinatra_metric_conf::server_total_recv_bytes, "");
default_metric_manger::create_metric_static<counter_t>(
default_metric_manager::create_metric_static<counter_t>(
cinatra_metric_conf::server_total_send_bytes, "");
default_metric_manger::create_metric_static<gauge_t>(
default_metric_manager::create_metric_static<gauge_t>(
cinatra_metric_conf::server_total_fd, "");
default_metric_manger::create_metric_static<histogram_t>(
default_metric_manager::create_metric_static<histogram_t>(
cinatra_metric_conf::server_req_latency, "",
std::vector<double>{30, 40, 50, 60, 70, 80, 90, 100, 150});
default_metric_manger::create_metric_static<histogram_t>(
default_metric_manager::create_metric_static<histogram_t>(
cinatra_metric_conf::server_read_latency, "",
std::vector<double>{3, 5, 7, 9, 13, 18, 23, 35, 50});
}
Expand Down
22 changes: 12 additions & 10 deletions include/cinatra/metric_conf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct cinatra_metric_conf {
}

static auto m =
ylt::default_metric_manger::get_metric_static<ylt::counter_t>(
ylt::default_metric_manager::get_metric_static<ylt::counter_t>(
server_total_req);
if (m == nullptr) {
return;
Expand All @@ -39,7 +39,7 @@ struct cinatra_metric_conf {
return;
}
static auto m =
ylt::default_metric_manger::get_metric_static<ylt::counter_t>(
ylt::default_metric_manager::get_metric_static<ylt::counter_t>(
server_failed_req);
if (m == nullptr) {
return;
Expand All @@ -51,8 +51,9 @@ struct cinatra_metric_conf {
if (!enable_metric) {
return;
}
static auto m = ylt::default_metric_manger::get_metric_static<ylt::gauge_t>(
server_total_fd);
static auto m =
ylt::default_metric_manager::get_metric_static<ylt::gauge_t>(
server_total_fd);
if (m == nullptr) {
return;
}
Expand All @@ -63,8 +64,9 @@ struct cinatra_metric_conf {
if (!enable_metric) {
return;
}
static auto m = ylt::default_metric_manger::get_metric_static<ylt::gauge_t>(
server_total_fd);
static auto m =
ylt::default_metric_manager::get_metric_static<ylt::gauge_t>(
server_total_fd);
if (m == nullptr) {
return;
}
Expand All @@ -76,7 +78,7 @@ struct cinatra_metric_conf {
return;
}
static auto m =
ylt::default_metric_manger::get_metric_static<ylt::counter_t>(
ylt::default_metric_manager::get_metric_static<ylt::counter_t>(
server_total_recv_bytes);
if (m == nullptr) {
return;
Expand All @@ -89,7 +91,7 @@ struct cinatra_metric_conf {
return;
}
static auto m =
ylt::default_metric_manger::get_metric_static<ylt::counter_t>(
ylt::default_metric_manager::get_metric_static<ylt::counter_t>(
server_total_send_bytes);
if (m == nullptr) {
return;
Expand All @@ -102,7 +104,7 @@ struct cinatra_metric_conf {
return;
}
static auto m =
ylt::default_metric_manger::get_metric_static<ylt::histogram_t>(
ylt::default_metric_manager::get_metric_static<ylt::histogram_t>(
server_req_latency);
if (m == nullptr) {
return;
Expand All @@ -115,7 +117,7 @@ struct cinatra_metric_conf {
return;
}
static auto m =
ylt::default_metric_manger::get_metric_static<ylt::histogram_t>(
ylt::default_metric_manager::get_metric_static<ylt::histogram_t>(
server_read_latency);
if (m == nullptr) {
return;
Expand Down
2 changes: 1 addition & 1 deletion include/cinatra/ylt/metric/metric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,5 +312,5 @@ struct metric_manager_t {
static inline std::once_flag flag_;
};

using default_metric_manger = metric_manager_t<0>;
using default_metric_manager = metric_manager_t<0>;
} // namespace ylt
22 changes: 11 additions & 11 deletions lang/how_to_use_metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,36 +174,36 @@ class metric_t {
```cpp
auto c = std::make_shared<counter_t>("qps_count", "qps help");
auto g = std::make_shared<gauge_t>("fd_count", "fd count help");
default_metric_manger::register_metric_static(c);
default_metric_manger::register_metric_static(g);
default_metric_manager::register_metric_static(c);
default_metric_manager::register_metric_static(g);

c->inc();
g->inc();

auto m = default_metric_manger::get_metric_static("qps_count");
auto m = default_metric_manager::get_metric_static("qps_count");
CHECK(m->as<counter_t>()->value() == 1);

auto m1 = default_metric_manger::get_metric_static("fd_count");
auto m1 = default_metric_manager::get_metric_static("fd_count");
CHECK(m1->as<gauge_t>()->value() == 1);
```

如果希望动态注册的到管理器则应该调用register_metric_dynamic接口,后面根据名称获取指标对象时则调用get_metric_dynamic接口,dynamic接口内部会加锁。
```cpp
auto c = std::make_shared<counter_t>("qps_count", "qps help");
auto g = std::make_shared<gauge_t>("fd_count", "fd count help");
default_metric_manger::register_metric_dynamic(c);
default_metric_manger::register_metric_dynamic(g);
default_metric_manager::register_metric_dynamic(c);
default_metric_manager::register_metric_dynamic(g);

c->inc();
g->inc();

auto m = default_metric_manger::get_metric_dynamic("qps_count");
auto m = default_metric_manager::get_metric_dynamic("qps_count");
CHECK(m->as<counter_t>()->value() == 1);

auto m1 = default_metric_manger::get_metric_dynamic("fd_count");
auto m1 = default_metric_manager::get_metric_dynamic("fd_count");
CHECK(m1->as<gauge_t>()->value() == 1);
```
注意:一旦注册时使用了static或者dynamic,那么后面调用default_metric_manger时则应该使用相同后缀的接口,比如注册时使用了get_metric_static,那么后面调用根据名称获取指标对象的方法必须是get_metric_static,否则会抛异常。同样,如果注册使用register_metric_dynamic,则后面应该get_metric_dynamic,否则会抛异常。
注意:一旦注册时使用了static或者dynamic,那么后面调用default_metric_manager时则应该使用相同后缀的接口,比如注册时使用了get_metric_static,那么后面调用根据名称获取指标对象的方法必须是get_metric_static,否则会抛异常。同样,如果注册使用register_metric_dynamic,则后面应该get_metric_dynamic,否则会抛异常。

指标管理器的api
```cpp
Expand Down Expand Up @@ -248,9 +248,9 @@ struct metric_manager_t {
static async_simple::coro::Lazy<std::string> serialize_static();
static async_simple::coro::Lazy<std::string> serialize_dynamic();
};
using default_metric_manger = metric_manager_t<0>;
using default_metric_manager = metric_manager_t<0>;
```
metric_manager_t默认为default_metric_manger,如果希望有多个metric manager,用户可以自定义新的metric manager,如:
metric_manager_t默认为default_metric_manager,如果希望有多个metric manager,用户可以自定义新的metric manager,如:

```cpp
constexpr size_t metric_id = 100;
Expand Down
32 changes: 16 additions & 16 deletions tests/test_metric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,55 +226,55 @@ TEST_CASE("test summary") {
TEST_CASE("test register metric") {
auto c = std::make_shared<counter_t>(std::string("get_count"),
std::string("get counter"));
default_metric_manger::register_metric_static(c);
CHECK_FALSE(default_metric_manger::register_metric_static(c));
default_metric_manager::register_metric_static(c);
CHECK_FALSE(default_metric_manager::register_metric_static(c));

auto g = std::make_shared<gauge_t>(std::string("get_guage_count"),
std::string("get counter"));
default_metric_manger::register_metric_static(g);
default_metric_manager::register_metric_static(g);

auto map1 = default_metric_manger::metric_map_static();
auto map1 = default_metric_manager::metric_map_static();
for (auto& [k, v] : map1) {
bool r = k == "get_count" || k == "get_guage_count";
break;
}

CHECK(default_metric_manger::metric_count_static() >= 2);
CHECK(default_metric_manger::metric_keys_static().size() >= 2);
CHECK(default_metric_manager::metric_count_static() >= 2);
CHECK(default_metric_manager::metric_keys_static().size() >= 2);

c->inc();
g->inc();

auto map = default_metric_manger::metric_map_static();
auto map = default_metric_manager::metric_map_static();
CHECK(map["get_count"]->as<counter_t>()->value() == 1);
CHECK(map["get_guage_count"]->as<gauge_t>()->value() == 1);

auto s =
async_simple::coro::syncAwait(default_metric_manger::serialize_static());
async_simple::coro::syncAwait(default_metric_manager::serialize_static());
std::cout << s << "\n";
CHECK(s.find("get_count 1") != std::string::npos);
CHECK(s.find("get_guage_count 1") != std::string::npos);

auto m = default_metric_manger::get_metric_static<counter_t>("get_count");
auto m = default_metric_manager::get_metric_static<counter_t>("get_count");
CHECK(m->as<counter_t>()->value() == 1);

auto m1 =
default_metric_manger::get_metric_static<gauge_t>("get_guage_count");
default_metric_manager::get_metric_static<gauge_t>("get_guage_count");
CHECK(m1->as<gauge_t>()->value() == 1);

{
// because the first regiter_metric is set no lock, so visit
// default_metric_manger with lock will throw.
// default_metric_manager with lock will throw.
auto c1 = std::make_shared<counter_t>(std::string(""), std::string(""));
CHECK_THROWS_AS(default_metric_manger::register_metric_dynamic(c1),
CHECK_THROWS_AS(default_metric_manager::register_metric_dynamic(c1),
std::invalid_argument);
CHECK_THROWS_AS(default_metric_manger::metric_count_dynamic(),
CHECK_THROWS_AS(default_metric_manager::metric_count_dynamic(),
std::invalid_argument);
CHECK_THROWS_AS(default_metric_manger::metric_keys_dynamic(),
CHECK_THROWS_AS(default_metric_manager::metric_keys_dynamic(),
std::invalid_argument);
CHECK_THROWS_AS(default_metric_manger::metric_map_dynamic(),
CHECK_THROWS_AS(default_metric_manager::metric_map_dynamic(),
std::invalid_argument);
CHECK_THROWS_AS(default_metric_manger::get_metric_dynamic<counter_t>(""),
CHECK_THROWS_AS(default_metric_manager::get_metric_dynamic<counter_t>(""),
std::invalid_argument);
}
}
Expand Down
Loading