|
| 1 | +// Copyright (c) The Thanos Authors. |
| 2 | +// Licensed under the Apache License 2.0. |
| 3 | + |
| 4 | +package query |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/efficientgo/core/testutil" |
| 10 | + "github.com/prometheus/client_golang/prometheus" |
| 11 | + "github.com/prometheus/prometheus/model/labels" |
| 12 | +) |
| 13 | + |
| 14 | +func TestAggregationLabelRewriter_Rewrite(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + |
| 17 | + for _, tc := range []struct { |
| 18 | + name string |
| 19 | + desiredLabelValue string // Empty means disabled |
| 20 | + inputMatchers []*labels.Matcher |
| 21 | + expectedMatchers []*labels.Matcher |
| 22 | + expectedSkipCount float64 |
| 23 | + expectedAddCount float64 |
| 24 | + expectedRewriteMap map[string]float64 |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "disabled rewriter should not modify label matchers", |
| 28 | + desiredLabelValue: "", |
| 29 | + inputMatchers: []*labels.Matcher{ |
| 30 | + labels.MustNewMatcher(labels.MatchEqual, "__name__", "test:sum"), |
| 31 | + }, |
| 32 | + expectedMatchers: []*labels.Matcher{ |
| 33 | + labels.MustNewMatcher(labels.MatchEqual, "__name__", "test:sum"), |
| 34 | + }, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "should add label for aggregated metric if no existing aggregation label", |
| 38 | + desiredLabelValue: "5m", |
| 39 | + inputMatchers: []*labels.Matcher{ |
| 40 | + labels.MustNewMatcher(labels.MatchEqual, "__name__", "test:sum"), |
| 41 | + }, |
| 42 | + expectedMatchers: []*labels.Matcher{ |
| 43 | + labels.MustNewMatcher(labels.MatchEqual, "__name__", "test:sum"), |
| 44 | + labels.MustNewMatcher(labels.MatchEqual, "__rollup__", "5m"), |
| 45 | + }, |
| 46 | + expectedAddCount: 1, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "should rewrite existing aggregation label for aggregated metric", |
| 50 | + desiredLabelValue: "5m", |
| 51 | + inputMatchers: []*labels.Matcher{ |
| 52 | + labels.MustNewMatcher(labels.MatchEqual, "__name__", "test:sum"), |
| 53 | + labels.MustNewMatcher(labels.MatchEqual, "__rollup__", "1h"), |
| 54 | + }, |
| 55 | + expectedMatchers: []*labels.Matcher{ |
| 56 | + labels.MustNewMatcher(labels.MatchEqual, "__name__", "test:sum"), |
| 57 | + labels.MustNewMatcher(labels.MatchEqual, "__rollup__", "5m"), |
| 58 | + }, |
| 59 | + expectedRewriteMap: map[string]float64{"1h": 1}, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "should skip non-aggregated metric", |
| 63 | + desiredLabelValue: "5m", |
| 64 | + inputMatchers: []*labels.Matcher{ |
| 65 | + labels.MustNewMatcher(labels.MatchEqual, "__name__", "test_metric"), |
| 66 | + }, |
| 67 | + expectedMatchers: []*labels.Matcher{ |
| 68 | + labels.MustNewMatcher(labels.MatchEqual, "__name__", "test_metric"), |
| 69 | + }, |
| 70 | + expectedSkipCount: 1, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "should skip non-equal name matcher", |
| 74 | + desiredLabelValue: "5m", |
| 75 | + inputMatchers: []*labels.Matcher{ |
| 76 | + labels.MustNewMatcher(labels.MatchRegexp, "__name__", "test:sum"), |
| 77 | + }, |
| 78 | + expectedMatchers: []*labels.Matcher{ |
| 79 | + labels.MustNewMatcher(labels.MatchRegexp, "__name__", "test:sum"), |
| 80 | + }, |
| 81 | + expectedSkipCount: 1, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "should skip when no name matcher", |
| 85 | + desiredLabelValue: "5m", |
| 86 | + inputMatchers: []*labels.Matcher{ |
| 87 | + labels.MustNewMatcher(labels.MatchEqual, "job", "prometheus"), |
| 88 | + }, |
| 89 | + expectedMatchers: []*labels.Matcher{ |
| 90 | + labels.MustNewMatcher(labels.MatchEqual, "job", "prometheus"), |
| 91 | + }, |
| 92 | + expectedSkipCount: 1, |
| 93 | + }, |
| 94 | + } { |
| 95 | + t.Run(tc.name, func(t *testing.T) { |
| 96 | + reg := prometheus.NewRegistry() |
| 97 | + rewriter := NewAggregationLabelRewriter( |
| 98 | + nil, |
| 99 | + reg, |
| 100 | + tc.desiredLabelValue, |
| 101 | + ) |
| 102 | + |
| 103 | + result := rewriter.Rewrite(tc.inputMatchers) |
| 104 | + testutil.Equals(t, len(tc.expectedMatchers), len(result)) |
| 105 | + for i := range tc.inputMatchers { |
| 106 | + testutil.Equals(t, tc.expectedMatchers[i].Name, result[i].Name) |
| 107 | + testutil.Equals(t, tc.expectedMatchers[i].Type, result[i].Type) |
| 108 | + testutil.Equals(t, tc.expectedMatchers[i].Value, result[i].Value) |
| 109 | + } |
| 110 | + |
| 111 | + metrics, err := reg.Gather() |
| 112 | + testutil.Ok(t, err) |
| 113 | + |
| 114 | + if tc.expectedSkipCount > 0 { |
| 115 | + var skipCount float64 |
| 116 | + for _, m := range metrics { |
| 117 | + if m.GetName() == "skipped_total" { |
| 118 | + skipCount += *m.Metric[0].Counter.Value |
| 119 | + } |
| 120 | + } |
| 121 | + testutil.Equals(t, tc.expectedSkipCount, skipCount) |
| 122 | + } |
| 123 | + |
| 124 | + if tc.expectedAddCount > 0 { |
| 125 | + var addCount float64 |
| 126 | + for _, m := range metrics { |
| 127 | + if m.GetName() == "label_added_total" { |
| 128 | + addCount += *m.Metric[0].Counter.Value |
| 129 | + } |
| 130 | + } |
| 131 | + testutil.Equals(t, tc.expectedAddCount, addCount) |
| 132 | + } |
| 133 | + |
| 134 | + if len(tc.expectedRewriteMap) > 0 { |
| 135 | + for _, m := range metrics { |
| 136 | + if m.GetName() == "label_rewritten_total" { |
| 137 | + for _, metric := range m.Metric { |
| 138 | + oldValue := "" |
| 139 | + for _, label := range metric.Label { |
| 140 | + if *label.Name == "old_value" { |
| 141 | + oldValue = *label.Value |
| 142 | + break |
| 143 | + } |
| 144 | + } |
| 145 | + if count, ok := tc.expectedRewriteMap[oldValue]; ok { |
| 146 | + testutil.Equals(t, count, *metric.Counter.Value) |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + }) |
| 153 | + } |
| 154 | +} |
0 commit comments