-
Notifications
You must be signed in to change notification settings - Fork 18
/
client.go
375 lines (320 loc) · 11.6 KB
/
client.go
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package statsd
/*
Copyright (c) 2017 Andrey Smirnov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import (
"log"
"os"
"strconv"
"sync"
"sync/atomic"
"time"
)
// Client implements statsd client
type Client struct {
trans *transport
metricPrefix string
defaultTags []Tag
}
type transport struct {
// these fields are updated with atomic operations,
// so they should be at the top for proper alignment
lostPacketsPeriod int64
lostPacketsOverall int64
maxPacketSize int
tagFormat *TagFormat
bufPool chan []byte
buf []byte
bufSize int
bufLock sync.Mutex
sendQueue chan []byte
shutdown chan struct{}
shutdownOnce sync.Once
shutdownWg sync.WaitGroup
}
// NewClient creates new statsd client and starts background processing
//
// Client connects to statsd server at addr ("host:port")
//
// Client settings could be controlled via functions of type Option
func NewClient(addr string, options ...Option) *Client {
opts := ClientOptions{
Addr: addr,
AddrNetwork: DefaultNetwork,
MetricPrefix: DefaultMetricPrefix,
MaxPacketSize: DefaultMaxPacketSize,
FlushInterval: DefaultFlushInterval,
ReconnectInterval: DefaultReconnectInterval,
ReportInterval: DefaultReportInterval,
RetryTimeout: DefaultRetryTimeout,
Logger: log.New(os.Stderr, DefaultLogPrefix, log.LstdFlags),
BufPoolCapacity: DefaultBufPoolCapacity,
SendQueueCapacity: DefaultSendQueueCapacity,
SendLoopCount: DefaultSendLoopCount,
TagFormat: TagFormatInfluxDB,
}
c := &Client{
trans: &transport{
shutdown: make(chan struct{}),
},
}
// 1024 is room for overflow metric
c.trans.bufSize = opts.MaxPacketSize + 1024
for _, option := range options {
option(&opts)
}
c.metricPrefix = opts.MetricPrefix
c.defaultTags = opts.DefaultTags
c.trans.tagFormat = opts.TagFormat
c.trans.maxPacketSize = opts.MaxPacketSize
c.trans.buf = make([]byte, 0, c.trans.bufSize)
c.trans.bufPool = make(chan []byte, opts.BufPoolCapacity)
c.trans.sendQueue = make(chan []byte, opts.SendQueueCapacity)
go c.trans.flushLoop(opts.FlushInterval)
for i := 0; i < opts.SendLoopCount; i++ {
c.trans.shutdownWg.Add(1)
go c.trans.sendLoop(opts.Addr, opts.AddrNetwork, opts.ReconnectInterval, opts.RetryTimeout, opts.Logger)
}
if opts.ReportInterval > 0 {
c.trans.shutdownWg.Add(1)
go c.trans.reportLoop(opts.ReportInterval, opts.Logger)
}
return c
}
// Close stops the client and all its clones. Calling it on a clone has the
// same effect as calling it on the original client - it is stopped with all
// its clones.
func (c *Client) Close() error {
c.trans.close()
return nil
}
func (t *transport) close() {
t.shutdownOnce.Do(func() {
close(t.shutdown)
})
t.shutdownWg.Wait()
}
// CloneWithPrefix returns a clone of the original client with different metricPrefix.
func (c *Client) CloneWithPrefix(prefix string) *Client {
clone := *c
clone.metricPrefix = prefix
return &clone
}
// CloneWithPrefixExtension returns a clone of the original client with the
// original prefixed extended with the specified string.
func (c *Client) CloneWithPrefixExtension(extension string) *Client {
clone := *c
clone.metricPrefix = clone.metricPrefix + extension
return &clone
}
// GetLostPackets returns number of packets lost during client lifecycle
func (c *Client) GetLostPackets() int64 {
return atomic.LoadInt64(&c.trans.lostPacketsOverall)
}
// Incr increments a counter metric
//
// Often used to note a particular event, for example incoming web request.
func (c *Client) Incr(stat string, count int64, tags ...Tag) {
if count != 0 {
c.trans.bufLock.Lock()
lastLen := len(c.trans.buf)
c.trans.buf = append(c.trans.buf, []byte(c.metricPrefix)...)
c.trans.buf = append(c.trans.buf, []byte(stat)...)
if c.trans.tagFormat.Placement == TagPlacementName {
c.trans.buf = c.formatTags(c.trans.buf, tags)
}
c.trans.buf = append(c.trans.buf, ':')
c.trans.buf = strconv.AppendInt(c.trans.buf, count, 10)
c.trans.buf = append(c.trans.buf, []byte("|c")...)
if c.trans.tagFormat.Placement == TagPlacementSuffix {
c.trans.buf = c.formatTags(c.trans.buf, tags)
}
c.trans.buf = append(c.trans.buf, '\n')
c.trans.checkBuf(lastLen)
c.trans.bufLock.Unlock()
}
}
// Decr decrements a counter metri
//
// Often used to note a particular event
func (c *Client) Decr(stat string, count int64, tags ...Tag) {
c.Incr(stat, -count, tags...)
}
// FIncr increments a float counter metric
func (c *Client) FIncr(stat string, count float64, tags ...Tag) {
if count != 0 {
c.trans.bufLock.Lock()
lastLen := len(c.trans.buf)
c.trans.buf = append(c.trans.buf, []byte(c.metricPrefix)...)
c.trans.buf = append(c.trans.buf, []byte(stat)...)
if c.trans.tagFormat.Placement == TagPlacementName {
c.trans.buf = c.formatTags(c.trans.buf, tags)
}
c.trans.buf = append(c.trans.buf, ':')
c.trans.buf = strconv.AppendFloat(c.trans.buf, count, 'f', -1, 64)
c.trans.buf = append(c.trans.buf, []byte("|c")...)
if c.trans.tagFormat.Placement == TagPlacementSuffix {
c.trans.buf = c.formatTags(c.trans.buf, tags)
}
c.trans.buf = append(c.trans.buf, '\n')
c.trans.checkBuf(lastLen)
c.trans.bufLock.Unlock()
}
}
// FDecr decrements a float counter metric
func (c *Client) FDecr(stat string, count float64, tags ...Tag) {
c.FIncr(stat, -count, tags...)
}
// Timing tracks a duration event, the time delta must be given in milliseconds
func (c *Client) Timing(stat string, delta int64, tags ...Tag) {
c.trans.bufLock.Lock()
lastLen := len(c.trans.buf)
c.trans.buf = append(c.trans.buf, []byte(c.metricPrefix)...)
c.trans.buf = append(c.trans.buf, []byte(stat)...)
if c.trans.tagFormat.Placement == TagPlacementName {
c.trans.buf = c.formatTags(c.trans.buf, tags)
}
c.trans.buf = append(c.trans.buf, ':')
c.trans.buf = strconv.AppendInt(c.trans.buf, delta, 10)
c.trans.buf = append(c.trans.buf, []byte("|ms")...)
if c.trans.tagFormat.Placement == TagPlacementSuffix {
c.trans.buf = c.formatTags(c.trans.buf, tags)
}
c.trans.buf = append(c.trans.buf, '\n')
c.trans.checkBuf(lastLen)
c.trans.bufLock.Unlock()
}
// PrecisionTiming track a duration event, the time delta has to be a duration
//
// Usually request processing time, time to run database query, etc. are used with
// this metric type.
func (c *Client) PrecisionTiming(stat string, delta time.Duration, tags ...Tag) {
c.trans.bufLock.Lock()
lastLen := len(c.trans.buf)
c.trans.buf = append(c.trans.buf, []byte(c.metricPrefix)...)
c.trans.buf = append(c.trans.buf, []byte(stat)...)
if c.trans.tagFormat.Placement == TagPlacementName {
c.trans.buf = c.formatTags(c.trans.buf, tags)
}
c.trans.buf = append(c.trans.buf, ':')
c.trans.buf = strconv.AppendFloat(c.trans.buf, float64(delta)/float64(time.Millisecond), 'f', -1, 64)
c.trans.buf = append(c.trans.buf, []byte("|ms")...)
if c.trans.tagFormat.Placement == TagPlacementSuffix {
c.trans.buf = c.formatTags(c.trans.buf, tags)
}
c.trans.buf = append(c.trans.buf, '\n')
c.trans.checkBuf(lastLen)
c.trans.bufLock.Unlock()
}
func (c *Client) igauge(stat string, sign []byte, value int64, tags ...Tag) {
c.trans.bufLock.Lock()
lastLen := len(c.trans.buf)
c.trans.buf = append(c.trans.buf, []byte(c.metricPrefix)...)
c.trans.buf = append(c.trans.buf, []byte(stat)...)
if c.trans.tagFormat.Placement == TagPlacementName {
c.trans.buf = c.formatTags(c.trans.buf, tags)
}
c.trans.buf = append(c.trans.buf, ':')
c.trans.buf = append(c.trans.buf, sign...)
c.trans.buf = strconv.AppendInt(c.trans.buf, value, 10)
c.trans.buf = append(c.trans.buf, []byte("|g")...)
if c.trans.tagFormat.Placement == TagPlacementSuffix {
c.trans.buf = c.formatTags(c.trans.buf, tags)
}
c.trans.buf = append(c.trans.buf, '\n')
c.trans.checkBuf(lastLen)
c.trans.bufLock.Unlock()
}
// Gauge sets or updates constant value for the interval
//
// Gauges are a constant data type. They are not subject to averaging,
// and they don’t change unless you change them. That is, once you set a gauge value,
// it will be a flat line on the graph until you change it again. If you specify
// delta to be true, that specifies that the gauge should be updated, not set. Due to the
// underlying protocol, you can't explicitly set a gauge to a negative number without
// first setting it to zero.
func (c *Client) Gauge(stat string, value int64, tags ...Tag) {
if value < 0 {
c.igauge(stat, nil, 0, tags...)
}
c.igauge(stat, nil, value, tags...)
}
// GaugeDelta sends a change for a gauge
func (c *Client) GaugeDelta(stat string, value int64, tags ...Tag) {
// Gauge Deltas are always sent with a leading '+' or '-'. The '-' takes care of itself but the '+' must added by hand
if value < 0 {
c.igauge(stat, nil, value, tags...)
} else {
c.igauge(stat, []byte{'+'}, value, tags...)
}
}
func (c *Client) fgauge(stat string, sign []byte, value float64, tags ...Tag) {
c.trans.bufLock.Lock()
lastLen := len(c.trans.buf)
c.trans.buf = append(c.trans.buf, []byte(c.metricPrefix)...)
c.trans.buf = append(c.trans.buf, []byte(stat)...)
if c.trans.tagFormat.Placement == TagPlacementName {
c.trans.buf = c.formatTags(c.trans.buf, tags)
}
c.trans.buf = append(c.trans.buf, ':')
c.trans.buf = append(c.trans.buf, sign...)
c.trans.buf = strconv.AppendFloat(c.trans.buf, value, 'f', -1, 64)
c.trans.buf = append(c.trans.buf, []byte("|g")...)
if c.trans.tagFormat.Placement == TagPlacementSuffix {
c.trans.buf = c.formatTags(c.trans.buf, tags)
}
c.trans.buf = append(c.trans.buf, '\n')
c.trans.checkBuf(lastLen)
c.trans.bufLock.Unlock()
}
// FGauge sends a floating point value for a gauge
func (c *Client) FGauge(stat string, value float64, tags ...Tag) {
if value < 0 {
c.igauge(stat, nil, 0, tags...)
}
c.fgauge(stat, nil, value, tags...)
}
// FGaugeDelta sends a floating point change for a gauge
func (c *Client) FGaugeDelta(stat string, value float64, tags ...Tag) {
if value < 0 {
c.fgauge(stat, nil, value, tags...)
} else {
c.fgauge(stat, []byte{'+'}, value, tags...)
}
}
// SetAdd adds unique element to a set
//
// Statsd server will provide cardinality of the set over aggregation period.
func (c *Client) SetAdd(stat string, value string, tags ...Tag) {
c.trans.bufLock.Lock()
lastLen := len(c.trans.buf)
c.trans.buf = append(c.trans.buf, []byte(c.metricPrefix)...)
c.trans.buf = append(c.trans.buf, []byte(stat)...)
if c.trans.tagFormat.Placement == TagPlacementName {
c.trans.buf = c.formatTags(c.trans.buf, tags)
}
c.trans.buf = append(c.trans.buf, ':')
c.trans.buf = append(c.trans.buf, []byte(value)...)
c.trans.buf = append(c.trans.buf, []byte("|s")...)
if c.trans.tagFormat.Placement == TagPlacementSuffix {
c.trans.buf = c.formatTags(c.trans.buf, tags)
}
c.trans.buf = append(c.trans.buf, '\n')
c.trans.checkBuf(lastLen)
c.trans.bufLock.Unlock()
}