Skip to content

Commit

Permalink
Refactor name tag fix, remove counter
Browse files Browse the repository at this point in the history
Resolves? #1570 (comment)
  • Loading branch information
Ivan Mirić committed Jul 23, 2020
1 parent 693d3d4 commit 4f30941
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 deletions.
4 changes: 2 additions & 2 deletions js/modules/k6/http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ func TestRequestAndBatch(t *testing.T) {

sampleContainers := stats.GetBufferedSamples(samples)
assertRequestMetricsEmitted(t, sampleContainers[0:1], "GET",
urlMasked, "", 401, "")
urlRaw, urlMasked, 401, "")
assertRequestMetricsEmitted(t, sampleContainers[1:2], "GET",
urlRaw, "", 200, "")
})
Expand Down Expand Up @@ -1919,6 +1919,6 @@ func TestDigestAuthWithBody(t *testing.T) {
"http://HTTPBIN_IP:HTTPBIN_PORT/digest-auth-with-post/auth/testuser/testpwd")

sampleContainers := stats.GetBufferedSamples(samples)
assertRequestMetricsEmitted(t, sampleContainers[0:1], "POST", urlMasked, "", 401, "")
assertRequestMetricsEmitted(t, sampleContainers[0:1], "POST", urlRaw, urlMasked, 401, "")
assertRequestMetricsEmitted(t, sampleContainers[1:2], "POST", urlRaw, "", 200, "")
}
2 changes: 1 addition & 1 deletion lib/netext/httpext/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func MakeRequest(ctx context.Context, preq *ParsedHTTPRequest) (*Response, error
}
}

tracerTransport := newTransport(ctx, state, tags, preq.URL)
tracerTransport := newTransport(ctx, state, tags, preq.URL.Name)
var transport http.RoundTripper = tracerTransport

if state.Options.HTTPDebug.String != "" {
Expand Down
32 changes: 12 additions & 20 deletions lib/netext/httpext/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ type transport struct {

lastRequest *unfinishedRequest
lastRequestLock *sync.Mutex
// Number of processed requests. It will be >1 when processing
// redirects, digest auth, etc.
numProcReqs uint16
// Original parsed URL, required for tagging purposes.
origURL *URL
origURLName string
}

// unfinishedRequest stores the request and the raw result returned from the
Expand Down Expand Up @@ -81,14 +77,14 @@ func newTransport(
ctx context.Context,
state *lib.State,
tags map[string]string,
origURL *URL,
origURLName string,
) *transport {
return &transport{
ctx: ctx,
state: state,
tags: tags,
lastRequestLock: new(sync.Mutex),
origURL: origURL,
origURLName: origURLName,
}
}

Expand Down Expand Up @@ -124,24 +120,22 @@ func (t *transport) measureAndEmitMetrics(unfReq *unfinishedRequest) *finishedRe
} else {
cleanURL := URL{u: unfReq.request.URL, URL: unfReq.request.URL.String()}.Clean()
if enabledTags.Has(stats.TagURL) {
// Tag the first request with the original clean URL and
// subsequent ones (e.g. part of a redirect chain or digest auth)
// with the updated clean URL.
if t.origURL != nil && t.numProcReqs == 1 {
tags["url"] = t.origURL.Clean()
} else {
tags["url"] = cleanURL
}
tags["url"] = cleanURL
}

// Only override the name tag if not specified by the user.
if _, ok := tags["name"]; !ok && enabledTags.Has(stats.TagName) {
if t.origURL != nil && t.numProcReqs == 1 {
tags["name"] = t.origURL.Name
// If a name tag is already set in the script, use it for all requests
// processed by this transport. Otherwise, use the original URL name
// to preserve any escaped query string arguments, but only for the
// first request in the chain.
if t.origURLName != "" {
tags["name"] = t.origURLName
t.origURLName = ""
} else {
tags["name"] = cleanURL
}
}

if enabledTags.Has(stats.TagMethod) {
tags["method"] = unfReq.request.Method
}
Expand Down Expand Up @@ -230,7 +224,5 @@ func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
err: err,
})

t.numProcReqs++

return resp, err
}

0 comments on commit 4f30941

Please sign in to comment.