2017-05-10 02:44:13 -07:00
|
|
|
// 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"
|
|
|
|
|
2018-10-10 14:48:32 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2017-05-10 02:44:13 -07:00
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
|
|
|
"github.com/prometheus/prometheus/storage"
|
|
|
|
)
|
|
|
|
|
2018-10-10 14:48:32 -07:00
|
|
|
var remoteReadQueries = prometheus.NewGaugeVec(
|
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: subsystem,
|
|
|
|
Name: "remote_read_queries",
|
|
|
|
Help: "The number of in-flight remote read queries.",
|
|
|
|
},
|
|
|
|
[]string{"client"},
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
prometheus.MustRegister(remoteReadQueries)
|
|
|
|
}
|
|
|
|
|
2017-11-11 17:15:27 -08:00
|
|
|
// QueryableClient returns a storage.Queryable which queries the given
|
|
|
|
// Client to select series sets.
|
|
|
|
func QueryableClient(c *Client) storage.Queryable {
|
2018-10-10 14:48:32 -07:00
|
|
|
remoteReadQueries.WithLabelValues(c.Name())
|
2017-11-11 17:15:27 -08:00
|
|
|
return storage.QueryableFunc(func(ctx context.Context, mint, maxt int64) (storage.Querier, error) {
|
|
|
|
return &querier{
|
|
|
|
ctx: ctx,
|
|
|
|
mint: mint,
|
|
|
|
maxt: maxt,
|
|
|
|
client: c,
|
|
|
|
}, nil
|
|
|
|
})
|
2017-05-10 02:44:13 -07:00
|
|
|
}
|
|
|
|
|
2017-11-11 17:15:27 -08:00
|
|
|
// querier is an adapter to make a Client usable as a storage.Querier.
|
2017-05-10 02:44:13 -07:00
|
|
|
type querier struct {
|
2017-11-11 17:15:27 -08:00
|
|
|
ctx context.Context
|
|
|
|
mint, maxt int64
|
|
|
|
client *Client
|
2017-05-10 02:44:13 -07:00
|
|
|
}
|
|
|
|
|
2017-11-11 17:15:27 -08:00
|
|
|
// Select implements storage.Querier and uses the given matchers to read series
|
|
|
|
// sets from the Client.
|
2019-01-02 03:10:13 -08:00
|
|
|
func (q *querier) Select(p *storage.SelectParams, matchers ...*labels.Matcher) (storage.SeriesSet, storage.Warnings, error) {
|
2018-05-08 01:48:13 -07:00
|
|
|
query, err := ToQuery(q.mint, q.maxt, matchers, p)
|
2017-05-10 02:44:13 -07:00
|
|
|
if err != nil {
|
2019-01-02 03:10:13 -08:00
|
|
|
return nil, nil, err
|
2017-05-10 02:44:13 -07:00
|
|
|
}
|
|
|
|
|
2018-10-10 14:48:32 -07:00
|
|
|
remoteReadGauge := remoteReadQueries.WithLabelValues(q.client.Name())
|
|
|
|
remoteReadGauge.Inc()
|
|
|
|
defer remoteReadGauge.Dec()
|
|
|
|
|
2017-10-23 13:28:17 -07:00
|
|
|
res, err := q.client.Read(q.ctx, query)
|
|
|
|
if err != nil {
|
2019-01-02 03:10:13 -08:00
|
|
|
return nil, nil, err
|
2017-05-10 02:44:13 -07:00
|
|
|
}
|
2017-10-23 13:28:17 -07:00
|
|
|
|
2018-11-30 06:27:12 -08:00
|
|
|
return FromQueryResult(res), nil, nil
|
2017-05-10 02:44:13 -07:00
|
|
|
}
|
|
|
|
|
2017-11-11 17:15:27 -08:00
|
|
|
// LabelValues implements storage.Querier and is a noop.
|
2017-05-10 02:44:13 -07:00
|
|
|
func (q *querier) LabelValues(name string) ([]string, error) {
|
|
|
|
// TODO implement?
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-11-19 02:21:14 -08:00
|
|
|
// LabelNames implements storage.Querier and is a noop.
|
|
|
|
func (q *querier) LabelNames() ([]string, error) {
|
|
|
|
// TODO implement?
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-11-11 17:15:27 -08:00
|
|
|
// Close implements storage.Querier and is a noop.
|
2017-05-10 02:44:13 -07:00
|
|
|
func (q *querier) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-26 00:49:53 -07:00
|
|
|
// ExternalLabelsHandler returns a storage.Queryable which creates a
|
2017-11-11 17:15:27 -08:00
|
|
|
// externalLabelsQuerier.
|
2019-03-08 08:29:25 -08:00
|
|
|
func ExternalLabelsHandler(next storage.Queryable, externalLabels labels.Labels) storage.Queryable {
|
2017-11-11 17:15:27 -08:00
|
|
|
return storage.QueryableFunc(func(ctx context.Context, mint, maxt int64) (storage.Querier, error) {
|
|
|
|
q, err := next.Querier(ctx, mint, maxt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &externalLabelsQuerier{Querier: q, externalLabels: externalLabels}, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// externalLabelsQuerier is a querier which ensures that Select() results match
|
|
|
|
// the configured external labels.
|
|
|
|
type externalLabelsQuerier struct {
|
|
|
|
storage.Querier
|
|
|
|
|
2019-03-08 08:29:25 -08:00
|
|
|
externalLabels labels.Labels
|
2017-11-11 17:15:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Select adds equality matchers for all external labels to the list of matchers
|
|
|
|
// before calling the wrapped storage.Queryable. The added external labels are
|
|
|
|
// removed from the returned series sets.
|
2019-01-02 03:10:13 -08:00
|
|
|
func (q externalLabelsQuerier) Select(p *storage.SelectParams, matchers ...*labels.Matcher) (storage.SeriesSet, storage.Warnings, error) {
|
2017-11-11 17:15:27 -08:00
|
|
|
m, added := q.addExternalLabels(matchers)
|
2019-01-02 03:10:13 -08:00
|
|
|
s, warnings, err := q.Querier.Select(p, m...)
|
2017-11-23 04:50:06 -08:00
|
|
|
if err != nil {
|
2019-01-02 03:10:13 -08:00
|
|
|
return nil, warnings, err
|
2017-11-23 04:50:06 -08:00
|
|
|
}
|
2019-01-02 03:10:13 -08:00
|
|
|
return newSeriesSetFilter(s, added), warnings, nil
|
2017-11-11 17:15:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// PreferLocalStorageFilter returns a QueryableFunc which creates a NoopQuerier
|
|
|
|
// if requested timeframe can be answered completely by the local TSDB, and
|
|
|
|
// reduces maxt if the timeframe can be partially answered by TSDB.
|
|
|
|
func PreferLocalStorageFilter(next storage.Queryable, cb startTimeCallback) storage.Queryable {
|
|
|
|
return storage.QueryableFunc(func(ctx context.Context, mint, maxt int64) (storage.Querier, error) {
|
|
|
|
localStartTime, err := cb()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cmaxt := maxt
|
|
|
|
// Avoid queries whose timerange is later than the first timestamp in local DB.
|
|
|
|
if mint > localStartTime {
|
|
|
|
return storage.NoopQuerier(), nil
|
|
|
|
}
|
|
|
|
// Query only samples older than the first timestamp in local DB.
|
|
|
|
if maxt > localStartTime {
|
|
|
|
cmaxt = localStartTime
|
|
|
|
}
|
|
|
|
return next.Querier(ctx, mint, cmaxt)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-11-11 17:23:20 -08:00
|
|
|
// RequiredMatchersFilter returns a storage.Queryable which creates a
|
|
|
|
// requiredMatchersQuerier.
|
|
|
|
func RequiredMatchersFilter(next storage.Queryable, required []*labels.Matcher) storage.Queryable {
|
|
|
|
return storage.QueryableFunc(func(ctx context.Context, mint, maxt int64) (storage.Querier, error) {
|
|
|
|
q, err := next.Querier(ctx, mint, maxt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &requiredMatchersQuerier{Querier: q, requiredMatchers: required}, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// requiredMatchersQuerier wraps a storage.Querier and requires Select() calls
|
|
|
|
// to match the given labelSet.
|
|
|
|
type requiredMatchersQuerier struct {
|
|
|
|
storage.Querier
|
|
|
|
|
|
|
|
requiredMatchers []*labels.Matcher
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select returns a NoopSeriesSet if the given matchers don't match the label
|
|
|
|
// set of the requiredMatchersQuerier. Otherwise it'll call the wrapped querier.
|
2019-01-02 03:10:13 -08:00
|
|
|
func (q requiredMatchersQuerier) Select(p *storage.SelectParams, matchers ...*labels.Matcher) (storage.SeriesSet, storage.Warnings, error) {
|
2017-11-11 17:23:20 -08:00
|
|
|
ms := q.requiredMatchers
|
|
|
|
for _, m := range matchers {
|
|
|
|
for i, r := range ms {
|
|
|
|
if m.Type == labels.MatchEqual && m.Name == r.Name && m.Value == r.Value {
|
|
|
|
ms = append(ms[:i], ms[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(ms) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(ms) > 0 {
|
2018-11-30 06:27:12 -08:00
|
|
|
return storage.NoopSeriesSet(), nil, nil
|
2017-11-11 17:23:20 -08:00
|
|
|
}
|
2018-01-09 08:44:23 -08:00
|
|
|
return q.Querier.Select(p, matchers...)
|
2017-11-11 17:23:20 -08:00
|
|
|
}
|
|
|
|
|
2017-05-10 02:44:13 -07:00
|
|
|
// 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.
|
2017-11-11 17:15:27 -08:00
|
|
|
func (q externalLabelsQuerier) addExternalLabels(ms []*labels.Matcher) ([]*labels.Matcher, model.LabelSet) {
|
2017-05-10 02:44:13 -07:00
|
|
|
el := make(model.LabelSet, len(q.externalLabels))
|
2019-03-08 08:29:25 -08:00
|
|
|
for _, l := range q.externalLabels {
|
|
|
|
el[model.LabelName(l.Name)] = model.LabelValue(l.Value)
|
2017-05-10 02:44:13 -07:00
|
|
|
}
|
2017-11-11 17:15:27 -08:00
|
|
|
for _, m := range ms {
|
2017-05-10 02:44:13 -07:00
|
|
|
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)
|
|
|
|
}
|
2017-11-11 17:15:27 -08:00
|
|
|
ms = append(ms, m)
|
2017-05-10 02:44:13 -07:00
|
|
|
}
|
2017-11-11 17:15:27 -08:00
|
|
|
return ms, el
|
2017-05-10 02:44:13 -07:00
|
|
|
}
|
|
|
|
|
2017-10-23 13:28:17 -07:00
|
|
|
func newSeriesSetFilter(ss storage.SeriesSet, toFilter model.LabelSet) storage.SeriesSet {
|
|
|
|
return &seriesSetFilter{
|
|
|
|
SeriesSet: ss,
|
|
|
|
toFilter: toFilter,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type seriesSetFilter struct {
|
|
|
|
storage.SeriesSet
|
|
|
|
toFilter model.LabelSet
|
2018-11-30 06:27:12 -08:00
|
|
|
querier storage.Querier
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ssf *seriesSetFilter) GetQuerier() storage.Querier {
|
|
|
|
return ssf.querier
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ssf *seriesSetFilter) SetQuerier(querier storage.Querier) {
|
|
|
|
ssf.querier = querier
|
2017-10-23 13:28:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ssf seriesSetFilter) At() storage.Series {
|
|
|
|
return seriesFilter{
|
|
|
|
Series: ssf.SeriesSet.At(),
|
|
|
|
toFilter: ssf.toFilter,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type seriesFilter struct {
|
|
|
|
storage.Series
|
|
|
|
toFilter model.LabelSet
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sf seriesFilter) Labels() labels.Labels {
|
|
|
|
labels := sf.Series.Labels()
|
|
|
|
for i := 0; i < len(labels); {
|
|
|
|
if _, ok := sf.toFilter[model.LabelName(labels[i].Name)]; ok {
|
|
|
|
labels = labels[:i+copy(labels[i:], labels[i+1:])]
|
|
|
|
continue
|
2017-05-10 02:44:13 -07:00
|
|
|
}
|
2017-10-23 13:28:17 -07:00
|
|
|
i++
|
2017-05-10 02:44:13 -07:00
|
|
|
}
|
2017-10-23 13:28:17 -07:00
|
|
|
return labels
|
2017-05-10 02:44:13 -07:00
|
|
|
}
|