Skip to content

Commit

Permalink
stats: stop using error codes, switch over to bool
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed Sep 10, 2024
1 parent e1bd374 commit 480ca10
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 35 deletions.
32 changes: 17 additions & 15 deletions src/statsd_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, int32_t value, float sample_rate)
bool StatsdClient::count(const std::string& key, int32_t value, float sample_rate)
{
return send(key, value, "c", sample_rate);
}

int StatsdClient::gauge(const std::string& key, int32_t value, float sample_rate)
bool StatsdClient::gauge(const std::string& key, int32_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, int32_t ms, float sample_rate)
bool StatsdClient::timing(const std::string& key, int32_t ms, float sample_rate)
{
return send(key, ms, "ms", sample_rate);
}

int StatsdClient::send(std::string key, int32_t value, const std::string& type, float sample_rate)
bool StatsdClient::send(std::string key, int32_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
Expand All @@ -160,14 +161,15 @@ int StatsdClient::send(std::string key, int32_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
Expand All @@ -184,17 +186,17 @@ 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);

if (::sendto(m_sock->Get(), message.data(), message.size(), /*flags=*/0,
reinterpret_cast<struct sockaddr*>(&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
35 changes: 15 additions & 20 deletions src/statsd_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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, int32_t value, float sample_rate = 1.f);
int gauge(const std::string& key, int32_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, int32_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, int32_t value, float sample_rate = 1.f);
bool gauge(const std::string& key, int32_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, int32_t ms, float sample_rate = 1.f);

/* (Low Level Api) manually send a message
* type = "c", "g" or "ms"
*/
int send(std::string key, int32_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, int32_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:
Expand Down

0 comments on commit 480ca10

Please sign in to comment.