-
Notifications
You must be signed in to change notification settings - Fork 790
/
distributor_queryable.go
324 lines (272 loc) · 10.7 KB
/
distributor_queryable.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package querier
import (
"context"
"sort"
"time"
"github.com/go-kit/log/level"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/exemplar"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/scrape"
"github.com/prometheus/prometheus/storage"
"github.com/cortexproject/cortex/pkg/cortexpb"
"github.com/cortexproject/cortex/pkg/ingester/client"
"github.com/cortexproject/cortex/pkg/prom1/storage/metric"
"github.com/cortexproject/cortex/pkg/querier/series"
"github.com/cortexproject/cortex/pkg/tenant"
"github.com/cortexproject/cortex/pkg/util"
"github.com/cortexproject/cortex/pkg/util/chunkcompat"
"github.com/cortexproject/cortex/pkg/util/math"
"github.com/cortexproject/cortex/pkg/util/spanlogger"
)
// Distributor is the read interface to the distributor, made an interface here
// to reduce package coupling.
type Distributor interface {
Query(ctx context.Context, from, to model.Time, matchers ...*labels.Matcher) (model.Matrix, error)
QueryStream(ctx context.Context, from, to model.Time, matchers ...*labels.Matcher) (*client.QueryStreamResponse, error)
QueryExemplars(ctx context.Context, from, to model.Time, matchers ...[]*labels.Matcher) (*client.ExemplarQueryResponse, error)
LabelValuesForLabelName(ctx context.Context, from, to model.Time, label model.LabelName, matchers ...*labels.Matcher) ([]string, error)
LabelValuesForLabelNameStream(ctx context.Context, from, to model.Time, label model.LabelName, matchers ...*labels.Matcher) ([]string, error)
LabelNames(context.Context, model.Time, model.Time) ([]string, error)
LabelNamesStream(context.Context, model.Time, model.Time) ([]string, error)
MetricsForLabelMatchers(ctx context.Context, from, through model.Time, matchers ...*labels.Matcher) ([]metric.Metric, error)
MetricsForLabelMatchersStream(ctx context.Context, from, through model.Time, matchers ...*labels.Matcher) ([]metric.Metric, error)
MetricsMetadata(ctx context.Context) ([]scrape.MetricMetadata, error)
}
func newDistributorQueryable(distributor Distributor, streaming bool, streamingMetdata bool, iteratorFn chunkIteratorFunc, queryIngestersWithin time.Duration) QueryableWithFilter {
return distributorQueryable{
distributor: distributor,
streaming: streaming,
streamingMetdata: streamingMetdata,
iteratorFn: iteratorFn,
queryIngestersWithin: queryIngestersWithin,
}
}
type distributorQueryable struct {
distributor Distributor
streaming bool
streamingMetdata bool
iteratorFn chunkIteratorFunc
queryIngestersWithin time.Duration
}
func (d distributorQueryable) Querier(ctx context.Context, mint, maxt int64) (storage.Querier, error) {
return &distributorQuerier{
distributor: d.distributor,
ctx: ctx,
mint: mint,
maxt: maxt,
streaming: d.streaming,
streamingMetadata: d.streamingMetdata,
chunkIterFn: d.iteratorFn,
queryIngestersWithin: d.queryIngestersWithin,
}, nil
}
func (d distributorQueryable) UseQueryable(now time.Time, _, queryMaxT int64) bool {
// Include ingester only if maxt is within QueryIngestersWithin w.r.t. current time.
return d.queryIngestersWithin == 0 || queryMaxT >= util.TimeToMillis(now.Add(-d.queryIngestersWithin))
}
type distributorQuerier struct {
distributor Distributor
ctx context.Context
mint, maxt int64
streaming bool
streamingMetadata bool
chunkIterFn chunkIteratorFunc
queryIngestersWithin time.Duration
}
// Select implements storage.Querier interface.
// The bool passed is ignored because the series is always sorted.
func (q *distributorQuerier) Select(_ bool, sp *storage.SelectHints, matchers ...*labels.Matcher) storage.SeriesSet {
log, ctx := spanlogger.New(q.ctx, "distributorQuerier.Select")
defer log.Span.Finish()
minT, maxT := q.mint, q.maxt
if sp != nil {
minT, maxT = sp.Start, sp.End
}
// If the querier receives a 'series' query, it means only metadata is needed.
// For this specific case we shouldn't apply the queryIngestersWithin
// time range manipulation, otherwise we'll end up returning no series at all for
// older time ranges (while in Cortex we do ignore the start/end and always return
// series in ingesters).
// Also, in the recent versions of Prometheus, we pass in the hint but with Func set to "series".
// See: https://github.com/prometheus/prometheus/pull/8050
if sp != nil && sp.Func == "series" {
var (
ms []metric.Metric
err error
)
if q.streamingMetadata {
ms, err = q.distributor.MetricsForLabelMatchersStream(ctx, model.Time(q.mint), model.Time(q.maxt), matchers...)
} else {
ms, err = q.distributor.MetricsForLabelMatchers(ctx, model.Time(q.mint), model.Time(q.maxt), matchers...)
}
if err != nil {
return storage.ErrSeriesSet(err)
}
return series.MetricsToSeriesSet(ms)
}
// If queryIngestersWithin is enabled, we do manipulate the query mint to query samples up until
// now - queryIngestersWithin, because older time ranges are covered by the storage. This
// optimization is particularly important for the blocks storage where the blocks retention in the
// ingesters could be way higher than queryIngestersWithin.
if q.queryIngestersWithin > 0 {
now := time.Now()
origMinT := minT
minT = math.Max64(minT, util.TimeToMillis(now.Add(-q.queryIngestersWithin)))
if origMinT != minT {
level.Debug(log).Log("msg", "the min time of the query to ingesters has been manipulated", "original", origMinT, "updated", minT)
}
if minT > maxT {
level.Debug(log).Log("msg", "empty query time range after min time manipulation")
return storage.EmptySeriesSet()
}
}
if q.streaming {
return q.streamingSelect(ctx, minT, maxT, matchers)
}
matrix, err := q.distributor.Query(ctx, model.Time(minT), model.Time(maxT), matchers...)
if err != nil {
return storage.ErrSeriesSet(err)
}
// Using MatrixToSeriesSet (and in turn NewConcreteSeriesSet), sorts the series.
return series.MatrixToSeriesSet(matrix)
}
func (q *distributorQuerier) streamingSelect(ctx context.Context, minT, maxT int64, matchers []*labels.Matcher) storage.SeriesSet {
userID, err := tenant.TenantID(ctx)
if err != nil {
return storage.ErrSeriesSet(err)
}
results, err := q.distributor.QueryStream(ctx, model.Time(minT), model.Time(maxT), matchers...)
if err != nil {
return storage.ErrSeriesSet(err)
}
sets := []storage.SeriesSet(nil)
if len(results.Timeseries) > 0 {
sets = append(sets, newTimeSeriesSeriesSet(results.Timeseries))
}
serieses := make([]storage.Series, 0, len(results.Chunkseries))
for _, result := range results.Chunkseries {
// Sometimes the ingester can send series that have no data.
if len(result.Chunks) == 0 {
continue
}
ls := cortexpb.FromLabelAdaptersToLabels(result.Labels)
sort.Sort(ls)
chunks, err := chunkcompat.FromChunks(userID, ls, result.Chunks)
if err != nil {
return storage.ErrSeriesSet(err)
}
serieses = append(serieses, &chunkSeries{
labels: ls,
chunks: chunks,
chunkIteratorFunc: q.chunkIterFn,
mint: minT,
maxt: maxT,
})
}
if len(serieses) > 0 {
sets = append(sets, series.NewConcreteSeriesSet(serieses))
}
if len(sets) == 0 {
return storage.EmptySeriesSet()
}
if len(sets) == 1 {
return sets[0]
}
// Sets need to be sorted. Both series.NewConcreteSeriesSet and newTimeSeriesSeriesSet take care of that.
return storage.NewMergeSeriesSet(sets, storage.ChainedSeriesMerge)
}
func (q *distributorQuerier) LabelValues(name string, matchers ...*labels.Matcher) ([]string, storage.Warnings, error) {
var (
lvs []string
err error
)
if q.streamingMetadata {
lvs, err = q.distributor.LabelValuesForLabelNameStream(q.ctx, model.Time(q.mint), model.Time(q.maxt), model.LabelName(name), matchers...)
} else {
lvs, err = q.distributor.LabelValuesForLabelName(q.ctx, model.Time(q.mint), model.Time(q.maxt), model.LabelName(name), matchers...)
}
return lvs, nil, err
}
func (q *distributorQuerier) LabelNames(matchers ...*labels.Matcher) ([]string, storage.Warnings, error) {
if len(matchers) > 0 {
return q.labelNamesWithMatchers(matchers...)
}
log, ctx := spanlogger.New(q.ctx, "distributorQuerier.LabelNames")
defer log.Span.Finish()
var (
ln []string
err error
)
if q.streamingMetadata {
ln, err = q.distributor.LabelNamesStream(ctx, model.Time(q.mint), model.Time(q.maxt))
} else {
ln, err = q.distributor.LabelNames(ctx, model.Time(q.mint), model.Time(q.maxt))
}
return ln, nil, err
}
// labelNamesWithMatchers performs the LabelNames call by calling ingester's MetricsForLabelMatchers method
func (q *distributorQuerier) labelNamesWithMatchers(matchers ...*labels.Matcher) ([]string, storage.Warnings, error) {
log, ctx := spanlogger.New(q.ctx, "distributorQuerier.labelNamesWithMatchers")
defer log.Span.Finish()
var (
ms []metric.Metric
err error
)
if q.streamingMetadata {
ms, err = q.distributor.MetricsForLabelMatchersStream(ctx, model.Time(q.mint), model.Time(q.maxt), matchers...)
} else {
ms, err = q.distributor.MetricsForLabelMatchers(ctx, model.Time(q.mint), model.Time(q.maxt), matchers...)
}
if err != nil {
return nil, nil, err
}
namesMap := make(map[string]struct{})
for _, m := range ms {
for name := range m.Metric {
namesMap[string(name)] = struct{}{}
}
}
names := make([]string, 0, len(namesMap))
for name := range namesMap {
names = append(names, name)
}
sort.Strings(names)
return names, nil, nil
}
func (q *distributorQuerier) Close() error {
return nil
}
type distributorExemplarQueryable struct {
distributor Distributor
}
func newDistributorExemplarQueryable(d Distributor) storage.ExemplarQueryable {
return &distributorExemplarQueryable{
distributor: d,
}
}
func (d distributorExemplarQueryable) ExemplarQuerier(ctx context.Context) (storage.ExemplarQuerier, error) {
return &distributorExemplarQuerier{
distributor: d.distributor,
ctx: ctx,
}, nil
}
type distributorExemplarQuerier struct {
distributor Distributor
ctx context.Context
}
// Select querys for exemplars, prometheus' storage.ExemplarQuerier's Select function takes the time range as two int64 values.
func (q *distributorExemplarQuerier) Select(start, end int64, matchers ...[]*labels.Matcher) ([]exemplar.QueryResult, error) {
allResults, err := q.distributor.QueryExemplars(q.ctx, model.Time(start), model.Time(end), matchers...)
if err != nil {
return nil, err
}
var e exemplar.QueryResult
ret := make([]exemplar.QueryResult, len(allResults.Timeseries))
for i, ts := range allResults.Timeseries {
e.SeriesLabels = cortexpb.FromLabelAdaptersToLabels(ts.Labels)
e.Exemplars = cortexpb.FromExemplarProtosToExemplars(ts.Exemplars)
ret[i] = e
}
return ret, nil
}