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 (
|
2017-10-23 03:51:48 -07:00
|
|
|
"context"
|
2022-07-01 09:59:50 -07:00
|
|
|
"fmt"
|
2019-12-12 12:47:23 -08:00
|
|
|
"net/url"
|
2017-05-10 02:44:13 -07:00
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
|
2020-09-24 11:44:18 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2019-12-12 12:47:23 -08:00
|
|
|
config_util "github.com/prometheus/common/config"
|
2020-10-29 02:43:23 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-10-22 02:00:08 -07:00
|
|
|
|
2019-12-12 12:47:23 -08:00
|
|
|
"github.com/prometheus/prometheus/config"
|
2021-11-08 06:23:17 -08:00
|
|
|
"github.com/prometheus/prometheus/model/labels"
|
2017-10-09 05:37:58 -07:00
|
|
|
"github.com/prometheus/prometheus/prompb"
|
|
|
|
"github.com/prometheus/prometheus/storage"
|
2017-05-10 02:44:13 -07:00
|
|
|
)
|
|
|
|
|
2019-12-12 12:47:23 -08:00
|
|
|
func TestNoDuplicateReadConfigs(t *testing.T) {
|
2021-11-19 12:21:45 -08:00
|
|
|
dir := t.TempDir()
|
2019-12-12 12:47:23 -08:00
|
|
|
|
|
|
|
cfg1 := config.RemoteReadConfig{
|
|
|
|
Name: "write-1",
|
|
|
|
URL: &config_util.URL{
|
|
|
|
URL: &url.URL{
|
|
|
|
Scheme: "http",
|
2020-06-24 06:41:52 -07:00
|
|
|
Host: "localhost1",
|
2019-12-12 12:47:23 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cfg2 := config.RemoteReadConfig{
|
|
|
|
Name: "write-2",
|
|
|
|
URL: &config_util.URL{
|
|
|
|
URL: &url.URL{
|
|
|
|
Scheme: "http",
|
2020-06-24 06:41:52 -07:00
|
|
|
Host: "localhost2",
|
2019-12-12 12:47:23 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cfg3 := config.RemoteReadConfig{
|
|
|
|
URL: &config_util.URL{
|
|
|
|
URL: &url.URL{
|
|
|
|
Scheme: "http",
|
2020-06-24 06:41:52 -07:00
|
|
|
Host: "localhost3",
|
2019-12-12 12:47:23 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
type testcase struct {
|
|
|
|
cfgs []*config.RemoteReadConfig
|
|
|
|
err bool
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []testcase{
|
|
|
|
{ // Duplicates but with different names, we should not get an error.
|
|
|
|
cfgs: []*config.RemoteReadConfig{
|
|
|
|
&cfg1,
|
|
|
|
&cfg2,
|
|
|
|
},
|
|
|
|
err: false,
|
|
|
|
},
|
|
|
|
{ // Duplicates but one with no name, we should not get an error.
|
|
|
|
cfgs: []*config.RemoteReadConfig{
|
|
|
|
&cfg1,
|
|
|
|
&cfg3,
|
|
|
|
},
|
|
|
|
err: false,
|
|
|
|
},
|
|
|
|
{ // Duplicates both with no name, we should get an error.
|
|
|
|
cfgs: []*config.RemoteReadConfig{
|
|
|
|
&cfg3,
|
|
|
|
&cfg3,
|
|
|
|
},
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2020-06-24 06:41:52 -07:00
|
|
|
t.Run("", func(t *testing.T) {
|
2020-11-19 07:23:03 -08:00
|
|
|
s := NewStorage(nil, nil, nil, dir, defaultFlushDeadline, nil)
|
2020-06-24 06:41:52 -07:00
|
|
|
conf := &config.Config{
|
|
|
|
GlobalConfig: config.DefaultGlobalConfig,
|
|
|
|
RemoteReadConfigs: tc.cfgs,
|
|
|
|
}
|
|
|
|
err := s.ApplyConfig(conf)
|
2020-09-24 11:44:18 -07:00
|
|
|
prometheus.Unregister(s.rws.highestTimestamp)
|
2020-06-24 06:41:52 -07:00
|
|
|
gotError := err != nil
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Equal(t, tc.err, gotError)
|
|
|
|
require.NoError(t, s.Close())
|
2020-06-24 06:41:52 -07:00
|
|
|
})
|
2017-11-11 17:15:27 -08:00
|
|
|
}
|
2019-03-13 03:02:36 -07:00
|
|
|
}
|
2017-11-11 17:15:27 -08:00
|
|
|
|
|
|
|
func TestExternalLabelsQuerierAddExternalLabels(t *testing.T) {
|
2017-05-10 02:44:13 -07:00
|
|
|
tests := []struct {
|
2019-03-08 08:29:25 -08:00
|
|
|
el labels.Labels
|
2017-05-10 02:44:13 -07:00
|
|
|
inMatchers []*labels.Matcher
|
|
|
|
outMatchers []*labels.Matcher
|
2019-03-13 03:02:36 -07:00
|
|
|
added labels.Labels
|
2017-05-10 02:44:13 -07:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
inMatchers: []*labels.Matcher{
|
2019-11-18 11:53:33 -08:00
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "job", "api-server"),
|
2017-05-10 02:44:13 -07:00
|
|
|
},
|
|
|
|
outMatchers: []*labels.Matcher{
|
2019-11-18 11:53:33 -08:00
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "job", "api-server"),
|
2017-05-10 02:44:13 -07:00
|
|
|
},
|
2019-03-13 03:02:36 -07:00
|
|
|
added: labels.Labels{},
|
2017-05-10 02:44:13 -07:00
|
|
|
},
|
|
|
|
{
|
2022-07-21 09:41:20 -07:00
|
|
|
el: labels.FromStrings("dc", "berlin-01", "region", "europe"),
|
2017-05-10 02:44:13 -07:00
|
|
|
inMatchers: []*labels.Matcher{
|
2019-11-18 11:53:33 -08:00
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "job", "api-server"),
|
2017-05-10 02:44:13 -07:00
|
|
|
},
|
|
|
|
outMatchers: []*labels.Matcher{
|
2019-11-18 11:53:33 -08:00
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "job", "api-server"),
|
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "region", "europe"),
|
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "dc", "berlin-01"),
|
2017-05-10 02:44:13 -07:00
|
|
|
},
|
2022-07-21 09:41:20 -07:00
|
|
|
added: labels.FromStrings("dc", "berlin-01", "region", "europe"),
|
2017-05-10 02:44:13 -07:00
|
|
|
},
|
|
|
|
{
|
2022-07-21 09:41:20 -07:00
|
|
|
el: labels.FromStrings("dc", "berlin-01", "region", "europe"),
|
2017-05-10 02:44:13 -07:00
|
|
|
inMatchers: []*labels.Matcher{
|
2019-11-18 11:53:33 -08:00
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "job", "api-server"),
|
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "dc", "munich-02"),
|
2017-05-10 02:44:13 -07:00
|
|
|
},
|
|
|
|
outMatchers: []*labels.Matcher{
|
2019-11-18 11:53:33 -08:00
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "job", "api-server"),
|
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "region", "europe"),
|
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "dc", "munich-02"),
|
2017-05-10 02:44:13 -07:00
|
|
|
},
|
2022-07-21 09:41:20 -07:00
|
|
|
added: labels.FromStrings("region", "europe"),
|
2017-05-10 02:44:13 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2020-06-24 06:41:52 -07:00
|
|
|
q := &querier{externalLabels: test.el}
|
2017-05-10 02:44:13 -07:00
|
|
|
matchers, added := q.addExternalLabels(test.inMatchers)
|
|
|
|
|
|
|
|
sort.Slice(test.outMatchers, func(i, j int) bool { return test.outMatchers[i].Name < test.outMatchers[j].Name })
|
|
|
|
sort.Slice(matchers, func(i, j int) bool { return matchers[i].Name < matchers[j].Name })
|
|
|
|
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Equal(t, test.outMatchers, matchers, "%d", i)
|
|
|
|
require.Equal(t, test.added, added, "%d", i)
|
2017-05-10 02:44:13 -07:00
|
|
|
}
|
|
|
|
}
|
2017-10-09 05:37:58 -07:00
|
|
|
|
2017-10-26 05:50:39 -07:00
|
|
|
func TestSeriesSetFilter(t *testing.T) {
|
2017-10-21 00:29:03 -07:00
|
|
|
tests := []struct {
|
2017-10-23 13:28:17 -07:00
|
|
|
in *prompb.QueryResult
|
2019-03-13 03:02:36 -07:00
|
|
|
toRemove labels.Labels
|
2017-10-23 13:28:17 -07:00
|
|
|
|
|
|
|
expected *prompb.QueryResult
|
2017-10-21 00:29:03 -07:00
|
|
|
}{
|
|
|
|
{
|
2019-03-13 03:02:36 -07:00
|
|
|
toRemove: labels.Labels{{Name: "foo", Value: "bar"}},
|
2017-10-23 13:28:17 -07:00
|
|
|
in: &prompb.QueryResult{
|
|
|
|
Timeseries: []*prompb.TimeSeries{
|
2019-08-12 09:22:02 -07:00
|
|
|
{Labels: labelsToLabelsProto(labels.FromStrings("foo", "bar", "a", "b"), nil), Samples: []prompb.Sample{}},
|
2017-10-23 13:28:17 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: &prompb.QueryResult{
|
|
|
|
Timeseries: []*prompb.TimeSeries{
|
2019-08-12 09:22:02 -07:00
|
|
|
{Labels: labelsToLabelsProto(labels.FromStrings("a", "b"), nil), Samples: []prompb.Sample{}},
|
2017-10-23 13:28:17 -07:00
|
|
|
},
|
|
|
|
},
|
2017-10-21 00:29:03 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-06-09 09:57:31 -07:00
|
|
|
for _, tc := range tests {
|
2020-03-12 02:36:09 -07:00
|
|
|
filtered := newSeriesSetFilter(FromQueryResult(true, tc.in), tc.toRemove)
|
2020-06-09 09:57:31 -07:00
|
|
|
act, ws, err := ToQueryResult(filtered, 1e6)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(ws))
|
|
|
|
require.Equal(t, tc.expected, act)
|
2017-10-09 05:37:58 -07:00
|
|
|
}
|
|
|
|
}
|
2017-10-18 04:08:14 -07:00
|
|
|
|
2020-06-24 06:41:52 -07:00
|
|
|
type mockedRemoteClient struct {
|
|
|
|
got *prompb.Query
|
|
|
|
store []*prompb.TimeSeries
|
2017-11-11 17:23:20 -08:00
|
|
|
}
|
|
|
|
|
2020-06-24 06:41:52 -07:00
|
|
|
func (c *mockedRemoteClient) Read(_ context.Context, query *prompb.Query) (*prompb.QueryResult, error) {
|
|
|
|
if c.got != nil {
|
2022-07-01 09:59:50 -07:00
|
|
|
return nil, fmt.Errorf("expected only one call to remote client got: %v", query)
|
2020-06-24 06:41:52 -07:00
|
|
|
}
|
|
|
|
c.got = query
|
2017-10-18 04:08:14 -07:00
|
|
|
|
2020-06-24 06:41:52 -07:00
|
|
|
matchers, err := FromLabelMatchers(query.Matchers)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-10-18 04:08:14 -07:00
|
|
|
}
|
|
|
|
|
2020-06-24 06:41:52 -07:00
|
|
|
q := &prompb.QueryResult{}
|
|
|
|
for _, s := range c.store {
|
|
|
|
l := labelProtosToLabels(s.Labels)
|
|
|
|
var notMatch bool
|
|
|
|
|
|
|
|
for _, m := range matchers {
|
|
|
|
if v := l.Get(m.Name); v != "" {
|
|
|
|
if !m.Matches(v) {
|
|
|
|
notMatch = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-10-18 04:08:14 -07:00
|
|
|
}
|
|
|
|
|
2020-06-24 06:41:52 -07:00
|
|
|
if !notMatch {
|
|
|
|
q.Timeseries = append(q.Timeseries, &prompb.TimeSeries{Labels: s.Labels})
|
2017-10-18 04:08:14 -07:00
|
|
|
}
|
|
|
|
}
|
2020-06-24 06:41:52 -07:00
|
|
|
return q, nil
|
2017-10-18 04:08:14 -07:00
|
|
|
}
|
2017-11-11 17:23:20 -08:00
|
|
|
|
2020-06-24 06:41:52 -07:00
|
|
|
func (c *mockedRemoteClient) reset() {
|
|
|
|
c.got = nil
|
|
|
|
}
|
2017-11-11 17:23:20 -08:00
|
|
|
|
2020-06-24 06:41:52 -07:00
|
|
|
// NOTE: We don't need to test ChunkQuerier as it's uses querier for all operations anyway.
|
|
|
|
func TestSampleAndChunkQueryableClient(t *testing.T) {
|
|
|
|
m := &mockedRemoteClient{
|
|
|
|
// Samples does not matter for below tests.
|
|
|
|
store: []*prompb.TimeSeries{
|
|
|
|
{Labels: []prompb.Label{{Name: "a", Value: "b"}}},
|
|
|
|
{Labels: []prompb.Label{{Name: "a", Value: "b3"}, {Name: "region", Value: "us"}}},
|
|
|
|
{Labels: []prompb.Label{{Name: "a", Value: "b2"}, {Name: "region", Value: "europe"}}},
|
|
|
|
},
|
2017-11-11 17:23:20 -08:00
|
|
|
}
|
|
|
|
|
2020-06-24 06:41:52 -07:00
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
2017-11-11 17:23:20 -08:00
|
|
|
matchers []*labels.Matcher
|
2020-06-24 06:41:52 -07:00
|
|
|
mint, maxt int64
|
|
|
|
externalLabels labels.Labels
|
|
|
|
requiredMatchers []*labels.Matcher
|
|
|
|
readRecent bool
|
|
|
|
callback startTimeCallback
|
|
|
|
|
|
|
|
expectedQuery *prompb.Query
|
|
|
|
expectedSeries []labels.Labels
|
2017-11-11 17:23:20 -08:00
|
|
|
}{
|
|
|
|
{
|
2020-06-24 06:41:52 -07:00
|
|
|
name: "empty",
|
|
|
|
mint: 1, maxt: 2,
|
2017-11-11 17:23:20 -08:00
|
|
|
matchers: []*labels.Matcher{
|
2020-06-24 06:41:52 -07:00
|
|
|
labels.MustNewMatcher(labels.MatchNotEqual, "a", "something"),
|
|
|
|
},
|
|
|
|
readRecent: true,
|
|
|
|
|
|
|
|
expectedQuery: &prompb.Query{
|
|
|
|
StartTimestampMs: 1,
|
|
|
|
EndTimestampMs: 2,
|
|
|
|
Matchers: []*prompb.LabelMatcher{
|
|
|
|
{Type: prompb.LabelMatcher_NEQ, Name: "a", Value: "something"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedSeries: []labels.Labels{
|
|
|
|
labels.FromStrings("a", "b"),
|
|
|
|
labels.FromStrings("a", "b2", "region", "europe"),
|
|
|
|
labels.FromStrings("a", "b3", "region", "us"),
|
2017-11-11 17:23:20 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-06-24 06:41:52 -07:00
|
|
|
name: "external labels specified, not explicitly requested",
|
|
|
|
mint: 1, maxt: 2,
|
2017-11-11 17:23:20 -08:00
|
|
|
matchers: []*labels.Matcher{
|
2020-06-24 06:41:52 -07:00
|
|
|
labels.MustNewMatcher(labels.MatchNotEqual, "a", "something"),
|
|
|
|
},
|
2022-07-21 09:41:20 -07:00
|
|
|
readRecent: true,
|
|
|
|
externalLabels: labels.FromStrings("region", "europe"),
|
2020-06-24 06:41:52 -07:00
|
|
|
|
|
|
|
expectedQuery: &prompb.Query{
|
|
|
|
StartTimestampMs: 1,
|
|
|
|
EndTimestampMs: 2,
|
|
|
|
Matchers: []*prompb.LabelMatcher{
|
|
|
|
{Type: prompb.LabelMatcher_NEQ, Name: "a", Value: "something"},
|
|
|
|
{Type: prompb.LabelMatcher_EQ, Name: "region", Value: "europe"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedSeries: []labels.Labels{
|
|
|
|
labels.FromStrings("a", "b"),
|
|
|
|
labels.FromStrings("a", "b2"),
|
2017-11-11 17:23:20 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-06-24 06:41:52 -07:00
|
|
|
name: "external labels specified, explicitly requested europe",
|
|
|
|
mint: 1, maxt: 2,
|
|
|
|
matchers: []*labels.Matcher{
|
|
|
|
labels.MustNewMatcher(labels.MatchNotEqual, "a", "something"),
|
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "region", "europe"),
|
|
|
|
},
|
2022-07-21 09:41:20 -07:00
|
|
|
readRecent: true,
|
|
|
|
externalLabels: labels.FromStrings("region", "europe"),
|
2020-06-24 06:41:52 -07:00
|
|
|
|
|
|
|
expectedQuery: &prompb.Query{
|
|
|
|
StartTimestampMs: 1,
|
|
|
|
EndTimestampMs: 2,
|
|
|
|
Matchers: []*prompb.LabelMatcher{
|
|
|
|
{Type: prompb.LabelMatcher_NEQ, Name: "a", Value: "something"},
|
|
|
|
{Type: prompb.LabelMatcher_EQ, Name: "region", Value: "europe"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedSeries: []labels.Labels{
|
|
|
|
labels.FromStrings("a", "b"),
|
|
|
|
labels.FromStrings("a", "b2", "region", "europe"),
|
2017-11-11 17:23:20 -08:00
|
|
|
},
|
2020-06-24 06:41:52 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "external labels specified, explicitly requested not europe",
|
|
|
|
mint: 1, maxt: 2,
|
2017-11-11 17:23:20 -08:00
|
|
|
matchers: []*labels.Matcher{
|
2020-06-24 06:41:52 -07:00
|
|
|
labels.MustNewMatcher(labels.MatchNotEqual, "a", "something"),
|
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "region", "us"),
|
|
|
|
},
|
2022-07-21 09:41:20 -07:00
|
|
|
readRecent: true,
|
|
|
|
externalLabels: labels.FromStrings("region", "europe"),
|
2020-06-24 06:41:52 -07:00
|
|
|
|
|
|
|
expectedQuery: &prompb.Query{
|
|
|
|
StartTimestampMs: 1,
|
|
|
|
EndTimestampMs: 2,
|
|
|
|
Matchers: []*prompb.LabelMatcher{
|
|
|
|
{Type: prompb.LabelMatcher_NEQ, Name: "a", Value: "something"},
|
|
|
|
{Type: prompb.LabelMatcher_EQ, Name: "region", Value: "us"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedSeries: []labels.Labels{
|
|
|
|
labels.FromStrings("a", "b"),
|
|
|
|
labels.FromStrings("a", "b3", "region", "us"),
|
2017-11-11 17:23:20 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-06-24 06:41:52 -07:00
|
|
|
name: "prefer local storage",
|
|
|
|
mint: 0, maxt: 50,
|
|
|
|
callback: func() (i int64, err error) { return 100, nil },
|
|
|
|
readRecent: false,
|
|
|
|
|
|
|
|
expectedQuery: &prompb.Query{
|
|
|
|
StartTimestampMs: 0,
|
|
|
|
EndTimestampMs: 50,
|
|
|
|
Matchers: []*prompb.LabelMatcher{},
|
2017-11-11 17:23:20 -08:00
|
|
|
},
|
2020-06-24 06:41:52 -07:00
|
|
|
expectedSeries: []labels.Labels{
|
|
|
|
labels.FromStrings("a", "b"),
|
|
|
|
labels.FromStrings("a", "b2", "region", "europe"),
|
|
|
|
labels.FromStrings("a", "b3", "region", "us"),
|
2017-11-11 17:23:20 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-06-24 06:41:52 -07:00
|
|
|
name: "prefer local storage, limited time",
|
|
|
|
mint: 0, maxt: 50,
|
|
|
|
callback: func() (i int64, err error) { return 20, nil },
|
|
|
|
readRecent: false,
|
|
|
|
|
|
|
|
expectedQuery: &prompb.Query{
|
|
|
|
StartTimestampMs: 0,
|
|
|
|
EndTimestampMs: 20,
|
|
|
|
Matchers: []*prompb.LabelMatcher{},
|
2017-11-11 17:23:20 -08:00
|
|
|
},
|
2020-06-24 06:41:52 -07:00
|
|
|
expectedSeries: []labels.Labels{
|
|
|
|
labels.FromStrings("a", "b"),
|
|
|
|
labels.FromStrings("a", "b2", "region", "europe"),
|
|
|
|
labels.FromStrings("a", "b3", "region", "us"),
|
2017-11-11 17:23:20 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-06-24 06:41:52 -07:00
|
|
|
name: "prefer local storage, skipped",
|
|
|
|
mint: 30, maxt: 50,
|
|
|
|
callback: func() (i int64, err error) { return 20, nil },
|
|
|
|
readRecent: false,
|
|
|
|
|
|
|
|
expectedQuery: nil,
|
|
|
|
expectedSeries: nil, // Noop should be used.
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "required matcher specified, user also specifies same",
|
|
|
|
mint: 1, maxt: 2,
|
|
|
|
matchers: []*labels.Matcher{
|
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "a", "b2"),
|
|
|
|
},
|
|
|
|
readRecent: true,
|
2017-11-11 17:23:20 -08:00
|
|
|
requiredMatchers: []*labels.Matcher{
|
2020-06-24 06:41:52 -07:00
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "a", "b2"),
|
2017-11-11 17:23:20 -08:00
|
|
|
},
|
2020-06-24 06:41:52 -07:00
|
|
|
|
|
|
|
expectedQuery: &prompb.Query{
|
|
|
|
StartTimestampMs: 1,
|
|
|
|
EndTimestampMs: 2,
|
|
|
|
Matchers: []*prompb.LabelMatcher{
|
|
|
|
{Type: prompb.LabelMatcher_EQ, Name: "a", Value: "b2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedSeries: []labels.Labels{
|
|
|
|
labels.FromStrings("a", "b2", "region", "europe"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "required matcher specified",
|
|
|
|
mint: 1, maxt: 2,
|
2017-11-11 17:23:20 -08:00
|
|
|
matchers: []*labels.Matcher{
|
2020-06-24 06:41:52 -07:00
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "a", "b2"),
|
|
|
|
},
|
|
|
|
readRecent: true,
|
|
|
|
requiredMatchers: []*labels.Matcher{
|
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "a", "b2"),
|
|
|
|
},
|
|
|
|
|
|
|
|
expectedQuery: &prompb.Query{
|
|
|
|
StartTimestampMs: 1,
|
|
|
|
EndTimestampMs: 2,
|
|
|
|
Matchers: []*prompb.LabelMatcher{
|
|
|
|
{Type: prompb.LabelMatcher_EQ, Name: "a", Value: "b2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedSeries: []labels.Labels{
|
|
|
|
labels.FromStrings("a", "b2", "region", "europe"),
|
2017-11-11 17:23:20 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-06-24 06:41:52 -07:00
|
|
|
name: "required matcher specified, given matcher does not match",
|
|
|
|
mint: 1, maxt: 2,
|
|
|
|
matchers: []*labels.Matcher{
|
|
|
|
labels.MustNewMatcher(labels.MatchNotEqual, "a", "something"),
|
|
|
|
},
|
|
|
|
readRecent: true,
|
2017-11-11 17:23:20 -08:00
|
|
|
requiredMatchers: []*labels.Matcher{
|
2020-06-24 06:41:52 -07:00
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "a", "b2"),
|
2017-11-11 17:23:20 -08:00
|
|
|
},
|
2020-06-24 06:41:52 -07:00
|
|
|
|
|
|
|
expectedQuery: nil,
|
|
|
|
expectedSeries: nil, // Given matchers does not match with required ones, noop expected.
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "required matcher specified, given matcher does not match2",
|
|
|
|
mint: 1, maxt: 2,
|
2017-11-11 17:23:20 -08:00
|
|
|
matchers: []*labels.Matcher{
|
2020-06-24 06:41:52 -07:00
|
|
|
labels.MustNewMatcher(labels.MatchNotEqual, "x", "something"),
|
|
|
|
},
|
|
|
|
readRecent: true,
|
|
|
|
requiredMatchers: []*labels.Matcher{
|
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "a", "b2"),
|
2017-11-11 17:23:20 -08:00
|
|
|
},
|
2020-06-24 06:41:52 -07:00
|
|
|
expectedQuery: nil,
|
|
|
|
expectedSeries: nil, // Given matchers does not match with required ones, noop expected.
|
2017-11-11 17:23:20 -08:00
|
|
|
},
|
2020-06-24 06:41:52 -07:00
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
m.reset()
|
|
|
|
|
|
|
|
c := NewSampleAndChunkQueryableClient(
|
|
|
|
m,
|
|
|
|
tc.externalLabels,
|
|
|
|
tc.requiredMatchers,
|
|
|
|
tc.readRecent,
|
|
|
|
tc.callback,
|
|
|
|
)
|
|
|
|
q, err := c.Querier(context.TODO(), tc.mint, tc.maxt)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer require.NoError(t, q.Close())
|
2020-06-24 06:41:52 -07:00
|
|
|
|
|
|
|
ss := q.Select(true, nil, tc.matchers...)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, storage.Warnings(nil), ss.Warnings())
|
2020-06-24 06:41:52 -07:00
|
|
|
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Equal(t, tc.expectedQuery, m.got)
|
2020-06-24 06:41:52 -07:00
|
|
|
|
|
|
|
var got []labels.Labels
|
|
|
|
for ss.Next() {
|
|
|
|
got = append(got, ss.At().Labels())
|
|
|
|
}
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, ss.Err())
|
|
|
|
require.Equal(t, tc.expectedSeries, got)
|
2020-06-24 06:41:52 -07:00
|
|
|
})
|
2017-11-11 17:23:20 -08:00
|
|
|
}
|
|
|
|
}
|