/
config.go
93 lines (82 loc) · 2.7 KB
/
config.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
package statsd
import (
"encoding/json"
"time"
"github.com/mstoykov/envconfig"
"gopkg.in/guregu/null.v3"
"github.com/ChipArtem/k6/lib/types"
"github.com/ChipArtem/k6/metrics"
)
// config defines the StatsD configuration.
type config struct {
Addr null.String `json:"addr,omitempty" envconfig:"K6_STATSD_ADDR"`
BufferSize null.Int `json:"bufferSize,omitempty" envconfig:"K6_STATSD_BUFFER_SIZE"`
Namespace null.String `json:"namespace,omitempty" envconfig:"K6_STATSD_NAMESPACE"`
PushInterval types.NullDuration `json:"pushInterval,omitempty" envconfig:"K6_STATSD_PUSH_INTERVAL"`
TagBlocklist metrics.EnabledTags `json:"tagBlocklist,omitempty" envconfig:"K6_STATSD_TAG_BLOCKLIST"`
EnableTags null.Bool `json:"enableTags,omitempty" envconfig:"K6_STATSD_ENABLE_TAGS"`
}
func processTags(t metrics.EnabledTags, tags map[string]string) []string {
var res []string
for key, value := range tags {
if value != "" && !t[key] {
res = append(res, key+":"+value)
}
}
return res
}
// Apply saves config non-zero config values from the passed config in the receiver.
func (c config) Apply(cfg config) config {
if cfg.Addr.Valid {
c.Addr = cfg.Addr
}
if cfg.BufferSize.Valid {
c.BufferSize = cfg.BufferSize
}
if cfg.Namespace.Valid {
c.Namespace = cfg.Namespace
}
if cfg.PushInterval.Valid {
c.PushInterval = cfg.PushInterval
}
if cfg.TagBlocklist != nil {
c.TagBlocklist = cfg.TagBlocklist
}
if cfg.EnableTags.Valid {
c.EnableTags = cfg.EnableTags
}
return c
}
// newConfig creates a new Config instance with default values for some fields.
func newConfig() config {
return config{
Addr: null.NewString("localhost:8125", false),
BufferSize: null.NewInt(20, false),
Namespace: null.NewString("k6.", false),
PushInterval: types.NewNullDuration(1*time.Second, false),
TagBlocklist: metrics.SystemTagSet(metrics.TagVU | metrics.TagIter | metrics.TagURL).Map(),
EnableTags: null.NewBool(false, false),
}
}
// getConsolidatedConfig combines {default config values + JSON config +
// environment vars}, and returns the final result.
func getConsolidatedConfig(jsonRawConf json.RawMessage, env map[string]string, _ string) (config, error) {
result := newConfig()
if jsonRawConf != nil {
jsonConf := config{}
if err := json.Unmarshal(jsonRawConf, &jsonConf); err != nil {
return result, err
}
result = result.Apply(jsonConf)
}
envConfig := config{}
_ = env // TODO: get rid of envconfig and actually use the env parameter...
if err := envconfig.Process("", &envConfig, func(key string) (string, bool) {
v, ok := env[key]
return v, ok
}); err != nil {
return result, err
}
result = result.Apply(envConfig)
return result, nil
}