Skip to content

Commit

Permalink
Emit http_req_failed with correct value even if tag passed is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Feb 19, 2021
1 parent 4546584 commit 17e5cc2
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 9 deletions.
5 changes: 4 additions & 1 deletion js/modules/k6/http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
Expand Down
71 changes: 70 additions & 1 deletion js/modules/k6/http/response_callback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
14 changes: 7 additions & 7 deletions lib/netext/httpext/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 17e5cc2

Please sign in to comment.