From ffb241f9e2763c5248f74ba63809da96f7d2b423 Mon Sep 17 00:00:00 2001 From: Santiago Gimeno Date: Fri, 3 Nov 2023 20:41:59 +0100 Subject: [PATCH] agents: fix profile/snapshot messages body format In general fix `ZmqAgent::send_command_message` so it handles correctly both an empty and a null body. PR-URL: https://github.com/nodesource/nsolid/pull/23 Reviewed-by: Trevor Norris --- agents/zmq/src/zmq_agent.cc | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/agents/zmq/src/zmq_agent.cc b/agents/zmq/src/zmq_agent.cc index db6255abd7..8989115448 100644 --- a/agents/zmq/src/zmq_agent.cc +++ b/agents/zmq/src/zmq_agent.cc @@ -1095,6 +1095,7 @@ int ZmqAgent::send_command_message(const char* command, return PrintZmqError(zmq::ZMQ_ERROR_SEND_COMMAND_NO_DATA_HANDLE, command); } + const char* real_body = body ? (strlen(body) ? body : "\"\"") : "null"; auto recorded = create_recorded(system_clock::now()); int r; if (request_id == nullptr) { @@ -1108,7 +1109,7 @@ int ZmqAgent::send_command_message(const char* command, 0, metrics_period_, version_, - body); + real_body); } else { r = snprintf(msg_buf_, @@ -1122,7 +1123,7 @@ int ZmqAgent::send_command_message(const char* command, 0, metrics_period_, version_, - body); + real_body); } if (r < 0) { @@ -1371,7 +1372,7 @@ int ZmqAgent::generate_snapshot(const json& message, } // send snapshot command reponse thru data channel - int r = send_command_message("snapshot", req_id.c_str(), ""); + int r = send_command_message("snapshot", req_id.c_str(), nullptr); if (r < 0) { return r; } @@ -2208,7 +2209,7 @@ std::string ZmqAgent::got_cpu_profile(int status, // send profile_stop command reponse thru data channel // only if the profile is complete if (profileStreamComplete) { - int r = send_command_message("profile_stop", req_id.c_str(), ""); + int r = send_command_message("profile_stop", req_id.c_str(), nullptr); if (r < 0) { return req_id; } @@ -2292,7 +2293,7 @@ int ZmqAgent::start_profiling(const json& message, } // send profile command reponse thru data channel - err = send_command_message(kProfile, req_id.c_str(), ""); + err = send_command_message(kProfile, req_id.c_str(), nullptr); if (err < 0) { return err; }