Skip to content

Commit

Permalink
[exporter/prometheusremotewrite] fix snappy double alloc (#34274)
Browse files Browse the repository at this point in the history
**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 #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 <[email protected]>
Co-authored-by: Matthew Wear <[email protected]>
  • Loading branch information
3 people committed Jul 30, 2024
1 parent 427f72d commit b418e59
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
27 changes: 27 additions & 0 deletions .chloggen/bc--optimizesnappybufferallocation.yaml
Original file line number Diff line number Diff line change
@@ -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: []
5 changes: 3 additions & 2 deletions exporter/prometheusremotewriteexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit b418e59

Please sign in to comment.