Skip to content

Commit

Permalink
Prevent dogstatsd sink from clobbering the metric key for other sinks (
Browse files Browse the repository at this point in the history
…#156)

If using the Fanout sink and including the dogstatsd sink, any sinks sent the metric AFTER dogstatsd could see invalid metric names. This was due to some inadvertent modifications to the original metric key. The new implementation ensure that when modifying the orignial metric key, a new backing array is used and will have key parts copied into it instead of changing elements in the original metric key.
  • Loading branch information
mkeeler authored Jun 8, 2023
1 parent 8586014 commit aee7470
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion datadog/dogstatsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ func (s *DogStatsdSink) parseKey(key []string) ([]string, []metrics.Label) {
// Splice the hostname out of the key
for i, el := range key {
if el == hostName {
key = append(key[:i], key[i+1:]...)
// We need an intermediate key to prevent clobbering the
// original backing array that other sinks might be consuming.
tempKey := append([]string{}, key[:i]...)
key = append(tempKey, key[i+1:]...)
break
}
}
Expand Down
8 changes: 8 additions & 0 deletions datadog/dogstatsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ func setupTestServerAndBuffer(t *testing.T) (*net.UDPConn, []byte) {
func TestParseKey(t *testing.T) {
for _, tt := range ParseKeyTests {
dog := mockNewDogStatsdSink(DogStatsdAddr, tt.Tags, tt.PropagateHostname)
// make a copy of the original key
original := make([]string, len(tt.KeyToParse))
copy(original, tt.KeyToParse)

key, tags := dog.parseKey(tt.KeyToParse)

if !reflect.DeepEqual(key, tt.ExpectedKey) {
Expand All @@ -95,6 +99,10 @@ func TestParseKey(t *testing.T) {
if !reflect.DeepEqual(tags, tt.ExpectedTags) {
t.Fatalf("Tag Parsing Failed for %v, %v != %v", tt.KeyToParse, tags, tt.ExpectedTags)
}

if !reflect.DeepEqual(original, tt.KeyToParse) {
t.Fatalf("Key parsing modified the original input key:, original: %v, after parse: %v", original, tt.KeyToParse)
}
}
}

Expand Down

0 comments on commit aee7470

Please sign in to comment.