Skip to content

Commit

Permalink
Log ID for each RPC call (#951)
Browse files Browse the repository at this point in the history
Now it's logged only on completion
  • Loading branch information
SergiySW authored and PlasmaPower committed Jul 13, 2018
1 parent 823e2d7 commit 28ebc92
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
14 changes: 8 additions & 6 deletions rai/node/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,12 @@ void rai::rpc::stop ()
acceptor.close ();
}

rai::rpc_handler::rpc_handler (rai::node & node_a, rai::rpc & rpc_a, std::string const & body_a, std::function<void(boost::property_tree::ptree const &)> const & response_a) :
rai::rpc_handler::rpc_handler (rai::node & node_a, rai::rpc & rpc_a, std::string const & body_a, std::string const & request_id_a, std::function<void(boost::property_tree::ptree const &)> const & response_a) :
body (body_a),
node (node_a),
rpc (rpc_a),
response (response_a)
response (response_a),
request_id (request_id_a)
{
}

Expand Down Expand Up @@ -4537,7 +4538,8 @@ void rai::rpc_connection::read ()
this_l->node->background ([this_l]() {
auto start (std::chrono::steady_clock::now ());
auto version (this_l->request.version ());
auto response_handler ([this_l, version, start](boost::property_tree::ptree const & tree_a) {
std::string request_id (boost::str (boost::format ("%1%") % boost::io::group (std::hex, std::showbase, reinterpret_cast<uintptr_t> (this_l.get ()))));
auto response_handler ([this_l, version, start, request_id](boost::property_tree::ptree const & tree_a) {

std::stringstream ostream;
boost::property_tree::write_json (ostream, tree_a);
Expand All @@ -4549,12 +4551,12 @@ void rai::rpc_connection::read ()

if (this_l->node->config.logging.log_rpc ())
{
BOOST_LOG (this_l->node->log) << boost::str (boost::format ("RPC request %2% completed in: %1% microseconds") % std::chrono::duration_cast<std::chrono::microseconds> (std::chrono::steady_clock::now () - start).count () % boost::io::group (std::hex, std::showbase, reinterpret_cast<uintptr_t> (this_l.get ())));
BOOST_LOG (this_l->node->log) << boost::str (boost::format ("RPC request %2% completed in: %1% microseconds") % std::chrono::duration_cast<std::chrono::microseconds> (std::chrono::steady_clock::now () - start).count () % request_id);
}
});
if (this_l->request.method () == boost::beast::http::verb::post)
{
auto handler (std::make_shared<rai::rpc_handler> (*this_l->node, this_l->rpc, this_l->request.body (), response_handler));
auto handler (std::make_shared<rai::rpc_handler> (*this_l->node, this_l->rpc, this_l->request.body (), request_id, response_handler));
handler->process_request ();
}
else
Expand Down Expand Up @@ -4607,7 +4609,7 @@ void rai::rpc_handler::process_request ()
}
if (node.config.logging.log_rpc ())
{
BOOST_LOG (node.log) << body;
BOOST_LOG (node.log) << boost::str (boost::format ("%1% ") % request_id) << body;
}
if (action == "account_balance")
{
Expand Down
3 changes: 2 additions & 1 deletion rai/node/rpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class payment_observer : public std::enable_shared_from_this<rai::payment_observ
class rpc_handler : public std::enable_shared_from_this<rai::rpc_handler>
{
public:
rpc_handler (rai::node &, rai::rpc &, std::string const &, std::function<void(boost::property_tree::ptree const &)> const &);
rpc_handler (rai::node &, rai::rpc &, std::string const &, std::string const &, std::function<void(boost::property_tree::ptree const &)> const &);
void process_request ();
void account_balance ();
void account_block_count ();
Expand Down Expand Up @@ -217,6 +217,7 @@ class rpc_handler : public std::enable_shared_from_this<rai::rpc_handler>
void work_peers ();
void work_peers_clear ();
std::string body;
std::string request_id;
rai::node & node;
rai::rpc & rpc;
boost::property_tree::ptree request;
Expand Down
7 changes: 4 additions & 3 deletions rai/node/rpc_secure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ void rai::rpc_connection_secure::read ()
this_l->node->background ([this_l]() {
auto start (std::chrono::steady_clock::now ());
auto version (this_l->request.version ());
auto response_handler ([this_l, version, start](boost::property_tree::ptree const & tree_a) {
std::string request_id (boost::str (boost::format ("%1%") % boost::io::group (std::hex, std::showbase, reinterpret_cast<uintptr_t> (this_l.get ()))));
auto response_handler ([this_l, version, start, request_id](boost::property_tree::ptree const & tree_a) {
std::stringstream ostream;
boost::property_tree::write_json (ostream, tree_a);
ostream.flush ();
Expand All @@ -171,13 +172,13 @@ void rai::rpc_connection_secure::read ()

if (this_l->node->config.logging.log_rpc ())
{
BOOST_LOG (this_l->node->log) << boost::str (boost::format ("TLS: RPC request %2% completed in: %1% microseconds") % std::chrono::duration_cast<std::chrono::microseconds> (std::chrono::steady_clock::now () - start).count () % boost::io::group (std::hex, std::showbase, reinterpret_cast<uintptr_t> (this_l.get ())));
BOOST_LOG (this_l->node->log) << boost::str (boost::format ("TLS: RPC request %2% completed in: %1% microseconds") % std::chrono::duration_cast<std::chrono::microseconds> (std::chrono::steady_clock::now () - start).count () % request_id);
}
});

if (this_l->request.method () == boost::beast::http::verb::post)
{
auto handler (std::make_shared<rai::rpc_handler> (*this_l->node, this_l->rpc, this_l->request.body (), response_handler));
auto handler (std::make_shared<rai::rpc_handler> (*this_l->node, this_l->rpc, this_l->request.body (), request_id, response_handler));
handler->process_request ();
}
else
Expand Down

0 comments on commit 28ebc92

Please sign in to comment.