-
Notifications
You must be signed in to change notification settings - Fork 18
/
options.go
274 lines (237 loc) · 8.21 KB
/
options.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
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 (
"time"
)
// Default settings
const (
DefaultMaxPacketSize = 1432
DefaultMetricPrefix = ""
DefaultFlushInterval = 100 * time.Millisecond
DefaultReconnectInterval = time.Duration(0)
DefaultReportInterval = time.Minute
DefaultRetryTimeout = 5 * time.Second
DefaultLogPrefix = "[STATSD] "
DefaultBufPoolCapacity = 20
DefaultSendQueueCapacity = 10
DefaultSendLoopCount = 1
DefaultNetwork = "udp"
)
// SomeLogger defines logging interface that allows using 3rd party loggers
// (e.g. github.com/sirupsen/logrus) with this Statsd client.
type SomeLogger interface {
Printf(fmt string, args ...interface{})
}
// ClientOptions are statsd client settings
type ClientOptions struct {
// Addr is statsd server address in "host:port" format
Addr string
// AddrNetwork is network type for the address. Defaults to udp.
AddrNetwork string
// MetricPrefix is metricPrefix to prepend to every metric being sent
//
// If not set defaults to empty string
MetricPrefix string
// MaxPacketSize is maximum UDP packet size
//
// Safe value is 1432 bytes, if your network supports jumbo frames,
// this value could be raised up to 8960 bytes
MaxPacketSize int
// FlushInterval controls flushing incomplete UDP packets which makes
// sure metric is not delayed longer than FlushInterval
//
// Default value is 100ms, setting FlushInterval to zero disables flushing
FlushInterval time.Duration
// ReconnectInterval controls UDP socket reconnects
//
// Reconnecting is important to follow DNS changes, e.g. in
// dynamic container environments like K8s where statsd server
// instance might be relocated leading to new IP address.
//
// By default reconnects are disabled
ReconnectInterval time.Duration
// RetryTimeout controls how often client should attempt reconnecting
// to statsd server on failure
//
// Default value is 5 seconds
RetryTimeout time.Duration
// ReportInterval instructs client to report number of packets lost
// each interval via Logger
//
// By default lost packets are reported every minute, setting to zero
// disables reporting
ReportInterval time.Duration
// Logger is used by statsd client to report errors and lost packets
//
// If not set, default logger to stderr with metricPrefix `[STATSD] ` is being used
Logger SomeLogger
// BufPoolCapacity controls size of pre-allocated buffer cache
//
// Each buffer is MaxPacketSize. Cache allows to avoid allocating
// new buffers during high load
//
// Default value is DefaultBufPoolCapacity
BufPoolCapacity int
// SendQueueCapacity controls length of the queue of packet ready to be sent
//
// Packets might stay in the queue during short load bursts or while
// client is reconnecting to statsd
//
// Default value is DefaultSendQueueCapacity
SendQueueCapacity int
// SendLoopCount controls number of goroutines sending UDP packets
//
// Default value is 1, so packets are sent from single goroutine, this
// value might need to be bumped under high load
SendLoopCount int
// TagFormat controls formatting of StatsD tags
//
// If tags are not used, value of this setting isn't used.
//
// There are two predefined formats: for InfluxDB and Datadog, default
// format is InfluxDB tag format.
TagFormat *TagFormat
// DefaultTags is a list of tags to be applied to every metric
DefaultTags []Tag
}
// Option is type for option transport
type Option func(c *ClientOptions)
// MetricPrefix is metricPrefix to prepend to every metric being sent
//
// Usually metrics are prefixed with app name, e.g. `app.`.
// To avoid providing this metricPrefix for every metric being collected,
// and to enable shared libraries to collect metric under app name,
// use MetricPrefix to set global metricPrefix for all the app metrics,
// e.g. `MetricPrefix("app.")`.
//
// If not set defaults to empty string
func MetricPrefix(prefix string) Option {
return func(c *ClientOptions) {
c.MetricPrefix = prefix
}
}
// MaxPacketSize control maximum UDP packet size
//
// Default value is DefaultMaxPacketSize
func MaxPacketSize(packetSize int) Option {
return func(c *ClientOptions) {
c.MaxPacketSize = packetSize
}
}
// FlushInterval controls flushing incomplete UDP packets which makes
// sure metric is not delayed longer than FlushInterval
//
// Default value is 100ms, setting FlushInterval to zero disables flushing
func FlushInterval(interval time.Duration) Option {
return func(c *ClientOptions) {
c.FlushInterval = interval
}
}
// ReconnectInterval controls UDP socket reconnects
//
// Reconnecting is important to follow DNS changes, e.g. in
// dynamic container environments like K8s where statsd server
// instance might be relocated leading to new IP address.
//
// By default reconnects are disabled
func ReconnectInterval(interval time.Duration) Option {
return func(c *ClientOptions) {
c.ReconnectInterval = interval
}
}
// RetryTimeout controls how often client should attempt reconnecting
// to statsd server on failure
//
// Default value is 5 seconds
func RetryTimeout(timeout time.Duration) Option {
return func(c *ClientOptions) {
c.RetryTimeout = timeout
}
}
// ReportInterval instructs client to report number of packets lost
// each interval via Logger
//
// By default lost packets are reported every minute, setting to zero
// disables reporting
func ReportInterval(interval time.Duration) Option {
return func(c *ClientOptions) {
c.ReportInterval = interval
}
}
// Logger is used by statsd client to report errors and lost packets
//
// If not set, default logger to stderr with metricPrefix `[STATSD] ` is being used
func Logger(logger SomeLogger) Option {
return func(c *ClientOptions) {
c.Logger = logger
}
}
// BufPoolCapacity controls size of pre-allocated buffer cache
//
// Each buffer is MaxPacketSize. Cache allows to avoid allocating
// new buffers during high load
//
// Default value is DefaultBufPoolCapacity
func BufPoolCapacity(capacity int) Option {
return func(c *ClientOptions) {
c.BufPoolCapacity = capacity
}
}
// SendQueueCapacity controls length of the queue of packet ready to be sent
//
// Packets might stay in the queue during short load bursts or while
// client is reconnecting to statsd
//
// Default value is DefaultSendQueueCapacity
func SendQueueCapacity(capacity int) Option {
return func(c *ClientOptions) {
c.SendQueueCapacity = capacity
}
}
// SendLoopCount controls number of goroutines sending UDP packets
//
// Default value is 1, so packets are sent from single goroutine, this
// value might need to be bumped under high load
func SendLoopCount(threads int) Option {
return func(c *ClientOptions) {
c.SendLoopCount = threads
}
}
// TagStyle controls formatting of StatsD tags
//
// There are two predefined formats: for InfluxDB and Datadog, default
// format is InfluxDB tag format.
func TagStyle(style *TagFormat) Option {
return func(c *ClientOptions) {
c.TagFormat = style
}
}
// DefaultTags defines a list of tags to be applied to every metric
func DefaultTags(tags ...Tag) Option {
return func(c *ClientOptions) {
c.DefaultTags = tags
}
}
// Network sets the network to use Dialing the statsd server
func Network(network string) Option {
return func(c *ClientOptions) {
c.AddrNetwork = network
}
}