Skip to content

Commit

Permalink
k6runner: send logs even if metrics are malformed
Browse files Browse the repository at this point in the history
  • Loading branch information
nadiamoe committed Jun 26, 2024
1 parent 20d19cb commit 698d447
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
14 changes: 7 additions & 7 deletions internal/k6runner/k6runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ func (r Processor) Run(ctx context.Context, registry *prometheus.Registry, logge
return false, err
}

if err := k6LogsToLogger(result.Logs, logger); err != nil {
internalLogger.Debug().
Err(err).
Msg("cannot load logs to logger")
return false, err
}

var (
collector sampleCollector
resultCollector checkResultCollector
Expand All @@ -118,13 +125,6 @@ func (r Processor) Run(ctx context.Context, registry *prometheus.Registry, logge
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 != "":
Expand Down
33 changes: 32 additions & 1 deletion internal/k6runner/k6runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ func TestScriptHTTPRun(t *testing.T) {
statusCode int
expectSuccess bool
expectError error
expectErrorAs any // To accomodate return of unnamed errors. If set, expectError is ignored.
expectLogs string
}{
{
Expand Down Expand Up @@ -242,6 +243,32 @@ func TestScriptHTTPRun(t *testing.T) {
expectError: nil,
expectLogs: nonDebugLogLine,
},
{
name: "borked logs are sent best-effort",
response: &RunResponse{
Metrics: testMetrics,
Logs: []byte(`level=error foo="b` + "\n"),
Error: "we killed k6",
ErrorCode: "user",
},
statusCode: http.StatusUnprocessableEntity,
expectSuccess: false,
expectError: nil,
expectLogs: `level="error"` + "\n",
},
{
name: "logs are sent on borked metrics",
response: &RunResponse{
Metrics: []byte("probe_succ{"),
Logs: testLogs,
Error: "we killed k6",
ErrorCode: "user",
},
statusCode: http.StatusUnprocessableEntity,
expectSuccess: false,
expectErrorAs: &expfmt.ParseError{},
expectLogs: nonDebugLogLine,
},
{
name: "inconsistent runner response A",
response: &RunResponse{
Expand Down Expand Up @@ -314,7 +341,11 @@ func TestScriptHTTPRun(t *testing.T) {
success, err := script.Run(ctx, registry, logger, zlogger)
require.Equal(t, tc.expectSuccess, success)
require.Equal(t, tc.expectLogs, logger.buf.String())
require.ErrorIs(t, err, tc.expectError)
if tc.expectErrorAs == nil {
require.ErrorIs(t, err, tc.expectError)
} else {
require.ErrorAs(t, err, tc.expectErrorAs)
}
})
}
}
Expand Down

0 comments on commit 698d447

Please sign in to comment.