Skip to content

Commit

Permalink
lib/options: Migrated RunTags to map[string]string type
Browse files Browse the repository at this point in the history
It facilitates the transition to a more efficient SampleTags.
  • Loading branch information
codebien committed Aug 3, 2022
1 parent 4435e87 commit 96f9c5f
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 24 deletions.
14 changes: 14 additions & 0 deletions cmd/config_consolidation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,20 @@ func getConfigConsolidationTestCases() []configConsolidationTestCase {
)
},
},

// Test-wide Tags
{
opts{
fs: defaultConfig(`{"tags": { "codeTagKey": "codeTagValue"}}`),
cli: []string{"--tag", "clitagkey=clitagvalue"},
},
exp{},
func(t *testing.T, c Config) {
exp := map[string]string{"clitagkey": "clitagvalue"}
assert.Equal(t, exp, c.RunTags)
},
},

// Test summary trend stats
{opts{}, exp{}, func(t *testing.T, c Config) {
assert.Equal(t, lib.DefaultSummaryTrendStats, c.Options.SummaryTrendStats)
Expand Down
2 changes: 1 addition & 1 deletion cmd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func getOptions(flags *pflag.FlagSet) (lib.Options, error) {
}
parsedRunTags[name] = value
}
opts.RunTags = metrics.IntoSampleTags(&parsedRunTags)
opts.RunTags = parsedRunTags
}

redirectConFile, err := flags.GetString("console-output")
Expand Down
14 changes: 10 additions & 4 deletions core/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,8 +690,14 @@ func TestRunTags(t *testing.T) {
t.Parallel()
tb := httpmultibin.NewHTTPMultiBin(t)

runTagsMap := map[string]string{"foo": "bar", "test": "mest", "over": "written"}
runTags := metrics.NewSampleTags(runTagsMap)
expectedRunTags := map[string]string{"foo": "bar", "test": "mest", "over": "written"}

// it copies the map so in the case the runner will overwrite
// some run tags' values it doesn't affect the assertion.
runTags := make(map[string]string)
for k, v := range expectedRunTags {
runTags[k] = v
}

script := []byte(tb.Replacer.Replace(`
import http from "k6/http";
Expand Down Expand Up @@ -780,14 +786,14 @@ func TestRunTags(t *testing.T) {
getExpectedOverVal := func(metricName string) string {
for _, sysMetric := range systemMetrics {
if sysMetric == metricName {
return runTagsMap["over"]
return expectedRunTags["over"]
}
}
return "the rainbow"
}

for _, s := range mockOutput.Samples {
for key, expVal := range runTagsMap {
for key, expVal := range expectedRunTags {
val, ok := s.Tags.Get(key)

if key == "over" {
Expand Down
7 changes: 4 additions & 3 deletions core/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ func (e *ExecutionScheduler) initVUsConcurrently(

func (e *ExecutionScheduler) emitVUsAndVUsMax(ctx context.Context, out chan<- metrics.SampleContainer) {
e.state.Test.Logger.Debug("Starting emission of VUs and VUsMax metrics...")
runTags := metrics.NewSampleTags(e.state.Test.Options.RunTags)

emitMetrics := func() {
t := time.Now()
Expand All @@ -233,15 +234,15 @@ func (e *ExecutionScheduler) emitVUsAndVUsMax(ctx context.Context, out chan<- me
Time: t,
Metric: e.state.Test.BuiltinMetrics.VUs,
Value: float64(e.state.GetCurrentlyActiveVUsCount()),
Tags: e.state.Test.Options.RunTags,
Tags: runTags,
}, {
Time: t,
Metric: e.state.Test.BuiltinMetrics.VUsMax,
Value: float64(e.state.GetInitializedVUsCount()),
Tags: e.state.Test.Options.RunTags,
Tags: runTags,
},
},
Tags: e.state.Test.Options.RunTags,
Tags: runTags,
Time: t,
}
metrics.PushIfNotDone(ctx, out, samples)
Expand Down
2 changes: 1 addition & 1 deletion js/modules/k6/execution/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ func TestOptionsTestFull(t *testing.T) {
sysm := metrics.TagIter | metrics.TagVU
return &sysm
}(),
RunTags: metrics.NewSampleTags(map[string]string{"runtag-key": "runtag-value"}),
RunTags: map[string]string{"runtag-key": "runtag-value"},
MetricSamplesBufferSize: null.IntFrom(8),
ConsoleOutput: null.StringFrom("loadtest.log"),
LocalIPs: func() types.NullIPPool {
Expand Down
12 changes: 10 additions & 2 deletions js/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func (r *Runner) newVU(idLocal, idGlobal uint64, samplesOut chan<- metrics.Sampl
VUID: vu.ID,
VUIDGlobal: vu.IDGlobal,
Samples: vu.Samples,
Tags: lib.NewTagMap(vu.Runner.Bundle.Options.RunTags.CloneTags()),
Tags: lib.NewTagMap(copyStringMap(vu.Runner.Bundle.Options.RunTags)),
Group: r.defaultGroup,
BuiltinMetrics: r.preInitState.BuiltinMetrics,
}
Expand Down Expand Up @@ -630,7 +630,7 @@ func (u *VU) Activate(params *lib.VUActivationParams) lib.ActiveVU {

opts := u.Runner.Bundle.Options
// TODO: maybe we can cache the original tags only clone them and add (if any) new tags on top ?
u.state.Tags = lib.NewTagMap(opts.RunTags.CloneTags())
u.state.Tags = lib.NewTagMap(copyStringMap(opts.RunTags))
for k, v := range params.Tags {
u.state.Tags.Set(k, v)
}
Expand Down Expand Up @@ -861,3 +861,11 @@ func (s *scriptException) Hint() string {
func (s *scriptException) ExitCode() exitcodes.ExitCode {
return exitcodes.ScriptException
}

func copyStringMap(m map[string]string) map[string]string {
clone := make(map[string]string, len(m))
for ktag, vtag := range m {
clone[ktag] = vtag
}
return clone
}
5 changes: 4 additions & 1 deletion lib/executor/base_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ func (bs *BaseExecutor) GetProgress() *pb.ProgressBar {
// getMetricTags returns a tag set that can be used to emit metrics by the
// executor. The VU ID is optional.
func (bs *BaseExecutor) getMetricTags(vuID *uint64) *metrics.SampleTags {
tags := bs.executionState.Test.Options.RunTags.CloneTags()
tags := make(map[string]string, len(bs.executionState.Test.Options.RunTags))
for k, v := range bs.executionState.Test.Options.RunTags {
tags[k] = v
}
if bs.executionState.Test.Options.SystemTags.Has(metrics.TagScenario) {
tags["scenario"] = bs.config.GetName()
}
Expand Down
7 changes: 0 additions & 7 deletions lib/netext/httpext/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ func TestMakeRequestError(t *testing.T) {
Compressions: []CompressionType{badCompressionType},
}
state := &lib.State{
Options: lib.Options{RunTags: &metrics.SampleTags{}},
Transport: http.DefaultTransport,
Logger: logrus.New(),
Tags: lib.NewTagMap(nil),
Expand All @@ -141,7 +140,6 @@ func TestMakeRequestError(t *testing.T) {
logger := logrus.New()
logger.Level = logrus.DebugLevel
state := &lib.State{
Options: lib.Options{RunTags: &metrics.SampleTags{}},
Transport: srv.Client().Transport,
Logger: logger,
Tags: lib.NewTagMap(nil),
Expand Down Expand Up @@ -192,7 +190,6 @@ func TestResponseStatus(t *testing.T) {
samples := make(chan<- metrics.SampleContainer, 1)
registry := metrics.NewRegistry()
state := &lib.State{
Options: lib.Options{RunTags: &metrics.SampleTags{}},
Transport: server.Client().Transport,
Logger: logger,
Samples: samples,
Expand Down Expand Up @@ -271,7 +268,6 @@ func TestMakeRequestTimeoutInTheMiddle(t *testing.T) {
registry := metrics.NewRegistry()
state := &lib.State{
Options: lib.Options{
RunTags: &metrics.SampleTags{},
SystemTags: &metrics.DefaultSystemTagSet,
},
Transport: srv.Client().Transport,
Expand Down Expand Up @@ -348,7 +344,6 @@ func TestTrailFailed(t *testing.T) {
registry := metrics.NewRegistry()
state := &lib.State{
Options: lib.Options{
RunTags: &metrics.SampleTags{},
SystemTags: &metrics.DefaultSystemTagSet,
},
Transport: srv.Client().Transport,
Expand Down Expand Up @@ -410,7 +405,6 @@ func TestMakeRequestDialTimeout(t *testing.T) {
registry := metrics.NewRegistry()
state := &lib.State{
Options: lib.Options{
RunTags: &metrics.SampleTags{},
SystemTags: &metrics.DefaultSystemTagSet,
},
Transport: &http.Transport{
Expand Down Expand Up @@ -469,7 +463,6 @@ func TestMakeRequestTimeoutInTheBegining(t *testing.T) {
registry := metrics.NewRegistry()
state := &lib.State{
Options: lib.Options{
RunTags: &metrics.SampleTags{},
SystemTags: &metrics.DefaultSystemTagSet,
},
Transport: srv.Client().Transport,
Expand Down
1 change: 0 additions & 1 deletion lib/netext/httpext/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func BenchmarkMeasureAndEmitMetrics(b *testing.B) {
registry := metrics.NewRegistry()
state := &lib.State{
Options: lib.Options{
RunTags: &metrics.SampleTags{},
SystemTags: &metrics.DefaultSystemTagSet,
},
BuiltinMetrics: metrics.RegisterBuiltinMetrics(registry),
Expand Down
6 changes: 3 additions & 3 deletions lib/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ type Options struct {
// Use pointer for identifying whether user provide any tag or not.
SystemTags *metrics.SystemTagSet `json:"systemTags" envconfig:"K6_SYSTEM_TAGS"`

// Tags to be applied to all samples for this running
RunTags *metrics.SampleTags `json:"tags" envconfig:"K6_TAGS"`
// Tags are key-value pairs to be applied to all samples for the run.
RunTags map[string]string `json:"tags" envconfig:"K6_TAGS"`

// Buffer size of the channel for metric samples; 0 means unbuffered
MetricSamplesBufferSize null.Int `json:"metricSamplesBufferSize" envconfig:"K6_METRIC_SAMPLES_BUFFER_SIZE"`
Expand Down Expand Up @@ -567,7 +567,7 @@ func (o Options) Apply(opts Options) Options {
if opts.SystemTags != nil {
o.SystemTags = opts.SystemTags
}
if !opts.RunTags.IsEmpty() {
if len(opts.RunTags) > 0 {
o.RunTags = opts.RunTags
}
if opts.MetricSamplesBufferSize.Valid {
Expand Down
2 changes: 1 addition & 1 deletion lib/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ func TestOptions(t *testing.T) {
})
t.Run("RunTags", func(t *testing.T) {
t.Parallel()
tags := metrics.IntoSampleTags(&map[string]string{"myTag": "hello"})
tags := map[string]string{"myTag": "hello"}
opts := Options{}.Apply(Options{RunTags: tags})
assert.Equal(t, tags, opts.RunTags)
})
Expand Down

0 comments on commit 96f9c5f

Please sign in to comment.