Skip to content

Commit

Permalink
all: switch to new SystemTagSet/Map
Browse files Browse the repository at this point in the history
fix golangci-lint
  • Loading branch information
cuonglm committed Oct 17, 2019
1 parent cbedb50 commit 98caab2
Show file tree
Hide file tree
Showing 33 changed files with 136 additions and 187 deletions.
12 changes: 8 additions & 4 deletions cmd/collectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/consts"
"github.com/loadimpact/k6/loader"
"github.com/loadimpact/k6/stats"
"github.com/loadimpact/k6/stats/cloud"
"github.com/loadimpact/k6/stats/csv"
"github.com/loadimpact/k6/stats/datadog"
Expand Down Expand Up @@ -124,9 +125,11 @@ func getCollector(collectorName, arg string, src *loader.SourceData, conf Config
if err != nil {
return nil, err
}

config = config.Apply(cmdConfig)
}
return csv.New(afero.NewOsFs(), conf.SystemTags, config)
return csv.New(afero.NewOsFs(), conf.SystemTags.Map(), config)

default:
return nil, errors.Errorf("unknown output type: %s", collectorName)
}
Expand All @@ -140,9 +143,10 @@ func newCollector(collectorName, arg string, src *loader.SourceData, conf Config

// Check if all required tags are present
missingRequiredTags := []string{}
for reqTag := range collector.GetRequiredSystemTags() {
if !conf.SystemTags[reqTag] {
missingRequiredTags = append(missingRequiredTags, reqTag)
requiredTags := collector.GetRequiredSystemTags()
for _, tag := range stats.SystemTagSetValues() {
if requiredTags.Has(tag) && !conf.SystemTags.Has(tag) {
missingRequiredTags = append(missingRequiredTags, tag.String())
}
}
if len(missingRequiredTags) > 0 {
Expand Down
3 changes: 2 additions & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/scheduler"
"github.com/loadimpact/k6/lib/types"
"github.com/loadimpact/k6/stats"
"github.com/loadimpact/k6/stats/cloud"
"github.com/loadimpact/k6/stats/csv"
"github.com/loadimpact/k6/stats/datadog"
Expand Down Expand Up @@ -313,7 +314,7 @@ func getConsolidatedConfig(fs afero.Fs, cliConf Config, runner lib.Runner) (conf
// Note that if you add option default value here, also add it in command line argument help text.
func applyDefault(conf Config) Config {
if conf.Options.SystemTags == nil {
conf = conf.Apply(Config{Options: lib.Options{SystemTags: lib.GetTagSet(lib.DefaultSystemTagList...)}})
conf = conf.Apply(Config{Options: lib.Options{SystemTags: stats.ToSystemTagSet(stats.DefaultSystemTagList)}})
}
return conf
}
Expand Down
15 changes: 11 additions & 4 deletions cmd/config_consolidation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/loadimpact/k6/lib/scheduler"
"github.com/loadimpact/k6/lib/testutils"
"github.com/loadimpact/k6/lib/types"
"github.com/loadimpact/k6/stats"
)

// A helper funcion for setting arbitrary environment variables and
Expand Down Expand Up @@ -393,16 +394,22 @@ func getConfigConsolidationTestCases() []configConsolidationTestCase {

// Test system tags
{opts{}, exp{}, func(t *testing.T, c Config) {
assert.Equal(t, lib.GetTagSet(lib.DefaultSystemTagList...), c.Options.SystemTags)
assert.Equal(t, stats.ToSystemTagSet(stats.DefaultSystemTagList), c.Options.SystemTags)
}},
{opts{cli: []string{"--system-tags", `""`}}, exp{}, func(t *testing.T, c Config) {
assert.Equal(t, lib.GetTagSet(), c.Options.SystemTags)
assert.Equal(t, stats.SystemTagSet(0), *c.Options.SystemTags)
}},
{
opts{runner: &lib.Options{SystemTags: lib.GetTagSet([]string{"proto", "url"}...)}},
opts{runner: &lib.Options{
SystemTags: stats.ToSystemTagSet([]string{stats.TagSubProto.String(), stats.TagURL.String()})},
},
exp{},
func(t *testing.T, c Config) {
assert.Equal(t, lib.GetTagSet("proto", "url"), c.Options.SystemTags)
assert.Equal(
t,
*stats.ToSystemTagSet([]string{stats.TagSubProto.String(), stats.TagURL.String()}),
*c.Options.SystemTags,
)
},
},
//TODO: test for differences between flagsets
Expand Down
4 changes: 2 additions & 2 deletions cmd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func optionFlagSet() *pflag.FlagSet {
// set it to nil here, and add the default in applyDefault() instead.
systemTagsCliHelpText := fmt.Sprintf(
"only include these system tags in metrics (default %s)",
lib.DefaultSystemTagList,
stats.DefaultSystemTagList,
)
flags.StringSlice("system-tags", nil, systemTagsCliHelpText)
flags.StringSlice("tag", nil, "add a `tag` to be applied to all samples, as `[name]=[value]`")
Expand Down Expand Up @@ -128,7 +128,7 @@ func getOptions(flags *pflag.FlagSet) (lib.Options, error) {
if err != nil {
return opts, err
}
opts.SystemTags = lib.GetTagSet(systemTagList...)
opts.SystemTags = stats.ToSystemTagSet(systemTagList)
}

blacklistIPStrings, err := flags.GetStringSlice("blacklist-ip")
Expand Down
4 changes: 2 additions & 2 deletions core/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ func TestRunTags(t *testing.T) {
VUsMax: null.IntFrom(2),
Hosts: tb.Dialer.Hosts,
RunTags: runTags,
SystemTags: lib.GetTagSet(lib.DefaultSystemTagList...),
SystemTags: stats.ToSystemTagSet(stats.DefaultSystemTagList),
InsecureSkipTLSVerify: null.BoolFrom(true),
}

Expand Down Expand Up @@ -800,7 +800,7 @@ func TestSetupTeardownThresholds(t *testing.T) {
)
require.NoError(t, err)
runner.SetOptions(runner.GetOptions().Apply(lib.Options{
SystemTags: lib.GetTagSet(lib.DefaultSystemTagList...),
SystemTags: stats.ToSystemTagSet(stats.DefaultSystemTagList),
SetupTimeout: types.NullDurationFrom(3 * time.Second),
TeardownTimeout: types.NullDurationFrom(3 * time.Second),
VUs: null.IntFrom(3),
Expand Down
2 changes: 1 addition & 1 deletion core/local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ func TestRealTimeAndSetupTeardownMetrics(t *testing.T) {
require.NoError(t, err)

options := lib.Options{
SystemTags: lib.GetTagSet(lib.DefaultSystemTagList...),
SystemTags: stats.ToSystemTagSet(stats.DefaultSystemTagList),
SetupTimeout: types.NullDurationFrom(4 * time.Second),
TeardownTimeout: types.NullDurationFrom(4 * time.Second),
}
Expand Down
5 changes: 3 additions & 2 deletions js/modules/k6/http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func newRuntime(
MaxRedirects: null.IntFrom(10),
UserAgent: null.StringFrom("TestUserAgent"),
Throw: null.BoolFrom(true),
SystemTags: lib.GetTagSet(lib.DefaultSystemTagList...),
SystemTags: stats.ToSystemTagSet(stats.DefaultSystemTagList),
//HttpDebug: null.StringFrom("full"),
}
samples := make(chan stats.SampleContainer, 1000)
Expand Down Expand Up @@ -1242,8 +1242,9 @@ func TestSystemTags(t *testing.T) {
state.Options.Apply(lib.Options{TLSVersion: &lib.TLSVersions{Max: lib.TLSVersion13}})

for num, tc := range testedSystemTags {
tc := tc
t.Run(fmt.Sprintf("TC %d with only %s", num, tc.tag), func(t *testing.T) {
state.Options.SystemTags = lib.GetTagSet(tc.tag)
state.Options.SystemTags = stats.ToSystemTagSet([]string{tc.tag})

_, err := common.RunString(rt, tc.code)
assert.NoError(t, err)
Expand Down
14 changes: 7 additions & 7 deletions js/modules/k6/k6.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ func (*K6) Group(ctx context.Context, name string, fn goja.Callable) (goja.Value
t := time.Now()

tags := state.Options.RunTags.CloneTags()
if state.Options.SystemTags["group"] {
if state.Options.SystemTags.Has(stats.TagGroup) {
tags["group"] = g.Path
}
if state.Options.SystemTags["vu"] {
if state.Options.SystemTags.Has(stats.TagVU) {
tags["vu"] = strconv.FormatInt(state.Vu, 10)
}
if state.Options.SystemTags["iter"] {
if state.Options.SystemTags.Has(stats.TagIter) {
tags["iter"] = strconv.FormatInt(state.Iteration, 10)
}

Expand All @@ -121,7 +121,7 @@ func (*K6) Check(ctx context.Context, arg0, checks goja.Value, extras ...goja.Va

// Prepare tags, make sure the `group` tag can't be overwritten.
commonTags := state.Options.RunTags.CloneTags()
if state.Options.SystemTags["group"] {
if state.Options.SystemTags.Has(stats.TagGroup) {
commonTags["group"] = state.Group.Path
}
if len(extras) > 0 {
Expand All @@ -130,10 +130,10 @@ func (*K6) Check(ctx context.Context, arg0, checks goja.Value, extras ...goja.Va
commonTags[k] = obj.Get(k).String()
}
}
if state.Options.SystemTags["vu"] {
if state.Options.SystemTags.Has(stats.TagVU) {
commonTags["vu"] = strconv.FormatInt(state.Vu, 10)
}
if state.Options.SystemTags["iter"] {
if state.Options.SystemTags.Has(stats.TagIter) {
commonTags["iter"] = strconv.FormatInt(state.Iteration, 10)
}

Expand All @@ -153,7 +153,7 @@ func (*K6) Check(ctx context.Context, arg0, checks goja.Value, extras ...goja.Va
if err != nil {
return false, err
}
if state.Options.SystemTags["check"] {
if state.Options.SystemTags.Has(stats.TagCheck) {
tags["check"] = check.Name
}

Expand Down
2 changes: 1 addition & 1 deletion js/modules/k6/k6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func TestCheck(t *testing.T) {
return &lib.State{
Group: root,
Options: lib.Options{
SystemTags: lib.GetTagSet(lib.DefaultSystemTagList...),
SystemTags: stats.ToSystemTagSet(stats.DefaultSystemTagList),
},
Samples: samples,
}, samples
Expand Down
2 changes: 1 addition & 1 deletion js/modules/k6/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (m Metric) Add(ctx context.Context, v goja.Value, addTags ...map[string]str
}

tags := state.Options.RunTags.CloneTags()
if state.Options.SystemTags["group"] {
if state.Options.SystemTags.Has(stats.TagGroup) {
tags["group"] = state.Group.Path
}

Expand Down
2 changes: 1 addition & 1 deletion js/modules/k6/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestMetrics(t *testing.T) {
child, _ := root.Group("child")
samples := make(chan stats.SampleContainer, 1000)
state := &lib.State{
Options: lib.Options{SystemTags: lib.GetTagSet("group")},
Options: lib.Options{SystemTags: stats.ToSystemTagSet([]string{stats.TagGroup.String()})},
Group: root,
Samples: samples,
}
Expand Down
10 changes: 5 additions & 5 deletions js/modules/k6/ws/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ func (*WS) Connect(ctx context.Context, url string, args ...goja.Value) (*WSHTTP

}

if state.Options.SystemTags["url"] {
if state.Options.SystemTags.Has(stats.TagURL) {
tags["url"] = url
}
if state.Options.SystemTags["group"] {
if state.Options.SystemTags.Has(stats.TagGroup) {
tags["group"] = state.Group.Path
}

Expand Down Expand Up @@ -186,7 +186,7 @@ func (*WS) Connect(ctx context.Context, url string, args ...goja.Value) (*WSHTTP
done: make(chan struct{}),
}

if state.Options.SystemTags["ip"] && conn.RemoteAddr() != nil {
if state.Options.SystemTags.Has(stats.TagIP) && conn.RemoteAddr() != nil {
if ip, _, err := net.SplitHostPort(conn.RemoteAddr().String()); err == nil {
tags["ip"] = ip
}
Expand All @@ -213,10 +213,10 @@ func (*WS) Connect(ctx context.Context, url string, args ...goja.Value) (*WSHTTP

defer func() { _ = conn.Close() }()

if state.Options.SystemTags["status"] {
if state.Options.SystemTags.Has(stats.TagStatus) {
tags["status"] = strconv.Itoa(httpResponse.StatusCode)
}
if state.Options.SystemTags["subproto"] {
if state.Options.SystemTags.Has(stats.TagSubProto) {
tags["subproto"] = httpResponse.Header.Get("Sec-WebSocket-Protocol")
}

Expand Down
24 changes: 17 additions & 7 deletions js/modules/k6/ws/ws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ func TestSession(t *testing.T) {
Group: root,
Dialer: tb.Dialer,
Options: lib.Options{
SystemTags: lib.GetTagSet("url", "proto", "status", "subproto"),
SystemTags: stats.ToSystemTagSet([]string{
stats.TagURL.String(),
stats.TagProto.String(),
stats.TagStatus.String(),
stats.TagSubProto.String(),
}),
},
Samples: samples,
TLSConfig: tb.TLSClientConfig,
Expand Down Expand Up @@ -293,7 +298,7 @@ func TestErrors(t *testing.T) {
Group: root,
Dialer: tb.Dialer,
Options: lib.Options{
SystemTags: lib.GetTagSet(lib.DefaultSystemTagList...),
SystemTags: stats.ToSystemTagSet(stats.DefaultSystemTagList),
},
Samples: samples,
}
Expand Down Expand Up @@ -405,7 +410,7 @@ func TestSystemTags(t *testing.T) {
state := &lib.State{
Group: root,
Dialer: tb.Dialer,
Options: lib.Options{SystemTags: lib.GetTagSet(testedSystemTags...)},
Options: lib.Options{SystemTags: stats.ToSystemTagSet(testedSystemTags)},
Samples: samples,
TLSConfig: tb.TLSClientConfig,
}
Expand All @@ -417,10 +422,9 @@ func TestSystemTags(t *testing.T) {
rt.Set("ws", common.Bind(rt, New(), &ctx))

for _, expectedTag := range testedSystemTags {
expectedTag := expectedTag
t.Run("only "+expectedTag, func(t *testing.T) {
state.Options.SystemTags = map[string]bool{
expectedTag: true,
}
state.Options.SystemTags = stats.ToSystemTagSet([]string{expectedTag})
_, err := common.RunString(rt, sr(`
let res = ws.connect("WSBIN_URL/ws-echo", function(socket){
socket.on("open", function() {
Expand Down Expand Up @@ -463,7 +467,13 @@ func TestTLSConfig(t *testing.T) {
Group: root,
Dialer: tb.Dialer,
Options: lib.Options{
SystemTags: lib.GetTagSet("url", "proto", "status", "subproto", "ip"),
SystemTags: stats.ToSystemTagSet([]string{
stats.TagURL.String(),
stats.TagProto.String(),
stats.TagStatus.String(),
stats.TagSubProto.String(),
stats.TagIP.String(),
}),
},
Samples: samples,
}
Expand Down
6 changes: 3 additions & 3 deletions js/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,13 +470,13 @@ func (u *VU) runFn(
}

tags := state.Options.RunTags.CloneTags()
if state.Options.SystemTags["vu"] {
if state.Options.SystemTags.Has(stats.TagVU) {
tags["vu"] = strconv.FormatInt(u.ID, 10)
}
if state.Options.SystemTags["iter"] {
if state.Options.SystemTags.Has(stats.TagIter) {
tags["iter"] = strconv.FormatInt(iter, 10)
}
if state.Options.SystemTags["group"] {
if state.Options.SystemTags.Has(stats.TagGroup) {
tags["group"] = group.Path
}

Expand Down
2 changes: 1 addition & 1 deletion js/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ func TestVUIntegrationHTTP2(t *testing.T) {
}
r1.SetOptions(lib.Options{
Throw: null.BoolFrom(true),
SystemTags: lib.GetTagSet("proto"),
SystemTags: stats.ToSystemTagSet([]string{stats.TagProto.String()}),
})

r2, err := NewFromArchive(r1.MakeArchive(), lib.RuntimeOptions{})
Expand Down
10 changes: 6 additions & 4 deletions lib/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (

"github.com/loadimpact/k6/lib/consts"
"github.com/loadimpact/k6/lib/fsext"
"github.com/loadimpact/k6/stats"

"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -140,7 +142,7 @@ func TestArchiveReadWrite(t *testing.T) {
K6Version: consts.Version,
Options: Options{
VUs: null.IntFrom(12345),
SystemTags: GetTagSet(DefaultSystemTagList...),
SystemTags: stats.ToSystemTagSet(stats.DefaultSystemTagList),
},
FilenameURL: &url.URL{Scheme: "file", Path: "/path/to/a.js"},
Data: []byte(`// a contents`),
Expand Down Expand Up @@ -190,7 +192,7 @@ func TestArchiveReadWrite(t *testing.T) {
Type: "js",
Options: Options{
VUs: null.IntFrom(12345),
SystemTags: GetTagSet(DefaultSystemTagList...),
SystemTags: stats.ToSystemTagSet(stats.DefaultSystemTagList),
},
FilenameURL: &url.URL{Scheme: "file", Path: fmt.Sprintf("%s/a.js", entry.Pwd)},
K6Version: consts.Version,
Expand All @@ -213,7 +215,7 @@ func TestArchiveReadWrite(t *testing.T) {
Type: "js",
Options: Options{
VUs: null.IntFrom(12345),
SystemTags: GetTagSet(DefaultSystemTagList...),
SystemTags: stats.ToSystemTagSet(stats.DefaultSystemTagList),
},
FilenameURL: &url.URL{Scheme: "file", Path: fmt.Sprintf("%s/a.js", entry.PwdNormAnon)},
K6Version: consts.Version,
Expand Down Expand Up @@ -342,7 +344,7 @@ func TestStrangePaths(t *testing.T) {
K6Version: consts.Version,
Options: Options{
VUs: null.IntFrom(12345),
SystemTags: GetTagSet(DefaultSystemTagList...),
SystemTags: stats.ToSystemTagSet(stats.DefaultSystemTagList),
},
FilenameURL: &url.URL{Scheme: "file", Path: pathToChange},
Data: []byte(`// ` + pathToChange + ` contents`),
Expand Down
Loading

0 comments on commit 98caab2

Please sign in to comment.