Files
Suraj Nathandzeekay 5e4cd1913d metrics-generator: Add per-label limiter to control cardinality (#6414)
* metrics-generator: Add per-label cardinality sanitizer

Adds a per-label cardinality cap that replaces only the high-cardinality
label value with "__overflow__" while preserving all other dimensions.

Implementation:
- CardinalitySanitizer: per-label HLL sketch tracking with cached
  overLimit flag updated every 15s. Always inserts original value hash
  to prevent estimate oscillation.
- ChainSanitizer: composes sanitizers in order (DRAIN first, then
  cardinality sanitizer as hard backstop).
- New config field: max_cardinality_per_label (0 = disabled).

* metrics-generator: Add docs for per-label cardinality sanitizer

Rename overflow value to __cardinality_overflow__ for clarity and add
troubleshooting docs explaining the per-label cardinality limit, its
PromQL query, configuration, and how it interacts with the series and
entity limiters.

* metrics-generator: Move max_cardinality_per_label to per-tenant override

Move max_cardinality_per_label from static registry config to a
per-tenant runtime override, consistent with max_active_series and
max_active_entities. The CardinalitySanitizer now receives the
Overrides interface and refreshes the cached value every maintenance
tick (15s), allowing operators to tune it per tenant without restart.

* move from piggybacking on sanitizer and have a standalone per label limiter

sanitizer is there to sanitize the labels, so we shouldn't piggyback and rely
on the chained sanitizer hack to ensure per label limiter runs after all sanitizer.

we create a new LabelLimiter interface and run it after Sanitizer and decouple from Sanitizer

* extract maxSeriesFunc and maxEntityFunc to type

* test the overflow -> recovery -> overflow liftcycle

* make the test more strict

* rework docs

* track label_names and add a demand estimate metric as well

* Add tests for MetricsGeneratorMaxCardinalityPerLabel override

* optimize hot path

* Update TestPerLabelLimiter_ConcurrentAccess test

* Add BenchmarkPerLabelLimiter to check perf and allocs

* Add CHANGELOG.md

* Update config index page

* drop noopLabelLimiter from tests

* always run s.doPeriodicMaintenance first to refresh the config value
2026-02-22 00:55:23 -08:00

79 lines
2.2 KiB
Go

package registry
import (
"testing"
"github.com/prometheus/prometheus/model/labels"
"github.com/stretchr/testify/assert"
)
func TestLabelBuilder(t *testing.T) {
builder := NewLabelBuilder(0, 0, newTestDrainSanitizer(SpanNameSanitizationDisabled), newTestLabelLimiter())
builder.Add("name", "value")
lbls, ok := builder.CloseAndBuildLabels()
assert.True(t, ok)
assert.Equal(t, labels.FromStrings("name", "value"), lbls)
// Using the builder after calling Labels() will panic to prevent memory
// corruption.
assert.Panics(t, func() {
builder.Add("test", "test")
})
}
func TestLabelBuilder_MaxLabelNameLength(t *testing.T) {
builder := NewLabelBuilder(10, 10, newTestDrainSanitizer(SpanNameSanitizationDisabled), newTestLabelLimiter())
builder.Add("name", "very_long_value")
builder.Add("very_long_name", "value")
lbls, ok := builder.CloseAndBuildLabels()
assert.True(t, ok)
assert.Equal(t, labels.FromStrings("name", "very_long_", "very_long_", "value"), lbls)
}
func TestLabelBuilder_InvalidUTF8(t *testing.T) {
builder := NewLabelBuilder(0, 0, newTestDrainSanitizer(SpanNameSanitizationDisabled), newTestLabelLimiter())
builder.Add("name", "svc-\xc3\x28") // Invalid UTF-8
_, ok := builder.CloseAndBuildLabels()
assert.False(t, ok)
}
func TestSafeBuilderPool(t *testing.T) {
pool := newSafeBuilderPool()
builder := pool.Get()
builder.Set("name", "value")
lbls := builder.Labels()
assert.Equal(t, labels.FromStrings("name", "value"), lbls)
// Putting the builder back into the pool should reset it.
pool.Put(builder)
reusedBuilder := pool.Get()
assert.Equal(t, builder, reusedBuilder)
assert.Equal(t, labels.EmptyLabels(), reusedBuilder.Labels())
}
type sanitizerFunc func(lbls labels.Labels) labels.Labels
var _ Sanitizer = (*sanitizerFunc)(nil)
func (s sanitizerFunc) Sanitize(lbls labels.Labels) labels.Labels {
return s(lbls)
}
func TestLabelBuilder_Sanitizer(t *testing.T) {
builder := NewLabelBuilder(0, 0, sanitizerFunc(func(_ labels.Labels) labels.Labels {
return labels.FromStrings("name", "sanitized_value")
}), newTestLabelLimiter())
builder.Add("name", "value")
lbls, ok := builder.CloseAndBuildLabels()
assert.True(t, ok)
assert.Equal(t, labels.FromStrings("name", "sanitized_value"), lbls)
}