44#include < cpp-statsd-client/UDPSender.hpp>
55#include < cstdint>
66#include < cstdio>
7+ #include < iomanip>
78#include < memory>
89#include < random>
10+ #include < sstream>
911#include < string>
1012#include < vector>
1113
@@ -63,7 +65,8 @@ class StatsdClient {
6365 const uint16_t port,
6466 const std::string& prefix,
6567 const uint64_t batchsize = 0 ,
66- const uint64_t sendInterval = 1000 ) noexcept ;
68+ const uint64_t sendInterval = 1000 ,
69+ const unsigned int gaugePrecision = 4 ) noexcept ;
6770
6871 StatsdClient (const StatsdClient&) = delete ;
6972 StatsdClient& operator =(const StatsdClient&) = delete ;
@@ -78,7 +81,8 @@ class StatsdClient {
7881 const uint16_t port,
7982 const std::string& prefix,
8083 const uint64_t batchsize = 0 ,
81- const uint64_t sendInterval = 1000 ) noexcept ;
84+ const uint64_t sendInterval = 1000 ,
85+ const unsigned int gaugePrecision = 4 ) noexcept ;
8286
8387 // ! Returns the error message as an std::string
8488 const std::string& errorMessage () const noexcept ;
@@ -100,8 +104,9 @@ class StatsdClient {
100104 const std::vector<std::string>& tags = {}) const noexcept ;
101105
102106 // ! Records a gauge for the key, with a given value, at a given frequency rate
107+ template <typename T>
103108 void gauge (const std::string& key,
104- const unsigned int value,
109+ const T value,
105110 float frequency = 1 .0f ,
106111 const std::vector<std::string>& tags = {}) const noexcept ;
107112
@@ -130,8 +135,9 @@ class StatsdClient {
130135 // @{
131136
132137 // ! Send a value for a key, according to its type, at a given frequency
138+ template <typename T>
133139 void send (const std::string& key,
134- const int value,
140+ const T value,
135141 const char * type,
136142 float frequency,
137143 const std::vector<std::string>& tags) const noexcept ;
@@ -150,6 +156,9 @@ class StatsdClient {
150156
151157 // ! The buffer string format our stats before sending them
152158 mutable std::string m_buffer;
159+
160+ // ! Fixed floating point precision of gauges
161+ unsigned int m_gaugePrecision;
153162};
154163
155164namespace detail {
@@ -172,8 +181,11 @@ inline StatsdClient::StatsdClient(const std::string& host,
172181 const uint16_t port,
173182 const std::string& prefix,
174183 const uint64_t batchsize,
175- const uint64_t sendInterval) noexcept
176- : m_prefix(detail::sanitizePrefix(prefix)), m_sender(new UDPSender{host, port, batchsize, sendInterval}) {
184+ const uint64_t sendInterval,
185+ const unsigned int gaugePrecision) noexcept
186+ : m_prefix(detail::sanitizePrefix(prefix)),
187+ m_sender(new UDPSender{host, port, batchsize, sendInterval}),
188+ m_gaugePrecision(gaugePrecision) {
177189 // Initialize the random generator to be used for sampling
178190 seed ();
179191 // Avoid re-allocations by reserving a generous buffer
@@ -184,9 +196,11 @@ inline void StatsdClient::setConfig(const std::string& host,
184196 const uint16_t port,
185197 const std::string& prefix,
186198 const uint64_t batchsize,
187- const uint64_t sendInterval) noexcept {
199+ const uint64_t sendInterval,
200+ const unsigned int gaugePrecision) noexcept {
188201 m_prefix = detail::sanitizePrefix (prefix);
189202 m_sender.reset (new UDPSender (host, port, batchsize, sendInterval));
203+ m_gaugePrecision = gaugePrecision;
190204}
191205
192206inline const std::string& StatsdClient::errorMessage () const noexcept {
@@ -196,45 +210,47 @@ inline const std::string& StatsdClient::errorMessage() const noexcept {
196210inline void StatsdClient::decrement (const std::string& key,
197211 float frequency,
198212 const std::vector<std::string>& tags) const noexcept {
199- return count (key, -1 , frequency, tags);
213+ count (key, -1 , frequency, tags);
200214}
201215
202216inline void StatsdClient::increment (const std::string& key,
203217 float frequency,
204218 const std::vector<std::string>& tags) const noexcept {
205- return count (key, 1 , frequency, tags);
219+ count (key, 1 , frequency, tags);
206220}
207221
208222inline void StatsdClient::count (const std::string& key,
209223 const int delta,
210224 float frequency,
211225 const std::vector<std::string>& tags) const noexcept {
212- return send (key, delta, detail::METRIC_TYPE_COUNT, frequency, tags);
226+ send (key, delta, detail::METRIC_TYPE_COUNT, frequency, tags);
213227}
214228
229+ template <typename T>
215230inline void StatsdClient::gauge (const std::string& key,
216- const unsigned int value,
231+ const T value,
217232 const float frequency,
218233 const std::vector<std::string>& tags) const noexcept {
219- return send (key, value, detail::METRIC_TYPE_GAUGE, frequency, tags);
234+ send (key, value, detail::METRIC_TYPE_GAUGE, frequency, tags);
220235}
221236
222237inline void StatsdClient::timing (const std::string& key,
223238 const unsigned int ms,
224239 float frequency,
225240 const std::vector<std::string>& tags) const noexcept {
226- return send (key, ms, detail::METRIC_TYPE_TIMING, frequency, tags);
241+ send (key, ms, detail::METRIC_TYPE_TIMING, frequency, tags);
227242}
228243
229244inline void StatsdClient::set (const std::string& key,
230245 const unsigned int sum,
231246 float frequency,
232247 const std::vector<std::string>& tags) const noexcept {
233- return send (key, sum, detail::METRIC_TYPE_SET, frequency, tags);
248+ send (key, sum, detail::METRIC_TYPE_SET, frequency, tags);
234249}
235250
251+ template <typename T>
236252inline void StatsdClient::send (const std::string& key,
237- const int value,
253+ const T value,
238254 const char * type,
239255 float frequency,
240256 const std::vector<std::string>& tags) const noexcept {
@@ -255,6 +271,9 @@ inline void StatsdClient::send(const std::string& key,
255271 }
256272
257273 // Format the stat message
274+ std::stringstream valueStream;
275+ valueStream << std::fixed << std::setprecision (m_gaugePrecision) << value;
276+
258277 m_buffer.clear ();
259278
260279 m_buffer.append (m_prefix);
@@ -264,7 +283,7 @@ inline void StatsdClient::send(const std::string& key,
264283
265284 m_buffer.append (key);
266285 m_buffer.push_back (' :' );
267- m_buffer.append (std::to_string (value ));
286+ m_buffer.append (valueStream. str ( ));
268287 m_buffer.push_back (' |' );
269288 m_buffer.append (type);
270289
0 commit comments