forked from grafana/k6
/
collectors.go
166 lines (153 loc) · 4.6 KB
/
collectors.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
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2016 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package cmd
import (
"fmt"
"strings"
"gopkg.in/guregu/null.v3"
"github.com/kelseyhightower/envconfig"
"github.com/pkg/errors"
"github.com/spf13/afero"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/consts"
"github.com/loadimpact/k6/loader"
"github.com/loadimpact/k6/stats"
"github.com/loadimpact/k6/stats/cloud"
"github.com/loadimpact/k6/stats/csv"
"github.com/loadimpact/k6/stats/datadog"
"github.com/loadimpact/k6/stats/influxdb"
jsonc "github.com/loadimpact/k6/stats/json"
"github.com/loadimpact/k6/stats/kafka"
"github.com/loadimpact/k6/stats/statsd"
"github.com/loadimpact/k6/stats/statsd/common"
)
const (
collectorInfluxDB = "influxdb"
collectorJSON = "json"
collectorKafka = "kafka"
collectorCloud = "cloud"
collectorStatsD = "statsd"
collectorDatadog = "datadog"
collectorCSV = "csv"
)
func parseCollector(s string) (t, arg string) {
parts := strings.SplitN(s, "=", 2)
switch len(parts) {
case 0:
return "", ""
case 1:
return parts[0], ""
default:
return parts[0], parts[1]
}
}
//TODO: totally refactor this...
func getCollector(
collectorName, arg string, src *loader.SourceData, conf Config, executionPlan []lib.ExecutionStep,
) (lib.Collector, error) {
switch collectorName {
case collectorJSON:
return jsonc.New(afero.NewOsFs(), arg)
case collectorInfluxDB:
config := influxdb.NewConfig().Apply(conf.Collectors.InfluxDB)
if err := envconfig.Process("", &config); err != nil {
return nil, err
}
urlConfig, err := influxdb.ParseURL(arg)
if err != nil {
return nil, err
}
config = config.Apply(urlConfig)
return influxdb.New(config)
case collectorCloud:
config := cloud.NewConfig().Apply(conf.Collectors.Cloud)
if err := envconfig.Process("", &config); err != nil {
return nil, err
}
if arg != "" {
config.Name = null.StringFrom(arg)
}
return cloud.New(config, src, conf.Options, executionPlan, consts.Version)
case collectorKafka:
config := kafka.NewConfig().Apply(conf.Collectors.Kafka)
if err := envconfig.Process("", &config); err != nil {
return nil, err
}
if arg != "" {
cmdConfig, err := kafka.ParseArg(arg)
if err != nil {
return nil, err
}
config = config.Apply(cmdConfig)
}
return kafka.New(config)
case collectorStatsD:
config := common.NewConfig().Apply(conf.Collectors.StatsD)
if err := envconfig.Process("k6_statsd", &config); err != nil {
return nil, err
}
return statsd.New(config)
case collectorDatadog:
config := datadog.NewConfig().Apply(conf.Collectors.Datadog)
if err := envconfig.Process("k6_datadog", &config); err != nil {
return nil, err
}
return datadog.New(config)
case collectorCSV:
config := csv.NewConfig().Apply(conf.Collectors.CSV)
if err := envconfig.Process("", &config); err != nil {
return nil, err
}
if arg != "" {
cmdConfig, err := csv.ParseArg(arg)
if err != nil {
return nil, err
}
config = config.Apply(cmdConfig)
}
return csv.New(afero.NewOsFs(), conf.SystemTags.Map(), config)
default:
return nil, errors.Errorf("unknown output type: %s", collectorName)
}
}
func newCollector(
collectorName, arg string, src *loader.SourceData, conf Config, executionPlan []lib.ExecutionStep,
) (lib.Collector, error) {
collector, err := getCollector(collectorName, arg, src, conf, executionPlan)
if err != nil {
return collector, err
}
// Check if all required tags are present
missingRequiredTags := []string{}
requiredTags := collector.GetRequiredSystemTags()
for _, tag := range stats.SystemTagSetValues() {
if requiredTags.Has(tag) && !conf.SystemTags.Has(tag) {
missingRequiredTags = append(missingRequiredTags, tag.String())
}
}
if len(missingRequiredTags) > 0 {
return collector, fmt.Errorf(
"the specified collector '%s' needs the following system tags enabled: %s",
collectorName,
strings.Join(missingRequiredTags, ", "),
)
}
return collector, nil
}