MCPcopy Create free account
hub / github.com/cortexproject/cortex / TestSumOfCounterPerUserWithLabels

Function TestSumOfCounterPerUserWithLabels

pkg/util/metrics_helper_test.go:321–378  ·  view source on GitHub ↗

TestSumOfCounterPerUserWithLabels tests to ensure multiple metrics for the same user with a matching label are summed correctly

(t *testing.T)

Source from the content-addressed store, hash-verified

319// TestSumOfCounterPerUserWithLabels tests to ensure multiple metrics for the same user with a matching label are
320// summed correctly
321func TestSumOfCounterPerUserWithLabels(t *testing.T) {
322 user1Metric := prometheus.NewCounterVec(prometheus.CounterOpts{Name: "test_metric"}, []string{"label_one", "label_two"})
323 user2Metric := prometheus.NewCounterVec(prometheus.CounterOpts{Name: "test_metric"}, []string{"label_one", "label_two"})
324 user1Metric.WithLabelValues("a", "b").Add(100)
325 user1Metric.WithLabelValues("a", "c").Add(80)
326 user2Metric.WithLabelValues("a", "b").Add(60)
327 user2Metric.WithLabelValues("a", "c").Add(40)
328
329 user1Reg := prometheus.NewRegistry()
330 user2Reg := prometheus.NewRegistry()
331 user1Reg.MustRegister(user1Metric)
332 user2Reg.MustRegister(user2Metric)
333
334 regs := NewUserRegistries()
335 regs.AddUserRegistry("user-1", user1Reg)
336 regs.AddUserRegistry("user-2", user2Reg)
337 mf := regs.BuildMetricFamiliesPerUser()
338
339 {
340 desc := prometheus.NewDesc("test_metric", "", []string{"user", "label_one"}, nil)
341 actual := collectMetrics(t, func(out chan prometheus.Metric) {
342 mf.SendSumOfCountersPerUserWithLabels(out, desc, "test_metric", "label_one")
343 })
344 expected := []*dto.Metric{
345 {Label: makeLabels("label_one", "a", "user", "user-1"), Counter: &dto.Counter{Value: proto.Float64(180)}},
346 {Label: makeLabels("label_one", "a", "user", "user-2"), Counter: &dto.Counter{Value: proto.Float64(100)}},
347 }
348 require.ElementsMatch(t, expected, actual)
349 }
350
351 {
352 desc := prometheus.NewDesc("test_metric", "", []string{"user", "label_two"}, nil)
353 actual := collectMetrics(t, func(out chan prometheus.Metric) {
354 mf.SendSumOfCountersPerUserWithLabels(out, desc, "test_metric", "label_two")
355 })
356 expected := []*dto.Metric{
357 {Label: makeLabels("label_two", "b", "user", "user-1"), Counter: &dto.Counter{Value: proto.Float64(100)}},
358 {Label: makeLabels("label_two", "c", "user", "user-1"), Counter: &dto.Counter{Value: proto.Float64(80)}},
359 {Label: makeLabels("label_two", "b", "user", "user-2"), Counter: &dto.Counter{Value: proto.Float64(60)}},
360 {Label: makeLabels("label_two", "c", "user", "user-2"), Counter: &dto.Counter{Value: proto.Float64(40)}},
361 }
362 require.ElementsMatch(t, expected, actual)
363 }
364
365 {
366 desc := prometheus.NewDesc("test_metric", "", []string{"user", "label_one", "label_two"}, nil)
367 actual := collectMetrics(t, func(out chan prometheus.Metric) {
368 mf.SendSumOfCountersPerUserWithLabels(out, desc, "test_metric", "label_one", "label_two")
369 })
370 expected := []*dto.Metric{
371 {Label: makeLabels("label_one", "a", "label_two", "b", "user", "user-1"), Counter: &dto.Counter{Value: proto.Float64(100)}},
372 {Label: makeLabels("label_one", "a", "label_two", "c", "user", "user-1"), Counter: &dto.Counter{Value: proto.Float64(80)}},
373 {Label: makeLabels("label_one", "a", "label_two", "b", "user", "user-2"), Counter: &dto.Counter{Value: proto.Float64(60)}},
374 {Label: makeLabels("label_one", "a", "label_two", "c", "user", "user-2"), Counter: &dto.Counter{Value: proto.Float64(40)}},
375 }
376 require.ElementsMatch(t, expected, actual)
377 }
378}

Callers

nothing calls this directly

Calls 7

AddUserRegistryMethod · 0.95
NewUserRegistriesFunction · 0.85
collectMetricsFunction · 0.85
makeLabelsFunction · 0.70
AddMethod · 0.45

Tested by

no test coverage detected