Skip to content

Commit

Permalink
stats: const-ify variables and arguments
Browse files Browse the repository at this point in the history
We cannot convert `DEFAULT_STATSD*` to `std::string_view`s as they're
being used as default arguments and `GetArgs` expects `std::string`s
  • Loading branch information
kwvg committed Sep 10, 2024
1 parent 961407f commit 7c153c2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
20 changes: 10 additions & 10 deletions src/statsd_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ bool StatsdClient::ShouldSend(float sample_rate)
return sample_rate > std::uniform_real_distribution<float>(0.f, 1.f)(insecure_rand);
}

StatsdClient::StatsdClient(const std::string& host, const std::string& nodename, uint16_t port, const std::string& ns,
bool enabled)
StatsdClient::StatsdClient(const std::string& host, const std::string& nodename, const uint16_t port,
const std::string& ns, const bool enabled)
: m_port{port}, m_host{host}, m_nodename{nodename}, m_ns{ns}
{
if (!enabled) {
Expand Down Expand Up @@ -106,37 +106,37 @@ void StatsdClient::cleanup(std::string& key)
}
}

bool StatsdClient::dec(const std::string& key, float sample_rate)
bool StatsdClient::dec(const std::string& key, const float sample_rate)
{
return count(key, -1, sample_rate);
}

bool StatsdClient::inc(const std::string& key, float sample_rate)
bool StatsdClient::inc(const std::string& key, const float sample_rate)
{
return count(key, 1, sample_rate);
}

bool StatsdClient::count(const std::string& key, uint32_t value, float sample_rate)
bool StatsdClient::count(const std::string& key, const uint32_t value, const float sample_rate)
{
return send(key, value, "c", sample_rate);
}

bool StatsdClient::gauge(const std::string& key, uint32_t value, float sample_rate)
bool StatsdClient::gauge(const std::string& key, const uint32_t value, const float sample_rate)
{
return send(key, value, "g", sample_rate);
}

bool StatsdClient::gaugeDouble(const std::string& key, double value, float sample_rate)
bool StatsdClient::gaugeDouble(const std::string& key, const double value, const float sample_rate)
{
return sendDouble(key, value, "g", sample_rate);
}

bool StatsdClient::timing(const std::string& key, uint32_t ms, float sample_rate)
bool StatsdClient::timing(const std::string& key, const uint32_t ms, const float sample_rate)
{
return send(key, ms, "ms", sample_rate);
}

bool StatsdClient::send(std::string key, uint32_t value, const std::string& type, float sample_rate)
bool StatsdClient::send(std::string key, const uint32_t value, const std::string& type, const float sample_rate)
{
if (!m_sock) {
return false;
Expand All @@ -161,7 +161,7 @@ bool StatsdClient::send(std::string key, uint32_t value, const std::string& type
return send(buf);
}

bool StatsdClient::sendDouble(std::string key, double value, const std::string& type, float sample_rate)
bool StatsdClient::sendDouble(std::string key, const double value, const std::string& type, const float sample_rate)
{
if (!m_sock) {
return false;
Expand Down
36 changes: 18 additions & 18 deletions src/statsd_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,36 @@
#include <string>
#include <memory>

static const bool DEFAULT_STATSD_ENABLE = false;
static const uint16_t DEFAULT_STATSD_PORT = 8125;
static const std::string DEFAULT_STATSD_HOST = "127.0.0.1";
static const std::string DEFAULT_STATSD_HOSTNAME = "";
static const std::string DEFAULT_STATSD_NAMESPACE = "";
static constexpr bool DEFAULT_STATSD_ENABLE{false};
static constexpr uint16_t DEFAULT_STATSD_PORT{8125};
static const std::string DEFAULT_STATSD_HOST{"127.0.0.1"};
static const std::string DEFAULT_STATSD_HOSTNAME{""};
static const std::string DEFAULT_STATSD_NAMESPACE{""};

// schedule periodic measurements, in seconds: default - 1 minute, min - 5 sec, max - 1h.
static const int DEFAULT_STATSD_PERIOD = 60;
static const int MIN_STATSD_PERIOD = 5;
static const int MAX_STATSD_PERIOD = 60 * 60;
static constexpr int DEFAULT_STATSD_PERIOD{60};
static constexpr int MIN_STATSD_PERIOD{5};
static constexpr int MAX_STATSD_PERIOD{60*60};

namespace statsd {
class StatsdClient {
public:
explicit StatsdClient(const std::string& host, const std::string& nodename, uint16_t port, const std::string& ns,
bool enabled);
explicit StatsdClient(const std::string& host, const std::string& nodename, const uint16_t port,
const std::string& ns, const bool enabled);

public:
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, uint32_t value, float sample_rate = 1.f);
bool gauge(const std::string& key, uint32_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, uint32_t ms, float sample_rate = 1.f);
bool inc(const std::string& key, const float sample_rate = 1.f);
bool dec(const std::string& key, const float sample_rate = 1.f);
bool count(const std::string& key, const uint32_t value, const float sample_rate = 1.f);
bool gauge(const std::string& key, const uint32_t value, const float sample_rate = 1.f);
bool gaugeDouble(const std::string& key, const double value, const float sample_rate = 1.f);
bool timing(const std::string& key, const uint32_t ms, const float sample_rate = 1.f);

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

private:
/**
Expand Down

0 comments on commit 7c153c2

Please sign in to comment.