// Copyright 2017 The Prometheus Authors // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package remote import ( "context" "sort" "github.com/prometheus/common/model" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/prompb" "github.com/prometheus/prometheus/storage" ) // Querier returns a new Querier on the storage. func (r *Storage) Querier(_ context.Context, mint, maxt int64) (storage.Querier, error) { r.mtx.Lock() defer r.mtx.Unlock() queriers := make([]storage.Querier, 0, len(r.clients)) localStartTime, err := r.localStartTimeCallback() if err != nil { return nil, err } for _, c := range r.clients { cmaxt := maxt if !c.readRecent { // Avoid queries whose timerange is later than the first timestamp in local DB. if mint > localStartTime { continue } // Query only samples older than the first timestamp in local DB. if maxt > localStartTime { cmaxt = localStartTime } } queriers = append(queriers, &querier{ mint: mint, maxt: cmaxt, client: c, externalLabels: r.externalLabels, }) } return newMergeQueriers(queriers), nil } // Store it in variable to make it mockable in tests since a mergeQuerier is not publicly exposed. var newMergeQueriers = storage.NewMergeQuerier // Querier is an adapter to make a Client usable as a storage.Querier. type querier struct { mint, maxt int64 client *Client externalLabels model.LabelSet } // Select returns a set of series that matches the given label matchers. func (q *querier) Select(matchers ...*labels.Matcher) storage.SeriesSet { m, added := q.addExternalLabels(matchers) res, err := q.client.Read(context.TODO(), q.mint, q.maxt, m) if err != nil { return errSeriesSet{err: err} } series := make([]storage.Series, 0, len(res)) for _, ts := range res { labels := labelPairsToLabels(ts.Labels) removeLabels(&labels, added) series = append(series, &concreteSeries{ labels: labels, samples: ts.Samples, }) } sort.Sort(byLabel(series)) return &concreteSeriesSet{ series: series, } } func labelPairsToLabels(labelPairs []*prompb.Label) labels.Labels { result := make(labels.Labels, 0, len(labelPairs)) for _, l := range labelPairs { result = append(result, labels.Label{ Name: l.Name, Value: l.Value, }) } sort.Sort(result) return result } type byLabel []storage.Series func (a byLabel) Len() int { return len(a) } func (a byLabel) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a byLabel) Less(i, j int) bool { return labels.Compare(a[i].Labels(), a[j].Labels()) < 0 } // LabelValues returns all potential values for a label name. func (q *querier) LabelValues(name string) ([]string, error) { // TODO implement? return nil, nil } // Close releases the resources of the Querier. func (q *querier) Close() error { return nil } // errSeriesSet implements storage.SeriesSet, just returning an error. type errSeriesSet struct { err error } func (errSeriesSet) Next() bool { return false } func (errSeriesSet) At() storage.Series { return nil } func (e errSeriesSet) Err() error { return e.err } // concreteSeriesSet implements storage.SeriesSet. type concreteSeriesSet struct { cur int series []storage.Series } func (c *concreteSeriesSet) Next() bool { c.cur++ return c.cur-1 < len(c.series) } func (c *concreteSeriesSet) At() storage.Series { return c.series[c.cur-1] } func (c *concreteSeriesSet) Err() error { return nil } // concreteSeries implementes storage.Series. type concreteSeries struct { labels labels.Labels samples []*prompb.Sample } func (c *concreteSeries) Labels() labels.Labels { return c.labels } func (c *concreteSeries) Iterator() storage.SeriesIterator { return newConcreteSeriersIterator(c) } // concreteSeriesIterator implements storage.SeriesIterator. type concreteSeriesIterator struct { cur int series *concreteSeries } func newConcreteSeriersIterator(series *concreteSeries) storage.SeriesIterator { return &concreteSeriesIterator{ cur: -1, series: series, } } // Seek implements storage.SeriesIterator. func (c *concreteSeriesIterator) Seek(t int64) bool { c.cur = sort.Search(len(c.series.samples), func(n int) bool { return c.series.samples[n].Timestamp >= t }) return c.cur < len(c.series.samples) } // At implements storage.SeriesIterator. func (c *concreteSeriesIterator) At() (t int64, v float64) { s := c.series.samples[c.cur] return s.Timestamp, s.Value } // Next implements storage.SeriesIterator. func (c *concreteSeriesIterator) Next() bool { c.cur++ return c.cur < len(c.series.samples) } // Err implements storage.SeriesIterator. func (c *concreteSeriesIterator) Err() error { return nil } // addExternalLabels adds matchers for each external label. External labels // that already have a corresponding user-supplied matcher are skipped, as we // assume that the user explicitly wants to select a different value for them. // We return the new set of matchers, along with a map of labels for which // matchers were added, so that these can later be removed from the result // time series again. func (q *querier) addExternalLabels(matchers []*labels.Matcher) ([]*labels.Matcher, model.LabelSet) { el := make(model.LabelSet, len(q.externalLabels)) for k, v := range q.externalLabels { el[k] = v } for _, m := range matchers { if _, ok := el[model.LabelName(m.Name)]; ok { delete(el, model.LabelName(m.Name)) } } for k, v := range el { m, err := labels.NewMatcher(labels.MatchEqual, string(k), string(v)) if err != nil { panic(err) } matchers = append(matchers, m) } return matchers, el } func removeLabels(l *labels.Labels, toDelete model.LabelSet) { for i := 0; i < len(*l); { if _, ok := toDelete[model.LabelName((*l)[i].Name)]; ok { *l = (*l)[:i+copy((*l)[i:], (*l)[i+1:])] } else { i++ } } }