From 2c5b86cff6242d0c72d3c7e5494bc38832a37d99 Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Sat, 1 Oct 2022 08:06:04 -0700 Subject: [PATCH] [chore] metricstransformprocessor: avoid unnecessary new metrics slice when possible (#14628) Signed-off-by: Bogdan Drutu Signed-off-by: Bogdan Drutu --- .../metrics_transform_processor_otlp.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/processor/metricstransformprocessor/metrics_transform_processor_otlp.go b/processor/metricstransformprocessor/metrics_transform_processor_otlp.go index 501c0310b95c..924b51e9c608 100644 --- a/processor/metricstransformprocessor/metrics_transform_processor_otlp.go +++ b/processor/metricstransformprocessor/metrics_transform_processor_otlp.go @@ -268,9 +268,9 @@ func (mtp *metricsTransformProcessor) processMetrics(_ context.Context, md pmetr combinedMetric.MoveTo(metrics.AppendEmpty()) } case Insert: - newMetrics := pmetric.NewMetricSlice() - newMetrics.EnsureCapacity(metrics.Len()) - for i := 0; i < metrics.Len(); i++ { + // Save len, so we don't iterate over the newly generated metrics that are appended at the end. + mLen := metrics.Len() + for i := 0; i < mLen; i++ { metric := metrics.At(i) newMetric := transform.MetricIncludeFilter.extractMatchedMetric(metric) if newMetric == (pmetric.Metric{}) { @@ -281,10 +281,9 @@ func (mtp *metricsTransformProcessor) processMetrics(_ context.Context, md pmetr metric.CopyTo(newMetric) } if transformMetric(newMetric, transform) { - newMetric.MoveTo(newMetrics.AppendEmpty()) + newMetric.MoveTo(metrics.AppendEmpty()) } } - newMetrics.MoveAndAppendTo(metrics) case Update: metrics.RemoveIf(func(metric pmetric.Metric) bool { if !transform.MetricIncludeFilter.matchMetric(metric) {