Skip to content

Commit 7c153c2

Browse files
committed
stats: const-ify variables and arguments
We cannot convert `DEFAULT_STATSD*` to `std::string_view`s as they're being used as default arguments and `GetArgs` expects `std::string`s
1 parent 961407f commit 7c153c2

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

src/statsd_client.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ bool StatsdClient::ShouldSend(float sample_rate)
6161
return sample_rate > std::uniform_real_distribution<float>(0.f, 1.f)(insecure_rand);
6262
}
6363

64-
StatsdClient::StatsdClient(const std::string& host, const std::string& nodename, uint16_t port, const std::string& ns,
65-
bool enabled)
64+
StatsdClient::StatsdClient(const std::string& host, const std::string& nodename, const uint16_t port,
65+
const std::string& ns, const bool enabled)
6666
: m_port{port}, m_host{host}, m_nodename{nodename}, m_ns{ns}
6767
{
6868
if (!enabled) {
@@ -106,37 +106,37 @@ void StatsdClient::cleanup(std::string& key)
106106
}
107107
}
108108

109-
bool StatsdClient::dec(const std::string& key, float sample_rate)
109+
bool StatsdClient::dec(const std::string& key, const float sample_rate)
110110
{
111111
return count(key, -1, sample_rate);
112112
}
113113

114-
bool StatsdClient::inc(const std::string& key, float sample_rate)
114+
bool StatsdClient::inc(const std::string& key, const float sample_rate)
115115
{
116116
return count(key, 1, sample_rate);
117117
}
118118

119-
bool StatsdClient::count(const std::string& key, uint32_t value, float sample_rate)
119+
bool StatsdClient::count(const std::string& key, const uint32_t value, const float sample_rate)
120120
{
121121
return send(key, value, "c", sample_rate);
122122
}
123123

124-
bool StatsdClient::gauge(const std::string& key, uint32_t value, float sample_rate)
124+
bool StatsdClient::gauge(const std::string& key, const uint32_t value, const float sample_rate)
125125
{
126126
return send(key, value, "g", sample_rate);
127127
}
128128

129-
bool StatsdClient::gaugeDouble(const std::string& key, double value, float sample_rate)
129+
bool StatsdClient::gaugeDouble(const std::string& key, const double value, const float sample_rate)
130130
{
131131
return sendDouble(key, value, "g", sample_rate);
132132
}
133133

134-
bool StatsdClient::timing(const std::string& key, uint32_t ms, float sample_rate)
134+
bool StatsdClient::timing(const std::string& key, const uint32_t ms, const float sample_rate)
135135
{
136136
return send(key, ms, "ms", sample_rate);
137137
}
138138

139-
bool StatsdClient::send(std::string key, uint32_t value, const std::string& type, float sample_rate)
139+
bool StatsdClient::send(std::string key, const uint32_t value, const std::string& type, const float sample_rate)
140140
{
141141
if (!m_sock) {
142142
return false;
@@ -161,7 +161,7 @@ bool StatsdClient::send(std::string key, uint32_t value, const std::string& type
161161
return send(buf);
162162
}
163163

164-
bool StatsdClient::sendDouble(std::string key, double value, const std::string& type, float sample_rate)
164+
bool StatsdClient::sendDouble(std::string key, const double value, const std::string& type, const float sample_rate)
165165
{
166166
if (!m_sock) {
167167
return false;

src/statsd_client.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,36 @@
1313
#include <string>
1414
#include <memory>
1515

16-
static const bool DEFAULT_STATSD_ENABLE = false;
17-
static const uint16_t DEFAULT_STATSD_PORT = 8125;
18-
static const std::string DEFAULT_STATSD_HOST = "127.0.0.1";
19-
static const std::string DEFAULT_STATSD_HOSTNAME = "";
20-
static const std::string DEFAULT_STATSD_NAMESPACE = "";
16+
static constexpr bool DEFAULT_STATSD_ENABLE{false};
17+
static constexpr uint16_t DEFAULT_STATSD_PORT{8125};
18+
static const std::string DEFAULT_STATSD_HOST{"127.0.0.1"};
19+
static const std::string DEFAULT_STATSD_HOSTNAME{""};
20+
static const std::string DEFAULT_STATSD_NAMESPACE{""};
2121

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

2727
namespace statsd {
2828
class StatsdClient {
2929
public:
30-
explicit StatsdClient(const std::string& host, const std::string& nodename, uint16_t port, const std::string& ns,
31-
bool enabled);
30+
explicit StatsdClient(const std::string& host, const std::string& nodename, const uint16_t port,
31+
const std::string& ns, const bool enabled);
3232

3333
public:
34-
bool inc(const std::string& key, float sample_rate = 1.f);
35-
bool dec(const std::string& key, float sample_rate = 1.f);
36-
bool count(const std::string& key, uint32_t value, float sample_rate = 1.f);
37-
bool gauge(const std::string& key, uint32_t value, float sample_rate = 1.f);
38-
bool gaugeDouble(const std::string& key, double value, float sample_rate = 1.f);
39-
bool timing(const std::string& key, uint32_t ms, float sample_rate = 1.f);
34+
bool inc(const std::string& key, const float sample_rate = 1.f);
35+
bool dec(const std::string& key, const float sample_rate = 1.f);
36+
bool count(const std::string& key, const uint32_t value, const float sample_rate = 1.f);
37+
bool gauge(const std::string& key, const uint32_t value, const float sample_rate = 1.f);
38+
bool gaugeDouble(const std::string& key, const double value, const float sample_rate = 1.f);
39+
bool timing(const std::string& key, const uint32_t ms, const float sample_rate = 1.f);
4040

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

4747
private:
4848
/**

0 commit comments

Comments
 (0)