From b418e590bdd7997d10491265305d9640ecbf1f2f Mon Sep 17 00:00:00 2001 From: ben-childs-docusign <73303353+ben-childs-docusign@users.noreply.github.com> Date: Tue, 30 Jul 2024 14:03:31 -0700 Subject: [PATCH] [exporter/prometheusremotewrite] fix snappy double alloc (#34274) **Description:** Snappy Encode function is not using the buffer passed in by prometheus remote write as it is too small. Removing this unused buffer allocation and just passing nil. Snappy will allocate the required buffers. **Link to tracking Issue:** Fixes https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/34273 **Testing:** Ran existing unit tests which cover this code Ran this in our integration environment and verified reduced allocations. Before: ![dfce1b99-8998-422e-a9e6-ccc5f6401393](https://github.com/user-attachments/assets/df103b83-48a7-4df7-b9da-c829de02616c) After: ![image](https://github.com/user-attachments/assets/d4dc638c-dab6-4407-b8f1-5ecc24d60435) --------- Co-authored-by: Raphael Philipe Mendes da Silva Co-authored-by: Matthew Wear --- .../bc--optimizesnappybufferallocation.yaml | 27 +++++++++++++++++++ .../prometheusremotewriteexporter/exporter.go | 5 ++-- 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 .chloggen/bc--optimizesnappybufferallocation.yaml diff --git a/.chloggen/bc--optimizesnappybufferallocation.yaml b/.chloggen/bc--optimizesnappybufferallocation.yaml new file mode 100644 index 000000000000..59ed0d893b80 --- /dev/null +++ b/.chloggen/bc--optimizesnappybufferallocation.yaml @@ -0,0 +1,27 @@ +# 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: exporter/prometheusremotewrite + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Reduce unnecessary memory allocation by removing buffer that was not used by Snappy encoding function. + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [34273] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# 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: [] diff --git a/exporter/prometheusremotewriteexporter/exporter.go b/exporter/prometheusremotewriteexporter/exporter.go index 0e8fd93f5cc2..1bb03af36435 100644 --- a/exporter/prometheusremotewriteexporter/exporter.go +++ b/exporter/prometheusremotewriteexporter/exporter.go @@ -274,8 +274,9 @@ func (prwe *prwExporter) execute(ctx context.Context, writeReq *prompb.WriteRequ if errMarshal != nil { return consumererror.NewPermanent(errMarshal) } - buf := make([]byte, len(data), cap(data)) - compressedData := snappy.Encode(buf, data) + // If we don't pass a buffer large enough, Snappy Encode function will not use it and instead will allocate a new buffer. + // Therefore we always let Snappy decide the size of the buffer. + compressedData := snappy.Encode(nil, data) // executeFunc can be used for backoff and non backoff scenarios. executeFunc := func() error {