-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfromflags.go
411 lines (341 loc) · 10 KB
/
fromflags.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
Copyright 2018 Turbine Labs, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package stats
import (
"errors"
"fmt"
"sort"
"strings"
"github.com/turbinelabs/idgen"
tbnflag "github.com/turbinelabs/nonstdlib/flag"
"github.com/turbinelabs/nonstdlib/ptr"
tbnstrings "github.com/turbinelabs/nonstdlib/strings"
)
//go:generate mockgen -source $GOFILE -destination mock_$GOFILE -package $GOPACKAGE --write_package_comment=false
const (
apiStatsName = "api"
dogstatsdName = "dogstatsd"
prometheusName = "prometheus"
statsdName = "statsd"
wavefrontName = "wavefront"
honeycombName = "honeycomb"
consoleName = "console"
maxNodeLen = 256
maxSourceLen = 256
)
// FromFlags produces a Stats from command line flags.
type FromFlags interface {
// Validate validates the command line flags.
Validate() error
// Make constructs a Stats from command line flags.
Make() (Stats, error)
// Node returns the value of the node tag (must be called after Make).
Node() string
// Source returns the value of the source tag (must be called after Make).
Source() string
}
// Option is an opaquely-typed option for NewFromFlags.
type Option func(*fromFlagsOptions)
// EnableAPIStatsBackend enables the API stats backend.
func EnableAPIStatsBackend() Option {
return func(ff *fromFlagsOptions) {
ff.enableAPIStats = true
}
}
// APIStatsOptions configures NewFromFlags to pass APIStatsOption
// values to the API Stats backend.
func APIStatsOptions(opts ...APIStatsOption) Option {
return func(ff *fromFlagsOptions) {
ff.apiStatsOptions = append(ff.apiStatsOptions, opts...)
}
}
// DefaultBackends configures NewFromFlags with default backends (that
// may be overridden by command line flags). Unknown backends are
// ignored.
func DefaultBackends(backends ...string) Option {
return func(ff *fromFlagsOptions) {
ff.defaultBackends = backends
}
}
// NewFromFlags produces a FromFlags configured by the given flagset
// and options.
func NewFromFlags(fs tbnflag.FlagSet, options ...Option) FromFlags {
ffOpts := &fromFlagsOptions{}
for _, apply := range options {
apply(ffOpts)
}
eventBackends := []string{
honeycombName,
consoleName,
}
backends := []string{
dogstatsdName,
prometheusName,
wavefrontName,
statsdName,
}
sffMap := map[string]statsFromFlags{
dogstatsdName: newDogstatsdFromFlags(fs.Scope(dogstatsdName, "")),
prometheusName: newPrometheusFromFlags(fs.Scope(prometheusName, "")),
wavefrontName: newWavefrontFromFlags(fs.Scope(wavefrontName, "")),
statsdName: newStatsdFromFlags(fs.Scope(statsdName, "")),
}
effMap := map[string]statsFromFlags{
honeycombName: newHoneycombFromFlags(fs.Scope(honeycombName, "")),
consoleName: newConsoleFromFlags(fs.Scope(consoleName, "")),
}
var defaultEventBackends []string
if len(ffOpts.defaultEventBackends) > 0 {
for _, backend := range ffOpts.defaultEventBackends {
backend = strings.ToLower(backend)
if _, ok := effMap[backend]; ok {
defaultEventBackends = append(defaultEventBackends, backend)
}
}
}
sort.Strings(eventBackends)
if ffOpts.enableAPIStats {
backends = append(backends, apiStatsName)
sffMap[apiStatsName] = newAPIStatsFromFlags(
fs.Scope(apiStatsName, ""),
ffOpts.apiStatsOptions...,
)
}
var defaultBackends []string
if len(ffOpts.defaultBackends) > 0 {
for _, backend := range ffOpts.defaultBackends {
backend = strings.ToLower(backend)
if _, ok := sffMap[backend]; ok {
defaultBackends = append(defaultBackends, backend)
}
}
}
sort.Strings(backends)
ff := &fromFlags{
statsFromFlagses: sffMap,
eventsFromFlagses: effMap,
flagScope: fs.GetScope(),
backends: tbnflag.NewStringsWithConstraint(backends...),
eventBackends: tbnflag.NewStringsWithConstraint(eventBackends...),
tags: tbnflag.NewStrings(),
}
ff.backends.ResetDefault(defaultBackends...)
ff.initFlags(fs)
return ff
}
type fromFlags struct {
statsFromFlagses map[string]statsFromFlags
eventsFromFlagses map[string]statsFromFlags
flagScope string
backends tbnflag.Strings
eventBackends tbnflag.Strings
nodeTag string
sourceTag string
uniqueSourceTag string
tags tbnflag.Strings
resolved bool
resolvedNodeTag string
resolvedSourceTag string
resolvedTags []Tag
}
type fromFlagsOptions struct {
enableAPIStats bool
apiStatsOptions []APIStatsOption
defaultBackends []string
defaultEventBackends []string
}
func (ff *fromFlags) initFlags(fs tbnflag.FlagSet) {
fs.Var(
&ff.backends,
"backends",
"Selects which stats backend(s) to use.",
)
fs.Var(
&ff.eventBackends,
"event-backends",
"Selects which stats backend(s) to use for structured events.",
)
fs.StringVar(
&ff.sourceTag,
"source",
"",
`If set, specifies the source to use when submitting stats to backends. Equivalent to adding "--{{PREFIX}}tags=source=value" to the command line. In either case, a UUID is appended to the value to insure that it is unique across proxies. Cannot be combined with --{{PREFIX}}unique-source.`,
)
fs.StringVar(
&ff.uniqueSourceTag,
"unique-source",
"",
`If set, specifies the source to use when submitting stats to backends. Equivalent to adding "--{{PREFIX}}tags=source=value" to the command line. Unlike --{{PREFIX}}source, failing to specify a unique value may prevent stats from being recorded correctly. Cannot be combined with --{{PREFIX}}source.`,
)
fs.StringVar(
&ff.nodeTag,
"node",
"",
`If set, specifies the node to use when submitting stats to backends. Equivalent to adding "--{{PREFIX}}tags=node=value" to the command line.`,
)
fs.Var(
&ff.tags,
"tags",
`Tags to be included with every stat. May be comma-delimited or specified more than once. Should be of the form "<key>=<value>" or "tag"`,
)
}
func (ff *fromFlags) Validate() error {
if len(ff.backends.Strings) == 0 && len(ff.eventBackends.Strings) == 0 {
return errors.New("no backends specified")
}
for _, backend := range ff.backends.Strings {
if sff, ok := ff.statsFromFlagses[backend]; ok {
if err := sff.Validate(); err != nil {
return err
}
}
}
for _, backend := range ff.eventBackends.Strings {
if eff, ok := ff.eventsFromFlagses[backend]; ok {
if err := eff.Validate(); err != nil {
return err
}
}
}
sourceTag, nodeTag, _, err := ff.parseTags()
if err != nil {
return err
}
if ff.nodeTag != "" {
if nodeTag != nil {
return fmt.Errorf("cannot combine --%stags=node=... with --%[1]snode", ff.flagScope)
}
nodeTag = &ff.nodeTag
}
if ff.sourceTag != "" || ff.uniqueSourceTag != "" {
if sourceTag != nil || (ff.sourceTag != "" && ff.uniqueSourceTag != "") {
return fmt.Errorf(
"cannot combine --%stags=source=... with --%[1]ssource or --%[1]sunique-source",
ff.flagScope,
)
}
sourceTag = &ff.sourceTag
}
if len(ptr.StringValue(nodeTag)) > maxNodeLen {
return fmt.Errorf(
"--%snode or --%[1]stags=node=... may not be longer than %d bytes",
ff.flagScope,
maxNodeLen,
)
}
if len(ptr.StringValue(sourceTag)) > maxSourceLen {
return fmt.Errorf(
"--%ssource or --%[1]stags=source=... may not be longer than %d bytes",
ff.flagScope,
maxSourceLen,
)
}
if len(ff.uniqueSourceTag) > maxSourceLen {
return fmt.Errorf(
"--%sunique-source may not be longer than %d bytes",
ff.flagScope,
maxSourceLen,
)
}
return nil
}
func (ff *fromFlags) parseTags() (*string, *string, []Tag, error) {
var source, node *string
result := make([]Tag, 0, len(ff.tags.Strings))
for _, tag := range ff.tags.Strings {
key, value := tbnstrings.SplitFirstEqual(tag)
switch key {
case SourceTag:
if value != "" {
if source != nil {
return nil, nil, nil, errors.New("cannot specify multiple tags named source")
}
source = &value
}
case NodeTag:
if value != "" {
if node != nil {
return nil, nil, nil, errors.New("cannot specify multiple tags named node")
}
node = &value
}
default:
result = append(result, NewKVTag(key, value))
}
}
return source, node, result, nil
}
func (ff *fromFlags) Make() (Stats, error) {
statses := make([]Stats, 0, len(ff.statsFromFlagses))
for _, backend := range ff.backends.Strings {
if sff, ok := ff.statsFromFlagses[backend]; ok {
sender, err := sff.Make()
if err != nil {
return nil, err
}
statses = append(statses, sender)
}
}
for _, backend := range ff.eventBackends.Strings {
if eff, ok := ff.eventsFromFlagses[backend]; ok {
sender, err := eff.Make()
if err != nil {
return nil, err
}
statses = append(statses, sender)
}
}
stats := NewMulti(statses...)
if !ff.resolved {
sourceTag, nodeTag, tags, err := ff.parseTags()
if err != nil {
return nil, err
}
if sourceTag != nil {
ff.sourceTag = *sourceTag
}
ff.resolvedNodeTag = ff.nodeTag
if nodeTag != nil {
ff.resolvedNodeTag = *nodeTag
}
if ff.uniqueSourceTag != "" {
ff.resolvedSourceTag = ff.uniqueSourceTag
} else {
uuid, err := idgen.NewUUID()()
if err != nil {
return nil, err
}
if ff.sourceTag != "" {
ff.resolvedSourceTag = fmt.Sprintf("%s-%s", ff.sourceTag, uuid)
} else {
ff.resolvedSourceTag = string(uuid)
}
}
ff.resolvedTags = tags
ff.resolved = true
}
stats.AddTags(ff.resolvedTags...)
if ff.resolvedNodeTag != "" {
stats.AddTags(NewKVTag(NodeTag, ff.resolvedNodeTag))
}
if ff.resolvedSourceTag != "" {
stats.AddTags(NewKVTag(SourceTag, ff.resolvedSourceTag))
}
return stats, nil
}
func (ff *fromFlags) Node() string {
return ff.resolvedNodeTag
}
func (ff *fromFlags) Source() string {
return ff.resolvedSourceTag
}