forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutputs.go
141 lines (127 loc) · 4.11 KB
/
outputs.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
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2021 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 (
"errors"
"fmt"
"sort"
"strings"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"go.k6.io/k6/lib"
"go.k6.io/k6/loader"
"go.k6.io/k6/output"
"go.k6.io/k6/output/cloud"
"go.k6.io/k6/output/csv"
"go.k6.io/k6/output/influxdb"
"go.k6.io/k6/output/json"
"go.k6.io/k6/output/statsd"
)
// TODO: move this to an output sub-module after we get rid of the old collectors?
func getAllOutputConstructors() (map[string]func(output.Params) (output.Output, error), error) {
// Start with the built-in outputs
result := map[string]func(output.Params) (output.Output, error){
"json": json.New,
"cloud": cloud.New,
"influxdb": influxdb.New,
"kafka": func(params output.Params) (output.Output, error) {
return nil, errors.New("the kafka output was deprecated in k6 v0.32.0 and removed in k6 v0.34.0, " +
"please use the new xk6 kafka output extension instead - https://github.com/k6io/xk6-output-kafka")
},
"statsd": statsd.New,
"datadog": func(params output.Params) (output.Output, error) {
return nil, errors.New("the datadog output was deprecated in k6 v0.32.0 and removed in k6 v0.34.0, " +
"please use the statsd output with env. variable K6_STATSD_ENABLE_TAGS=true instead")
},
"csv": csv.New,
}
exts := output.GetExtensions()
for k, v := range exts {
if _, ok := result[k]; ok {
return nil, fmt.Errorf("invalid output extension %s, built-in output with the same type already exists", k)
}
result[k] = v
}
return result, nil
}
func getPossibleIDList(constrs map[string]func(output.Params) (output.Output, error)) string {
res := make([]string, 0, len(constrs))
for k := range constrs {
if k == "kafka" || k == "datadog" {
continue
}
res = append(res, k)
}
sort.Strings(res)
return strings.Join(res, ", ")
}
func createOutputs(
outputFullArguments []string, src *loader.SourceData, conf Config, rtOpts lib.RuntimeOptions,
executionPlan []lib.ExecutionStep, osEnvironment map[string]string, logger logrus.FieldLogger,
globalFlags *commandFlags,
) ([]output.Output, error) {
outputConstructors, err := getAllOutputConstructors()
if err != nil {
return nil, err
}
baseParams := output.Params{
ScriptPath: src.URL,
Logger: logger,
Environment: osEnvironment,
StdOut: globalFlags.stdout,
StdErr: globalFlags.stderr,
FS: afero.NewOsFs(),
ScriptOptions: conf.Options,
RuntimeOptions: rtOpts,
ExecutionPlan: executionPlan,
}
result := make([]output.Output, 0, len(outputFullArguments))
for _, outputFullArg := range outputFullArguments {
outputType, outputArg := parseOutputArgument(outputFullArg)
outputConstructor, ok := outputConstructors[outputType]
if !ok {
return nil, fmt.Errorf(
"invalid output type '%s', available types are: %s",
outputType, getPossibleIDList(outputConstructors),
)
}
params := baseParams
params.OutputType = outputType
params.ConfigArgument = outputArg
params.JSONConfig = conf.Collectors[outputType]
output, err := outputConstructor(params)
if err != nil {
return nil, fmt.Errorf("could not create the '%s' output: %w", outputType, err)
}
result = append(result, output)
}
return result, nil
}
func parseOutputArgument(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]
}
}