This repository has been archived by the owner on Jun 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add http interface to get perf counter info (#349)
- Loading branch information
zhao liwei
authored and
Wu Tao
committed
Dec 10, 2019
1 parent
fdb19b1
commit daff2f9
Showing
7 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) 2017-present, Xiaomi, Inc. All rights reserved. | ||
// This source code is licensed under the Apache License Version 2.0, which | ||
// can be found in the LICENSE file in the root directory of this source tree. | ||
|
||
#include <dsn/utility/output_utils.h> | ||
#include "perf_counter_http_service.h" | ||
|
||
namespace dsn { | ||
|
||
void perf_counter_http_service::get_perf_counter_handler(const http_request &req, | ||
http_response &resp) | ||
{ | ||
std::string perf_counter_name; | ||
for (const auto &p : req.query_args) { | ||
if ("name" == p.first) { | ||
perf_counter_name = p.second; | ||
} else { | ||
resp.status_code = http_status_code::bad_request; | ||
return; | ||
} | ||
} | ||
|
||
// get perf counter by perf counter name | ||
perf_counter_ptr perf_counter = perf_counters::instance().get_counter(perf_counter_name); | ||
|
||
// insert perf counter info into table printer | ||
dsn::utils::table_printer tp; | ||
if (perf_counter) { | ||
tp.add_row_name_and_data("name", perf_counter_name); | ||
if (COUNTER_TYPE_NUMBER_PERCENTILES == perf_counter->type()) { | ||
tp.add_row_name_and_data("p99", perf_counter->get_percentile(COUNTER_PERCENTILE_99)); | ||
tp.add_row_name_and_data("p999", perf_counter->get_percentile(COUNTER_PERCENTILE_999)); | ||
} else { | ||
tp.add_row_name_and_data("value", perf_counter->get_value()); | ||
} | ||
tp.add_row_name_and_data("type", dsn_counter_type_to_string(perf_counter->type())); | ||
tp.add_row_name_and_data("description", perf_counter->dsptr()); | ||
} | ||
|
||
std::ostringstream out; | ||
tp.output(out, dsn::utils::table_printer::output_format::kJsonCompact); | ||
resp.body = out.str(); | ||
resp.status_code = http_status_code::ok; | ||
} | ||
} // namespace dsn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) 2017-present, Xiaomi, Inc. All rights reserved. | ||
// This source code is licensed under the Apache License Version 2.0, which | ||
// can be found in the LICENSE file in the root directory of this source tree. | ||
|
||
#pragma once | ||
|
||
#include <dsn/tool-api/http_server.h> | ||
|
||
namespace dsn { | ||
|
||
class perf_counter_http_service : public http_service | ||
{ | ||
public: | ||
perf_counter_http_service() | ||
{ | ||
// GET ip:port/perfCounter?name={perf_counter_name} | ||
register_handler("", | ||
std::bind(&perf_counter_http_service::get_perf_counter_handler, | ||
this, | ||
std::placeholders::_1, | ||
std::placeholders::_2)); | ||
} | ||
|
||
std::string path() const override { return "perfCounter"; } | ||
|
||
void get_perf_counter_handler(const http_request &req, http_response &resp); | ||
}; | ||
} // namespace dsn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright (c) 2018, Xiaomi, Inc. All rights reserved. | ||
// This source code is licensed under the Apache License Version 2.0, which | ||
// can be found in the LICENSE file in the root directory of this source tree. | ||
|
||
#include <gtest/gtest.h> | ||
#include <dsn/perf_counter/perf_counters.h> | ||
#include <dsn/tool-api/http_server.h> | ||
#include <dist/http/perf_counter_http_service.h> | ||
|
||
namespace dsn { | ||
|
||
class perf_counter_http_service_test : public testing::Test | ||
{ | ||
public: | ||
perf_counter_http_service _perf_counter_http_service; | ||
}; | ||
|
||
TEST_F(perf_counter_http_service_test, get_perf_counter) | ||
{ | ||
struct test_case | ||
{ | ||
const char *app; | ||
const char *section; | ||
const char *name; | ||
dsn_perf_counter_type_t type; | ||
const char *description; | ||
} tests[] = { | ||
{"replica", "http", "number", COUNTER_TYPE_NUMBER, "number type"}, | ||
{"replica", "http", "volatile", COUNTER_TYPE_VOLATILE_NUMBER, "volatile type"}, | ||
{"replica", "http", "rate", COUNTER_TYPE_RATE, "rate type"}, | ||
{"replica", "http", "percentline", COUNTER_TYPE_NUMBER_PERCENTILES, "percentline type"}}; | ||
|
||
for (auto test : tests) { | ||
// create perf counter | ||
perf_counter_wrapper counter; | ||
counter.init_global_counter(test.app, test.section, test.name, test.type, test.description); | ||
|
||
std::string perf_counter_name; | ||
perf_counter::build_full_name(test.app, test.section, test.name, perf_counter_name); | ||
|
||
// get perf counter info through the http interface | ||
http_request fake_req; | ||
http_response fake_resp; | ||
fake_req.query_args.emplace("name", perf_counter_name); | ||
_perf_counter_http_service.get_perf_counter_handler(fake_req, fake_resp); | ||
|
||
// get fake json based on the perf counter info which is getting above | ||
std::string fake_json; | ||
if (COUNTER_TYPE_NUMBER_PERCENTILES == test.type) { | ||
fake_json = R"({"name":")" + perf_counter_name + R"(",)" + | ||
R"("p99":"0.00","p999":"0.00",)" + | ||
R"("type":")" + dsn_counter_type_to_string(test.type) + R"(",)" + | ||
R"("description":")" + test.description + R"("})" + "\n"; | ||
} else { | ||
fake_json = R"({"name":")" + perf_counter_name + R"(",)" + | ||
R"("value":"0.00",)" + | ||
R"("type":")" + dsn_counter_type_to_string(test.type) + R"(",)" + | ||
R"("description":")" + test.description + R"("})" + "\n"; | ||
} | ||
|
||
ASSERT_EQ(fake_resp.status_code, http_status_code::ok); | ||
ASSERT_EQ(fake_resp.body, fake_json); | ||
} | ||
} | ||
} // namespace dsn |