11#ifndef STATSD_CLIENT_HPP
22#define STATSD_CLIENT_HPP
33
4+ #include < cstdlib>
45#include < experimental/optional>
56#include < string>
67#include " UDPSender.hpp"
@@ -33,9 +34,6 @@ class StatsdClient
3334 const std::string& prefix,
3435 const std::experimental::optional<uint64_t > batchsize = std::experimental::nullopt ) noexcept ;
3536
36- // ! Default destructor
37- ~StatsdClient () = default ;
38-
3937 // !@}
4038
4139 // !@name Methods
@@ -67,16 +65,6 @@ class StatsdClient
6765
6866 // !@}
6967
70- private:
71-
72- // @name Private methods
73- // @{
74-
75- // ! Returns a cleaned key
76- inline std::string clean (const std::string& key) const noexcept ;
77-
78- // @}
79-
8068private:
8169
8270 // ! The prefix to be used for metrics
@@ -96,7 +84,7 @@ StatsdClient(
9684, m_sender(host, port, batchsize)
9785{
9886 // Initialize the randorm generator to be used for sampling
99- srandom (time (NULL ));
87+ std::srand (time (nullptr ));
10088}
10189
10290void
@@ -162,48 +150,29 @@ send(const std::string& key, const int value, const std::string& type, const flo
162150 // Test if one should send or not, according to the frequency rate
163151 if (!isFrequencyOne (frequency))
164152 {
165- if (frequency < ( float ) random ( ) / RAND_MAX)
153+ if (frequency < static_cast < float >( std::rand () ) / RAND_MAX)
166154 {
167155 return ;
168156 }
169157 }
170158
171- // Clean the key
172- clean (key);
173-
174159 // Prepare the buffer, with a sampling rate if specified different from 1.0f
175160 char buffer[256 ];
176161 if (isFrequencyOne (frequency))
177162 {
178163 // Sampling rate is 1.0f, no need to specify it
179- snprintf (buffer, sizeof (buffer), " %s%s:%zd|%s" , m_prefix.c_str (), key.c_str (), static_cast <signed size_t >(value), type.c_str ());
164+ snprintf (buffer, sizeof (buffer), " %s%s:%zd|%s" , m_prefix.c_str (), key.c_str (), static_cast <int64_t >(value), type.c_str ());
180165 }
181166 else
182167 {
183168 // Sampling rate is different from 1.0f, hence specify it
184- snprintf (buffer, sizeof (buffer), " %s%s:%zd|%s|@%.2f" , m_prefix.c_str (), key.c_str (), static_cast <signed size_t >(value), type.c_str (), frequency);
169+ snprintf (buffer, sizeof (buffer), " %s%s:%zd|%s|@%.2f" , m_prefix.c_str (), key.c_str (), static_cast <int64_t >(value), type.c_str (), frequency);
185170 }
186171
187172 // Send the message via the UDP sender
188173 m_sender.send (buffer);
189174}
190175
191- std::string
192- StatsdClient::
193- clean (const std::string& key) const noexcept
194- {
195- std::string cleanKey = key;
196- size_t pos = key.find_first_of (" :|@" );
197-
198- // Add the '_' appropriately to the key
199- while (pos != std::string::npos)
200- {
201- cleanKey[pos] = ' _' ;
202- pos = cleanKey.find_first_of (" :|@" );
203- }
204- return cleanKey;
205- }
206-
207176}
208177
209178#endif
0 commit comments