From c044b5ac27842781144d8829a71839d31333a370 Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Mon, 16 Oct 2023 16:45:29 -0700 Subject: [PATCH] [chore][exporter/splunkhec] reuse metric buffer (#27776) Reuse the byte buffer used when encoding metrics to HEC events JSON. --- exporter/splunkhecexporter/client.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/exporter/splunkhecexporter/client.go b/exporter/splunkhecexporter/client.go index 89432fde233e..55271cfd57e9 100644 --- a/exporter/splunkhecexporter/client.go +++ b/exporter/splunkhecexporter/client.go @@ -248,6 +248,7 @@ func (c *client) fillMetricsBuffer(metrics pmetric.Metrics, buf buffer, is iterS jsonStream := jsonStreamPool.Get().(*jsoniter.Stream) defer jsonStreamPool.Put(jsonStream) + tempBuf := bytes.NewBuffer(make([]byte, 0, c.config.MaxContentLengthMetrics)) for i := is.resource; i < metrics.ResourceMetrics().Len(); i++ { rm := metrics.ResourceMetrics().At(i) for j := is.library; j < rm.ScopeMetrics().Len(); j++ { @@ -259,7 +260,7 @@ func (c *client) fillMetricsBuffer(metrics pmetric.Metrics, buf buffer, is iterS // Parsing metric record to Splunk event. events := mapMetricToSplunkEvent(rm.Resource(), metric, c.config, c.logger) - tempBuf := bytes.NewBuffer(make([]byte, 0, c.config.MaxContentLengthMetrics)) + tempBuf.Reset() for _, event := range events { // JSON encoding event and writing to buffer. b, err := marshalEvent(event, c.config.MaxEventSize, jsonStream)