Skip to content

Commit

Permalink
Fixes missing labesl from the generated metrics and adds a test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
prateek committed Jan 1, 2025
1 parent 84b8425 commit f812de1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
3 changes: 2 additions & 1 deletion metrics/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,8 @@ func (c *Collector) Run() error {
labelValues = append(labelValues, split[1])
}

mutableState := &metricState{seriesCount: c.cfg.SeriesCount}
c.labelKeys = labelKeys
mutableState := &metricState{seriesCount: c.cfg.SeriesCount, labelValues: labelValues}
// unsafe means you need to lock c.mu to use it.
unsafeReadOnlyGetState := func() metricState { return *mutableState }

Expand Down
46 changes: 46 additions & 0 deletions metrics/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,49 @@ func TestRunMetricsSpikeChange(t *testing.T) {
}
}
}

func TestCollectorLabels(t *testing.T) {
testCfg := Config{
GaugeMetricCount: 1,
LabelCount: 2,
SeriesCount: 100,
MaxSeriesCount: 30,
MinSeriesCount: 10,
SpikeMultiplier: 1.5,
MetricLength: 1,
LabelLength: 1,
SeriesOperationMode: spikeOpMode,
ConstLabels: []string{"constLabel=test"},
}

assert.NoError(t, testCfg.Validate())

reg := prometheus.NewRegistry()
col := NewCollector(testCfg)
reg.MustRegister(col)

go col.Run()
t.Cleanup(func() {
col.Stop(nil)
})

time.Sleep((2 * time.Second))
metricsFamilies, err := reg.Gather()
assert.NotEmpty(t, metricsFamilies)
assert.NoError(t,err)

for _, mf := range metricsFamilies {
for _, m := range mf.Metric {
labels := m.GetLabel()
labelMap := make(map[string]string)
for _, l := range labels {
labelMap[l.GetName()] = l.GetValue()
}
assert.Equal(t, "test", labelMap["constLabel"])
assert.Contains(t, labelMap, "label_key_k_0")
assert.Contains(t, labelMap, "label_key_k_1")
assert.Contains(t, labelMap, "series_id")
assert.Contains(t, labelMap, "cycle_id")
}
}
}

0 comments on commit f812de1

Please sign in to comment.