Skip to content

Commit

Permalink
feat: allow name tag, and overwrite url tag with name tag if set (#60)
Browse files Browse the repository at this point in the history
* feat: allow name tag, and overwrite url tag with name tag if set

* fix: remove unnecessary assignment on name tag. use same value for both url and name tags.
  • Loading branch information
mkadirtan authored Feb 21, 2024
1 parent 7c450fa commit d021a4a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
9 changes: 8 additions & 1 deletion websockets/websockets.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,14 @@ func (w *webSocket) establishConnection(params *wsParams) {
}
w.conn = conn

w.tagsAndMeta.SetSystemTagOrMetaIfEnabled(systemTags, metrics.TagURL, w.url.String())
nameTagValue, nameTagManuallySet := params.tagsAndMeta.Tags.Get(metrics.TagName.String())
// After k6 v0.41.0, the `name` and `url` tags have the exact same values:
if nameTagManuallySet {
w.tagsAndMeta.SetSystemTagOrMetaIfEnabled(systemTags, metrics.TagURL, nameTagValue)
} else {
w.tagsAndMeta.SetSystemTagOrMetaIfEnabled(systemTags, metrics.TagURL, w.url.String())
w.tagsAndMeta.SetSystemTagOrMetaIfEnabled(systemTags, metrics.TagName, w.url.String())
}

w.emitConnectionMetrics(ctx, start, connectionDuration)
if connErr != nil {
Expand Down
40 changes: 40 additions & 0 deletions websockets/websockets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,46 @@ func TestCookiesDefaultJar(t *testing.T) {
assert.Len(t, ts.errors, 0)
}

func TestManualNameTag(t *testing.T) {
t.Parallel()
ts := newTestState(t)
sr := ts.tb.Replacer.Replace

ts.vu.StateField.Options.SystemTags = metrics.ToSystemTagSet([]string{"url", "name"})

err := ts.ev.Start(func() error {
_, runErr := ts.rt.RunString(sr(`
var ws = new WebSocket("WSBIN_URL/ws-echo", null, { tags: { name: "custom" } } )
ws.onopen = () => {
ws.send("test")
}
ws.onmessage = (event) => {
if (event.data != "test") {
throw new Error ("echo'd data doesn't match our message!");
}
ws.close()
}
ws.onerror = (e) => { throw JSON.stringify(e) }
`))
return runErr
})
require.NoError(t, err)

containers := metrics.GetBufferedSamples(ts.samples)
require.NotEmpty(t, containers)

for _, sampleContainer := range containers {
require.NotEmpty(t, sampleContainer.GetSamples())
for _, sample := range sampleContainer.GetSamples() {
dataToCheck := sample.Tags.Map()
require.NotEmpty(t, dataToCheck)

assert.Equal(t, "custom", dataToCheck["url"])
assert.Equal(t, "custom", dataToCheck["name"])
}
}
}

func TestSystemTags(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit d021a4a

Please sign in to comment.