forked from grafana-cold-storage/xk6-output-opentelemetry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
279 lines (228 loc) · 8.67 KB
/
Copy pathconfig.go
File metadata and controls
279 lines (228 loc) · 8.67 KB
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
package opentelemetry
import (
"encoding/json"
"errors"
"fmt"
"strings"
"time"
"github.com/mstoykov/envconfig"
"go.k6.io/k6/errext"
"go.k6.io/k6/errext/exitcodes"
k6Const "go.k6.io/k6/lib/consts"
"go.k6.io/k6/lib/types"
"gopkg.in/guregu/null.v3"
)
const (
// grpcExporterType GRPC exporter type
grpcExporterType = "grpc"
// httpExporterType HTTP exporter type
httpExporterType = "http"
)
// Config represents the configuration for the OpenTelemetry output
type Config struct {
// ServiceName is the name of the service to use for the metrics
// export, if not set it will use "k6"
ServiceName null.String `json:"serviceName" envconfig:"K6_OTEL_SERVICE_NAME"`
// ServiceVersion is the version of the service to use for the metrics
// export, if not set it will use k6's library version
ServiceVersion null.String `json:"serviceVersion" envconfig:"K6_OTEL_SERVICE_VERSION"`
// MetricPrefix is the prefix to use for the metrics
MetricPrefix null.String `json:"metricPrefix" envconfig:"K6_OTEL_METRIC_PREFIX"`
// FlushInterval is the interval at which to flush metrics from the k6
FlushInterval types.NullDuration `json:"flushInterval" envconfig:"K6_OTEL_FLUSH_INTERVAL"`
// ExporterType sets the type of OpenTelemetry Exporter to use
ExporterType null.String `json:"exporterType" envconfig:"K6_OTEL_EXPORTER_TYPE"`
// ExportInterval configures the intervening time between metrics exports
ExportInterval types.NullDuration `json:"exportInterval" envconfig:"K6_OTEL_EXPORT_INTERVAL"`
// Headers in W3C Correlation-Context format without additional semi-colon delimited metadata (i.e. "k1=v1,k2=v2")
Headers null.String `json:"headers" envconfig:"K6_OTEL_HEADERS"`
// TLSInsecureSkipVerify disables verification of the server's certificate chain
TLSInsecureSkipVerify null.Bool `json:"tlsInsecureSkipVerify" envconfig:"K6_OTEL_TLS_INSECURE_SKIP_VERIFY"`
// TLSCertificate is the path to the certificate file (rootCAs) to use for the exporter's TLS connection
TLSCertificate null.String `json:"tlsCertificate" envconfig:"K6_OTEL_TLS_CERTIFICATE"`
// TLSClientCertificate is the path to the certificate file (must be PEM encoded data)
// to use for the exporter's TLS connection
TLSClientCertificate null.String `json:"tlsClientCertificate" envconfig:"K6_OTEL_TLS_CLIENT_CERTIFICATE"`
// TLSClientKey is the path to the private key file (must be PEM encoded data) to use for the exporter's TLS connection
TLSClientKey null.String `json:"tlsClientKey" envconfig:"K6_OTEL_TLS_CLIENT_KEY"`
// HTTPExporterInsecure disables client transport security for the Exporter's HTTP
// connection.
HTTPExporterInsecure null.Bool `json:"httpExporterInsecure" envconfig:"K6_OTEL_HTTP_EXPORTER_INSECURE"`
// HTTPExporterEndpoint sets the target endpoint the OpenTelemetry Exporter
// will connect to.
HTTPExporterEndpoint null.String `json:"httpExporterEndpoint" envconfig:"K6_OTEL_HTTP_EXPORTER_ENDPOINT"`
// HTTPExporterURLPath sets the target URL path the OpenTelemetry Exporter
HTTPExporterURLPath null.String `json:"httpExporterURLPath" envconfig:"K6_OTEL_HTTP_EXPORTER_URL_PATH"`
// GRPCExporterEndpoint sets the target endpoint the OpenTelemetry Exporter
// will connect to.
GRPCExporterEndpoint null.String `json:"grpcExporterEndpoint" envconfig:"K6_OTEL_GRPC_EXPORTER_ENDPOINT"`
// GRPCExporterInsecure disables client transport security for the Exporter's gRPC
// connection.
GRPCExporterInsecure null.Bool `json:"grpcExporterInsecure" envconfig:"K6_OTEL_GRPC_EXPORTER_INSECURE"`
}
// GetConsolidatedConfig combines the options' values from the different sources
// and returns the merged options. The Order of precedence used is documented
// in the k6 Documentation https://grafana.com/docs/k6/latest/using-k6/k6-options/how-to/#order-of-precedence.
func GetConsolidatedConfig(jsonRawConf json.RawMessage, env map[string]string) (Config, error) {
cfg := newDefaultConfig()
if jsonRawConf != nil {
jsonConf, err := parseJSON(jsonRawConf)
if err != nil {
return cfg, fmt.Errorf("parse JSON options failed: %w", err)
}
cfg = cfg.Apply(jsonConf)
}
if len(env) > 0 {
envConf, err := parseEnvs(env)
if err != nil {
return cfg, fmt.Errorf("parse environment variables options failed: %w", err)
}
cfg = cfg.Apply(envConf)
}
if err := cfg.Validate(); err != nil {
// TODO: check why k6's still exiting with 255
return cfg, errext.WithExitCodeIfNone(
fmt.Errorf("error validating OpenTelemetry output config: %w", err),
exitcodes.InvalidConfig,
)
}
return cfg, nil
}
// newDefaultConfig creates a new default config with default values
func newDefaultConfig() Config {
return Config{
ServiceName: null.StringFrom("k6"),
ServiceVersion: null.StringFrom(k6Const.Version),
ExporterType: null.StringFrom(grpcExporterType),
HTTPExporterInsecure: null.BoolFrom(false),
HTTPExporterEndpoint: null.StringFrom("localhost:4318"),
HTTPExporterURLPath: null.StringFrom("/v1/metrics"),
GRPCExporterInsecure: null.BoolFrom(false),
GRPCExporterEndpoint: null.StringFrom("localhost:4317"),
ExportInterval: types.NullDurationFrom(10 * time.Second),
FlushInterval: types.NullDurationFrom(1 * time.Second),
}
}
// Apply applies the new config to the existing one
func (cfg Config) Apply(v Config) Config {
if v.ServiceName.Valid {
cfg.ServiceName = v.ServiceName
}
if v.ServiceVersion.Valid {
cfg.ServiceVersion = v.ServiceVersion
}
if v.MetricPrefix.Valid {
cfg.MetricPrefix = v.MetricPrefix
}
if v.FlushInterval.Valid {
cfg.FlushInterval = v.FlushInterval
}
if v.ExporterType.Valid {
cfg.ExporterType = v.ExporterType
}
if v.ExportInterval.Valid {
cfg.ExportInterval = v.ExportInterval
}
if v.HTTPExporterInsecure.Valid {
cfg.HTTPExporterInsecure = v.HTTPExporterInsecure
}
if v.HTTPExporterEndpoint.Valid {
cfg.HTTPExporterEndpoint = v.HTTPExporterEndpoint
}
if v.HTTPExporterURLPath.Valid {
cfg.HTTPExporterURLPath = v.HTTPExporterURLPath
}
if v.GRPCExporterEndpoint.Valid {
cfg.GRPCExporterEndpoint = v.GRPCExporterEndpoint
}
if v.GRPCExporterInsecure.Valid {
cfg.GRPCExporterInsecure = v.GRPCExporterInsecure
}
if v.TLSInsecureSkipVerify.Valid {
cfg.TLSInsecureSkipVerify = v.TLSInsecureSkipVerify
}
if v.TLSCertificate.Valid {
cfg.TLSCertificate = v.TLSCertificate
}
if v.TLSClientCertificate.Valid {
cfg.TLSClientCertificate = v.TLSClientCertificate
}
if v.TLSClientKey.Valid {
cfg.TLSClientKey = v.TLSClientKey
}
if v.Headers.Valid {
cfg.Headers = v.Headers
}
return cfg
}
// Validate validates the config
func (cfg Config) Validate() error {
if cfg.ServiceName.String == "" {
return errors.New("providing service name is required")
}
// TODO: it's not actually required, but we should probably have a default
// check if it works without it
if cfg.ServiceVersion.String == "" {
return errors.New("providing service version is required")
}
if cfg.ExporterType.String != grpcExporterType && cfg.ExporterType.String != httpExporterType {
return fmt.Errorf(
"unsupported exporter type %q, currently only %q and %q are supported",
cfg.ExporterType.String,
grpcExporterType,
httpExporterType,
)
}
if cfg.ExporterType.String == grpcExporterType {
if cfg.GRPCExporterEndpoint.String == "" {
return errors.New("gRPC exporter endpoint is required")
}
}
if cfg.ExporterType.String == httpExporterType {
endpoint := cfg.HTTPExporterEndpoint.String
if endpoint == "" {
return errors.New("HTTP exporter endpoint is required")
}
if strings.HasPrefix(endpoint, "http://") ||
strings.HasPrefix(endpoint, "https://") {
return errors.New("HTTP exporter endpoint must only be host and port, no scheme")
}
}
return nil
}
// String returns a string representation of the config
func (cfg Config) String() string {
var endpoint string
exporter := cfg.ExporterType.String
if cfg.ExporterType.String == httpExporterType {
endpoint = "http"
if !cfg.HTTPExporterInsecure.Bool {
endpoint += "s"
}
endpoint += "://" + cfg.HTTPExporterEndpoint.String + cfg.HTTPExporterURLPath.String
} else {
endpoint = cfg.GRPCExporterEndpoint.String
if cfg.GRPCExporterInsecure.Bool {
exporter += " (insecure)"
}
}
return fmt.Sprintf("%s, %s", exporter, endpoint)
}
// parseJSON parses the supplied JSON into a Config.
func parseJSON(data json.RawMessage) (Config, error) {
var c Config
err := json.Unmarshal(data, &c)
return c, err
}
// parseEnvs parses the supplied environment variables into a Config.
func parseEnvs(env map[string]string) (Config, error) {
cfg := Config{}
if serviceName, ok := env["OTEL_SERVICE_NAME"]; ok {
cfg.ServiceName = null.StringFrom(serviceName)
}
err := envconfig.Process("K6_OTEL_", &cfg, func(key string) (string, bool) {
v, ok := env[key]
return v, ok
})
return cfg, err
}