Skip to content

Commit 8e7a3d1

Browse files
committed
JsonRpcConnection: Log message processing stats
1 parent be98b1b commit 8e7a3d1

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

lib/remote/jsonrpcconnection.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ void JsonRpcConnection::Start()
6060

6161
void JsonRpcConnection::HandleIncomingMessages(boost::asio::yield_context yc)
6262
{
63+
namespace ch = std::chrono;
64+
6365
m_Stream->next_layer().SetSeen(&m_Seen);
6466

6567
for (;;) {
@@ -78,9 +80,37 @@ void JsonRpcConnection::HandleIncomingMessages(boost::asio::yield_context yc)
7880
m_Seen = Utility::GetTime();
7981

8082
try {
83+
auto start (ch::steady_clock::now());
84+
ch::steady_clock::duration cpuBoundDuration;
85+
86+
String rpcMethod;
87+
88+
Defer addLogStats ([this, &rpcMethod, &start, &cpuBoundDuration]() {
89+
auto severity = LogDebug;
90+
91+
auto duration = ch::steady_clock::now() - start;
92+
if (duration >= ch::seconds(5)) {
93+
// Processing that RPC message seems to take an unexpectedly long time,
94+
// so promote the log entry from debug to warning.
95+
severity = LogWarning;
96+
}
97+
98+
Log statsLog(severity, "JsonRpcConnection");
99+
statsLog << "Processing JSON-RPC '" << rpcMethod << "' message for identity '" << m_Identity << "'";
100+
101+
if (cpuBoundDuration >= ch::seconds(1)) {
102+
statsLog << " waited '" << ch::duration_cast<ch::milliseconds>(cpuBoundDuration).count() << "ms' on semaphore and";
103+
}
104+
105+
statsLog << " took total " << ch::duration_cast<ch::milliseconds>(duration).count() << "ms.";
106+
});
107+
81108
CpuBoundWork handleMessage (yc);
82109

83-
MessageHandler(message);
110+
// Cache the elapsed time to acquire a CPU semaphore used to detect extremely heavy workloads.
111+
cpuBoundDuration = ch::steady_clock::now() - start;
112+
113+
MessageHandler(message, rpcMethod);
84114

85115
l_TaskStats.InsertValue(Utility::GetTime(), 1);
86116
} catch (const std::exception& ex) {
@@ -245,9 +275,13 @@ void JsonRpcConnection::Disconnect()
245275
});
246276
}
247277

248-
void JsonRpcConnection::MessageHandler(const String& jsonString)
278+
void JsonRpcConnection::MessageHandler(const String& jsonString, String& rpcMethod)
249279
{
250280
Dictionary::Ptr message = JsonRpc::DecodeMessage(jsonString);
281+
rpcMethod = message->Get("method");
282+
if (rpcMethod.IsEmpty()) {
283+
rpcMethod = "UNKNOWN";
284+
}
251285

252286
if (m_Endpoint && message->Contains("ts")) {
253287
double ts = message->Get("ts");

lib/remote/jsonrpcconnection.hpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,23 @@ class JsonRpcConnection final : public Object
8888
void CheckLiveness(boost::asio::yield_context yc);
8989

9090
bool ProcessMessage();
91-
void MessageHandler(const String& jsonString);
91+
92+
/**
93+
* MessageHandler routes the provided message to its corresponding handler (if any).
94+
*
95+
* This will first JSON decode the specified message and verifies the timestamp of that RPC message (if any).
96+
* Subsequently, any message whose timestamp is less than the remote log position of the Endpoint will be rejected;
97+
* otherwise, the endpoint's remote log position is updated to that timestamp. It is not expected to happen, but any
98+
* message lacking an RPC method or referring to a non-existent one is also discarded. Afterwards, the RPC handler
99+
* is then called for that message and sends it's result back to the sender if the message contains an ID.
100+
*
101+
* Note: The "rpcMethod" param is used as an output variable and is set to the RPC method of the given
102+
* message if any, otherwise to "UNKNOWN".
103+
*
104+
* @param jsonString String The RPC message you want to process.
105+
* @param rpcMethod String The output parameter for the RPC method.
106+
*/
107+
void MessageHandler(const String& jsonString, String& rpcMethod);
92108

93109
void CertificateRequestResponseHandler(const Dictionary::Ptr& message);
94110

0 commit comments

Comments
 (0)