forked from TykTechnologies/tyk-pump
-
Notifications
You must be signed in to change notification settings - Fork 1
/
instrumentation_statsd_sink.go
372 lines (308 loc) · 8.81 KB
/
instrumentation_statsd_sink.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
package main
import (
"bytes"
"net"
"strconv"
"time"
"github.com/gocraft/health"
)
type StatsDSinkSanitizationFunc func(*bytes.Buffer, string)
type eventKey struct {
job string
event string
suffix string
}
type prefixBuffer struct {
*bytes.Buffer
prefixLen int
}
type StatsDSinkOptions struct {
// Prefix is something like "metroid"
// Events emitted to StatsD would be metroid.myevent.wat
// Eg, don't include a trailing dot in the prefix.
// It can be "", that's fine.
Prefix string
// SanitizationFunc sanitizes jobs and events before sending them to statsd
SanitizationFunc StatsDSinkSanitizationFunc
// SkipNestedEvents will skip {events,timers,gauges} from sending the job.event version
// and will only send the event version.
SkipNestedEvents bool
// SkipTopLevelEvents will skip {events,timers,gauges} from sending the event version
// and will only send the job.event version.
SkipTopLevelEvents bool
}
var defaultStatsDOptions = StatsDSinkOptions{SanitizationFunc: sanitizeKey}
type StatsDSink struct {
options StatsDSinkOptions
cmdChan chan statsdEmitCmd
drainDoneChan chan struct{}
stopDoneChan chan struct{}
flushPeriod time.Duration
udpBuf bytes.Buffer
timingBuf []byte
udpConn *net.UDPConn
udpAddr *net.UDPAddr
// map of {job,event,suffix} to a re-usable buffer prefixed with the key.
// Since each timing/gauge has a unique component (the time), we'll truncate to the prefix, write the timing,
// and write the statsD suffix (eg, "|ms\n"). Then copy that to the UDP buffer.
prefixBuffers map[eventKey]prefixBuffer
}
type statsdCmdKind int
const (
statsdCmdKindEvent statsdCmdKind = iota
statsdCmdKindEventErr
statsdCmdKindTiming
statsdCmdKindGauge
statsdCmdKindComplete
statsdCmdKindFlush
statsdCmdKindDrain
statsdCmdKindStop
)
type statsdEmitCmd struct {
Kind statsdCmdKind
Job string
Event string
Nanos int64
Value float64
Status health.CompletionStatus
}
const cmdChanBuffSize = 8192 // random-ass-guess
const maxUdpBytes = 1440 // 1500(Ethernet MTU) - 60(Max UDP header size
func NewStatsDSink(addr string, options *StatsDSinkOptions) (*StatsDSink, error) {
c, err := net.ListenPacket("udp", ":0")
if err != nil {
return nil, err
}
ra, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return nil, err
}
s := &StatsDSink{
udpConn: c.(*net.UDPConn),
udpAddr: ra,
cmdChan: make(chan statsdEmitCmd, cmdChanBuffSize),
drainDoneChan: make(chan struct{}),
stopDoneChan: make(chan struct{}),
flushPeriod: 100 * time.Millisecond,
prefixBuffers: make(map[eventKey]prefixBuffer),
}
if options != nil {
s.options = *options
if s.options.SanitizationFunc == nil {
s.options.SanitizationFunc = sanitizeKey
}
} else {
s.options = defaultStatsDOptions
}
go s.loop()
return s, nil
}
func (s *StatsDSink) Stop() {
s.cmdChan <- statsdEmitCmd{Kind: statsdCmdKindStop}
<-s.stopDoneChan
}
func (s *StatsDSink) Drain() {
s.cmdChan <- statsdEmitCmd{Kind: statsdCmdKindDrain}
<-s.drainDoneChan
}
func (s *StatsDSink) EmitEvent(job string, event string, kvs map[string]string) {
s.cmdChan <- statsdEmitCmd{Kind: statsdCmdKindEvent, Job: job, Event: event}
}
func (s *StatsDSink) EmitEventErr(job string, event string, inputErr error, kvs map[string]string) {
s.cmdChan <- statsdEmitCmd{Kind: statsdCmdKindEventErr, Job: job, Event: event}
}
func (s *StatsDSink) EmitTiming(job string, event string, nanos int64, kvs map[string]string) {
s.cmdChan <- statsdEmitCmd{Kind: statsdCmdKindTiming, Job: job, Event: event, Nanos: nanos}
}
func (s *StatsDSink) EmitGauge(job string, event string, value float64, kvs map[string]string) {
s.cmdChan <- statsdEmitCmd{Kind: statsdCmdKindGauge, Job: job, Event: event, Value: value}
}
func (s *StatsDSink) EmitComplete(job string, status health.CompletionStatus, nanos int64, kvs map[string]string) {
s.cmdChan <- statsdEmitCmd{Kind: statsdCmdKindComplete, Job: job, Status: status, Nanos: nanos}
}
func (s *StatsDSink) loop() {
cmdChan := s.cmdChan
ticker := time.NewTicker(s.flushPeriod)
go func() {
for range ticker.C {
cmdChan <- statsdEmitCmd{Kind: statsdCmdKindFlush}
}
}()
LOOP:
for cmd := range cmdChan {
switch cmd.Kind {
case statsdCmdKindDrain:
DRAIN_LOOP:
for {
select {
case cmd := <-cmdChan:
s.processCmd(&cmd)
default:
s.flush()
s.drainDoneChan <- struct{}{}
break DRAIN_LOOP
}
}
case statsdCmdKindStop:
s.stopDoneChan <- struct{}{}
break LOOP
case statsdCmdKindFlush:
s.flush()
default:
s.processCmd(&cmd)
}
}
ticker.Stop()
}
func (s *StatsDSink) processCmd(cmd *statsdEmitCmd) {
switch cmd.Kind {
case statsdCmdKindEvent:
s.processEvent(cmd.Job, cmd.Event)
case statsdCmdKindEventErr:
s.processEventErr(cmd.Job, cmd.Event)
case statsdCmdKindTiming:
s.processTiming(cmd.Job, cmd.Event, cmd.Nanos)
case statsdCmdKindGauge:
s.processGauge(cmd.Job, cmd.Event, cmd.Value)
case statsdCmdKindComplete:
s.processComplete(cmd.Job, cmd.Status, cmd.Nanos)
}
}
func (s *StatsDSink) processEvent(job string, event string) {
if !s.options.SkipTopLevelEvents {
pb := s.getPrefixBuffer("", event, "")
pb.WriteString("1|c\n")
s.writeStatsDMetric(pb.Bytes())
}
if !s.options.SkipNestedEvents {
pb := s.getPrefixBuffer(job, event, "")
pb.WriteString("1|c\n")
s.writeStatsDMetric(pb.Bytes())
}
}
func (s *StatsDSink) processEventErr(job string, event string) {
if !s.options.SkipTopLevelEvents {
pb := s.getPrefixBuffer("", event, "error")
pb.WriteString("1|c\n")
s.writeStatsDMetric(pb.Bytes())
}
if !s.options.SkipNestedEvents {
pb := s.getPrefixBuffer(job, event, "error")
pb.WriteString("1|c\n")
s.writeStatsDMetric(pb.Bytes())
}
}
func (s *StatsDSink) processTiming(job string, event string, nanos int64) {
s.writeNanosToTimingBuf(nanos)
if !s.options.SkipTopLevelEvents {
pb := s.getPrefixBuffer("", event, "")
pb.Write(s.timingBuf)
pb.WriteString("|ms\n")
s.writeStatsDMetric(pb.Bytes())
}
if !s.options.SkipNestedEvents {
pb := s.getPrefixBuffer(job, event, "")
pb.Write(s.timingBuf)
pb.WriteString("|ms\n")
s.writeStatsDMetric(pb.Bytes())
}
}
func (s *StatsDSink) processGauge(job string, event string, value float64) {
s.timingBuf = s.timingBuf[0:0]
prec := 2
if (value < 0.1) && (value > -0.1) {
prec = -1
}
s.timingBuf = strconv.AppendFloat(s.timingBuf, value, 'f', prec, 64)
if !s.options.SkipTopLevelEvents {
pb := s.getPrefixBuffer("", event, "")
pb.Write(s.timingBuf)
pb.WriteString("|g\n")
s.writeStatsDMetric(pb.Bytes())
}
if !s.options.SkipNestedEvents {
pb := s.getPrefixBuffer(job, event, "")
pb.Write(s.timingBuf)
pb.WriteString("|g\n")
s.writeStatsDMetric(pb.Bytes())
}
}
func (s *StatsDSink) processComplete(job string, status health.CompletionStatus, nanos int64) {
s.writeNanosToTimingBuf(nanos)
statusString := status.String()
pb := s.getPrefixBuffer(job, "", statusString)
pb.Write(s.timingBuf)
pb.WriteString("|ms\n")
s.writeStatsDMetric(pb.Bytes())
}
func (s *StatsDSink) flush() {
if s.udpBuf.Len() > 0 {
s.udpConn.WriteToUDP(s.udpBuf.Bytes(), s.udpAddr)
s.udpBuf.Truncate(0)
}
}
// assumes b is a well-formed statsd metric like "job.event:1|c\n" (including newline)
func (s *StatsDSink) writeStatsDMetric(b []byte) {
lenb := len(b)
if lenb == 0 {
return
}
// single metric exceeds limit. sad day.
if lenb > maxUdpBytes {
return
}
lenUdpBuf := s.udpBuf.Len()
if (lenb + lenUdpBuf) > maxUdpBytes {
s.udpConn.WriteToUDP(s.udpBuf.Bytes(), s.udpAddr)
s.udpBuf.Truncate(0)
}
s.udpBuf.Write(b)
}
func (s *StatsDSink) getPrefixBuffer(job, event, suffix string) prefixBuffer {
key := eventKey{job, event, suffix}
b, ok := s.prefixBuffers[key]
if !ok {
b.Buffer = &bytes.Buffer{}
s.writeSanitizedKeys(b.Buffer, s.options.Prefix, job, event, suffix)
b.WriteByte(':')
b.prefixLen = b.Len()
// 123456789.99|ms\n 16 bytes. timing value represents 11 days max
b.Grow(16)
s.prefixBuffers[key] = b
} else {
b.Truncate(b.prefixLen)
}
return b
}
func (s *StatsDSink) writeSanitizedKeys(b *bytes.Buffer, keys ...string) {
needDot := false
for _, k := range keys {
if k != "" {
if needDot {
b.WriteByte('.')
}
s.options.SanitizationFunc(b, k)
needDot = true
}
}
}
func (s *StatsDSink) writeNanosToTimingBuf(nanos int64) {
s.timingBuf = s.timingBuf[0:0]
if nanos >= 10e6 {
// More than 10 milliseconds. We'll just print as an integer
s.timingBuf = strconv.AppendInt(s.timingBuf, nanos/1e6, 10)
} else {
s.timingBuf = strconv.AppendFloat(s.timingBuf, float64(nanos)/float64(time.Millisecond), 'f', 2, 64)
}
}
func sanitizeKey(b *bytes.Buffer, s string) {
b.Grow(len(s) + 1)
for i := 0; i < len(s); i++ {
si := s[i]
if ('A' <= si && si <= 'Z') || ('a' <= si && si <= 'z') || ('0' <= si && s[i] <= '9') || si == '_' || si == '.' || si == '-' {
b.WriteByte(si)
} else {
b.WriteByte('$')
}
}
}