diff --git a/.chloggen/rm-copies-deltatorateprocessor.yaml b/.chloggen/rm-copies-deltatorateprocessor.yaml new file mode 100644 index 000000000000..5e26c4fbcb51 --- /dev/null +++ b/.chloggen/rm-copies-deltatorateprocessor.yaml @@ -0,0 +1,22 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: 'enhancement' + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: deltatorateprocessor + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Remove unnecessary data copies. + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [35165] + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [user] diff --git a/processor/deltatorateprocessor/processor.go b/processor/deltatorateprocessor/processor.go index 8da2382ef0ee..a9ba3aca4697 100644 --- a/processor/deltatorateprocessor/processor.go +++ b/processor/deltatorateprocessor/processor.go @@ -55,33 +55,26 @@ func (dtrp *deltaToRateProcessor) processMetrics(_ context.Context, md pmetric.M dtrp.logger.Info(fmt.Sprintf("Configured metric for rate calculation %s is not a delta sum\n", metric.Name())) continue } - newDoubleDataPointSlice := pmetric.NewNumberDataPointSlice() - dataPoints := metric.Sum().DataPoints() + dataPointSlice := metric.Sum().DataPoints() - for i := 0; i < dataPoints.Len(); i++ { - fromDataPoint := dataPoints.At(i) - newDp := newDoubleDataPointSlice.AppendEmpty() - fromDataPoint.CopyTo(newDp) + for i := 0; i < dataPointSlice.Len(); i++ { + dataPoint := dataPointSlice.At(i) - durationNanos := time.Duration(fromDataPoint.Timestamp() - fromDataPoint.StartTimestamp()) + durationNanos := time.Duration(dataPoint.Timestamp() - dataPoint.StartTimestamp()) var rate float64 - switch fromDataPoint.ValueType() { + switch dataPoint.ValueType() { case pmetric.NumberDataPointValueTypeDouble: - rate = calculateRate(fromDataPoint.DoubleValue(), durationNanos) + rate = calculateRate(dataPoint.DoubleValue(), durationNanos) case pmetric.NumberDataPointValueTypeInt: - rate = calculateRate(float64(fromDataPoint.IntValue()), durationNanos) + rate = calculateRate(float64(dataPoint.IntValue()), durationNanos) default: - return md, consumererror.NewPermanent(fmt.Errorf("invalid data point type:%d", fromDataPoint.ValueType())) + return md, consumererror.NewPermanent(fmt.Errorf("invalid data point type:%d", dataPoint.ValueType())) } - newDp.SetDoubleValue(rate) + dataPoint.SetDoubleValue(rate) } - dps := metric.SetEmptyGauge().DataPoints() - dps.EnsureCapacity(newDoubleDataPointSlice.Len()) - for d := 0; d < newDoubleDataPointSlice.Len(); d++ { - dp := dps.AppendEmpty() - newDoubleDataPointSlice.At(d).CopyTo(dp) - } + // Setting the data type removed all the data points, so we must move them back to the metric. + dataPointSlice.MoveAndAppendTo(metric.SetEmptyGauge().DataPoints()) } } }