-
Notifications
You must be signed in to change notification settings - Fork 417
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
profiler: make configured tags read-only (#1383)
* profiler: make configured tags read-only After being bitten by a bug which was in part caused by implicitly relying on append() creating a slice backed by new memory rather than sharing the same allocation, we should regard the configured tags as read-only. See PR #1377. This commit adds a small immutable package with a StringSlice type which wraps the slice and provides fresh copies on any access or modification, and uses the package for the configured tags. Co-authored-by: Felix Geisendörfer <felix@datadoghq.com>
- Loading branch information
Showing
7 changed files
with
123 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016 Datadog, Inc. | ||
|
||
// Package immutable provides read-only types | ||
package immutable | ||
|
||
// StringSlice holds a slice which cannot be modified and must be copied to | ||
// access. The zero value for a StringSlice is an empty slice (not nil). All StringSlice | ||
// methods are safe to call from multiple goroutines concurrently. | ||
type StringSlice struct { | ||
s []string | ||
} | ||
|
||
// NewStringSlice creates a StringSlice from a copy of the input slice | ||
func NewStringSlice(s []string) StringSlice { | ||
return StringSlice{s: append([]string{}, s...)} | ||
} | ||
|
||
// Slice returns a copy of the slice held by s | ||
func (s StringSlice) Slice() []string { | ||
return append([]string{}, s.s...) | ||
} | ||
|
||
// Append creates a new StringSlice by concatenating the given strings to a copy | ||
// of the slice held by s. | ||
func (s StringSlice) Append(strings ...string) StringSlice { | ||
dup := make([]string, len(s.s)+len(strings)) | ||
copy(dup, s.s) | ||
copy(dup[len(s.s):], strings) | ||
return StringSlice{s: dup} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016 Datadog, Inc. | ||
|
||
package immutable_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"gopkg.in/DataDog/dd-trace-go.v1/profiler/internal/immutable" | ||
) | ||
|
||
func TestStringSlice(t *testing.T) { | ||
tags := []string{"service:foo", "env:bar", "ggthingy:baz"} | ||
f := immutable.NewStringSlice(tags) | ||
assert.Equal(t, tags, f.Slice()) | ||
} | ||
|
||
func TestStringSliceModify(t *testing.T) { | ||
t.Run("modify-original", func(t *testing.T) { | ||
tags := []string{"service:foo", "env:bar", "thingy:baz"} | ||
f := immutable.NewStringSlice(tags) | ||
tags[0] = "service:different" | ||
assert.Equal(t, "service:foo", f.Slice()[0]) | ||
}) | ||
|
||
t.Run("modify-copy", func(t *testing.T) { | ||
tags := []string{"service:foo", "env:bar", "thingy:baz"} | ||
f := immutable.NewStringSlice(tags) | ||
dup := f.Slice() | ||
dup[0] = "service:different" | ||
assert.Equal(t, "service:foo", tags[0]) | ||
}) | ||
|
||
t.Run("modify-2-copies", func(t *testing.T) { | ||
tags := []string{"service:foo", "env:bar", "thingy:baz"} | ||
f := immutable.NewStringSlice(tags) | ||
dup := f.Slice() | ||
dup[0] = "service:different" | ||
dup2 := f.Slice() | ||
dup2[0] = "service:alsodifferent" | ||
assert.Equal(t, "service:foo", tags[0]) | ||
assert.Equal(t, "service:different", dup[0]) | ||
assert.Equal(t, "service:alsodifferent", dup2[0]) | ||
}) | ||
|
||
t.Run("append-duplicates", func(t *testing.T) { | ||
var f immutable.StringSlice | ||
before := f.Slice() | ||
g := f.Append("foo:bar") | ||
h := f.Append("other:tag") | ||
after := g.Slice() | ||
after2 := h.Slice() | ||
assert.NotEqual(t, before, after) | ||
assert.NotEqual(t, before, after2) | ||
assert.NotEqual(t, after, after2) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters