diff --git a/js/modules/k6/http/request_test.go b/js/modules/k6/http/request_test.go index ad2cb13579c..a03ca69e052 100644 --- a/js/modules/k6/http/request_test.go +++ b/js/modules/k6/http/request_test.go @@ -131,7 +131,7 @@ func assertRequestMetricsEmitted(t *testing.T, sampleContainers []stats.SampleCo assert.True(t, seenReceiving, "url %s didn't emit Receiving", url) } -func assertRequestMetricsEmittedSingle(t *testing.T, sampleContainer stats.SampleContainer, expectedTags map[string]string, metrics []*stats.Metric) { +func assertRequestMetricsEmittedSingle(t *testing.T, sampleContainer stats.SampleContainer, expectedTags map[string]string, metrics []*stats.Metric, callback func(sample stats.Sample)) { t.Helper() metricMap := make(map[string]bool, len(metrics)) @@ -145,6 +145,9 @@ func assertRequestMetricsEmittedSingle(t *testing.T, sampleContainer stats.Sampl assert.False(t, v, "second metric %s", sample.Metric.Name) metricMap[sample.Metric.Name] = true assert.EqualValues(t, expectedTags, tags, "%s", tags) + if callback != nil { + callback(sample) + } } for k, v := range metricMap { assert.True(t, v, "didn't emit %s", k) diff --git a/js/modules/k6/http/response_callback_test.go b/js/modules/k6/http/response_callback_test.go index 3b20c4938d5..188eb46ec62 100644 --- a/js/modules/k6/http/response_callback_test.go +++ b/js/modules/k6/http/response_callback_test.go @@ -26,6 +26,7 @@ import ( "github.com/dop251/goja" "github.com/loadimpact/k6/js/common" + "github.com/loadimpact/k6/lib" "github.com/loadimpact/k6/lib/metrics" "github.com/loadimpact/k6/stats" "github.com/stretchr/testify/assert" @@ -296,8 +297,76 @@ func TestResponseCallbackInAction(t *testing.T) { require.Equal(t, len(testCase.expectedSamples), reqsCount) for i, expectedSample := range testCase.expectedSamples { - assertRequestMetricsEmittedSingle(t, bufSamples[i], expectedSample.tags, expectedSample.metrics) + assertRequestMetricsEmittedSingle(t, bufSamples[i], expectedSample.tags, expectedSample.metrics, nil) } }) } } + +func TestResponseCallbackInActionWithoutPassedTag(t *testing.T) { + t.Parallel() + tb, state, samples, rt, _ := newRuntime(t) + defer tb.Cleanup() + sr := tb.Replacer.Replace + allHTTPMetrics := []*stats.Metric{ + metrics.HTTPReqs, + metrics.HTTPReqFailed, + metrics.HTTPReqBlocked, + metrics.HTTPReqConnecting, + metrics.HTTPReqDuration, + metrics.HTTPReqReceiving, + metrics.HTTPReqSending, + metrics.HTTPReqWaiting, + metrics.HTTPReqTLSHandshaking, + } + deleteSystemTag(state, stats.TagPassed.String()) + + state.HTTPResponseCallback = DefaultHTTPResponseCallback() + + _, err := rt.RunString(sr(`http.request("GET", "HTTPBIN_URL/redirect/1", null, {responseCallback: http.expectedStatuses(200)});`)) + assert.NoError(t, err) + bufSamples := stats.GetBufferedSamples(samples) + + reqsCount := 0 + for _, container := range bufSamples { + for _, sample := range container.GetSamples() { + if sample.Metric.Name == "http_reqs" { + reqsCount++ + } + } + } + + require.Equal(t, 2, reqsCount) + + tags := map[string]string{ + "method": "GET", + "url": sr("HTTPBIN_URL/redirect/1"), + "name": sr("HTTPBIN_URL/redirect/1"), + "status": "302", + "group": "", + "proto": "HTTP/1.1", + } + assertRequestMetricsEmittedSingle(t, bufSamples[0], tags, allHTTPMetrics, func(sample stats.Sample) { + if sample.Metric.Name == metrics.HTTPReqFailed.Name { + require.EqualValues(t, sample.Value, 1) + } + }) + tags["url"] = sr("HTTPBIN_URL/get") + tags["name"] = tags["url"] + tags["status"] = "200" + assertRequestMetricsEmittedSingle(t, bufSamples[1], tags, allHTTPMetrics, func(sample stats.Sample) { + if sample.Metric.Name == metrics.HTTPReqFailed.Name { + require.EqualValues(t, sample.Value, 0) + } + }) +} + +func deleteSystemTag(state *lib.State, tag string) { + enabledTags := state.Options.SystemTags.Map() + delete(enabledTags, tag) + tagsList := make([]string, 0, len(enabledTags)) + for k := range enabledTags { + tagsList = append(tagsList, k) + } + state.Options.SystemTags = stats.ToSystemTagSet(tagsList) +} diff --git a/lib/netext/httpext/transport.go b/lib/netext/httpext/transport.go index 2cd1671f801..e06ea3869bc 100644 --- a/lib/netext/httpext/transport.go +++ b/lib/netext/httpext/transport.go @@ -175,14 +175,14 @@ func (t *transport) measureAndEmitMetrics(unfReq *unfinishedRequest) *finishedRe statusCode = unfReq.response.StatusCode } passed := t.responseCallback(statusCode) + if passed { + failed = 0 + } else { + failed = 1 + } + if enabledTags.Has(stats.TagPassed) { - if passed { - failed = 0 - tags[stats.TagPassed.String()] = "true" - } else { - tags[stats.TagPassed.String()] = "false" - failed = 1 - } + tags[stats.TagPassed.String()] = strconv.FormatBool(passed) } }