diff --git a/.chloggen/dynatrace-no-sort.yaml b/.chloggen/dynatrace-no-sort.yaml new file mode 100755 index 000000000000..968646eda95e --- /dev/null +++ b/.chloggen/dynatrace-no-sort.yaml @@ -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] diff --git a/exporter/dynatraceexporter/internal/serialization/sum.go b/exporter/dynatraceexporter/internal/serialization/sum.go index 82d302357b88..6226bcd00050 100644 --- a/exporter/dynatraceexporter/internal/serialization/sum.go +++ b/exporter/dynatraceexporter/internal/serialization/sum.go @@ -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" @@ -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) diff --git a/exporter/dynatraceexporter/internal/serialization/sum_test.go b/exporter/dynatraceexporter/internal/serialization/sum_test.go index 63333d295218..1c5d8a86ad7d 100644 --- a/exporter/dynatraceexporter/internal/serialization/sum_test.go +++ b/exporter/dynatraceexporter/internal/serialization/sum_test.go @@ -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 +}