Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions modules/generator/registry/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,8 @@ func (c *counter) Inc(labelValueCombo *LabelValueCombo, value float64) {
}

func (c *counter) newSeries(labelValueCombo *LabelValueCombo, value float64) *counterSeries {
lb := labels.NewBuilder(getLabelsFromValueCombo(labelValueCombo))
for name, value := range c.externalLabels {
lb.Set(name, value)
}

lb.Set(labels.MetricName, c.metricName)

return &counterSeries{
labels: lb.Labels(),
labels: getSeriesLabels(c.metricName, labelValueCombo, c.externalLabels),
value: atomic.NewFloat64(value),
lastUpdated: atomic.NewInt64(time.Now().UnixMilli()),
firstSeries: atomic.NewBool(true),
Expand Down
9 changes: 1 addition & 8 deletions modules/generator/registry/gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,8 @@ func (g *gauge) updateSeries(labelValueCombo *LabelValueCombo, value float64, op
}

func (g *gauge) newSeries(labelValueCombo *LabelValueCombo, value float64) *gaugeSeries {
lb := labels.NewBuilder(getLabelsFromValueCombo(labelValueCombo))
for name, value := range g.externalLabels {
lb.Set(name, value)
}

lb.Set(labels.MetricName, g.metricName)

return &gaugeSeries{
labels: lb.Labels(),
labels: getSeriesLabels(g.metricName, labelValueCombo, g.externalLabels),
value: atomic.NewFloat64(value),
lastUpdated: atomic.NewInt64(time.Now().UnixMilli()),
}
Expand Down
5 changes: 1 addition & 4 deletions modules/generator/registry/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,7 @@ func (h *histogram) newSeries(labelValueCombo *LabelValueCombo, value float64, t
// Precompute all labels for all sub-metrics upfront

// Create and populate label builder
lb := labels.NewBuilder(getLabelsFromValueCombo(labelValueCombo))
for name, value := range h.externalLabels {
lb.Set(name, value)
}
lb := newSeriesLabelsBuilder(labelValueCombo, h.externalLabels)

// _count
lb.Set(labels.MetricName, h.nameCount)
Expand Down
6 changes: 1 addition & 5 deletions modules/generator/registry/native_histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,7 @@ func (h *nativeHistogram) newSeries(labelValueCombo *LabelValueCombo, value floa

h.updateSeries(newSeries, value, traceID, multiplier)

lb := labels.NewBuilder(getLabelsFromValueCombo(labelValueCombo))
// set external labels
for name, value := range h.externalLabels {
lb.Set(name, value)
}
lb := newSeriesLabelsBuilder(labelValueCombo, h.externalLabels)

lb.Set(labels.MetricName, h.metricName)

Expand Down
10 changes: 9 additions & 1 deletion modules/generator/registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestManagedRegistry_counter(t *testing.T) {

counter := registry.NewCounter("my_counter")

counter.Inc(newLabelValueCombo([]string{"label"}, []string{"value-1"}), 1.0)
counter.Inc(newLabelValueCombo([]string{"label", "label"}, []string{"repeated-value", "value-1"}), 1.0)

expectedSamples := []sample{
newSample(map[string]string{"__name__": "my_counter", "label": "value-1", "__metrics_gen_instance": mustGetHostname()}, 0, 0.0),
Expand Down Expand Up @@ -304,6 +304,14 @@ func collectRegistryMetricsAndAssert(t *testing.T, r *ManagedRegistry, appender
collectionTimeMs := time.Now().UnixMilli()
r.CollectMetrics(context.Background())

// Validate that there are no duplicate label names in any sample
for i, sample := range appender.samples {
if duplicateLabel, hasDuplicate := sample.l.HasDuplicateLabelNames(); hasDuplicate {
t.Errorf("Sample %d has duplicate label name %q. Full labels: %v",
i, duplicateLabel, sample.l)
}
}

// Ignore the collection time on expected samples, since we won't know when the collection will actually take place.
for i := range expectedSamples {
expectedSamples[i].t = collectionTimeMs
Expand Down
9 changes: 9 additions & 0 deletions modules/generator/registry/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,12 @@ func (t *testHistogram) countActiveSeries() int {
// countSeriesDemand is a stub to satisfy optional estimator usage in registry.
// Test registry does not track estimates, so return 0.
func (t *testHistogram) countSeriesDemand() int { return 0 }

func getLabelsFromValueCombo(labelValueCombo *LabelValueCombo) labels.Labels {
lbls := labelValueCombo.getLabelPair()
lb := make([]labels.Label, len(lbls.names))
for i := range lbls.names {
lb[i] = labels.Label{Name: lbls.names[i], Value: lbls.values[i]}
}
return labels.New(lb...)
}
24 changes: 18 additions & 6 deletions modules/generator/registry/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@ package registry

import "github.com/prometheus/prometheus/model/labels"

func getLabelsFromValueCombo(labelValueCombo *LabelValueCombo) labels.Labels {
lbls := labelValueCombo.getLabelPair()
lb := make([]labels.Label, len(lbls.names))
for i := range lbls.names {
lb[i] = labels.Label{Name: lbls.names[i], Value: lbls.values[i]}
// newSeriesLabelsBuilder creates a labels builder with user labels and external labels pre-populated.
func newSeriesLabelsBuilder(labelValueCombo *LabelValueCombo, externalLabels map[string]string) *labels.Builder {
builder := labels.NewBuilder(labels.Labels{})
if labelValueCombo != nil {
for i := range labelValueCombo.labels.names {
builder.Set(labelValueCombo.labels.names[i], labelValueCombo.labels.values[i])
}
}
return labels.New(lb...)
for name, value := range externalLabels {
builder.Set(name, value)
}
return builder
}

// Returns the labels for the metric series including external labels
func getSeriesLabels(metricName string, labelValueCombo *LabelValueCombo, externalLabels map[string]string) labels.Labels {
builder := newSeriesLabelsBuilder(labelValueCombo, externalLabels)
builder.Set(labels.MetricName, metricName)
return builder.Labels()
}