1
1
package chunk
2
2
3
3
import (
4
+ "context"
4
5
"encoding/json"
5
6
"flag"
6
7
"fmt"
7
8
"sort"
8
9
9
10
"github.com/prometheus/client_golang/prometheus"
10
11
"github.com/prometheus/common/model"
12
+ "github.com/prometheus/prometheus/pkg/labels"
11
13
"github.com/prometheus/prometheus/promql"
12
- "github.com/prometheus/prometheus/storage/local"
13
- "github.com/prometheus/prometheus/storage/metric"
14
- "golang.org/x/net/context"
15
14
16
15
"github.com/weaveworks/common/user"
17
16
"github.com/weaveworks/cortex/pkg/util"
@@ -150,7 +149,7 @@ func (c *Store) calculateDynamoWrites(userID string, chunks []Chunk) (WriteBatch
150
149
}
151
150
152
151
// Get implements ChunkStore
153
- func (c * Store ) Get (ctx context.Context , from , through model.Time , allMatchers ... * metric. LabelMatcher ) ([]local. SeriesIterator , error ) {
152
+ func (c * Store ) Get (ctx context.Context , from , through model.Time , allMatchers ... * labels. Matcher ) (model. Matrix , error ) {
154
153
if through < from {
155
154
return nil , fmt .Errorf ("invalid query, through < from (%d < %d)" , through , from )
156
155
}
@@ -159,7 +158,7 @@ func (c *Store) Get(ctx context.Context, from, through model.Time, allMatchers .
159
158
if from .After (now ) {
160
159
// time-span start is in future ... regard as legal
161
160
util .WithContext (ctx ).Debugf ("Whole timerange %v..%v in future (now=%v) yield empty resultset" , through , from , now )
162
- return []local. SeriesIterator {} , nil
161
+ return nil , nil
163
162
}
164
163
165
164
if through .After (now ) {
@@ -170,23 +169,23 @@ func (c *Store) Get(ctx context.Context, from, through model.Time, allMatchers .
170
169
171
170
// Fetch metric name chunks if the matcher is of type equal,
172
171
metricNameMatcher , matchers , ok := util .ExtractMetricNameMatcherFromMatchers (allMatchers )
173
- if ok && metricNameMatcher .Type == metric . Equal {
174
- return c .getMetricNameIterators (ctx , from , through , matchers , metricNameMatcher .Value )
172
+ if ok && metricNameMatcher .Type == labels . MatchEqual {
173
+ return c .getMetricNameMatrix (ctx , from , through , matchers , metricNameMatcher .Value )
175
174
}
176
175
177
176
// Otherwise we will create lazy iterators for all series in our index
178
- return c .getSeriesIterators (ctx , from , through , matchers , metricNameMatcher )
177
+ return c .getSeriesMatrix (ctx , from , through , matchers , metricNameMatcher )
179
178
}
180
179
181
- func (c * Store ) getMetricNameIterators (ctx context.Context , from , through model.Time , allMatchers []* metric. LabelMatcher , metricName model. LabelValue ) ([]local. SeriesIterator , error ) {
180
+ func (c * Store ) getMetricNameMatrix (ctx context.Context , from , through model.Time , allMatchers []* labels. Matcher , metricName string ) (model. Matrix , error ) {
182
181
chunks , err := c .getMetricNameChunks (ctx , from , through , allMatchers , metricName )
183
182
if err != nil {
184
183
return nil , err
185
184
}
186
- return chunksToIterators (chunks )
185
+ return chunksToMatrix (chunks )
187
186
}
188
187
189
- func (c * Store ) getMetricNameChunks (ctx context.Context , from , through model.Time , allMatchers []* metric. LabelMatcher , metricName model. LabelValue ) ([]Chunk , error ) {
188
+ func (c * Store ) getMetricNameChunks (ctx context.Context , from , through model.Time , allMatchers []* labels. Matcher , metricName string ) ([]Chunk , error ) {
190
189
logger := util .WithContext (ctx )
191
190
filters , matchers := util .SplitFiltersAndMatchers (allMatchers )
192
191
chunks , err := c .lookupChunksByMetricName (ctx , from , through , matchers , metricName )
@@ -228,7 +227,7 @@ func (c *Store) getMetricNameChunks(ctx context.Context, from, through model.Tim
228
227
outer:
229
228
for _ , chunk := range allChunks {
230
229
for _ , filter := range filters {
231
- if ! filter .Match ( chunk .Metric [filter .Name ] ) {
230
+ if ! filter .Matches ( string ( chunk .Metric [model . LabelName ( filter .Name )]) ) {
232
231
continue outer
233
232
}
234
233
}
@@ -238,7 +237,13 @@ outer:
238
237
return filteredChunks , nil
239
238
}
240
239
241
- func (c * Store ) getSeriesIterators (ctx context.Context , from , through model.Time , allMatchers []* metric.LabelMatcher , metricNameMatcher * metric.LabelMatcher ) ([]local.SeriesIterator , error ) {
240
+ type byMatcherLabel []* labels.Matcher
241
+
242
+ func (lms byMatcherLabel ) Len () int { return len (lms ) }
243
+ func (lms byMatcherLabel ) Swap (i , j int ) { lms [i ], lms [j ] = lms [j ], lms [i ] }
244
+ func (lms byMatcherLabel ) Less (i , j int ) bool { return lms [i ].Name < lms [j ].Name }
245
+
246
+ func (c * Store ) getSeriesMatrix (ctx context.Context , from , through model.Time , allMatchers []* labels.Matcher , metricNameMatcher * labels.Matcher ) (model.Matrix , error ) {
242
247
// Get all series from the index
243
248
userID , err := user .ExtractOrgID (ctx )
244
249
if err != nil {
@@ -253,7 +258,7 @@ func (c *Store) getSeriesIterators(ctx context.Context, from, through model.Time
253
258
return nil , err
254
259
}
255
260
256
- lazyIterators := make ([]local. SeriesIterator , 0 , len (seriesEntries ))
261
+ matrix := make (model. Matrix , 0 , len (seriesEntries ))
257
262
outer:
258
263
for _ , seriesEntry := range seriesEntries {
259
264
metric , err := parseSeriesRangeValue (seriesEntry .RangeValue , seriesEntry .Value )
@@ -262,40 +267,52 @@ outer:
262
267
}
263
268
264
269
// Apply metric name matcher
265
- if metricNameMatcher != nil && ! metricNameMatcher .Match ( metric [metricNameMatcher .Name ] ) {
270
+ if metricNameMatcher != nil && ! metricNameMatcher .Matches ( string ( metric [model . LabelName ( metricNameMatcher .Name )]) ) {
266
271
continue outer
267
272
}
268
273
269
274
// Apply matchers
270
275
for _ , matcher := range allMatchers {
271
- if ! matcher .Match ( metric [matcher .Name ] ) {
276
+ if ! matcher .Matches ( string ( metric [model . LabelName ( matcher .Name )]) ) {
272
277
continue outer
273
278
}
274
279
}
275
280
276
- orgID , err := user .ExtractOrgID (ctx )
277
- if err != nil {
278
- return nil , err
281
+ // TODO(prom2): Does this contraption over-match?
282
+ var matchers []* labels.Matcher
283
+ for labelName , labelValue := range metric {
284
+ if labelName == "__name__" {
285
+ continue
286
+ }
287
+
288
+ matcher , err := labels .NewMatcher (labels .MatchEqual , string (labelName ), string (labelValue ))
289
+ if err != nil {
290
+ return nil , err
291
+ }
292
+ matchers = append (matchers , matcher )
279
293
}
280
- newIterator , err := NewLazySeriesIterator (c , metric , from , through , orgID )
294
+ // TODO(prom2): why is sorting needed?
295
+ sort .Sort (byMatcherLabel (matchers ))
296
+
297
+ m , err := c .getMetricNameMatrix (ctx , from , through , matchers , metricNameMatcher .Name )
281
298
if err != nil {
282
299
return nil , err
283
300
}
284
301
285
- lazyIterators = append (lazyIterators , newIterator )
302
+ matrix = append (matrix , m ... )
286
303
}
287
- return lazyIterators , nil
304
+ return matrix , nil
288
305
}
289
306
290
- func (c * Store ) lookupChunksByMetricName (ctx context.Context , from , through model.Time , matchers []* metric. LabelMatcher , metricName model. LabelValue ) ([]Chunk , error ) {
307
+ func (c * Store ) lookupChunksByMetricName (ctx context.Context , from , through model.Time , matchers []* labels. Matcher , metricName string ) ([]Chunk , error ) {
291
308
userID , err := user .ExtractOrgID (ctx )
292
309
if err != nil {
293
310
return nil , err
294
311
}
295
312
296
313
// Just get chunks for metric if there are no matchers
297
314
if len (matchers ) == 0 {
298
- queries , err := c .schema .GetReadQueriesForMetric (from , through , userID , metricName )
315
+ queries , err := c .schema .GetReadQueriesForMetric (from , through , userID , model . LabelValue ( metricName ) )
299
316
if err != nil {
300
317
return nil , err
301
318
}
@@ -312,14 +329,14 @@ func (c *Store) lookupChunksByMetricName(ctx context.Context, from, through mode
312
329
incomingChunkSets := make (chan ByKey )
313
330
incomingErrors := make (chan error )
314
331
for _ , matcher := range matchers {
315
- go func (matcher * metric. LabelMatcher ) {
332
+ go func (matcher * labels. Matcher ) {
316
333
// Lookup IndexQuery's
317
334
var queries []IndexQuery
318
335
var err error
319
- if matcher .Type != metric . Equal {
320
- queries , err = c .schema .GetReadQueriesForMetricLabel (from , through , userID , metricName , matcher .Name )
336
+ if matcher .Type != labels . MatchEqual {
337
+ queries , err = c .schema .GetReadQueriesForMetricLabel (from , through , userID , model . LabelValue ( metricName ), model . LabelName ( matcher .Name ) )
321
338
} else {
322
- queries , err = c .schema .GetReadQueriesForMetricLabelValue (from , through , userID , metricName , matcher .Name , matcher .Value )
339
+ queries , err = c .schema .GetReadQueriesForMetricLabelValue (from , through , userID , model . LabelValue ( metricName ), model . LabelName ( matcher .Name ), model . LabelValue ( matcher .Value ) )
323
340
}
324
341
if err != nil {
325
342
incomingErrors <- err
@@ -409,7 +426,7 @@ func (c *Store) lookupEntriesByQuery(ctx context.Context, query IndexQuery) ([]I
409
426
return entries , nil
410
427
}
411
428
412
- func (c * Store ) convertIndexEntriesToChunks (ctx context.Context , entries []IndexEntry , matcher * metric. LabelMatcher ) (ByKey , error ) {
429
+ func (c * Store ) convertIndexEntriesToChunks (ctx context.Context , entries []IndexEntry , matcher * labels. Matcher ) (ByKey , error ) {
413
430
userID , err := user .ExtractOrgID (ctx )
414
431
if err != nil {
415
432
return nil , err
@@ -437,7 +454,7 @@ func (c *Store) convertIndexEntriesToChunks(ctx context.Context, entries []Index
437
454
chunk .metadataInIndex = true
438
455
}
439
456
440
- if matcher != nil && ! matcher .Match ( labelValue ) {
457
+ if matcher != nil && ! matcher .Matches ( string ( labelValue ) ) {
441
458
util .WithContext (ctx ).Debug ("Dropping chunk for non-matching metric " , chunk .Metric )
442
459
continue
443
460
}
0 commit comments