Skip to content

Commit

Permalink
Add cloud output specific option to stop engine on error
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Apr 14, 2021
1 parent a83723a commit 60e3dd3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
4 changes: 4 additions & 0 deletions cloudapi/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Config struct {
PushRefID null.String `json:"pushRefID" envconfig:"K6_CLOUD_PUSH_REF_ID"`
WebAppURL null.String `json:"webAppURL" envconfig:"K6_CLOUD_WEB_APP_URL"`
NoCompress null.Bool `json:"noCompress" envconfig:"K6_CLOUD_NO_COMPRESS"`
StopOnError null.Bool `json:"stopOnError" envconfig:"K6_CLOUD_STOP_ON_ERROR"`

MaxMetricSamplesPerPackage null.Int `json:"maxMetricSamplesPerPackage" envconfig:"K6_CLOUD_MAX_METRIC_SAMPLES_PER_PACKAGE"`

Expand Down Expand Up @@ -209,6 +210,9 @@ func (c Config) Apply(cfg Config) Config {
if cfg.NoCompress.Valid {
c.NoCompress = cfg.NoCompress
}
if cfg.StopOnError.Valid {
c.StopOnError = cfg.StopOnError
}
if cfg.MaxMetricSamplesPerPackage.Valid {
c.MaxMetricSamplesPerPackage = cfg.MaxMetricSamplesPerPackage
}
Expand Down
1 change: 1 addition & 0 deletions cloudapi/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func TestConfigApply(t *testing.T) {
PushRefID: null.NewString("PushRefID", true),
WebAppURL: null.NewString("foo", true),
NoCompress: null.NewBool(true, true),
StopOnError: null.NewBool(true, true),
MaxMetricSamplesPerPackage: null.NewInt(2, true),
MetricPushInterval: types.NewNullDuration(1*time.Second, true),
MetricPushConcurrency: null.NewInt(3, true),
Expand Down
4 changes: 3 additions & 1 deletion output/cloud/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,9 @@ func (out *Output) pushMetrics() {
if err != nil {
if out.shouldStopSendingMetrics(err) {
out.logger.WithError(err).Warn("Stopped sending metrics to cloud due to an error")
out.engineStopFunc(err)
if out.config.StopOnError.Bool {
out.engineStopFunc(err)
}
close(out.stopSendingMetrics)
break
}
Expand Down
23 changes: 20 additions & 3 deletions output/cloud/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,18 @@ func TestCloudOutputMaxPerPacket(t *testing.T) {

func TestCloudOutputStopSendingMetric(t *testing.T) {
t.Parallel()
t.Run("stop engine on error", func(t *testing.T) {
t.Parallel()
testCloudOutputStopSendingMetric(t, true)
})

t.Run("don't stop engine on error", func(t *testing.T) {
t.Parallel()
testCloudOutputStopSendingMetric(t, false)
})
}

func testCloudOutputStopSendingMetric(t *testing.T, stopOnError bool) {
tb := httpmultibin.NewHTTPMultiBin(t)
tb.Mux.HandleFunc("/v1/tests", http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
Expand Down Expand Up @@ -417,8 +429,9 @@ func TestCloudOutputStopSendingMetric(t *testing.T) {
JSONConfig: json.RawMessage(fmt.Sprintf(`{
"host": "%s", "noCompress": true,
"maxMetricSamplesPerPackage": 50,
"name": "something-that-should-be-overwritten"
}`, tb.ServerHTTP.URL)),
"name": "something-that-should-be-overwritten",
"stopOnError": %t
}`, tb.ServerHTTP.URL, stopOnError)),
ScriptOptions: lib.Options{
Duration: types.NullDurationFrom(1 * time.Second),
SystemTags: &stats.DefaultSystemTagSet,
Expand All @@ -428,6 +441,10 @@ func TestCloudOutputStopSendingMetric(t *testing.T) {
},
ScriptPath: &url.URL{Path: "/script.js"},
})
var expectedEngineStopFuncCalled int64
if stopOnError {
expectedEngineStopFuncCalled = 1
}
var engineStopFuncCalled int64
out.engineStopFunc = func(error) {
atomic.AddInt64(&engineStopFuncCalled, 1)
Expand Down Expand Up @@ -500,7 +517,7 @@ func TestCloudOutputStopSendingMetric(t *testing.T) {
t.Fatal("sending metrics wasn't stopped")
}
require.Equal(t, max, count)
require.EqualValues(t, engineStopFuncCalled, 1)
require.Equal(t, expectedEngineStopFuncCalled, engineStopFuncCalled)

nBufferSamples := len(out.bufferSamples)
nBufferHTTPTrails := len(out.bufferHTTPTrails)
Expand Down

0 comments on commit 60e3dd3

Please sign in to comment.