forked from martymarron/xk6-output-prometheus-pushgateway
/
config.go
79 lines (69 loc) · 1.96 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
package pushgateway
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/pkg/errors"
"go.k6.io/k6/output"
)
const (
defaultPushGWUrl = "http://localhost:9091"
defaultPushInterval = 10 * time.Second
defaultJobName = "k6_load_testing"
defaultNamespace = ""
)
// Config is the config for the template collector
type Config struct {
JobName string
Labels map[string]string
PushGWUrl string
PushInterval time.Duration
// Used to prefix all tags with a custom namespace
Namespace string
}
// NewConfig creates a new Config instance from the provided output.Params
func NewConfig(p output.Params) (Config, error) {
cfg := Config{
JobName: defaultJobName,
Labels: map[string]string{},
PushGWUrl: defaultPushGWUrl,
PushInterval: defaultPushInterval,
Namespace: defaultNamespace,
}
if val, ok := p.ScriptOptions.External["pushgateway"]; ok {
err := json.Unmarshal(val, &cfg.Labels)
if err != nil {
j, err := json.Marshal(&val)
if err != nil {
return cfg, errors.Wrap(err, fmt.Sprintf(
"unable to get labels for JSON options.ext.pushgateway dictionary %s", string(j)))
} else {
return cfg, errors.Wrap(err, "unable to get labels for JSON options.ext.pushgateway dictionary")
}
}
p.Logger.Debugf("Pushgateway labels from JSON options.ext.pushgateway dictionary %+v", cfg.Labels)
}
for k, v := range p.Environment {
switch k {
case "K6_PUSH_INTERVAL":
var err error
cfg.PushInterval, err = time.ParseDuration(v)
if err != nil {
return cfg, fmt.Errorf("error parsing environment variable 'K6_TEMPLATE_PUSH_INTERVAL': %w", err)
}
case "K6_PUSHGATEWAY_URL":
cfg.PushGWUrl = v
case "K6_PUSHGATEWAY_NAMESPACE":
cfg.Namespace = v
case "K6_JOB_NAME":
cfg.JobName = v
}
if strings.HasPrefix(k, "K6_LABEL_") {
key := strings.ToLower(k[9:])
cfg.Labels[key] = strings.ToLower(v)
}
}
p.Logger.Debugf("Pushgateway labels %+v", cfg.Labels)
return cfg, nil
}