forked from alexcesaro/statsd
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconn.go
363 lines (324 loc) · 8.48 KB
/
conn.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
package statsd
import (
"io"
"math"
"math/rand"
"net"
"strconv"
"strings"
"sync"
"time"
)
type conn struct {
// config
errorHandler func(error)
flushPeriod time.Duration
maxPacketSize int
tagFormat TagFormat
inlineFlush bool
// state
mu sync.Mutex // mu synchronises internal state
closed bool // closed indicates if w has been closed (triggered by first client close)
w io.WriteCloser // w is the writer for the connection
buf []byte // buf is the buffer for the connection
rateCache map[float32]string // rateCache caches string representations of sampling rates
trimTrailingNewline bool // trimTrailingNewline is for UDP, see also conn.flush
}
func newConn(conf connConfig, muted bool) (*conn, error) {
c := &conn{
errorHandler: conf.ErrorHandler,
flushPeriod: conf.FlushPeriod,
maxPacketSize: conf.MaxPacketSize,
tagFormat: conf.TagFormat,
inlineFlush: conf.InlineFlush,
w: conf.WriteCloser,
trimTrailingNewline: conf.TrimTrailingNewline,
}
// exit if muted
if muted {
// close and clear any provided writer
if c.w != nil {
_ = c.w.Close()
c.w = nil
}
// return muted client
return c, nil
}
if c.w == nil {
// initialise writer if not provided
if err := c.connect(conf.Network, conf.Addr, conf.UDPCheck == nil || *conf.UDPCheck); err != nil {
return c, err
}
} else if conf.UDPCheck != nil && *conf.UDPCheck {
// udp check was explicitly set
if err := c.udpCheck(); err != nil {
return c, err
}
}
// To prevent a buffer overflow add some capacity to the buffer to allow for
// an additional metric.
c.buf = make([]byte, 0, c.maxPacketSize+200)
// start the flush worker only if we have a rate and it's not unnecessary
if c.flushPeriod > 0 && !c.inlineFlush {
go c.flushWorker()
}
return c, nil
}
func (c *conn) flushWorker() {
ticker := time.NewTicker(c.flushPeriod)
defer ticker.Stop()
for range ticker.C {
if func() bool {
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return true
}
c.flush(0)
return false
}() {
return
}
}
}
// When using UDP do a quick check to see if something is listening on the
// given port to return an error as soon as possible.
//
// See also doc for UDPCheck option (factory func) and https://github.com/alexcesaro/statsd/issues/6
func (c *conn) udpCheck() error {
for i := 0; i < 2; i++ {
if _, err := c.w.Write(nil); err != nil {
_ = c.w.Close()
c.w = nil
return err
}
}
return nil
}
func (c *conn) connect(network string, address string, UDPCheck bool) error {
var err error
c.w, err = dialTimeout(network, address, 5*time.Second)
if err != nil {
return err
}
if strings.HasPrefix(network, "udp") {
// udp retains behavior from the original implementation where it would strip a trailing newline
c.trimTrailingNewline = true
if UDPCheck {
if err := c.udpCheck(); err != nil {
return err
}
}
}
return nil
}
func (c *conn) metric(prefix, bucket string, n interface{}, typ string, rate float32, tags string) {
c.mu.Lock()
l := len(c.buf)
c.appendBucket(prefix, bucket, tags)
c.appendNumber(n)
c.appendType(typ)
c.appendRate(rate)
c.closeMetric(tags)
c.flushIfNecessary(l)
c.mu.Unlock()
}
func (c *conn) gaugeRelative(prefix, bucket string, value interface{}, tags string) {
c.mu.Lock()
l := len(c.buf)
c.appendBucket(prefix, bucket, tags)
// add a (positive) sign if necessary (if there's no negative sign)
// this is complicated by the special case of negative zero (IEEE-754 floating point thing)
// note that NaN ends up "+NaN" and invalid values end up "+" (both probably going to do nothing / error)
if f, ok := floatValue(value); (!ok && !isNegativeInteger(value)) ||
(ok && (f != f || (f == 0 && !math.Signbit(f)) || (f > 0 && f <= math.MaxFloat64))) {
c.appendByte('+')
}
c.appendGauge(value, tags)
c.flushIfNecessary(l)
c.mu.Unlock()
}
func (c *conn) gauge(prefix, bucket string, value interface{}, tags string) {
c.mu.Lock()
l := len(c.buf)
// To set a gauge to a negative value we must first set it to 0.
// https://github.com/etsy/statsd/blob/master/docs/metric_types.md#gauges
// the presence of a sign (/^[-+]/) requires the special case handling
// https://github.com/statsd/statsd/blob/2041f6fb5e64bbf779a8bcb3e9729e63fe207e2f/stats.js#L307
// +Inf doesn't get this special case, no particular reason, it's just existing behavior
if f, ok := floatValue(value); ok && f == 0 {
// special case to handle negative zero (IEEE-754 floating point thing)
value = 0
} else if (ok && f < 0) || (!ok && isNegativeInteger(value)) {
// note this case includes -Inf, which is just existing behavior that's been retained
c.appendBucket(prefix, bucket, tags)
c.appendGauge(0, tags)
}
c.appendBucket(prefix, bucket, tags)
c.appendGauge(value, tags)
c.flushIfNecessary(l)
c.mu.Unlock()
}
func (c *conn) appendGauge(value interface{}, tags string) {
c.appendNumber(value)
c.appendType("g")
c.closeMetric(tags)
}
func (c *conn) unique(prefix, bucket string, value string, tags string) {
c.mu.Lock()
l := len(c.buf)
c.appendBucket(prefix, bucket, tags)
c.appendString(value)
c.appendType("s")
c.closeMetric(tags)
c.flushIfNecessary(l)
c.mu.Unlock()
}
func (c *conn) appendByte(b byte) {
c.buf = append(c.buf, b)
}
func (c *conn) appendString(s string) {
c.buf = append(c.buf, s...)
}
func (c *conn) appendNumber(v interface{}) {
switch n := v.(type) {
case int:
c.buf = strconv.AppendInt(c.buf, int64(n), 10)
case uint:
c.buf = strconv.AppendUint(c.buf, uint64(n), 10)
case int64:
c.buf = strconv.AppendInt(c.buf, n, 10)
case uint64:
c.buf = strconv.AppendUint(c.buf, n, 10)
case int32:
c.buf = strconv.AppendInt(c.buf, int64(n), 10)
case uint32:
c.buf = strconv.AppendUint(c.buf, uint64(n), 10)
case int16:
c.buf = strconv.AppendInt(c.buf, int64(n), 10)
case uint16:
c.buf = strconv.AppendUint(c.buf, uint64(n), 10)
case int8:
c.buf = strconv.AppendInt(c.buf, int64(n), 10)
case uint8:
c.buf = strconv.AppendUint(c.buf, uint64(n), 10)
case float64:
c.buf = strconv.AppendFloat(c.buf, n, 'f', -1, 64)
case float32:
c.buf = strconv.AppendFloat(c.buf, float64(n), 'f', -1, 32)
}
}
func isNegativeInteger(n interface{}) bool {
switch n := n.(type) {
case int:
return n < 0
case int64:
return n < 0
case int32:
return n < 0
case int16:
return n < 0
case int8:
return n < 0
default:
return false
}
}
func floatValue(n interface{}) (float64, bool) {
switch n := n.(type) {
case float64:
return n, true
case float32:
return float64(n), true
default:
return 0, false
}
}
func (c *conn) appendBucket(prefix, bucket string, tags string) {
c.appendString(prefix)
c.appendString(bucket)
if c.tagFormat == InfluxDB {
c.appendString(tags)
}
c.appendByte(':')
}
func (c *conn) appendType(t string) {
c.appendByte('|')
c.appendString(t)
}
func (c *conn) appendRate(rate float32) {
if rate == 1 {
return
}
if c.rateCache == nil {
c.rateCache = make(map[float32]string)
}
c.appendString("|@")
if s, ok := c.rateCache[rate]; ok {
c.appendString(s)
} else {
s = strconv.FormatFloat(float64(rate), 'f', -1, 32)
c.rateCache[rate] = s
c.appendString(s)
}
}
func (c *conn) closeMetric(tags string) {
if c.tagFormat == Datadog {
c.appendString(tags)
}
c.appendByte('\n')
}
func (c *conn) flushNecessary() bool {
if c.inlineFlush {
return true
}
if len(c.buf) > c.maxPacketSize {
return true
}
return false
}
func (c *conn) flushIfNecessary(lastSafeLen int) {
if c.inlineFlush {
lastSafeLen = 0
}
if c.flushNecessary() {
c.flush(lastSafeLen)
}
}
// flush flushes the first n bytes of the buffer.
// If n is 0, the whole buffer is flushed.
func (c *conn) flush(n int) {
if len(c.buf) == 0 {
return
}
if n == 0 {
n = len(c.buf)
}
// write
buffer := c.buf[:n]
if c.trimTrailingNewline {
// https://github.com/cactus/go-statsd-client/issues/17
// Trim the last \n, StatsD does not like it.
buffer = buffer[:len(buffer)-1]
}
_, err := c.w.Write(buffer)
c.handleError(err)
// consume
if n < len(c.buf) {
copy(c.buf, c.buf[n:])
}
c.buf = c.buf[:len(c.buf)-n]
}
func (c *conn) handleError(err error) {
if err != nil && c.errorHandler != nil {
c.errorHandler(err)
}
}
// Stubbed out for testing.
var (
dialTimeout = net.DialTimeout
now = time.Now
randFloat = rand.Float32
resolveUDPAddress = net.ResolveUDPAddr
listenUDP = net.ListenUDP
)