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

internal/globalconfig: make statsTags un-changeable from outside of the globalconfig package #2842

Merged
merged 3 commits into from
Sep 6, 2024
Merged
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
10 changes: 8 additions & 2 deletions internal/globalconfig/globalconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,21 @@ func SetDogstatsdAddr(addr string) {
func StatsTags() []string {
cfg.mu.RLock()
defer cfg.mu.RUnlock()
return cfg.statsTags
// Copy the slice before returning it, so that callers cannot pollute the underlying array
tags := make([]string, len(cfg.statsTags))
copy(tags, cfg.statsTags)
return tags
}

// SetStatsTags configures the list of tags that should be applied to contribs' statsd.Client as global tags
// It should only be called by the tracer package
func SetStatsTags(tags []string) {
cfg.mu.Lock()
defer cfg.mu.Unlock()
cfg.statsTags = tags
// Copy the slice before setting it, so that any changes to the slice provided to SetStatsTags does not pollute the underlying array of statsTags
statsTags := make([]string, len(tags))
copy(statsTags, tags)
cfg.statsTags = statsTags
}

// RuntimeID returns this process's unique runtime id.
Expand Down
14 changes: 14 additions & 0 deletions internal/globalconfig/globalconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,17 @@ func TestHeaderTag(t *testing.T) {
assert.Equal(t, "tag1", cfg.headersAsTags.Get("header1"))
assert.Equal(t, "tag2", cfg.headersAsTags.Get("header2"))
}

// Assert that APIs to access cfg.statsTags protect against pollution from external changes
func TestStatsTags(t *testing.T) {
array := [6]string{"aaa", "bbb", "ccc"}
slice1 := array[:]
SetStatsTags(slice1)
slice1 = append(slice1, []string{"ddd", "eee", "fff"}...)
slice1[0] = "zzz"
assert.Equal(t, cfg.statsTags[:3], []string{"aaa", "bbb", "ccc"})

tags := StatsTags()
tags[1] = "yyy"
assert.Equal(t, cfg.statsTags[1], "bbb")
}
Loading