Skip to content

Commit

Permalink
[exporter/dynatrace] Do not mutate the original metrics (open-telemet…
Browse files Browse the repository at this point in the history
…ry#16506)

The  `Map.Sort()` call mutates the original data which can lead to subtle bugs given that the exporter marked as not mutating
  • Loading branch information
dmitryax authored and shalper2 committed Dec 6, 2022
1 parent 745d513 commit f6d1261
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
11 changes: 11 additions & 0 deletions .chloggen/dynatrace-no-sort.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: exporter/dynatrace

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Make sure the original metrics are not mutated

# One or more tracking issues related to the change
issues: [16506]
11 changes: 7 additions & 4 deletions exporter/dynatraceexporter/internal/serialization/sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package serialization // import "github.com/open-telemetry/opentelemetry-collect

import (
"fmt"
"sort"
"strings"

dtMetric "github.com/dynatrace-oss/dynatrace-metric-utils-go/metric"
"github.com/dynatrace-oss/dynatrace-metric-utils-go/metric/dimensions"
Expand Down Expand Up @@ -149,12 +151,13 @@ func serializeCumulativeCounter(name, prefix string, dims dimensions.NormalizedD
}

func convertTotalCounterToDelta(name, prefix string, dims dimensions.NormalizedDimensionList, dp pmetric.NumberDataPoint, prevCounters *ttlmap.TTLMap) (*dtMetric.Metric, error) {
id := name

dp.Attributes().Sort().Range(func(k string, v pcommon.Value) bool {
id += fmt.Sprintf(",%s=%s", k, v.AsString())
attrPairs := make([]string, 0, dp.Attributes().Len())
dp.Attributes().Range(func(k string, v pcommon.Value) bool {
attrPairs = append(attrPairs, k+"="+v.AsString())
return true
})
sort.Strings(attrPairs)
id := name + strings.Join(attrPairs, ",")

prevCounter := prevCounters.Get(id)

Expand Down
12 changes: 12 additions & 0 deletions exporter/dynatraceexporter/internal/serialization/sum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,15 @@ func Test_serializeSum(t *testing.T) {
})
})
}

func Test_convertTotalCounterToDelta_notMutating(t *testing.T) {
dp := pmetric.NewNumberDataPoint()
dp.SetIntValue(5)
dp.Attributes().PutStr("attr2", "val2")
dp.Attributes().PutStr("attr1", "val1")
orig := pmetric.NewNumberDataPoint()
dp.CopyTo(orig)
_, err := convertTotalCounterToDelta("m", "prefix", dimensions.NormalizedDimensionList{}, dp, ttlmap.New(1, 1))
assert.NoError(t, err)
assert.Equal(t, orig, dp) // make sure the original data point is not mutated
}

0 comments on commit f6d1261

Please sign in to comment.