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

Function TestGetMetricsWithLabelNames

pkg/util/metrics_helper_test.go:60–96  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

58}
59
60func TestGetMetricsWithLabelNames(t *testing.T) {
61 labels := []string{"a", "b"}
62
63 require.Equal(t, map[string]metricsWithLabels{}, getMetricsWithLabelNames(nil, labels))
64 require.Equal(t, map[string]metricsWithLabels{}, getMetricsWithLabelNames(&dto.MetricFamily{}, labels))
65
66 m1 := &dto.Metric{Label: makeLabels("a", "5"), Counter: &dto.Counter{Value: proto.Float64(1)}}
67 m2 := &dto.Metric{Label: makeLabels("a", "10", "b", "20"), Counter: &dto.Counter{Value: proto.Float64(1.5)}}
68 m3 := &dto.Metric{Label: makeLabels("a", "10", "b", "20", "c", "1"), Counter: &dto.Counter{Value: proto.Float64(2)}}
69 m4 := &dto.Metric{Label: makeLabels("a", "10", "b", "20", "c", "2"), Counter: &dto.Counter{Value: proto.Float64(3)}}
70 m5 := &dto.Metric{Label: makeLabels("a", "11", "b", "21"), Counter: &dto.Counter{Value: proto.Float64(4)}}
71 m6 := &dto.Metric{Label: makeLabels("ignored", "123", "a", "12", "b", "22", "c", "30"), Counter: &dto.Counter{Value: proto.Float64(4)}}
72
73 out := getMetricsWithLabelNames(&dto.MetricFamily{Metric: []*dto.Metric{m1, m2, m3, m4, m5, m6}}, labels)
74
75 // m1 is not returned at all, as it doesn't have both required labels.
76 require.Equal(t, map[string]metricsWithLabels{
77 getLabelsString([]string{"10", "20"}): {
78 labelValues: []string{"10", "20"},
79 metrics: []*dto.Metric{m2, m3, m4}},
80 getLabelsString([]string{"11", "21"}): {
81 labelValues: []string{"11", "21"},
82 metrics: []*dto.Metric{m5}},
83 getLabelsString([]string{"12", "22"}): {
84 labelValues: []string{"12", "22"},
85 metrics: []*dto.Metric{m6}},
86 }, out)
87
88 // no labels -- returns all metrics in single key. this isn't very efficient, and there are other functions
89 // (without labels) to handle this better, but it still works.
90 out2 := getMetricsWithLabelNames(&dto.MetricFamily{Metric: []*dto.Metric{m1, m2, m3, m4, m5, m6}}, nil)
91 require.Equal(t, map[string]metricsWithLabels{
92 getLabelsString(nil): {
93 labelValues: []string{},
94 metrics: []*dto.Metric{m1, m2, m3, m4, m5, m6}},
95 }, out2)
96}
97
98func BenchmarkGetMetricsWithLabelNames(b *testing.B) {
99 const (

Callers

nothing calls this directly

Calls 4

getMetricsWithLabelNamesFunction · 0.85
getLabelsStringFunction · 0.85
makeLabelsFunction · 0.70
EqualMethod · 0.65

Tested by

no test coverage detected