-
Notifications
You must be signed in to change notification settings - Fork 22
/
k6runner.go
520 lines (434 loc) · 13.3 KB
/
k6runner.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
package k6runner
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os/exec"
"strings"
"time"
"github.com/go-logfmt/logfmt"
"github.com/grafana/synthetic-monitoring-agent/internal/prober/logger"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
"github.com/prometheus/common/model"
"github.com/rs/zerolog"
"github.com/spf13/afero"
)
type Runner interface {
WithLogger(logger *zerolog.Logger) Runner
Run(ctx context.Context, script []byte) (*RunResponse, error)
}
type RunnerOpts struct {
Uri string
BlacklistedIP string
}
func New(opts RunnerOpts) Runner {
var r Runner
logger := zerolog.Nop()
if strings.HasPrefix(opts.Uri, "http") {
r = &HttpRunner{
url: opts.Uri,
logger: &logger,
}
} else {
r = &LocalRunner{
k6path: opts.Uri,
logger: &logger,
fs: afero.NewOsFs(),
blacklistedIP: "10.0.0.0/8",
}
r.(*LocalRunner).withOpts(opts)
}
return r
}
func (r *LocalRunner) withOpts(opts RunnerOpts) {
if opts.BlacklistedIP != "" {
r.blacklistedIP = opts.BlacklistedIP
}
}
type Script struct {
runner Runner
script []byte
}
func NewScript(script []byte, k6runner Runner) (*Script, error) {
r := Script{
runner: k6runner,
script: script,
}
return &r, nil
}
var (
ErrBuggyRunner = errors.New("runner returned buggy response")
ErrFromRunner = errors.New("runner reported an error")
)
func (r Script) Run(ctx context.Context, registry *prometheus.Registry, logger logger.Logger, internalLogger zerolog.Logger) (bool, error) {
k6runner := r.runner.WithLogger(&internalLogger)
result, err := k6runner.Run(ctx, r.script)
if err != nil {
internalLogger.Debug().
Err(err).
Msg("k6 script exited with error code")
return false, err
}
var (
collector sampleCollector
resultCollector checkResultCollector
)
if err := extractMetricSamples(result.Metrics, internalLogger, collector.process, resultCollector.process); err != nil {
internalLogger.Debug().
Err(err).
Msg("cannot extract metric samples")
return false, err
}
if err := registry.Register(&collector.collector); err != nil {
internalLogger.Error().
Err(err).
Msg("cannot register collector")
return false, err
}
if err := k6LogsToLogger(result.Logs, logger); err != nil {
internalLogger.Debug().
Err(err).
Msg("cannot load logs to logger")
return false, err
}
// If only one of Error and ErrorCode are non-empty, the proxy is misbehaving.
switch {
case result.Error == "" && result.ErrorCode != "":
fallthrough
case result.Error != "" && result.ErrorCode == "":
return false, fmt.Errorf(
"%w: only one of error (%q) and errorCode (%q) is non-empty",
ErrBuggyRunner, result.Error, result.ErrorCode,
)
}
// https://github.com/grafana/sm-k6-runner/blob/b811839d444a7e69fd056b0a4e6ccf7e914197f3/internal/mq/runner.go#L51
switch result.ErrorCode {
case "":
// No error, all good.
return true, nil
case "timeout", "killed", "user":
// These are user errors. The probe failed, but we don't return an error.
return false, nil
default:
// We got an "unknown" error, or some other code we do not recognize. Return it so we log it.
return false, fmt.Errorf("%w: %s: %s", ErrFromRunner, result.ErrorCode, result.Error)
}
}
type customCollector struct {
metrics []prometheus.Metric
}
func (c *customCollector) Describe(ch chan<- *prometheus.Desc) {
// We do not send any descriptions in order to create an unchecked
// metric. This allows us to have a metric with the same name and
// different label values.
//
// TODO(mem): reevaluate if this is really want we want to do.
}
func (c *customCollector) Collect(ch chan<- prometheus.Metric) {
for _, m := range c.metrics {
ch <- m
}
}
type sampleProcessorFunc func(*dto.MetricFamily, *model.Sample) error
type sampleCollector struct {
collector customCollector
}
func (sc *sampleCollector) process(mf *dto.MetricFamily, sample *model.Sample) error {
// TODO(mem): This is really crappy. We have a
// set of metric families, and we are
// converting back to samples so that we can
// add that to the registry. We need to rework
// the logic in the prober so that it can
// return a set of metric families. The probes
// that don't have this, can create a registry
// locally and get the metric families from
// that.
name, found := sample.Metric[model.MetricNameLabel]
if !found {
return fmt.Errorf("missing metric name")
}
desc := prometheus.NewDesc(string(name), mf.GetHelp(), nil, metricToLabels(sample.Metric))
// TODO(mem): maybe change this to untyped?
m, err := prometheus.NewConstMetric(desc, prometheus.GaugeValue, float64(sample.Value))
if err != nil {
return fmt.Errorf("creating prometheus metric: %w", err)
}
sc.collector.metrics = append(sc.collector.metrics, m)
return nil
}
type checkResultCollector struct {
failure bool
}
func (rc *checkResultCollector) process(mf *dto.MetricFamily, sample *model.Sample) error {
if sample.Metric[model.MetricNameLabel] != "probe_checks_total" {
return nil
}
if sample.Metric["result"] != "fail" {
return nil
}
if sample.Value != 0 {
rc.failure = true
}
return nil
}
func extractMetricSamples(metrics []byte, logger zerolog.Logger, processors ...sampleProcessorFunc) error {
promDecoder := expfmt.NewDecoder(bytes.NewBuffer(metrics), expfmt.NewFormat(expfmt.TypeTextPlain))
decoderOpts := expfmt.DecodeOptions{Timestamp: model.Now()}
for {
var mf dto.MetricFamily
switch err := promDecoder.Decode(&mf); err {
case nil:
// Got one metric family
samples, err := expfmt.ExtractSamples(&decoderOpts, &mf)
if err != nil {
logger.Error().Err(err).Msg("extracting samples")
return fmt.Errorf("extracting samples: %w", err)
}
for _, sample := range samples {
for _, p := range processors {
err := p(&mf, sample)
if err != nil {
logger.Error().Err(err).Msg("processing samples")
return err
}
}
}
case io.EOF:
// nothing was returned, we are done
return nil
default:
logger.Error().Err(err).Msg("decoding prometheus metrics")
return fmt.Errorf("decoding prometheus metrics: %w", err)
}
}
}
func metricToLabels(metrics model.Metric) prometheus.Labels {
// Ugh.
labels := make(prometheus.Labels)
for name, value := range metrics {
name := string(name)
if name == model.MetricNameLabel {
continue
}
labels[name] = string(value)
}
return labels
}
func k6LogsToLogger(logs []byte, logger logger.Logger) error {
// This seems a little silly, we should be able to take the out of k6
// and pass it directly to Loki. The problem with that is that the only
// thing probers have access to is the logger.Logger.
//
// We could pass another object to the prober, that would take Loki log
// entries, and the publisher could decorate that with the necessary
// labels.
dec := logfmt.NewDecoder(bytes.NewBuffer(logs))
NEXT_RECORD:
for dec.ScanRecord() {
var line []interface{}
var source, level string
for dec.ScanKeyval() {
key := string(dec.Key())
value := string(dec.Value())
switch key {
case "source":
source = value
case "level":
level = value
}
line = append(line, key, value)
}
if level == "debug" && source == "" { // if there's no source, it's probably coming from k6
continue NEXT_RECORD
}
_ = logger.Log(line...)
}
return nil
}
type HttpRunner struct {
url string
logger *zerolog.Logger
}
type requestError struct {
Err string `json:"error"`
Message string `json:"msg"`
}
var _ error = requestError{}
func (r requestError) Error() string {
return fmt.Sprintf("%s: %s", r.Err, r.Message)
}
type Settings struct {
Timeout int64 `json:"timeout"`
}
type RunRequest struct {
Script []byte `json:"script"`
Settings Settings `json:"settings"`
}
type RunResponse struct {
Error string `json:"error,omitempty"`
ErrorCode string `json:"errorCode,omitempty"`
Metrics []byte `json:"metrics"`
Logs []byte `json:"logs"`
}
func (r HttpRunner) WithLogger(logger *zerolog.Logger) Runner {
return HttpRunner{
url: r.url,
logger: logger,
}
}
var ErrUnexpectedStatus = errors.New("unexpected status code")
func (r HttpRunner) Run(ctx context.Context, script []byte) (*RunResponse, error) {
req, err := json.Marshal(&RunRequest{
Script: script,
Settings: Settings{
Timeout: getTimeout(ctx).Milliseconds(),
},
})
if err != nil {
return nil, fmt.Errorf("running script: %w", err)
}
resp, err := http.Post(r.url, "application/json", bytes.NewBuffer(req))
if err != nil {
r.logger.Error().Err(err).Msg("sending request")
return nil, fmt.Errorf("running script: %w", err)
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusOK, http.StatusRequestTimeout, http.StatusUnprocessableEntity, http.StatusInternalServerError:
// These are status code that come with a machine-readable response. The response may contain an error, which is
// handled later.
// See: https://github.com/grafana/sm-k6-runner/blob/main/internal/mq/proxy.go#L215
default:
return nil, fmt.Errorf("%w %d", ErrUnexpectedStatus, resp.StatusCode)
}
var result RunResponse
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
r.logger.Error().Err(err).Msg("decoding script result")
return nil, fmt.Errorf("decoding script result: %w", err)
}
r.logger.Debug().Bytes("metrics", result.Metrics).Bytes("logs", result.Logs).Msg("script result")
return &result, nil
}
type LocalRunner struct {
k6path string
logger *zerolog.Logger
fs afero.Fs
blacklistedIP string
}
func (r LocalRunner) WithLogger(logger *zerolog.Logger) Runner {
return LocalRunner{
k6path: r.k6path,
fs: r.fs,
logger: logger,
}
}
func (r LocalRunner) Run(ctx context.Context, script []byte) (*RunResponse, error) {
afs := afero.Afero{Fs: r.fs}
workdir, err := afs.TempDir("", "k6-runner")
if err != nil {
return nil, fmt.Errorf("cannot create temporary directory: %w", err)
}
defer func() {
if err := r.fs.RemoveAll(workdir); err != nil {
r.logger.Error().Err(err).Str("severity", "critical").Msg("cannot remove temporary directory")
}
}()
metricsFn, err := mktemp(r.fs, workdir, "*.json")
if err != nil {
return nil, fmt.Errorf("cannot obtain temporary metrics filename: %w", err)
}
logsFn, err := mktemp(r.fs, workdir, "*.log")
if err != nil {
return nil, fmt.Errorf("cannot obtain temporary logs filename: %w", err)
}
scriptFn, err := mktemp(r.fs, workdir, "*.js")
if err != nil {
return nil, fmt.Errorf("cannot obtain temporary script filename: %w", err)
}
if err := afs.WriteFile(scriptFn, script, 0o644); err != nil {
return nil, fmt.Errorf("cannot write temporary script file: %w", err)
}
k6Path, err := exec.LookPath(r.k6path)
if err != nil {
return nil, fmt.Errorf("cannot find k6 executable: %w", err)
}
timeout := getTimeout(ctx)
// #nosec G204 -- the variables are not user-controlled
cmd := exec.CommandContext(
ctx,
k6Path,
"run",
"--out", "sm="+metricsFn,
"--log-format", "logfmt",
"--log-output", "file="+logsFn,
"--vus", "1",
"--iterations", "1",
"--duration", timeout.String(),
"--max-redirects", "10",
"--batch", "10",
"--batch-per-host", "4",
"--no-connection-reuse",
"--blacklist-ip", r.blacklistedIP,
"--block-hostnames", "*.cluster.local", // TODO(mem): make this configurable
"--summary-time-unit", "s",
// "--discard-response-bodies", // TODO(mem): make this configurable
"--dns", "ttl=30s,select=random,policy=preferIPv4", // TODO(mem): this needs fixing, preferIPv4 is probably not what we want
"--no-thresholds",
"--no-usage-report",
"--no-color",
"--no-summary",
"--verbose",
scriptFn,
)
var stdout, stderr bytes.Buffer
cmd.Dir = workdir
cmd.Stdin = nil
cmd.Stdout = &stdout
cmd.Stderr = &stderr
start := time.Now()
r.logger.Info().Str("command", cmd.String()).Bytes("script", script).Msg("running k6 script")
if err := cmd.Run(); err != nil {
r.logger.Error().Err(err).Str("stdout", stdout.String()).Str("stderr", stderr.String()).Msg("k6 exited with error")
return nil, fmt.Errorf("cannot execute k6 script: %w", err)
}
duration := time.Since(start)
r.logger.Debug().Str("stdout", stdout.String()).Str("stderr", stderr.String()).Dur("duration", duration).Msg("ran k6 script")
r.logger.Info().Dur("duration", duration).Msg("ran k6 script")
var result RunResponse
result.Metrics, err = afs.ReadFile(metricsFn)
if err != nil {
r.logger.Error().Err(err).Str("filename", metricsFn).Msg("cannot read metrics file")
return nil, fmt.Errorf("cannot read metrics: %w", err)
}
result.Logs, err = afs.ReadFile(logsFn)
if err != nil {
r.logger.Error().Err(err).Str("filename", logsFn).Msg("cannot read metrics file")
return nil, fmt.Errorf("cannot read logs: %w", err)
}
r.logger.Debug().Bytes("metrics", result.Metrics).Bytes("logs", result.Logs).Msg("k6 result")
return &result, nil
}
func mktemp(fs afero.Fs, dir, pattern string) (string, error) {
f, err := afero.TempFile(fs, dir, pattern)
if err != nil {
return "", fmt.Errorf("cannot create temporary file: %w", err)
}
if err := f.Close(); err != nil {
return "", fmt.Errorf("cannot close temporary file: %w", err)
}
return f.Name(), nil
}
func getTimeout(ctx context.Context) time.Duration {
deadline, ok := ctx.Deadline()
if !ok {
return 10 * time.Second
}
return time.Until(deadline)
}