mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-25 13:44:05 -08:00
38d32e0686
Sorting the heads postings can be quite slow. We only need sorted series when merging with another querier, so only sort then. This will make big queries that only touch the head faster, though queries that touch both the head and a block will still be the same speed. This probably won't help much with graphing unless the range is under an hour, however it should make most recording rules faster. Add gaurantee that remote read streaming produces sorted series. PromQL benchmarks for histograms show only 2-3% improvement, but they're only over 1k series. benchmark old ns/op new ns/op delta BenchmarkQuerierSelect/Head/1of1000000-4 1375486282 507657736 -63.09% BenchmarkQuerierSelect/Head/10of1000000-4 1387859004 507769850 -63.41% BenchmarkQuerierSelect/Head/100of1000000-4 1387087935 506029110 -63.52% BenchmarkQuerierSelect/Head/1000of1000000-4 1386869064 504521986 -63.62% BenchmarkQuerierSelect/Head/10000of1000000-4 1386213685 505210422 -63.55% BenchmarkQuerierSelect/Head/100000of1000000-4 1392754988 529842406 -61.96% BenchmarkQuerierSelect/Head/1000000of1000000-4 1569414722 725059506 -53.80% BenchmarkQuerierSelect/SortedHead/1of1000000-4 1381019902 1370495863 -0.76% BenchmarkQuerierSelect/SortedHead/10of1000000-4 1375696209 1366789468 -0.65% BenchmarkQuerierSelect/SortedHead/100of1000000-4 1386009422 1364519297 -1.55% BenchmarkQuerierSelect/SortedHead/1000of1000000-4 1377700532 1364486191 -0.96% BenchmarkQuerierSelect/SortedHead/10000of1000000-4 1383539536 1369545314 -1.01% BenchmarkQuerierSelect/SortedHead/100000of1000000-4 1410089163 1394731339 -1.09% BenchmarkQuerierSelect/SortedHead/1000000of1000000-4 1634744148 1581554956 -3.25% BenchmarkQuerierSelect/Block/1of1000000-4 881741242 879839470 -0.22% BenchmarkQuerierSelect/Block/10of1000000-4 880381562 882846038 +0.28% BenchmarkQuerierSelect/Block/100of1000000-4 887519357 881016916 -0.73% BenchmarkQuerierSelect/Block/1000of1000000-4 902194205 883433524 -2.08% BenchmarkQuerierSelect/Block/10000of1000000-4 892321964 885130170 -0.81% BenchmarkQuerierSelect/Block/100000of1000000-4 938604466 933527150 -0.54% BenchmarkQuerierSelect/Block/1000000of1000000-4 1313510845 1295881124 -1.34% benchmark old allocs new allocs delta BenchmarkQuerierSelect/Head/1of1000000-4 4000056 4000018 -0.00% BenchmarkQuerierSelect/Head/10of1000000-4 4000074 4000036 -0.00% BenchmarkQuerierSelect/Head/100of1000000-4 4000254 4000216 -0.00% BenchmarkQuerierSelect/Head/1000of1000000-4 4002054 4002016 -0.00% BenchmarkQuerierSelect/Head/10000of1000000-4 4020054 4020016 -0.00% BenchmarkQuerierSelect/Head/100000of1000000-4 4200054 4200016 -0.00% BenchmarkQuerierSelect/Head/1000000of1000000-4 6000054 6000016 -0.00% BenchmarkQuerierSelect/SortedHead/1of1000000-4 4000071 4000071 +0.00% BenchmarkQuerierSelect/SortedHead/10of1000000-4 4000089 4000089 +0.00% BenchmarkQuerierSelect/SortedHead/100of1000000-4 4000269 4000269 +0.00% BenchmarkQuerierSelect/SortedHead/1000of1000000-4 4002069 4002069 +0.00% BenchmarkQuerierSelect/SortedHead/10000of1000000-4 4020069 4020069 +0.00% BenchmarkQuerierSelect/SortedHead/100000of1000000-4 4200069 4200069 +0.00% BenchmarkQuerierSelect/SortedHead/1000000of1000000-4 6000069 6000069 +0.00% BenchmarkQuerierSelect/Block/1of1000000-4 6000023 6000022 -0.00% BenchmarkQuerierSelect/Block/10of1000000-4 6000059 6000058 -0.00% BenchmarkQuerierSelect/Block/100of1000000-4 6000419 6000418 -0.00% BenchmarkQuerierSelect/Block/1000of1000000-4 6004019 6004018 -0.00% BenchmarkQuerierSelect/Block/10000of1000000-4 6040019 6040018 -0.00% BenchmarkQuerierSelect/Block/100000of1000000-4 6400019 6400018 -0.00% BenchmarkQuerierSelect/Block/1000000of1000000-4 10000020 10000019 -0.00% benchmark old bytes new bytes delta BenchmarkQuerierSelect/Head/1of1000000-4 229192200 176001176 -23.21% BenchmarkQuerierSelect/Head/10of1000000-4 229193352 176002328 -23.21% BenchmarkQuerierSelect/Head/100of1000000-4 229204872 176013848 -23.21% BenchmarkQuerierSelect/Head/1000of1000000-4 229320072 176129048 -23.20% BenchmarkQuerierSelect/Head/10000of1000000-4 230472072 177281048 -23.08% BenchmarkQuerierSelect/Head/100000of1000000-4 241992072 188801048 -21.98% BenchmarkQuerierSelect/Head/1000000of1000000-4 357192072 304001048 -14.89% BenchmarkQuerierSelect/SortedHead/1of1000000-4 229193928 229193928 +0.00% BenchmarkQuerierSelect/SortedHead/10of1000000-4 229195080 229195080 +0.00% BenchmarkQuerierSelect/SortedHead/100of1000000-4 229206600 229206600 +0.00% BenchmarkQuerierSelect/SortedHead/1000of1000000-4 229321800 229321800 +0.00% BenchmarkQuerierSelect/SortedHead/10000of1000000-4 230473800 230473800 +0.00% BenchmarkQuerierSelect/SortedHead/100000of1000000-4 241993800 241993800 +0.00% BenchmarkQuerierSelect/SortedHead/1000000of1000000-4 357193800 357193800 +0.00% BenchmarkQuerierSelect/Block/1of1000000-4 227201516 227201500 -0.00% BenchmarkQuerierSelect/Block/10of1000000-4 227202924 227202908 -0.00% BenchmarkQuerierSelect/Block/100of1000000-4 227217036 227217020 -0.00% BenchmarkQuerierSelect/Block/1000of1000000-4 227358156 227358140 -0.00% BenchmarkQuerierSelect/Block/10000of1000000-4 228769356 228769340 -0.00% BenchmarkQuerierSelect/Block/100000of1000000-4 242881356 242881340 -0.00% BenchmarkQuerierSelect/Block/1000000of1000000-4 384001616 384001600 -0.00% Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
277 lines
8.2 KiB
Go
277 lines
8.2 KiB
Go
// 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"
|
|
"fmt"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
|
"github.com/prometheus/prometheus/storage"
|
|
)
|
|
|
|
var remoteReadQueries = prometheus.NewGaugeVec(
|
|
prometheus.GaugeOpts{
|
|
Namespace: namespace,
|
|
Subsystem: subsystem,
|
|
Name: "remote_read_queries",
|
|
Help: "The number of in-flight remote read queries.",
|
|
},
|
|
[]string{remoteName, endpoint},
|
|
)
|
|
|
|
func init() {
|
|
prometheus.MustRegister(remoteReadQueries)
|
|
}
|
|
|
|
// QueryableClient returns a storage.Queryable which queries the given
|
|
// Client to select series sets.
|
|
func QueryableClient(c *Client) storage.Queryable {
|
|
remoteReadQueries.WithLabelValues(c.remoteName, c.url.String())
|
|
return storage.QueryableFunc(func(ctx context.Context, mint, maxt int64) (storage.Querier, error) {
|
|
return &querier{
|
|
ctx: ctx,
|
|
mint: mint,
|
|
maxt: maxt,
|
|
client: c,
|
|
}, nil
|
|
})
|
|
}
|
|
|
|
// querier is an adapter to make a Client usable as a storage.Querier.
|
|
type querier struct {
|
|
ctx context.Context
|
|
mint, maxt int64
|
|
client *Client
|
|
}
|
|
|
|
// Select implements storage.Querier and uses the given matchers to read series
|
|
// sets from the Client.
|
|
func (q *querier) Select(p *storage.SelectParams, matchers ...*labels.Matcher) (storage.SeriesSet, storage.Warnings, error) {
|
|
return q.SelectSorted(p, matchers...)
|
|
}
|
|
|
|
// SelectSorted implements storage.Querier and uses the given matchers to read series
|
|
// sets from the Client.
|
|
func (q *querier) SelectSorted(p *storage.SelectParams, matchers ...*labels.Matcher) (storage.SeriesSet, storage.Warnings, error) {
|
|
query, err := ToQuery(q.mint, q.maxt, matchers, p)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
remoteReadGauge := remoteReadQueries.WithLabelValues(q.client.remoteName, q.client.url.String())
|
|
remoteReadGauge.Inc()
|
|
defer remoteReadGauge.Dec()
|
|
|
|
res, err := q.client.Read(q.ctx, query)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("remote_read: %v", err)
|
|
}
|
|
|
|
// FromQueryResult sorts.
|
|
return FromQueryResult(res), nil, nil
|
|
}
|
|
|
|
// LabelValues implements storage.Querier and is a noop.
|
|
func (q *querier) LabelValues(name string) ([]string, storage.Warnings, error) {
|
|
// TODO implement?
|
|
return nil, nil, nil
|
|
}
|
|
|
|
// LabelNames implements storage.Querier and is a noop.
|
|
func (q *querier) LabelNames() ([]string, storage.Warnings, error) {
|
|
// TODO implement?
|
|
return nil, nil, nil
|
|
}
|
|
|
|
// Close implements storage.Querier and is a noop.
|
|
func (q *querier) Close() error {
|
|
return nil
|
|
}
|
|
|
|
// ExternalLabelsHandler returns a storage.Queryable which creates a
|
|
// externalLabelsQuerier.
|
|
func ExternalLabelsHandler(next storage.Queryable, externalLabels labels.Labels) 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 &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
|
|
|
|
externalLabels labels.Labels
|
|
}
|
|
|
|
// 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.
|
|
func (q externalLabelsQuerier) Select(p *storage.SelectParams, matchers ...*labels.Matcher) (storage.SeriesSet, storage.Warnings, error) {
|
|
m, added := q.addExternalLabels(matchers)
|
|
s, warnings, err := q.Querier.Select(p, m...)
|
|
if err != nil {
|
|
return nil, warnings, err
|
|
}
|
|
return newSeriesSetFilter(s, added), warnings, nil
|
|
}
|
|
|
|
// 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)
|
|
})
|
|
}
|
|
|
|
// 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.
|
|
func (q requiredMatchersQuerier) Select(p *storage.SelectParams, matchers ...*labels.Matcher) (storage.SeriesSet, storage.Warnings, error) {
|
|
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 {
|
|
return storage.NoopSeriesSet(), nil, nil
|
|
}
|
|
return q.Querier.Select(p, matchers...)
|
|
}
|
|
|
|
// 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 externalLabelsQuerier) addExternalLabels(ms []*labels.Matcher) ([]*labels.Matcher, labels.Labels) {
|
|
el := make(labels.Labels, len(q.externalLabels))
|
|
copy(el, q.externalLabels)
|
|
|
|
// ms won't be sorted, so have to O(n^2) the search.
|
|
for _, m := range ms {
|
|
for i := 0; i < len(el); {
|
|
if el[i].Name == m.Name {
|
|
el = el[:i+copy(el[i:], el[i+1:])]
|
|
continue
|
|
}
|
|
i++
|
|
}
|
|
}
|
|
|
|
for _, l := range el {
|
|
m, err := labels.NewMatcher(labels.MatchEqual, l.Name, l.Value)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
ms = append(ms, m)
|
|
}
|
|
return ms, el
|
|
}
|
|
|
|
func newSeriesSetFilter(ss storage.SeriesSet, toFilter labels.Labels) storage.SeriesSet {
|
|
return &seriesSetFilter{
|
|
SeriesSet: ss,
|
|
toFilter: toFilter,
|
|
}
|
|
}
|
|
|
|
type seriesSetFilter struct {
|
|
storage.SeriesSet
|
|
toFilter labels.Labels
|
|
querier storage.Querier
|
|
}
|
|
|
|
func (ssf *seriesSetFilter) GetQuerier() storage.Querier {
|
|
return ssf.querier
|
|
}
|
|
|
|
func (ssf *seriesSetFilter) SetQuerier(querier storage.Querier) {
|
|
ssf.querier = querier
|
|
}
|
|
|
|
func (ssf seriesSetFilter) At() storage.Series {
|
|
return seriesFilter{
|
|
Series: ssf.SeriesSet.At(),
|
|
toFilter: ssf.toFilter,
|
|
}
|
|
}
|
|
|
|
type seriesFilter struct {
|
|
storage.Series
|
|
toFilter labels.Labels
|
|
}
|
|
|
|
func (sf seriesFilter) Labels() labels.Labels {
|
|
labels := sf.Series.Labels()
|
|
for i, j := 0, 0; i < len(labels) && j < len(sf.toFilter); {
|
|
if labels[i].Name < sf.toFilter[j].Name {
|
|
i++
|
|
} else if labels[i].Name > sf.toFilter[j].Name {
|
|
j++
|
|
} else {
|
|
labels = labels[:i+copy(labels[i:], labels[i+1:])]
|
|
j++
|
|
}
|
|
}
|
|
return labels
|
|
}
|