-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Statsdreceiver optimizations (#33683)
**Description:** Optimize statsdreceiver code to reduce heap usage Also reduce failCnt increment to reduce memory footprint in cases of tons of malformed statsd messages **Link to tracking Issue:** <Issue number if applicable> **Testing:** - Tested internally and saw a 17% reduction in object allocation from our workloads (with ~2.5k/s statsd metrics input) mostly from reduction of strings.Split Screenshot is from `go tool pprof -http:8081 -diff_base baseline.heap.gz optimized.heap.gz` ![Screenshot 2024-06-20 at 12 45 17 PM](https://github.com/open-telemetry/opentelemetry-collector-contrib/assets/142453/e8065097-2533-4934-9b78-c0e828e075ac) Note: I also saw lots of allocations from attribute.NewSet but idk the best way to go about reducing that. (Screenshot below is from unoptimized statsdreceiver) ![Screenshot 2024-06-20 at 12 44 32 PM](https://github.com/open-telemetry/opentelemetry-collector-contrib/assets/142453/293553c0-92c8-465b-872a-ddb7b68299a0) I tried changing statsDMetricDescription's attrs field to `map[string]any` in the hopes of using attributes.FromRaw in buildGaugeMetric but it would require replacing statsDMetricDescription as the type of the map key in instruments.gauges, etc.
- Loading branch information
1 parent
93a78c6
commit 46e828e
Showing
5 changed files
with
116 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: statsdreceiver | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Optimize statsdreceiver to reduce object allocations | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [33683] | ||
|
||
# (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: [user] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package transport // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/internal/transport" | ||
|
||
import "bytes" | ||
|
||
// SplitBytes iterates over a byte buffer, returning chunks split by a given | ||
// delimiter byte. It does not perform any allocations, and does not modify the | ||
// buffer it is given. It is not safe for use by concurrent goroutines. | ||
// | ||
// sb := NewSplitBytes(buf, '\n') | ||
// for sb.Next() { | ||
// fmt.Printf("%q\n", sb.Chunk()) | ||
// } | ||
// | ||
// The sequence of chunks returned by SplitBytes is equivalent to calling | ||
// bytes.Split, except without allocating an intermediate slice. | ||
type SplitBytes struct { | ||
buf []byte | ||
delim byte | ||
currentChunk []byte | ||
lastChunk bool | ||
} | ||
|
||
// NewSplitBytes initializes a SplitBytes struct with the provided buffer and delimiter. | ||
func NewSplitBytes(buf []byte, delim byte) *SplitBytes { | ||
return &SplitBytes{ | ||
buf: buf, | ||
delim: delim, | ||
} | ||
} | ||
|
||
// Next advances SplitBytes to the next chunk, returning true if a new chunk | ||
// actually exists and false otherwise. | ||
func (sb *SplitBytes) Next() bool { | ||
if sb.lastChunk { | ||
// we do not check the length here, this ensures that we return the | ||
// last chunk in the sequence (even if it's empty) | ||
return false | ||
} | ||
|
||
next := bytes.IndexByte(sb.buf, sb.delim) | ||
if next == -1 { | ||
// no newline, consume the entire buffer | ||
sb.currentChunk = sb.buf | ||
sb.buf = nil | ||
sb.lastChunk = true | ||
} else { | ||
sb.currentChunk = sb.buf[:next] | ||
sb.buf = sb.buf[next+1:] | ||
} | ||
return true | ||
} | ||
|
||
// Chunk returns the current chunk. | ||
func (sb *SplitBytes) Chunk() []byte { | ||
return sb.currentChunk | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters