Skip to content

Commit bae575e

Browse files
committed
docs: update README
1 parent a06a5b9 commit bae575e

File tree

2 files changed

+85
-65
lines changed

2 files changed

+85
-65
lines changed

README.md

Lines changed: 84 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,15 @@ The client allows:
1616

1717
## Install and Test
1818

19-
### Makefile
20-
2119
In order to install the header files and/or run the tests, simply use the Makefile and execute
2220

23-
```sh
24-
make install
25-
```
26-
27-
and
28-
2921
```sh
3022
make test
3123
```
3224

33-
### Conan
34-
35-
If you are using [Conan](https://www.conan.io/) to manage your dependencies, merely add statsdclient/x.y.z@vthiery/stable to your conanfile.py's requires, where x.y.z is the release version you want to use. Please file issues here if you experience problems with the packages. You can also directly download the latest version [here](https://bintray.com/vthiery/conan-packages/statsdclient%3Avthiery/_latestVersion).
36-
3725
## Usage
3826

39-
### Example
27+
### Simple example
4028

4129
A simple example of how to use the client:
4230

@@ -45,11 +33,8 @@ A simple example of how to use the client:
4533
using namespace Statsd;
4634

4735
int main() {
48-
// Define the client on localhost, with port 8080,
49-
// using a prefix,
50-
// a batching size of 20 bytes,
51-
// and three points of precision for floating point gauge values
52-
StatsdClient client{ "127.0.0.1", 8080, "myPrefix", 20, 3 };
36+
// Define the client on localhost, with port 8080 and using a prefix
37+
StatsdClient client{"localhost", 8080, "myPrefix"};
5338

5439
// Increment the metric "coco"
5540
client.increment("coco");
@@ -60,91 +45,126 @@ int main() {
6045
// Adjusts "toto" by +3
6146
client.count("toto", 2, 0.1f);
6247

63-
// Record a gauge "titi" to 3
64-
client.gauge("titi", 3);
48+
// Record a gauge "titi" to 3.2 with tags
49+
client.gauge("titi", 3.2, 0.1f, {"foo", "bar"});
6550

6651
// Record a timing of 2ms for "myTiming"
6752
client.timing("myTiming", 2, 0.1f);
6853

69-
// Send a metric explicitly
70-
client.send("tutu", 4, "c", 2.0f);
54+
// Record a count of unique occurences of "tutu"
55+
client.set("tutu", 4, 2.0f);
56+
7157
exit(0);
7258
}
7359
```
7460

75-
### Advanced Testing
61+
### Client configuration
7662

77-
A simple mock StatsD server can be found at `tests/StatsdServer.hpp`. This can be used to do simple validation of your application's metrics, typically in the form of unit tests. In fact this is the primary means by which this library is tested. The mock server itself is not distributed with the library so to use it you'd need to vendor this project into your project. Once you have though, you can test your application's use of the client like so:
63+
The configuration of the client must be input when one instantiates it. Nevertheless, the API allows the configuration ot change afterwards. For example, one can do the following:
7864

7965
```cpp
8066
#include "StatsdClient.hpp"
81-
#include "StatsdServer.hpp"
82-
83-
#include <cassert>
84-
8567
using namespace Statsd;
8668

87-
struct MyApp {
88-
void doWork() const {
89-
m_client.count("bar", 3);
90-
}
91-
private:
92-
StatsdClient m_client{"localhost", 8125, "foo"};
93-
};
69+
int main()
70+
{
71+
// Define the client on localhost, with port 8080 and using a prefix
72+
StatsdClient client{"localhost", 8080, "myPrefix"};
9473

95-
int main() {
96-
StatsdServer mockServer;
74+
client.increment("coco");
9775

98-
MyApp app;
99-
app.doWork();
76+
// Set a new configuration, using a different port and a different prefix
77+
client.setConfig("localhost", 8081, "anotherPrefix");
10078

101-
assert(mockServer.receive() == "foo.bar:3|c");
102-
exit(0);
79+
client.decrement("kiki");
10380
}
10481
```
10582

106-
### Configuration
83+
#### Batching
10784

108-
The configuration of the client must be input when one instantiates it. Nevertheless, the API allows the configuration ot change afterwards. For example, one can do the following:
85+
The client supports batching of the metrics. The batch size parameter is the number of bytes to allow in each batch (UDP datagram payload) to be sent to the statsd process. E.g.
10986

11087
```cpp
111-
#include "StatsdClient.hpp"
112-
using namespace Statsd;
88+
StatsdClient client{"localhost", 8080, "myPrefix", 64};
89+
```
11390
114-
int main()
115-
{
116-
// Define the client on localhost, with port 8080,
117-
// using a prefix,
118-
// a batching size of 20 bytes,
119-
// and three points of precision for floating point gauge values
120-
StatsdClient client{ "127.0.0.1", 8080, "myPrefix", 20, 3 };
91+
This number is not a hard limit. If appending the current stat to the current batch (separated by the `'\n'` character) pushes the current batch over the batch size, that batch is enqueued (not sent) and a new batch is started. If batch size is 0, the default, then each stat is sent individually to the statsd process and no batches are enqueued.
12192
122-
client.increment("coco");
93+
##### Sending interval
12394
124-
// Set a new configuration, using a different port, a different prefix, and more gauge precision
125-
client.setConfig("127.0.0.1", 8000, "anotherPrefix", 6);
95+
As previously mentioned, if batching is disabled (by setting the batch size to 0) then every stat is sent immediately in a blocking fashion.
12696
127-
client.decrement("kiki");
128-
}
97+
If batching is enabled (ie non-zero) then you may also set the send interval. E.g.
98+
99+
```cpp
100+
StatsdClient client{"localhost", 8080, "myPrefix", 64, 1000};
129101
```
130102

131-
The batchsize is the only parameter that cannot be changed for the time being.
103+
The send interval controls the time, in milliseconds, to wait before flushing/sending the queued stats batches to the statsd process. When the send interval is non-zero a background thread is spawned which will do the flushing/sending at the configured send interval, in other words asynchronously. The queuing mechanism in this case is _not_ lock-free. If batching is enabled but the send interval is set to zero then the queued batchs of stats will not be sent automatically by a background thread but must be sent manually via the `flush` method. The `flush` method is a blocking call.
132104

133-
### Batching
105+
##### Gauge precision
134106

135-
The client supports batching of the metrics. The batch size parameter is the number of bytes to allow in each batch (UDP datagram payload) to be sent to the statsd process. This number is not a hard limit. If appending the current stat to the current batch (separated by the `'\n'` character) pushes the current batch over the batch size, that batch is enqueued (not sent) and a new batch is started. If batch size is 0, the default, then each stat is sent individually to the statsd process and no batches are enqueued.
107+
Since gauge metrics support floats, it may be useful to configure the desired precision. The client support this via the `gaugePrecision` parameter. E.g.
136108

137-
### Sending
109+
```cpp
110+
StatsdClient client{"localhost", 8080, "myPrefix", 0, 0, 4};
111+
```
112+
113+
which would set a precision of `4` on every gauge value.
138114
139-
As previously mentioned, if batching is disabled (by setting the batch size to 0) then every stat is sent immediately in a blocking fashion. If batching is enabled (ie non-zero) then you may also set the send interval. The send interval controls the time, in milliseconds, to wait before flushing/sending the queued stats batches to the statsd process. When the send interval is non-zero a background thread is spawned which will do the flushing/sending at the configured send interval, in other words asynchronously. The queuing mechanism in this case is *not* lock-free. If batching is enabled but the send interval is set to zero then the queued batchs of stats will not be sent automatically by a background thread but must be sent manually via the `flush` method. The `flush` method is a blocking call.
115+
### Options when sending metrics
140116
117+
#### Frequency rate
141118
142-
### Frequency rate
119+
When sending a metric, a frequency rate can be set in order to limit the metrics' sampling. E.g.
120+
121+
```cpp
122+
client.gauge("titi", 3.2, 0.1f);
123+
```
143124

144-
When sending a metric, a frequency rate can be set in order to limit the metrics' sampling. By default, the frequency rate is set to one and won't affect the sampling. If set to a value `epsilon` (0.0001 for the time being) close to one, the sampling is not affected either.
125+
By default, the frequency rate is set to one and won't affect the sampling. If set to a value `epsilon` (0.0001 for the time being) close to one, the sampling is not affected either.
145126

146127
If the frequency rate is set and `epsilon` different from one, the sending will be rejected randomly (the higher the frequency rate, the lower the probability of rejection).
147128

129+
#### Tags
130+
131+
One may also attach tags to a metrics, e.g.
132+
133+
```cpp
134+
client.gauge("titi", 3.2, 0.1f, {"foo", "bar"});
135+
```
136+
137+
## Advanced Testing
138+
139+
A simple mock StatsD server can be found at `tests/StatsdServer.hpp`. This can be used to do simple validation of your application's metrics, typically in the form of unit tests. In fact this is the primary means by which this library is tested. The mock server itself is not distributed with the library so to use it you'd need to vendor this project into your project. Once you have though, you can test your application's use of the client like so:
140+
141+
```cpp
142+
#include "StatsdClient.hpp"
143+
#include "StatsdServer.hpp"
144+
145+
#include <cassert>
146+
147+
using namespace Statsd;
148+
149+
struct MyApp {
150+
void doWork() const {
151+
m_client.count("bar", 3);
152+
}
153+
private:
154+
StatsdClient m_client{"localhost", 8125, "foo"};
155+
};
156+
157+
int main() {
158+
StatsdServer mockServer;
159+
160+
MyApp app;
161+
app.doWork();
162+
163+
assert(mockServer.receive() == "foo.bar:3|c");
164+
exit(0);
165+
}
166+
```
167+
148168
## License
149169

150170
This library is under MIT license.

include/cpp-statsd-client/StatsdClient.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class StatsdClient {
7676
//!@name Methods
7777
//!@{
7878

79-
//! Sets a configuration { host, port, prefix, batchsize }
79+
//! Sets a configuration
8080
void setConfig(const std::string& host,
8181
const uint16_t port,
8282
const std::string& prefix,

0 commit comments

Comments
 (0)