Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

k6runner: handle errors reported by http runners #694

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 17 additions & 18 deletions internal/k6runner/k6runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -111,7 +112,8 @@ func (r Script) Run(ctx context.Context, registry *prometheus.Registry, logger l
return false, err
}

return !resultCollector.failure, nil
success := result.Error == "" && result.ErrorCode == ""
return success, nil
Copy link
Contributor Author

@roobre roobre Apr 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if at this point we should create an error with result.Error and return it 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense to do I think

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delved a bit more on what this error is used for, and it seems like it is just log.Warned?

func (p Prober) Probe(ctx context.Context, target string, registry *prometheus.Registry, logger logger.Logger) bool {
success, err := p.script.Run(ctx, registry, logger, p.logger)
if err != nil {
p.logger.Warn().Err(err).Msg("running probe")
return false
}
return success
}

If this is the case we might want to return an error when the code is something that is not-on-us.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refined (slang for "made more complicated") the error handling logic in the latest commit, LMK what you think :)

}

type customCollector struct {
Expand Down Expand Up @@ -293,8 +295,10 @@ type RunRequest struct {
}

type RunResponse struct {
Metrics []byte `json:"metrics"`
Logs []byte `json:"logs"`
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 {
Expand All @@ -304,6 +308,8 @@ func (r HttpRunner) WithLogger(logger *zerolog.Logger) Runner {
}
}

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,
Expand All @@ -323,24 +329,17 @@ func (r HttpRunner) Run(ctx context.Context, script []byte) (*RunResponse, error

defer resp.Body.Close()

dec := json.NewDecoder(resp.Body)

if resp.StatusCode != http.StatusOK {
var result requestError

err := dec.Decode(&result)
if err != nil {
r.logger.Error().Err(err).Msg("decoding request response")
return nil, fmt.Errorf("running script: %w", err)
}

r.logger.Error().Err(result).Msg("request response")
return nil, fmt.Errorf("running script: %w", result)
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 = dec.Decode(&result)
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)
Expand Down
98 changes: 98 additions & 0 deletions internal/k6runner/k6runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,104 @@ func TestHttpRunnerRunError(t *testing.T) {
require.Error(t, err)
}

// TestScriptHTTPRun tests that Script reports what it should depending on the status code and responses of the HTTP
// runner.
func TestScriptHTTPRun(t *testing.T) {
t.Parallel()

var (
testMetrics = testhelper.MustReadFile(t, "testdata/test.out")
testLogs = testhelper.MustReadFile(t, "testdata/test.log")
)

for _, tc := range []struct {
name string
response *RunResponse
statusCode int
expectSuccess bool
expectError error
}{
{
name: "all good",
response: &RunResponse{
Metrics: testMetrics,
Logs: testLogs,
},
statusCode: http.StatusOK,
expectSuccess: true,
expectError: nil,
},
{
// HTTP runner returns an error when the upstream status is not recognized.
// Script should report that error and failure.
name: "unexpected status",
response: &RunResponse{},
statusCode: 999,
expectSuccess: false,
expectError: ErrUnexpectedStatus,
},
{
// HTTP runner should report failure but no error if there is an error in the response.
// Other than checking for known status codes, it is ignored in this logic.
name: "error in response status 200",
response: &RunResponse{
Metrics: testMetrics,
Logs: testLogs,
ErrorCode: "something-wrong",
},
statusCode: http.StatusOK,
expectSuccess: false,
expectError: nil,
},
{
// HTTP runner should report failure but no error if there is an error in the response.
// Other than checking for known status codes, it is ignored in this logic.
name: "error in response status 4XX",
response: &RunResponse{
Metrics: testMetrics,
Logs: testLogs,
ErrorCode: "something-wrong",
},
statusCode: http.StatusUnprocessableEntity,
expectSuccess: false,
expectError: nil,
},
} {
tc := tc

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

mux := http.NewServeMux()
mux.HandleFunc("/run", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(tc.statusCode)
_ = json.NewEncoder(w).Encode(tc.response)
})
srv := httptest.NewServer(mux)
t.Cleanup(srv.Close)

runner := New(RunnerOpts{Uri: srv.URL + "/run"})
script, err := NewScript([]byte("tee-hee"), runner)
require.NoError(t, err)

ctx, cancel := testhelper.Context(context.Background(), t)
t.Cleanup(cancel)

var (
registry = prometheus.NewRegistry()
logger testLogger
buf bytes.Buffer
zlogger = zerolog.New(&buf)
)

success, err := script.Run(ctx, registry, &logger, zlogger)
require.Equal(t, tc.expectSuccess, success)
require.ErrorIs(t, err, tc.expectError)
})
}
}

type testRunner struct {
metrics []byte
logs []byte
Expand Down