diff --git a/src/httprpc.cpp b/src/httprpc.cpp index f602e89b29a..14010f52e8d 100644 --- a/src/httprpc.cpp +++ b/src/httprpc.cpp @@ -234,7 +234,7 @@ static bool HTTPReq_JSONRPC(HTTPRequest* req, const std::string &) req->WriteHeader("Content-Type", "application/json"); req->WriteReply(HTTP_OK, strReply); - if (DEFAULT_RPC_STATS) statsRPC.add(jreq.strMethod, GetTimeMillis() - time, strReply.length()); + if (statsRPC.active) statsRPC.add(jreq.strMethod, GetTimeMillis() - time, strReply.length()); } catch (const UniValue& objError) { JSONErrorReply(req, objError, jreq.id); @@ -285,7 +285,7 @@ bool StartHTTPRPC() assert(eventBase); httpRPCTimerInterface = std::make_unique(eventBase); RPCSetTimerInterface(httpRPCTimerInterface.get()); - if (DEFAULT_RPC_STATS) statsRPC.load(); + if (statsRPC.active) statsRPC.load(); return true; } @@ -305,7 +305,7 @@ void StopHTTPRPC() RPCUnsetTimerInterface(httpRPCTimerInterface.get()); httpRPCTimerInterface.reset(); } - if (DEFAULT_RPC_STATS) statsRPC.save(); + if (statsRPC.active) statsRPC.save(); } CRPCStats statsRPC; diff --git a/src/init.cpp b/src/init.cpp index 2d73a41b079..32a5df98b35 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -590,7 +590,7 @@ void SetupServerArgs() gArgs.AddArg("-rpcworkqueue=", strprintf("Set the depth of the work queue to service RPC calls (default: %d)", DEFAULT_HTTP_WORKQUEUE), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::RPC); gArgs.AddArg("-server", "Accept command line and JSON-RPC commands", ArgsManager::ALLOW_ANY, OptionsCategory::RPC); gArgs.AddArg("-rpcallowcors=", "Allow CORS requests from the given host origin. Include scheme and port (eg: -rpcallowcors=http://127.0.0.1:5000)", ArgsManager::ALLOW_ANY, OptionsCategory::RPC); - gArgs.AddArg("-rpcstats", strprintf("Log RPC stats. (default: %u)", false), ArgsManager::ALLOW_ANY, OptionsCategory::RPC); + gArgs.AddArg("-rpcstats", strprintf("Log RPC stats. (default: %u)", DEFAULT_RPC_STATS), ArgsManager::ALLOW_ANY, OptionsCategory::RPC); #if HAVE_DECL_DAEMON gArgs.AddArg("-daemon", "Run in the background as a daemon and accept commands", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); @@ -2075,7 +2075,7 @@ bool AppInitMain(InitInterfaces& interfaces) }); } - if (gArgs.GetBoolArg("-rpcstats", DEFAULT_RPC_STATS)) DEFAULT_RPC_STATS = true; + if (gArgs.GetBoolArg("-rpcstats", DEFAULT_RPC_STATS)) statsRPC.active = true; return true; } diff --git a/src/rpc/stats.cpp b/src/rpc/stats.cpp index 9a49a2c8b88..a3b25226658 100644 --- a/src/rpc/stats.cpp +++ b/src/rpc/stats.cpp @@ -126,7 +126,7 @@ static UniValue getrpcstats(const JSONRPCRequest& request) }, }.Check(request); - if (!DEFAULT_RPC_STATS) { + if (!statsRPC.active) { throw JSONRPCError(RPC_INVALID_REQUEST, "Rpcstats flag is not set. Activate it by restarting node with -rpcstats."); } @@ -168,7 +168,7 @@ static UniValue listrpcstats(const JSONRPCRequest& request) }, }.Check(request); - if (!DEFAULT_RPC_STATS) { + if (!statsRPC.active) { throw JSONRPCError(RPC_INVALID_REQUEST, "Rpcstats flag is not set. Activate it by restarting node with -rpcstats."); } diff --git a/src/rpc/stats.h b/src/rpc/stats.h index e0ea0b6714e..86d6d3daf50 100644 --- a/src/rpc/stats.h +++ b/src/rpc/stats.h @@ -11,7 +11,7 @@ const char * const DEFAULT_STATSFILE = "stats.log"; static const uint8_t RPC_STATS_HISTORY_SIZE = 5; -bool DEFAULT_RPC_STATS = false; +const bool DEFAULT_RPC_STATS = false; struct MinMaxStatEntry { int64_t min; @@ -56,6 +56,8 @@ class CRPCStats private: std::map map; public: + bool active{DEFAULT_RPC_STATS}; + void add(const std::string& name, const int64_t latency, const int64_t payload); std::optional get(const std::string& name) {