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

Implemented AnonymizeQueryStringValues option (proposal 1 from #927). #1171

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions js/modules/k6/http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,36 @@ func TestRequestAndBatch(t *testing.T) {
}
}
})

t.Run("anonymizeQueryStringValues", func(t *testing.T) {
t.Run("disabled", func(t *testing.T) {
oldOpts := state.Options
defer func() { state.Options = oldOpts }()
state.Options.AnonymizeQueryStringValues = null.BoolFrom(false)

_, err := common.RunString(rt, sr(`
let res = http.get("HTTPBIN_URL/get?a=1&b=some-value&c=https%3A%2F%2Fsome-url.com%3Fa%3D1");
if (res.status != 200) { throw new Error("wrong status: " + res.status); }
`))
assert.NoError(t, err)

assertRequestMetricsEmitted(t, stats.GetBufferedSamples(samples), "GET", sr("HTTPBIN_URL/get?a=1&b=some-value&c=https%3A%2F%2Fsome-url.com%3Fa%3D1"), "", 200, "")
marklagendijk marked this conversation as resolved.
Show resolved Hide resolved
})

t.Run("enabled", func(t *testing.T) {
oldOpts := state.Options
defer func() { state.Options = oldOpts }()
state.Options.AnonymizeQueryStringValues = null.BoolFrom(true)

_, err := common.RunString(rt, sr(`
let res = http.get("HTTPBIN_URL/get?a=1&b=some-value&c=https%3A%2F%2Fsome-url.com%3Fa%3D1");
if (res.status != 200) { throw new Error("wrong status: " + res.status); }
`))
assert.NoError(t, err)

assertRequestMetricsEmitted(t, stats.GetBufferedSamples(samples), "GET", sr("HTTPBIN_URL/get?a=1&b=some-value&c=https%3A%2F%2Fsome-url.com%3Fa%3D1"), sr("HTTPBIN_URL/get?a=[]&b=[]&c=[]"), 200, "")
marklagendijk marked this conversation as resolved.
Show resolved Hide resolved
})
})
})
})

Expand Down
8 changes: 7 additions & 1 deletion lib/netext/httpext/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"net/http"
"net/http/cookiejar"
"net/url"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -217,7 +218,12 @@ func MakeRequest(ctx context.Context, preq *ParsedHTTPRequest) (*Response, error

// Only set the name system tag if the user didn't explicitly set it beforehand
if _, ok := tags["name"]; !ok && state.Options.SystemTags["name"] {
tags["name"] = preq.URL.Name
var name = preq.URL.Name
if state.Options.AnonymizeQueryStringValues.Bool {
var qsValueRegex = regexp.MustCompile("=[^&]*")
marklagendijk marked this conversation as resolved.
Show resolved Hide resolved
name = qsValueRegex.ReplaceAllString(name, "=[]")
}
tags["name"] = name
}
if state.Options.SystemTags["group"] {
tags["group"] = state.Group.Path
Expand Down
5 changes: 5 additions & 0 deletions lib/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ type Options struct {
// Tags to be applied to all samples for this running
RunTags *stats.SampleTags `json:"tags" envconfig:"tags"`

AnonymizeQueryStringValues null.Bool `json:"anonymizeQueryStringValues" envconfig:"anonymize_query_string_values"`

// Buffer size of the channel for metric samples; 0 means unbuffered
MetricSamplesBufferSize null.Int `json:"metricSamplesBufferSize" envconfig:"metric_samples_buffer_size"`

Expand Down Expand Up @@ -471,6 +473,9 @@ func (o Options) Apply(opts Options) Options {
if !opts.RunTags.IsEmpty() {
o.RunTags = opts.RunTags
}
if opts.AnonymizeQueryStringValues.Valid {
o.AnonymizeQueryStringValues = opts.AnonymizeQueryStringValues
}
if opts.MetricSamplesBufferSize.Valid {
o.MetricSamplesBufferSize = opts.MetricSamplesBufferSize
}
Expand Down