-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfluxed_statsd_client.h
189 lines (165 loc) · 5.87 KB
/
influxed_statsd_client.h
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
#pragma once
#include <string>
#include <utility>
#include <vector>
#include "./non_blocking_sender.h"
namespace base {
typedef std::pair<std::string, std::string> TAG;
typedef std::vector<TAG> TAGS;
typedef statsd::AbstractSender Sender;
/**
* Non blocking statsd client with influxdb extension
* Best practice: please use a shared instance in you application
*/
class InfluxedStatsdClient {
public:
InfluxedStatsdClient();
explicit InfluxedStatsdClient(std::string ns);
~InfluxedStatsdClient();
/**
* Initialize client with a specific sender. Serve the purpose to do unit test.
* Not recommend to be used in practice unless you know what you are doing.
*/
explicit InfluxedStatsdClient(Sender* sender);
// statsd high level apis
public:
/**
* Adjusts the specified counter by a given delta.
*
* This method is non-blocking and is guaranteed not to throw an exception\
*
* @param key
* the name of the counter to adjust
* @param value
* the amount to adjust the counter by
* @param sampleRate
* the sampling rate being employed. For example, a rate of 0.1 would tell StatsD that this counter is being sent
* sampled every 1/10th of the time.
*/
void Count(const std::string& key, int64 value, float sampleRate = 1.0) const;
/**
* Convenience method equivalent to {Count(key, 1, sampleRate)} with a value of 1.
*/
void Inc(const std::string& key, float sampleRate = 1.0) const;
/**
* Convenience method equivalent to {Count(key, -1, sampleRate)} with a value of -1.
*/
void Dec(const std::string& key, float sampleRate = 1.0) const;
/**
* Records the latest fixed value for the specified named gauge.
*
* This method is non-blocking and is guaranteed not to throw an exception.
*
* @param key
* the name of the gauge
* @param value
* the new reading of the gauge
* @param sampleRate
* the sampling rate being employed. For example, a rate of 0.1 would tell StatsD that this counter is being sent
* sampled every 1/10th of the time.
*/
void Gauge(const std::string& key, double value, float sampleRate = 1.0) const;
/**
* Records an execution time in milliseconds for the specified named operation.
*
* This method is non-blocking and is guaranteed not to throw an exception.
*
* @param key
* the name of the timed operation
* @param ms
* the time in milliseconds
* @param sampleRate
* the sampling rate being employed. For example, a rate of 0.1 would tell StatsD that this counter is being sent
* sampled every 1/10th of the time.
*/
void Time(const std::string& key, int64 ms, float sampleRate = 1.0) const;
/**
* Records an execution time in milliseconds for the specified named operation and systemTimeMillisAtStart.
*
* This method is non-blocking and is guaranteed not to throw an exception.
*
* @param key
* the name of the timed operation
* @param ms
* the time in milliseconds
* @param sampleRate
* the sampling rate being employed. For example, a rate of 0.1 would tell StatsD that this counter is being sent
* sampled every 1/10th of the time.
*/
void TimeMillisToNow(const std::string& key, int64 systemTimeMillisAtStart, float sampleRate = 1.0) const;
/**
* Records an execution time in milliseconds for the specified named operation and systemTimeMicrosAtStart.
*
* This method is non-blocking and is guaranteed not to throw an exception.
*
* @param key
* the name of the timed operation
* @param ms
* the time in milliseconds
* @param sampleRate
* the sampling rate being employed. For example, a rate of 0.1 would tell StatsD that this counter is being sent
* sampled every 1/10th of the time.
*/
void TimeMicrosToNow(const std::string& key, int64 systemTimeMicrosAtStart, float sampleRate = 1.0) const;
// statsd low level apis
public:
/* (Low Level Api) manually send a message, all high level apis will eventually invoke this api
* type = "c", "g" or "ms"
*/
void Send(const std::string& key, const std::string value, const std::string& type, float sampleRate = 1.0) const;
void Send(const std::string& key, const int64 value, const std::string& type, float sampleRate = 1.0) const;
// helpers
public:
/**
* Clone a new instance
*/
InfluxedStatsdClient Clone() const;
/**
* Make a new InfluxedStatsdClient with a new @param ns based on current one.
*/
InfluxedStatsdClient Ns(std::string ns) const;
/**
* Make a new InfluxedStatsdClient with a new @param subNs based on current one.
* Will generate a new client with namespace as ns + "." + subNs
*/
InfluxedStatsdClient ImmutableApendSubNs(std::string subNs) const;
/**
* append subNs to current ns.
* Warning: please aware the side effect
*/
InfluxedStatsdClient& ApendSubNs(std::string subNs);
/**
* Make a new InfluxedStatsdClient with a new @param tags based on current one.
*/
InfluxedStatsdClient Tags(TAGS tags) const;
/**
* Make a new InfluxedStatsdClient with a new @param tag inserted based on current one.
* Will generate a new client with namespace as ns + "." + subNs
*/
InfluxedStatsdClient ImmutableAddTag(TAG tag) const;
/**
* Add a new tag to current tags
* Warning: please aware the side effect
*/
InfluxedStatsdClient& AddTag(TAG tag);
private:
std::string makeInfluxedKey(const std::string& key) const;
InfluxedStatsdClient(Sender* sender_, const std::string& ns, const TAGS& tags);
private:
Sender* sender_;
/**
* every sending keys will has ns_ as prefix
*/
std::string ns_;
/**
* extend keys with tags_ to support influxdb protocol
* PS: ',' and '=' is not allowed in tag name and its value
*/
TAGS tags_;
static std::string NS_DEL;
static std::string COMMA;
static std::string EMPTY;
static std::string TAG_EQ;
};
}
// end namespace