-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
statsd.hpp
198 lines (173 loc) · 5.13 KB
/
statsd.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* StatsD Client
*
* Copyright (c) 2012-2014 Axel Etcheverry
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
#pragma once
#include <string>
#include <random>
# include <iostream>
#ifdef _WIN32
#include <winsock2.h>
#else
#include <netinet/in.h>
#endif
# define statsd_error(message) \
std::cerr << "StatsD: " << message << std::endl;
class statsd
{
public:
/**
* Open udp/tcp socket
*
* @param host The host of StatsD server
* @param port The port of StatsD server
* @param mode SOCK_DGRAM/SOCK_STREAM for UDP/TCP
* return int 0 for success.
*/
static int open(const std::string& host, int16_t port = 8125, int mode = SOCK_DGRAM);
/**
* Log timing information
*
* @param key The metric to in log timing info for.
* @param value The ellapsed time (ms) to log
* @param sample_rate The rate (0-1) for sampling.
*/
static void timing(const std::string& key, const int64_t value, const float sample_rate = 1.0);
/**
* Increments one or more stats counters
*
* @param key The metric(s) to increment.
* @param sample_rate The rate (0-1) for sampling.
*/
static void increment(const std::string& key, const float sample_rate = 1.0);
/**
* Decrements one or more stats counters.
*
* @param key The metric(s) to decrement.
* @param sample_rate The rate (0-1) for sampling.
*/
static void decrement(const std::string& key, const float sample_rate = 1.0);
/**
* Count is the default statsd method for counting
*
* @param key The metric(s) to count
* @param value The count value
* @param sample_rate The rate (0-1) for sampling.
*/
static void count(const std::string& key, const int64_t value, const float sample_rate = 1.0);
/**
* Gauge
*
* @param key The metric(s) to count
* @param value The count value
* @param sample_rate The rate (0-1) for sampling.
*/
static void gauge(const std::string& key, const int64_t value, const float sample_rate = 1.0);
/**
* gaugeIncBy
*
* @param key The metric(s) to increment
* @param value The increment value
* @param sample_rate The rate (0-1) for sampling.
*/
static void gaugeIncBy(const std::string& key, const int64_t value, const float sample_rate = 1.0);
/**
* gaugeDecBy
*
* @param key The metric(s) to decrement
* @param value The decrement value
* @param sample_rate The rate (0-1) for sampling.
*/
static void gaugeDecBy(const std::string& key, const int64_t value, const float sample_rate = 1.0);
/**
* Set
*
* @param key The metric(s) to count
* @param value The count value
* @param sample_rate The rate (0-1) for sampling.
*/
static void set(const std::string& key, const int64_t value, const float sample_rate = 1.0);
/**
* setPrefix
*
* @param prefix The prefix to prepend
*/
static void setPrefix(const std::string& _prefix);
/**
* setGlobalTags
*
* @param global_tags The global tags
*/
static void setGlobalTags(std::vector<std::string> global_tags);
/**
* Close socket
*/
static void close();
/**
* Normalize key
*
* @param key The key of metric(s)
* @return The normalized key
*/
static std::string normalize(const std::string& key);
/**
* Prepare message
*
* @param key The grahite node
* @param value The count value
* @param sample_rate The rate (0-1) for sampling.
* @param unit The unit value
* @param sign The sign (for gauges)
* @return The message
*/
static std::string prepare(
const std::string& key,
const int64_t value,
const std::vector<std::string> tags,
const float sample_rate,
const std::string& unit,
const std::string& sign = ""
);
/**
* Get version
*
* @return The string of version
*/
static const char* version();
private:
struct statsd_t
{
struct sockaddr_in server = {};
int sock = -1;
int type = SOCK_DGRAM;
};
static statsd_t info;
static std::string prefix;
static std::string global_tags_str;
/**
* Send
*
* @param key The grahite node
* @param value The count value
* @param sample_rate The rate (0-1) for sampling.
* @param unit The unit value
* @param sign The sign (for gauges)
*/
static void send(
const std::string& key,
const int64_t value,
const float sample_rate,
const std::string& unit,
const std::string& sign = ""
);
/**
*
* @param sample_rate The rate (0-1) for sampling.
* @return True if send
*/
static bool should_send(const float sample_rate);
};