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 3, 2024
1 parent f6f6c19 commit e0b2012
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 31 deletions.
33 changes: 18 additions & 15 deletions src/statsd_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,40 +90,41 @@ 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, size_t value, float sample_rate)
bool StatsdClient::count(const std::string& key, size_t value, float sample_rate)
{
return send(key, value, "c", sample_rate);
}

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

int StatsdClient::send(std::string key, size_t value, const std::string& type, float sample_rate)
bool StatsdClient::send(std::string key, size_t value, const std::string& type, float sample_rate)
{
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 @@ -140,10 +141,11 @@ int StatsdClient::send(std::string key, size_t value, const std::string& type, f
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 (!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,18 +162,19 @@ 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)
{
if (!m_sock)
return -3;
if (!m_sock) {
return false;
}

if (sendto(m_sock->Get(), message.data(), message.size(), /*flags=*/0,
reinterpret_cast<struct sockaddr*>(&m_server.first), m_server.second) == SOCKET_ERROR) {
LogPrintf("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
27 changes: 11 additions & 16 deletions src/statsd_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,27 @@ class StatsdClient {
bool enabled);

public:
int inc(const std::string& key, float sample_rate = 1.0);
int dec(const std::string& key, float sample_rate = 1.0);
int count(const std::string& key, size_t value, float sample_rate = 1.0);
int gauge(const std::string& key, size_t value, float sample_rate = 1.0);
int gaugeDouble(const std::string& key, double value, float sample_rate = 1.0);
int timing(const std::string& key, size_t ms, float sample_rate = 1.0);
bool inc(const std::string& key, float sample_rate = 1.0);
bool dec(const std::string& key, float sample_rate = 1.0);
bool count(const std::string& key, size_t value, float sample_rate = 1.0);
bool gauge(const std::string& key, size_t value, float sample_rate = 1.0);
bool gaugeDouble(const std::string& key, double value, float sample_rate = 1.0);
bool timing(const std::string& key, size_t ms, float sample_rate = 1.0);

public:
private:
/**
* (Low Level Api) manually send a message
* which might be composed of several lines.
*/
int send(const std::string& message);
bool send(const std::string& message);

/* (Low Level Api) manually send a message
* type = "c", "g" or "ms"
*/
int send(std::string key, size_t value,
const std::string& type, float sample_rate);
int sendDouble(std::string key, double value,
const std::string& type, float sample_rate);
bool send(std::string key, size_t value, const std::string& type, float sample_rate);
bool sendDouble(std::string key, double value, const std::string& type, float sample_rate);

protected:
static void cleanup(std::string& key);

private:
void cleanup(std::string& key);
bool ShouldSend(float sample_rate);

private:
Expand Down

0 comments on commit e0b2012

Please sign in to comment.