Skip to content
This repository was archived by the owner on Oct 4, 2019. It is now read-only.

Commit 56e388a

Browse files
committed
Merge pull request #1228 from GolosChain/1222-view-slow-api-requests
Add setting to view slow API requests #1222
2 parents 82edada + 4be7bb5 commit 56e388a

3 files changed

Lines changed: 50 additions & 12 deletions

File tree

plugins/json_rpc/include/golos/plugins/json_rpc/plugin.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ namespace golos {
9696
APPBASE_PLUGIN_REQUIRES();
9797

9898
void set_program_options(boost::program_options::options_description &,
99-
boost::program_options::options_description &) override {
100-
}
99+
boost::program_options::options_description &);
101100

102101
static const std::string &name() {
103102
static std::string name = STEEM_JSON_RPC_PLUGIN_NAME;

plugins/json_rpc/plugin.cpp

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
namespace golos {
1414
namespace plugins {
1515
namespace json_rpc {
16+
17+
namespace bpo = boost::program_options;
18+
1619
struct json_rpc_error {
1720
json_rpc_error() : code(0) {
1821
}
@@ -273,24 +276,41 @@ namespace golos {
273276
}
274277

275278
struct dump_rpc_time {
276-
dump_rpc_time(const fc::variant& data)
277-
: data_(data) {
279+
dump_rpc_time(const fc::variant& data, uint64_t log_rpc_calls_slower_msec)
280+
: data_(data), log_rpc_calls_slower_msec_(log_rpc_calls_slower_msec) {
278281

279282
dlog("data: ${data}", ("data", fc::json::to_string(data_)));
280283
}
281284

282285
~dump_rpc_time() {
286+
auto msecs = (fc::time_point::now() - start_).count() / 1000;
287+
283288
if (error_.empty()) {
284289
dlog(
285-
"elapsed: ${time} sec, data: ${data}",
290+
"elapsed: ${time} msec, data: ${data}",
286291
("data", fc::json::to_string(data_))
287-
("time", double((fc::time_point::now() - start_).count()) / 1000000.0));
292+
("time", msecs));
288293
} else {
289294
dlog(
290-
"elapsed: ${time} sec, error: '${error}', data: ${data}",
295+
"elapsed: ${time} msec, error: '${error}', data: ${data}",
291296
("data", fc::json::to_string(data_))
292297
("error", error_)
293-
("time", double((fc::time_point::now() - start_).count()) / 1000000.0));
298+
("time", msecs));
299+
}
300+
301+
if (uint64_t(msecs) > log_rpc_calls_slower_msec_) {
302+
if (error_.empty()) {
303+
wlog(
304+
"Too slow RPC call: ${time} msec, data: ${data}",
305+
("data", fc::json::to_string(data_))
306+
("time", msecs));
307+
} else {
308+
wlog(
309+
"Too slow RPC call: ${time} msec, error: '${error}', data: ${data}",
310+
("data", fc::json::to_string(data_))
311+
("error", error_)
312+
("time", msecs));
313+
}
294314
}
295315
}
296316

@@ -302,10 +322,11 @@ namespace golos {
302322
fc::time_point start_ = fc::time_point::now();
303323
std::string error_;
304324
const fc::variant& data_;
325+
uint64_t log_rpc_calls_slower_msec_;
305326
};
306327

307328
void rpc(const fc::variant& data, msg_pack& msg) {
308-
dump_rpc_time dump(data);
329+
dump_rpc_time dump(data, _log_rpc_calls_slower_msec);
309330

310331
try {
311332
rpc_jsonrpc(data, msg);
@@ -406,6 +427,7 @@ namespace golos {
406427
map<string, api_description> _registered_apis;
407428
vector<string> _methods;
408429
map<string, map<string, api_method_signature> > _method_sigs;
430+
uint64_t _log_rpc_calls_slower_msec = UINT64_MAX;
409431
private:
410432
// This is a reindex which allows to get parent plugin by method
411433
// unordered_map[method] -> plugin
@@ -422,10 +444,18 @@ namespace golos {
422444
plugin::~plugin() {
423445
}
424446

447+
void plugin::set_program_options(bpo::options_description& cli, bpo::options_description& cfg) {
448+
cfg.add_options() (
449+
"log-rpc-calls-slower-msec", bpo::value<uint64_t>()->default_value(UINT64_MAX),
450+
"Maximal milliseconds of RPC call or dump it as too slow. If not set, do not dump"
451+
);
452+
}
453+
425454
void plugin::plugin_initialize(const boost::program_options::variables_map &options) {
426455
ilog("json_rpc plugin: plugin_initialize() begin");
427456
pimpl = std::make_unique<impl>();
428457
pimpl->initialize();
458+
pimpl->_log_rpc_calls_slower_msec = options.at("log-rpc-calls-slower-msec").as<uint64_t>();
429459
ilog("json_rpc plugin: plugin_initialize() end");
430460
}
431461

tests/plugin_tests/json_rpc.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,18 @@ BOOST_FIXTURE_TEST_SUITE(json_rpc, database_fixture)
117117
auto &rpc_plugin = appbase::app().register_plugin<json_rpc_plugin>();
118118
auto &testing_api = appbase::app().register_plugin<test_plugin::testing_api>();
119119

120-
boost::program_options::variables_map options;
121-
rpc_plugin.plugin_initialize(options);
122-
testing_api.plugin_initialize(options);
120+
{
121+
boost::program_options::options_description desc;
122+
rpc_plugin.set_program_options(desc, desc);
123+
124+
boost::program_options::variables_map options;
125+
boost::program_options::store(parse_command_line(0, (char**)NULL, desc), options);
126+
rpc_plugin.plugin_initialize(options);
127+
}
128+
{
129+
boost::program_options::variables_map options;
130+
testing_api.plugin_initialize(options);
131+
}
123132

124133
open_database();
125134

0 commit comments

Comments
 (0)