Skip to content

Commit

Permalink
Remove unnecessary copies in deltatorateprocessor
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu committed Sep 12, 2024
1 parent 91751e5 commit ad4c27b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
22 changes: 22 additions & 0 deletions .chloggen/rm-copies-deltatorateprocessor.yaml
Original file line number Diff line number Diff line change
@@ -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]
29 changes: 11 additions & 18 deletions processor/deltatorateprocessor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := pmetric.NewNumberDataPointSlice()
metric.Sum().DataPoints().MoveAndAppendTo(dataPointSlice)

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)
}
dataPointSlice.MoveAndAppendTo(metric.SetEmptyGauge().DataPoints())
}
}
}
Expand Down

0 comments on commit ad4c27b

Please sign in to comment.