/
statsd.go
297 lines (256 loc) · 8.03 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
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
package metrics
import (
"fmt"
"time"
"github.com/Jeffail/benthos/v3/internal/docs"
"github.com/Jeffail/benthos/v3/lib/log"
statsd "github.com/smira/go-statsd"
)
//------------------------------------------------------------------------------
func init() {
Constructors[TypeStatsd] = TypeSpec{
constructor: NewStatsd,
Summary: `
Pushes metrics using the [StatsD protocol](https://github.com/statsd/statsd).
Supported tagging formats are 'legacy', 'none', 'datadog' and 'influxdb'.`,
Description: `
The underlying client library has recently been updated in order to support
tagging. The tag format 'legacy' is default and causes Benthos to continue using
the old library in order to preserve backwards compatibility.
The legacy library aggregated timing metrics, so dashboards and alerts may need
to be updated when migrating to the new library.
The 'network' field is deprecated and scheduled for removal. If you currently
rely on sending Statsd metrics over TCP and want it to be supported long term
please [raise an issue](https://github.com/Jeffail/benthos/issues).`,
FieldSpecs: docs.FieldSpecs{
docs.FieldCommon("prefix", "A string prefix to add to all metrics."),
pathMappingDocs(false, false),
docs.FieldCommon("address", "The address to send metrics to."),
docs.FieldCommon("flush_period", "The time interval between metrics flushes."),
docs.FieldCommon("tag_format", "Metrics tagging is supported in a variety of formats. The format 'legacy' is a special case that forces Benthos to use a deprecated library for backwards compatibility.").HasOptions(
"none", "datadog", "influxdb", "legacy",
),
docs.FieldDeprecated("network"),
},
}
}
//------------------------------------------------------------------------------
type wrappedDatadogLogger struct {
log log.Modular
}
func (s wrappedDatadogLogger) Printf(msg string, args ...interface{}) {
s.log.Warnf(fmt.Sprintf(msg, args...))
}
//------------------------------------------------------------------------------
// StatsdConfig is config for the Statsd metrics type.
type StatsdConfig struct {
Prefix string `json:"prefix" yaml:"prefix"`
PathMapping string `json:"path_mapping" yaml:"path_mapping"`
Address string `json:"address" yaml:"address"`
FlushPeriod string `json:"flush_period" yaml:"flush_period"`
Network string `json:"network" yaml:"network"`
TagFormat string `json:"tag_format" yaml:"tag_format"`
}
// NewStatsdConfig creates an StatsdConfig struct with default values.
func NewStatsdConfig() StatsdConfig {
return StatsdConfig{
Prefix: "benthos",
PathMapping: "",
Address: "localhost:4040",
FlushPeriod: "100ms",
Network: "udp",
TagFormat: TagFormatLegacy,
}
}
// Tag formats supported by the statsd metric type.
const (
TagFormatNone = "none"
TagFormatDatadog = "datadog"
TagFormatInfluxDB = "influxdb"
TagFormatLegacy = "legacy"
)
//------------------------------------------------------------------------------
// StatsdStat is a representation of a single metric stat. Interactions with
// this stat are thread safe.
type StatsdStat struct {
path string
s *statsd.Client
tags []statsd.Tag
}
// Incr increments a metric by an amount.
func (s *StatsdStat) Incr(count int64) error {
s.s.Incr(s.path, count, s.tags...)
return nil
}
// Decr decrements a metric by an amount.
func (s *StatsdStat) Decr(count int64) error {
s.s.Decr(s.path, count, s.tags...)
return nil
}
// Timing sets a timing metric.
func (s *StatsdStat) Timing(delta int64) error {
s.s.Timing(s.path, delta, s.tags...)
return nil
}
// Set sets a gauge metric.
func (s *StatsdStat) Set(value int64) error {
s.s.Gauge(s.path, value, s.tags...)
return nil
}
//------------------------------------------------------------------------------
// Statsd is a stats object with capability to hold internal stats as a JSON
// endpoint.
type Statsd struct {
config Config
s *statsd.Client
log log.Modular
pathMapping *pathMapping
}
// NewStatsd creates and returns a new Statsd object.
func NewStatsd(config Config, opts ...func(Type)) (Type, error) {
if config.Statsd.Network != "udp" || config.Statsd.TagFormat == TagFormatLegacy {
return NewStatsdLegacy(config, opts...)
}
flushPeriod, err := time.ParseDuration(config.Statsd.FlushPeriod)
if err != nil {
return nil, fmt.Errorf("failed to parse flush period: %s", err)
}
s := &Statsd{
config: config,
log: log.Noop(),
}
for _, opt := range opts {
opt(s)
}
if s.pathMapping, err = newPathMapping(config.Statsd.PathMapping, s.log); err != nil {
return nil, fmt.Errorf("failed to init path mapping: %v", err)
}
prefix := config.Statsd.Prefix
if len(prefix) > 0 && prefix[len(prefix)-1] != '.' {
prefix += "."
}
statsdOpts := []statsd.Option{
statsd.FlushInterval(flushPeriod),
statsd.MetricPrefix(prefix),
statsd.Logger(wrappedDatadogLogger{log: s.log}),
}
switch config.Statsd.TagFormat {
case TagFormatInfluxDB:
statsdOpts = append(statsdOpts, statsd.TagStyle(statsd.TagFormatInfluxDB))
case TagFormatDatadog:
statsdOpts = append(statsdOpts, statsd.TagStyle(statsd.TagFormatDatadog))
case TagFormatNone:
default:
return nil, fmt.Errorf("tag format '%s' was not recognised", config.Statsd.TagFormat)
}
client := statsd.NewClient(config.Statsd.Address, statsdOpts...)
s.s = client
return s, nil
}
//------------------------------------------------------------------------------
// GetCounter returns a stat counter object for a path.
func (h *Statsd) GetCounter(path string) StatCounter {
if path = h.pathMapping.mapPathNoTags(path); path == "" {
return DudStat{}
}
return &StatsdStat{
path: path,
s: h.s,
}
}
// GetCounterVec returns a stat counter object for a path with the labels
func (h *Statsd) GetCounterVec(path string, n []string) StatCounterVec {
if path = h.pathMapping.mapPathNoTags(path); path == "" {
return fakeCounterVec(func([]string) StatCounter {
return DudStat{}
})
}
return &fCounterVec{
f: func(l []string) StatCounter {
return &StatsdStat{
path: path,
s: h.s,
tags: tags(n, l),
}
},
}
}
// GetTimer returns a stat timer object for a path.
func (h *Statsd) GetTimer(path string) StatTimer {
if path = h.pathMapping.mapPathNoTags(path); path == "" {
return DudStat{}
}
return &StatsdStat{
path: path,
s: h.s,
}
}
// GetTimerVec returns a stat timer object for a path with the labels
func (h *Statsd) GetTimerVec(path string, n []string) StatTimerVec {
if path = h.pathMapping.mapPathNoTags(path); path == "" {
return fakeTimerVec(func([]string) StatTimer {
return DudStat{}
})
}
return &fTimerVec{
f: func(l []string) StatTimer {
return &StatsdStat{
path: path,
s: h.s,
tags: tags(n, l),
}
},
}
}
// GetGauge returns a stat gauge object for a path.
func (h *Statsd) GetGauge(path string) StatGauge {
if path = h.pathMapping.mapPathNoTags(path); path == "" {
return DudStat{}
}
return &StatsdStat{
path: path,
s: h.s,
}
}
// GetGaugeVec returns a stat timer object for a path with the labels
func (h *Statsd) GetGaugeVec(path string, n []string) StatGaugeVec {
if path = h.pathMapping.mapPathNoTags(path); path == "" {
return fakeGaugeVec(func([]string) StatGauge {
return DudStat{}
})
}
return &fGaugeVec{
f: func(l []string) StatGauge {
return &StatsdStat{
path: path,
s: h.s,
tags: tags(n, l),
}
},
}
}
// SetLogger sets the logger used to print connection errors.
func (h *Statsd) SetLogger(log log.Modular) {
h.log = log
}
// Close stops the Statsd object from aggregating metrics and cleans up
// resources.
func (h *Statsd) Close() error {
h.s.Close()
return nil
}
// tags merges tag labels with their interpolated values
//
// no attempt is made to merge labels and values if slices
// are not the same length
func tags(labels, values []string) []statsd.Tag {
if len(labels) != len(values) {
return nil
}
tags := make([]statsd.Tag, len(labels))
for i := range labels {
tags[i] = statsd.StringTag(labels[i], values[i])
}
return tags
}
//------------------------------------------------------------------------------