Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions internal/cmd/config_consolidation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,15 +359,18 @@ func getConfigConsolidationTestCases() []configConsolidationTestCase {
},
},

// Test-wide Tags
// Test-wide Tags: CLI and file tags are merged; CLI wins on key collision
{
opts{
fs: defaultConfig(`{"tags": { "codeTagKey": "codeTagValue"}}`),
cli: []string{"--tag", "clitagkey=clitagvalue"},
},
exp{},
func(t *testing.T, c Config) {
exp := map[string]string{"clitagkey": "clitagvalue"}
exp := map[string]string{
"codeTagKey": "codeTagValue",
"clitagkey": "clitagvalue",
}
assert.Equal(t, exp, c.RunTags)
},
},
Expand Down
11 changes: 10 additions & 1 deletion lib/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/pem"
"errors"
"fmt"
"maps"
"net"
"reflect"
"slices"
Expand Down Expand Up @@ -490,7 +491,15 @@ func (o Options) Apply(opts Options) Options {
o.SystemTags = opts.SystemTags
}
if len(opts.RunTags) > 0 {
o.RunTags = opts.RunTags
// Tags are merged across config layers rather than replaced wholesale,
// so that each layer can contribute its own keys. When the same key
// appears in multiple layers the higher-priority layer wins, consistent
// with the order of precedence documented at:
// https://grafana.com/docs/k6/latest/using-k6/k6-options/how-to/#order-of-precedence
merged := make(map[string]string, len(o.RunTags)+len(opts.RunTags))
maps.Copy(merged, o.RunTags)
maps.Copy(merged, opts.RunTags)
o.RunTags = merged
}
if opts.MetricSamplesBufferSize.Valid {
o.MetricSamplesBufferSize = opts.MetricSamplesBufferSize
Expand Down
12 changes: 12 additions & 0 deletions lib/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,18 @@ func TestOptions(t *testing.T) {
opts := Options{}.Apply(Options{RunTags: tags})
assert.Equal(t, tags, opts.RunTags)
})
t.Run("RunTagsMerge", func(t *testing.T) {
t.Parallel()
base := Options{RunTags: map[string]string{"env": "staging", "team": "backend"}}
// higher-priority layer adds a new key and overrides an existing one
override := Options{RunTags: map[string]string{"env": "prod", "region": "us-east"}}
opts := base.Apply(override)
assert.Equal(t, map[string]string{
"env": "prod", // higher-priority layer wins on collision
"team": "backend", // lower-priority key preserved
"region": "us-east", // higher-priority new key added
}, opts.RunTags)
})
t.Run("DiscardResponseBodies", func(t *testing.T) {
t.Parallel()
opts := Options{}.Apply(Options{DiscardResponseBodies: null.BoolFrom(true)})
Expand Down