forked from go-kit/kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statsd.go
215 lines (183 loc) · 6.42 KB
/
statsd.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
// Package statsd implements a statsd backend for package metrics.
//
// The current implementation ignores fields. In the future, it would be good
// to have an implementation that accepted a set of predeclared field names at
// construction time, and used field values to produce delimiter-separated
// bucket (key) names. That is,
//
// c := NewFieldedCounter(..., "path", "status")
// c.Add(1) // "myprefix.unknown.unknown:1|c\n"
// c2 := c.With("path", "foo").With("status": "200")
// c2.Add(1) // "myprefix.foo.200:1|c\n"
//
package statsd
import (
"bytes"
"fmt"
"io"
"log"
"math"
"time"
"sync/atomic"
"github.com/go-kit/kit/metrics"
)
// statsd metrics take considerable influence from
// https://github.com/streadway/handy package statsd.
const maxBufferSize = 1400 // bytes
type counter struct {
key string
c chan string
}
// NewCounter returns a Counter that emits observations in the statsd protocol
// to the passed writer. Observations are buffered for the report interval or
// until the buffer exceeds a max packet size, whichever comes first. Fields
// are ignored.
//
// TODO: support for sampling.
func NewCounter(w io.Writer, key string, reportInterval time.Duration) metrics.Counter {
return NewCounterTick(w, key, time.Tick(reportInterval))
}
// NewCounterTick is the same as NewCounter, but allows the user to pass in a
// ticker channel instead of invoking time.Tick.
func NewCounterTick(w io.Writer, key string, reportTicker <-chan time.Time) metrics.Counter {
c := &counter{
key: key,
c: make(chan string),
}
go fwd(w, key, reportTicker, c.c)
return c
}
func (c *counter) Name() string { return c.key }
func (c *counter) With(metrics.Field) metrics.Counter { return c }
func (c *counter) Add(delta uint64) { c.c <- fmt.Sprintf("%d|c", delta) }
type gauge struct {
key string
lastValue uint64 // math.Float64frombits
g chan string
}
// NewGauge returns a Gauge that emits values in the statsd protocol to the
// passed writer. Values are buffered for the report interval or until the
// buffer exceeds a max packet size, whichever comes first. Fields are
// ignored.
//
// TODO: support for sampling.
func NewGauge(w io.Writer, key string, reportInterval time.Duration) metrics.Gauge {
return NewGaugeTick(w, key, time.Tick(reportInterval))
}
// NewGaugeTick is the same as NewGauge, but allows the user to pass in a ticker
// channel instead of invoking time.Tick.
func NewGaugeTick(w io.Writer, key string, reportTicker <-chan time.Time) metrics.Gauge {
g := &gauge{
key: key,
g: make(chan string),
}
go fwd(w, key, reportTicker, g.g)
return g
}
func (g *gauge) Name() string { return g.key }
func (g *gauge) With(metrics.Field) metrics.Gauge { return g }
func (g *gauge) Add(delta float64) {
// https://github.com/etsy/statsd/blob/master/docs/metric_types.md#gauges
sign := "+"
if delta < 0 {
sign, delta = "-", -delta
}
g.g <- fmt.Sprintf("%s%f|g", sign, delta)
}
func (g *gauge) Set(value float64) {
atomic.StoreUint64(&g.lastValue, math.Float64bits(value))
g.g <- fmt.Sprintf("%f|g", value)
}
func (g *gauge) Get() float64 {
return math.Float64frombits(atomic.LoadUint64(&g.lastValue))
}
// NewCallbackGauge emits values in the statsd protocol to the passed writer.
// It collects values every scrape interval from the callback. Values are
// buffered for the report interval or until the buffer exceeds a max packet
// size, whichever comes first. The report and scrape intervals may be the
// same. The callback determines the value, and fields are ignored, so
// NewCallbackGauge returns nothing.
func NewCallbackGauge(w io.Writer, key string, reportInterval, scrapeInterval time.Duration, callback func() float64) {
NewCallbackGaugeTick(w, key, time.Tick(reportInterval), time.Tick(scrapeInterval), callback)
}
// NewCallbackGaugeTick is the same as NewCallbackGauge, but allows the user to
// pass in ticker channels instead of durations to control report and scrape
// intervals.
func NewCallbackGaugeTick(w io.Writer, key string, reportTicker, scrapeTicker <-chan time.Time, callback func() float64) {
go fwd(w, key, reportTicker, emitEvery(scrapeTicker, callback))
}
func emitEvery(emitTicker <-chan time.Time, callback func() float64) <-chan string {
c := make(chan string)
go func() {
for range emitTicker {
c <- fmt.Sprintf("%f|g", callback())
}
}()
return c
}
type histogram struct {
key string
h chan string
}
// NewHistogram returns a Histogram that emits observations in the statsd
// protocol to the passed writer. Observations are buffered for the reporting
// interval or until the buffer exceeds a max packet size, whichever comes
// first. Fields are ignored.
//
// NewHistogram is mapped to a statsd Timing, so observations should represent
// milliseconds. If you observe in units of nanoseconds, you can make the
// translation with a ScaledHistogram:
//
// NewScaledHistogram(statsdHistogram, time.Millisecond)
//
// You can also enforce the constraint in a typesafe way with a millisecond
// TimeHistogram:
//
// NewTimeHistogram(statsdHistogram, time.Millisecond)
//
// TODO: support for sampling.
func NewHistogram(w io.Writer, key string, reportInterval time.Duration) metrics.Histogram {
return NewHistogramTick(w, key, time.Tick(reportInterval))
}
// NewHistogramTick is the same as NewHistogram, but allows the user to pass a
// ticker channel instead of invoking time.Tick.
func NewHistogramTick(w io.Writer, key string, reportTicker <-chan time.Time) metrics.Histogram {
h := &histogram{
key: key,
h: make(chan string),
}
go fwd(w, key, reportTicker, h.h)
return h
}
func (h *histogram) Name() string { return h.key }
func (h *histogram) With(metrics.Field) metrics.Histogram { return h }
func (h *histogram) Observe(value int64) {
h.h <- fmt.Sprintf("%d|ms", value)
}
func (h *histogram) Distribution() ([]metrics.Bucket, []metrics.Quantile) {
// TODO(pb): no way to do this without introducing e.g. codahale/hdrhistogram
return []metrics.Bucket{}, []metrics.Quantile{}
}
func fwd(w io.Writer, key string, reportTicker <-chan time.Time, c <-chan string) {
buf := &bytes.Buffer{}
for {
select {
case s := <-c:
fmt.Fprintf(buf, "%s:%s\n", key, s)
if buf.Len() > maxBufferSize {
flush(w, buf)
}
case <-reportTicker:
flush(w, buf)
}
}
}
func flush(w io.Writer, buf *bytes.Buffer) {
if buf.Len() <= 0 {
return
}
if _, err := w.Write(buf.Bytes()); err != nil {
log.Printf("error: could not write to statsd: %v", err)
}
buf.Reset()
}