1313namespace 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
0 commit comments