-
Notifications
You must be signed in to change notification settings - Fork 1
/
statsd.go
208 lines (172 loc) · 5.15 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
package statsd
import (
"math/rand"
"net"
"os"
"strconv"
"time"
)
// Address of the StatsD server.
var Address = "localhost:8125"
// AlsoAppendHost appends hostname along with any metric sent.
var AlsoAppendHost = true
// DefaultOptions holds the default options, used by the "...WithOptions"
// functions when `nil` is passed
var DefaultOptions = &Options{
Rate: 1.0,
AlwaysSend: false,
}
// Cache the connection for performance
var conn net.Conn
var host string
func init() {
var err error
rand.Seed(time.Now().UTC().UnixNano())
if host, err = os.Hostname(); err != nil {
panic(err)
}
}
// Options holds key/value pairs to be used when calling the functions
// with the "...WithOptions" suffix.
type Options struct {
// Rate specifies the sampling rate to use, between 0 and 1. A value of
// 0.1 means that this value was sampled 1 out of every 10 times. Under
// normal circumstances, the remaining 9 out of 10 times will send no data
// to the server, to reduce network use. The `AlwaysSend` option below can
// be used to change this behaviour.
Rate float64
// AlwaysSend specifies whether the packet should be sent to the
// server regardless of the sampling rate used. Under normal circumstances,
// every call that would result in sending data to the server checks the
// sampling rate and uses that to decide whether to send the data or not
// (if the rate is 0.1, only 1 out of 10 packets would be sent). When this
// option is set to "true", the packet will be sent regardless. This is useful
// if the sampling is being done on the calling side.
AlwaysSend bool
}
// Timing keeps track of a point in time, and makes it simpler to send
// timing stats to the server based on that starting point.
type Timing struct {
start time.Time
}
// Timer returns a new Timing set to `time.Now()`
func Timer() Timing {
return Timing{time.Now()}
}
// Reset sets the start time for the timer to `time.Now()`
func (t *Timing) Reset() {
t.start = time.Now()
}
// Send takes a list of remote timer names, and submits the time that
// has elapsed since the creation of the timer to each in turn.
// It returns a time.Duration representing the amount of time that was sent.
func (t *Timing) Send(names ...interface{}) (took time.Duration) {
return t.SendWithOptions(nil, names...)
}
// SendWithOptions works like Send but sends the timing information
// using the provided options.
func (t *Timing) SendWithOptions(
options *Options,
names ...interface{},
) (took time.Duration) {
took = time.Since(t.start)
var message string
if sampled, suffix := check(options); sampled {
message = ":" +
strconv.FormatUint(uint64(took.Nanoseconds()/1e6), 10) +
"|ms" + suffix
} else {
return
}
for _, name := range names {
send(name.(string), message)
}
return
}
// Gauge sets arbitrary numeric value for a given metric.
func Gauge(name string, value int64) {
GaugeWithOptions(nil, name, value)
return
}
// GaugeWithOptions sets arbitrary numeric value for a given metric
// using the provided options.
func GaugeWithOptions(options *Options, name string, value int64) {
var message string
if sampled, suffix := check(options); sampled {
message = ":" + strconv.FormatInt(value, 10) + "|g" + suffix
} else {
return
}
send(name, message)
}
// Inc increments a counter.
func Inc(name string) {
IncWithOptions(nil, name)
}
// IncSampled increments a counter with the given sample rate.
// Note that this function will send the data to the server every time
// it is called. It is the caller's responsibility to implement the
// sampling.
//
// Deprecated: Use IncWithOptions and pass the sampling rate
// using the Options struct.
func IncSampled(name string, rate float64) {
IncWithOptions(&Options{Rate: rate, AlwaysSend: true}, name)
}
// IncWithOptions increments a counter using the provided options.
func IncWithOptions(options *Options, name string) {
var message string
if sampled, suffix := check(options); sampled {
message = ":1|c" + suffix
} else {
return
}
send(name, message)
}
// Time sends duration in ms for a given metric.
func Time(name string, took time.Duration) {
TimeWithOptions(nil, name, took)
}
// TimeWithOptions sends duration in ms for a given metric
// using the provided options.
func TimeWithOptions(options *Options, name string, took time.Duration) {
var message string
if sampled, suffix := check(options); sampled {
message = ":" +
strconv.FormatUint(uint64(took.Nanoseconds()/1e6), 10) +
"|ms" + suffix
} else {
return
}
send(name, message)
}
func send(name string, message string) {
if err := getConnection(); err != nil {
return
}
conn.Write([]byte(name + message))
// Send a host suffixed stat too.
if AlsoAppendHost {
conn.Write([]byte(name + "." + host + message))
}
return
}
func check(options *Options) (bool, string) {
if options == nil {
options = DefaultOptions
}
if !options.AlwaysSend && rand.Float64() >= options.Rate {
return false, ""
}
return true, "|@" + strconv.FormatFloat(options.Rate, 'f', -1, 64)
}
func getConnection() (err error) {
// If we don't have a conn, make one.
if conn == nil {
if conn, err = net.Dial("udp", Address); err != nil {
conn = nil
return
}
}
return
}