|
| 1 | +package ingester |
| 2 | + |
| 3 | +import ( |
| 4 | + "io/ioutil" |
| 5 | + "math" |
| 6 | + "os" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/cortexproject/cortex/pkg/ingester/client" |
| 11 | + "github.com/cortexproject/cortex/pkg/ring" |
| 12 | + "github.com/cortexproject/cortex/pkg/util/test" |
| 13 | + "github.com/cortexproject/cortex/pkg/util/validation" |
| 14 | + "github.com/prometheus/common/model" |
| 15 | + "github.com/prometheus/prometheus/pkg/labels" |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | + "github.com/weaveworks/common/user" |
| 19 | + "golang.org/x/net/context" |
| 20 | +) |
| 21 | + |
| 22 | +func Test_Ingester_v2MetricsForLabelMatchers(t *testing.T) { |
| 23 | + fixtures := []struct { |
| 24 | + lbls labels.Labels |
| 25 | + value float64 |
| 26 | + timestamp int64 |
| 27 | + }{ |
| 28 | + {labels.Labels{{Name: labels.MetricName, Value: "test_1"}, {Name: "status", Value: "200"}}, 1, 100000}, |
| 29 | + {labels.Labels{{Name: labels.MetricName, Value: "test_1"}, {Name: "status", Value: "500"}}, 1, 110000}, |
| 30 | + {labels.Labels{{Name: labels.MetricName, Value: "test_2"}}, 2, 200000}, |
| 31 | + // The two following series have the same FastFingerprint=e002a3a451262627 |
| 32 | + {labels.Labels{{Name: labels.MetricName, Value: "collision"}, {Name: "app", Value: "l"}, {Name: "uniq0", Value: "0"}, {Name: "uniq1", Value: "1"}}, 1, 300000}, |
| 33 | + {labels.Labels{{Name: labels.MetricName, Value: "collision"}, {Name: "app", Value: "m"}, {Name: "uniq0", Value: "1"}, {Name: "uniq1", Value: "1"}}, 1, 300000}, |
| 34 | + } |
| 35 | + |
| 36 | + tests := map[string]struct { |
| 37 | + from int64 |
| 38 | + to int64 |
| 39 | + matchers []*client.LabelMatchers |
| 40 | + expected []*client.Metric |
| 41 | + }{ |
| 42 | + "should return an empty response if no metric match": { |
| 43 | + from: math.MinInt64, |
| 44 | + to: math.MaxInt64, |
| 45 | + matchers: []*client.LabelMatchers{{ |
| 46 | + Matchers: []*client.LabelMatcher{ |
| 47 | + {Type: client.EQUAL, Name: model.MetricNameLabel, Value: "unknown"}, |
| 48 | + }, |
| 49 | + }}, |
| 50 | + expected: []*client.Metric{}, |
| 51 | + }, |
| 52 | + "should filter metrics by single matcher": { |
| 53 | + from: math.MinInt64, |
| 54 | + to: math.MaxInt64, |
| 55 | + matchers: []*client.LabelMatchers{{ |
| 56 | + Matchers: []*client.LabelMatcher{ |
| 57 | + {Type: client.EQUAL, Name: model.MetricNameLabel, Value: "test_1"}, |
| 58 | + }, |
| 59 | + }}, |
| 60 | + expected: []*client.Metric{ |
| 61 | + {Labels: client.FromLabelsToLabelAdapters(fixtures[0].lbls)}, |
| 62 | + {Labels: client.FromLabelsToLabelAdapters(fixtures[1].lbls)}, |
| 63 | + }, |
| 64 | + }, |
| 65 | + "should filter metrics by multiple matchers": { |
| 66 | + from: math.MinInt64, |
| 67 | + to: math.MaxInt64, |
| 68 | + matchers: []*client.LabelMatchers{ |
| 69 | + { |
| 70 | + Matchers: []*client.LabelMatcher{ |
| 71 | + {Type: client.EQUAL, Name: "status", Value: "200"}, |
| 72 | + }, |
| 73 | + }, |
| 74 | + { |
| 75 | + Matchers: []*client.LabelMatcher{ |
| 76 | + {Type: client.EQUAL, Name: model.MetricNameLabel, Value: "test_2"}, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + expected: []*client.Metric{ |
| 81 | + {Labels: client.FromLabelsToLabelAdapters(fixtures[0].lbls)}, |
| 82 | + {Labels: client.FromLabelsToLabelAdapters(fixtures[2].lbls)}, |
| 83 | + }, |
| 84 | + }, |
| 85 | + "should filter metrics by time range": { |
| 86 | + from: 100000, |
| 87 | + to: 100000, |
| 88 | + matchers: []*client.LabelMatchers{{ |
| 89 | + Matchers: []*client.LabelMatcher{ |
| 90 | + {Type: client.EQUAL, Name: model.MetricNameLabel, Value: "test_1"}, |
| 91 | + }, |
| 92 | + }}, |
| 93 | + expected: []*client.Metric{ |
| 94 | + {Labels: client.FromLabelsToLabelAdapters(fixtures[0].lbls)}, |
| 95 | + }, |
| 96 | + }, |
| 97 | + "should not return duplicated metrics on overlapping matchers": { |
| 98 | + from: math.MinInt64, |
| 99 | + to: math.MaxInt64, |
| 100 | + matchers: []*client.LabelMatchers{ |
| 101 | + { |
| 102 | + Matchers: []*client.LabelMatcher{ |
| 103 | + {Type: client.EQUAL, Name: model.MetricNameLabel, Value: "test_1"}, |
| 104 | + }, |
| 105 | + }, |
| 106 | + { |
| 107 | + Matchers: []*client.LabelMatcher{ |
| 108 | + {Type: client.REGEX_MATCH, Name: model.MetricNameLabel, Value: "test.*"}, |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + expected: []*client.Metric{ |
| 113 | + {Labels: client.FromLabelsToLabelAdapters(fixtures[0].lbls)}, |
| 114 | + {Labels: client.FromLabelsToLabelAdapters(fixtures[1].lbls)}, |
| 115 | + {Labels: client.FromLabelsToLabelAdapters(fixtures[2].lbls)}, |
| 116 | + }, |
| 117 | + }, |
| 118 | + "should return all matching metrics even if their FastFingerprint collide": { |
| 119 | + from: math.MinInt64, |
| 120 | + to: math.MaxInt64, |
| 121 | + matchers: []*client.LabelMatchers{{ |
| 122 | + Matchers: []*client.LabelMatcher{ |
| 123 | + {Type: client.EQUAL, Name: model.MetricNameLabel, Value: "collision"}, |
| 124 | + }, |
| 125 | + }}, |
| 126 | + expected: []*client.Metric{ |
| 127 | + {Labels: client.FromLabelsToLabelAdapters(fixtures[3].lbls)}, |
| 128 | + {Labels: client.FromLabelsToLabelAdapters(fixtures[4].lbls)}, |
| 129 | + }, |
| 130 | + }, |
| 131 | + } |
| 132 | + |
| 133 | + // Create ingester |
| 134 | + i, cleanup, err := newIngesterMockWithTSDBStorage(defaultIngesterTestConfig()) |
| 135 | + require.NoError(t, err) |
| 136 | + defer i.Shutdown() |
| 137 | + defer cleanup() |
| 138 | + |
| 139 | + // Wait until it's ACTIVE |
| 140 | + test.Poll(t, 1*time.Second, ring.ACTIVE, func() interface{} { |
| 141 | + return i.lifecycler.GetState() |
| 142 | + }) |
| 143 | + |
| 144 | + // Push fixtures |
| 145 | + ctx := user.InjectOrgID(context.Background(), "test") |
| 146 | + |
| 147 | + for _, series := range fixtures { |
| 148 | + req, _ := mockWriteRequest(series.lbls, series.value, series.timestamp) |
| 149 | + _, err := i.v2Push(ctx, req) |
| 150 | + require.NoError(t, err) |
| 151 | + } |
| 152 | + |
| 153 | + // Run tests |
| 154 | + for testName, testData := range tests { |
| 155 | + testData := testData |
| 156 | + |
| 157 | + t.Run(testName, func(t *testing.T) { |
| 158 | + req := &client.MetricsForLabelMatchersRequest{ |
| 159 | + StartTimestampMs: testData.from, |
| 160 | + EndTimestampMs: testData.to, |
| 161 | + MatchersSet: testData.matchers, |
| 162 | + } |
| 163 | + |
| 164 | + res, err := i.v2MetricsForLabelMatchers(ctx, req) |
| 165 | + require.NoError(t, err) |
| 166 | + assert.ElementsMatch(t, testData.expected, res.Metric) |
| 167 | + }) |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +func mockWriteRequest(lbls labels.Labels, value float64, timestampMs int64) (*client.WriteRequest, *client.QueryResponse) { |
| 172 | + samples := []client.Sample{ |
| 173 | + { |
| 174 | + TimestampMs: timestampMs, |
| 175 | + Value: value, |
| 176 | + }, |
| 177 | + } |
| 178 | + |
| 179 | + req := client.ToWriteRequest([]labels.Labels{lbls}, samples, client.API) |
| 180 | + |
| 181 | + // Generate the expected response |
| 182 | + expectedResponse := &client.QueryResponse{ |
| 183 | + Timeseries: []client.TimeSeries{ |
| 184 | + { |
| 185 | + Labels: client.FromLabelsToLabelAdapters(lbls), |
| 186 | + Samples: samples, |
| 187 | + }, |
| 188 | + }, |
| 189 | + } |
| 190 | + |
| 191 | + return req, expectedResponse |
| 192 | +} |
| 193 | + |
| 194 | +func newIngesterMockWithTSDBStorage(ingesterCfg Config) (*Ingester, func(), error) { |
| 195 | + clientCfg := defaultClientTestConfig() |
| 196 | + limits := defaultLimitsTestConfig() |
| 197 | + |
| 198 | + overrides, err := validation.NewOverrides(limits) |
| 199 | + if err != nil { |
| 200 | + return nil, nil, err |
| 201 | + } |
| 202 | + |
| 203 | + // Create a temporary directory for TSDB |
| 204 | + tempDir, err := ioutil.TempDir("", "tsdb") |
| 205 | + if err != nil { |
| 206 | + return nil, nil, err |
| 207 | + } |
| 208 | + |
| 209 | + ingesterCfg.TSDBEnabled = true |
| 210 | + ingesterCfg.TSDBConfig.Dir = tempDir |
| 211 | + ingesterCfg.TSDBConfig.Backend = "s3" |
| 212 | + ingesterCfg.TSDBConfig.S3.Endpoint = "localhost" |
| 213 | + |
| 214 | + ingester, err := NewV2(ingesterCfg, clientCfg, overrides, nil, nil) |
| 215 | + if err != nil { |
| 216 | + return nil, nil, err |
| 217 | + } |
| 218 | + |
| 219 | + // Create a cleanup function that the caller should call with defer |
| 220 | + cleanup := func() { |
| 221 | + os.RemoveAll(tempDir) |
| 222 | + } |
| 223 | + |
| 224 | + return ingester, cleanup, nil |
| 225 | +} |
0 commit comments