prometheus / prometheus/otlptranslator

LabelNamer.Build() causes memory fragmentation with frequent label transformations

Open
#68 0 comments 0 reactions 0 assignees View on GitHub

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

  1. Use OpenTelemetry Collector to collect a large number of metrics
  2. 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:

  1. Return the original string directly without allocation
  2. 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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.