prometheus / prometheus/otlptranslator
LabelNamer.Build() causes memory fragmentation with frequent label transformations
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 16
- Forks
- 11
- Avg merge
- 8h 6m
- Merged PRs (30d)
- 4
Description
Description
LabelNamer.Build() allocates a new string on every call, even when the label name is already compliant. This causes significant memory fragmentation, especially in high-frequency label transformation
scenarios (e.g., OpenTelemetry Collector Contrib's Prometheus remote write exporter).
Steps to Reproduce
- Use OpenTelemetry Collector to collect a large number of metrics
- Observe significant memory fragmentation in the process
Analysis
Current Build() implementation:
normalizedName := sanitizeLabelName(label, ln.PreserveMultipleUnderscores)
sanitizeLabelName uses strings.Builder internally to create a new string on every call:
- Allocates new memory
- Copies the entire label string
- Returns a new string
Expected Behavior
For already-compliant labels (e.g., http_method), should:
- Return the original string directly without allocation
- Or reuse cached transformation results
Reference Implementation
VictoriaMetrics' FastStringTransformer uses sync.Map to cache transformation results, avoiding repeated allocations.
Environment
- OpenTelemetry Collector Contrib
- Prometheus remote write exporter
- High-cardinality metrics scenario
Related Components
- LabelNamer.Build()
- sanitizeLabelName()
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating LabelNamer.Build() and sanitizeLabelName() in the Go source, then reproduce the repeated transformation scenario described for compliant labels such as http_method. Compare the current allocation behavior with the expected direct return or cached transformation, and consider the work done when compliant labels no longer allocate unnecessarily without changing label names.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- performance
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 56/100