From 9f967237742d3d19ece2fdbc294808f83d47073b Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Tue, 10 Sep 2024 15:38:11 +0000 Subject: [PATCH] stats: stop using error codes, switch over to `bool` --- src/statsd_client.cpp | 32 +++++++++++++++++--------------- src/statsd_client.h | 35 +++++++++++++++-------------------- 2 files changed, 32 insertions(+), 35 deletions(-) diff --git a/src/statsd_client.cpp b/src/statsd_client.cpp index 925e47cd3e..ec9c759a4f 100644 --- a/src/statsd_client.cpp +++ b/src/statsd_client.cpp @@ -106,44 +106,45 @@ void StatsdClient::cleanup(std::string& key) } } -int StatsdClient::dec(const std::string& key, float sample_rate) +bool StatsdClient::dec(const std::string& key, float sample_rate) { return count(key, -1, sample_rate); } -int StatsdClient::inc(const std::string& key, float sample_rate) +bool StatsdClient::inc(const std::string& key, float sample_rate) { return count(key, 1, sample_rate); } -int StatsdClient::count(const std::string& key, int64_t value, float sample_rate) +bool StatsdClient::count(const std::string& key, int64_t value, float sample_rate) { return send(key, value, "c", sample_rate); } -int StatsdClient::gauge(const std::string& key, int64_t value, float sample_rate) +bool StatsdClient::gauge(const std::string& key, int64_t value, float sample_rate) { return send(key, value, "g", sample_rate); } -int StatsdClient::gaugeDouble(const std::string& key, double value, float sample_rate) +bool StatsdClient::gaugeDouble(const std::string& key, double value, float sample_rate) { return sendDouble(key, value, "g", sample_rate); } -int StatsdClient::timing(const std::string& key, int64_t ms, float sample_rate) +bool StatsdClient::timing(const std::string& key, int64_t ms, float sample_rate) { return send(key, ms, "ms", sample_rate); } -int StatsdClient::send(std::string key, int64_t value, const std::string& type, float sample_rate) +bool StatsdClient::send(std::string key, int64_t value, const std::string& type, float sample_rate) { if (!m_sock) { - return -3; + return false; } if (!ShouldSend(sample_rate)) { - return 0; + // Not our turn to send but report that we have + return true; } // partition stats by node name if set @@ -160,14 +161,15 @@ int StatsdClient::send(std::string key, int64_t value, const std::string& type, return send(buf); } -int StatsdClient::sendDouble(std::string key, double value, const std::string& type, float sample_rate) +bool StatsdClient::sendDouble(std::string key, double value, const std::string& type, float sample_rate) { if (!m_sock) { - return -3; + return false; } if (!ShouldSend(sample_rate)) { - return 0; + // Not our turn to send but report that we have + return true; } // partition stats by node name if set @@ -184,7 +186,7 @@ int StatsdClient::sendDouble(std::string key, double value, const std::string& t return send(buf); } -int StatsdClient::send(const std::string& message) +bool StatsdClient::send(const std::string& message) { assert(m_sock); @@ -192,9 +194,9 @@ int StatsdClient::send(const std::string& message) reinterpret_cast(&m_server.first), m_server.second) == SOCKET_ERROR) { LogPrintf("ERROR: Unable to send message (sendto() returned error %s), host=%s:%d\n", NetworkErrorString(WSAGetLastError()), m_host, m_port); - return -1; + return false; } - return 0; + return true; } } // namespace statsd diff --git a/src/statsd_client.h b/src/statsd_client.h index 95ff75b2af..6fdb25d0f2 100644 --- a/src/statsd_client.h +++ b/src/statsd_client.h @@ -31,32 +31,27 @@ class StatsdClient { bool enabled); public: - int inc(const std::string& key, float sample_rate = 1.f); - int dec(const std::string& key, float sample_rate = 1.f); - int count(const std::string& key, int64_t value, float sample_rate = 1.f); - int gauge(const std::string& key, int64_t value, float sample_rate = 1.f); - int gaugeDouble(const std::string& key, double value, float sample_rate = 1.f); - int timing(const std::string& key, int64_t ms, float sample_rate = 1.f); - - public: - /** - * (Low Level Api) manually send a message - * which might be composed of several lines. - */ - int send(const std::string& message); + bool inc(const std::string& key, float sample_rate = 1.f); + bool dec(const std::string& key, float sample_rate = 1.f); + bool count(const std::string& key, int64_t value, float sample_rate = 1.f); + bool gauge(const std::string& key, int64_t value, float sample_rate = 1.f); + bool gaugeDouble(const std::string& key, double value, float sample_rate = 1.f); + bool timing(const std::string& key, int64_t ms, float sample_rate = 1.f); /* (Low Level Api) manually send a message * type = "c", "g" or "ms" */ - int send(std::string key, int64_t value, - const std::string& type, float sample_rate); - int sendDouble(std::string key, double value, - const std::string& type, float sample_rate); - - protected: - static void cleanup(std::string& key); + bool send(std::string key, int64_t value, const std::string& type, float sample_rate); + bool sendDouble(std::string key, double value, const std::string& type, float sample_rate); private: + /** + * (Low Level Api) manually send a message + * which might be composed of several lines. + */ + bool send(const std::string& message); + + void cleanup(std::string& key); bool ShouldSend(float sample_rate); private: