Skip to content

Commit fe5567e

Browse files
committed
Place the include files under include/cpp-statsd-client to be more standard
Format the code
1 parent cdb99e1 commit fe5567e

File tree

4 files changed

+93
-173
lines changed

4 files changed

+93
-173
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ cmake_minimum_required(VERSION 3.2)
22

33
project(StatsdClient)
44

5-
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
5+
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/cpp-statsd-client)
66

7-
install(FILES src/UDPSender.hpp src/StatsdClient.hpp DESTINATION include)
7+
install(FILES UDPSender.hpp StatsdClient.hpp DESTINATION include)
88

99
find_package(Threads)
1010
add_executable(testStatsdClient ${CMAKE_CURRENT_SOURCE_DIR}/tests/testStatsdClient.cpp)

src/StatsdClient.hpp renamed to include/cpp-statsd-client/StatsdClient.hpp

Lines changed: 37 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
#include <string>
77
#include "UDPSender.hpp"
88

9-
namespace Statsd
10-
{
9+
namespace Statsd {
1110
/*!
1211
*
1312
* Statsd client
@@ -20,159 +19,124 @@ namespace Statsd
2019
* set if batching is not desired.
2120
*
2221
*/
23-
class StatsdClient
24-
{
22+
class StatsdClient {
2523
public:
26-
2724
//!@name Constructor and destructor
2825
//!@{
2926

3027
//! Constructor
31-
StatsdClient(
32-
const std::string& host,
33-
const uint16_t port,
34-
const std::string& prefix,
35-
const std::experimental::optional<uint64_t> batchsize = std::experimental::nullopt) noexcept;
28+
StatsdClient(const std::string &host,
29+
const uint16_t port,
30+
const std::string &prefix,
31+
const std::experimental::optional<uint64_t> batchsize = std::experimental::nullopt) noexcept;
3632

3733
//!@}
3834

3935
//!@name Methods
4036
//!@{
4137

4238
//! Sets a configuration { host, port, prefix }
43-
inline void setConfig(const std::string& host, const uint16_t port, const std::string& prefix) noexcept;
39+
inline void setConfig(const std::string &host, const uint16_t port, const std::string &prefix) noexcept;
4440

4541
//! Returns the error message as an optional std::string
4642
inline std::experimental::optional<std::string> errorMessage() const noexcept;
4743

4844
//! Increments the key, at a given frequency rate
49-
inline void increment(const std::string& key, const float frequency = 1.0f) const noexcept;
45+
inline void increment(const std::string &key, const float frequency = 1.0f) const noexcept;
5046

5147
//! Increments the key, at a given frequency rate
52-
inline void decrement(const std::string& key, const float frequency = 1.0f) const noexcept;
48+
inline void decrement(const std::string &key, const float frequency = 1.0f) const noexcept;
5349

5450
//! Adjusts the specified key by a given delta, at a given frequency rate
55-
inline void count(const std::string& key, const int delta, const float frequency = 1.0f) const noexcept;
51+
inline void count(const std::string &key, const int delta, const float frequency = 1.0f) const noexcept;
5652

5753
//! Records a gauge for the key, with a given value, at a given frequency rate
58-
inline void gauge(const std::string& key, const unsigned int value, const float frequency = 1.0f) const noexcept;
54+
inline void gauge(const std::string &key, const unsigned int value, const float frequency = 1.0f) const noexcept;
5955

6056
//! Records a timing for a key, at a given frequency
61-
inline void timing(const std::string& key, const unsigned int ms, const float frequency = 1.0f) const noexcept;
57+
inline void timing(const std::string &key, const unsigned int ms, const float frequency = 1.0f) const noexcept;
6258

6359
//! Send a value for a key, according to its type, at a given frequency
64-
void send(const std::string& key, const int value, const std::string& type, const float frequency = 1.0f) const noexcept;
60+
void send(const std::string &key, const int value, const std::string &type, const float frequency = 1.0f) const
61+
noexcept;
6562

6663
//!@}
6764

6865
private:
69-
7066
//! The prefix to be used for metrics
7167
std::string m_prefix;
7268

7369
//! The UDP sender to be used for actual sending
7470
mutable UDPSender m_sender;
7571
};
7672

77-
StatsdClient::
78-
StatsdClient(
79-
const std::string& host,
80-
const uint16_t port,
81-
const std::string& prefix,
82-
const std::experimental::optional<uint64_t> batchsize) noexcept
83-
: m_prefix(prefix)
84-
, m_sender(host, port, batchsize)
85-
{
73+
StatsdClient::StatsdClient(const std::string &host,
74+
const uint16_t port,
75+
const std::string &prefix,
76+
const std::experimental::optional<uint64_t> batchsize) noexcept
77+
: m_prefix(prefix), m_sender(host, port, batchsize) {
8678
// Initialize the randorm generator to be used for sampling
8779
std::srand(time(nullptr));
8880
}
8981

90-
void
91-
StatsdClient::
92-
setConfig(const std::string& host, const uint16_t port, const std::string& prefix) noexcept
93-
{
82+
void StatsdClient::setConfig(const std::string &host, const uint16_t port, const std::string &prefix) noexcept {
9483
m_prefix = prefix;
9584
m_sender.setConfig(host, port);
9685
}
9786

98-
std::experimental::optional<std::string>
99-
StatsdClient::
100-
errorMessage() const noexcept
101-
{
87+
std::experimental::optional<std::string> StatsdClient::errorMessage() const noexcept {
10288
return m_sender.errorMessage();
10389
}
10490

105-
void
106-
StatsdClient::
107-
decrement(const std::string& key, const float frequency) const noexcept
108-
{
91+
void StatsdClient::decrement(const std::string &key, const float frequency) const noexcept {
10992
return count(key, -1, frequency);
11093
}
11194

112-
void
113-
StatsdClient::
114-
increment(const std::string& key, const float frequency) const noexcept
115-
{
95+
void StatsdClient::increment(const std::string &key, const float frequency) const noexcept {
11696
return count(key, 1, frequency);
11797
}
11898

119-
void
120-
StatsdClient::
121-
count(const std::string& key, const int delta, const float frequency) const noexcept
122-
{
99+
void StatsdClient::count(const std::string &key, const int delta, const float frequency) const noexcept {
123100
return send(key, delta, "c", frequency);
124101
}
125102

126-
void
127-
StatsdClient::
128-
gauge(const std::string& key, const unsigned int value, const float frequency) const noexcept
129-
{
103+
void StatsdClient::gauge(const std::string &key, const unsigned int value, const float frequency) const noexcept {
130104
return send(key, value, "g", frequency);
131105
}
132106

133-
void
134-
StatsdClient::
135-
timing(const std::string& key, const unsigned int ms, const float frequency) const noexcept
136-
{
107+
void StatsdClient::timing(const std::string &key, const unsigned int ms, const float frequency) const noexcept {
137108
return send(key, ms, "ms", frequency);
138109
}
139110

140-
void
141-
StatsdClient::
142-
send(const std::string& key, const int value, const std::string& type, const float frequency) const noexcept
143-
{
144-
const auto isFrequencyOne = [](const float frequency) noexcept
145-
{
146-
constexpr float epsilon{ 0.0001f };
111+
void StatsdClient::send(const std::string &key, const int value, const std::string &type, const float frequency) const
112+
noexcept {
113+
const auto isFrequencyOne = [](const float frequency) noexcept {
114+
constexpr float epsilon{0.0001f};
147115
return std::fabs(frequency - 1.0f) < epsilon;
148116
};
149117

150118
// Test if one should send or not, according to the frequency rate
151-
if (!isFrequencyOne(frequency))
152-
{
153-
if (frequency < static_cast<float>(std::rand()) / RAND_MAX)
154-
{
119+
if (!isFrequencyOne(frequency)) {
120+
if (frequency < static_cast<float>(std::rand()) / RAND_MAX) {
155121
return;
156122
}
157123
}
158124

159125
// Prepare the buffer, with a sampling rate if specified different from 1.0f
160126
char buffer[256];
161-
if (isFrequencyOne(frequency))
162-
{
127+
if (isFrequencyOne(frequency)) {
163128
// Sampling rate is 1.0f, no need to specify it
164129
std::snprintf(buffer, sizeof(buffer), "%s%s:%d|%s", m_prefix.c_str(), key.c_str(), value, type.c_str());
165-
}
166-
else
167-
{
130+
} else {
168131
// Sampling rate is different from 1.0f, hence specify it
169-
std::snprintf(buffer, sizeof(buffer), "%s%s:%d|%s|@%.2f", m_prefix.c_str(), key.c_str(), value, type.c_str(), frequency);
132+
std::snprintf(
133+
buffer, sizeof(buffer), "%s%s:%d|%s|@%.2f", m_prefix.c_str(), key.c_str(), value, type.c_str(), frequency);
170134
}
171135

172136
// Send the message via the UDP sender
173137
m_sender.send(buffer);
174138
}
175139

176-
}
140+
} // namespace Statsd
177141

178142
#endif

0 commit comments

Comments
 (0)