2016-04-13 14:08:22 +00:00
|
|
|
// Copyright 2016 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.
|
|
|
|
|
2015-03-30 17:13:36 +00:00
|
|
|
package promql
|
|
|
|
|
|
|
|
import (
|
2017-10-04 19:04:15 +00:00
|
|
|
"context"
|
2019-10-10 00:06:53 +00:00
|
|
|
"errors"
|
2020-01-27 22:29:44 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-01-17 11:21:44 +00:00
|
|
|
"sort"
|
2015-03-30 17:13:36 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-08-11 18:45:52 +00:00
|
|
|
"github.com/go-kit/kit/log"
|
2020-10-22 09:00:08 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"go.uber.org/goleak"
|
|
|
|
|
2017-04-18 13:51:10 +00:00
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
2020-07-23 22:05:43 +00:00
|
|
|
"github.com/prometheus/prometheus/pkg/timestamp"
|
2020-02-03 16:32:23 +00:00
|
|
|
"github.com/prometheus/prometheus/promql/parser"
|
2018-02-15 12:08:00 +00:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
2015-03-30 17:13:36 +00:00
|
|
|
)
|
|
|
|
|
2020-07-24 09:10:20 +00:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
goleak.VerifyTestMain(m)
|
|
|
|
}
|
|
|
|
|
2015-09-23 09:06:51 +00:00
|
|
|
func TestQueryConcurrency(t *testing.T) {
|
2020-01-27 22:29:44 +00:00
|
|
|
maxConcurrency := 10
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "test_concurrency")
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.NoError(t, err)
|
2020-01-27 22:29:44 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
queryTracker := NewActiveQueryTracker(dir, maxConcurrency, nil)
|
|
|
|
|
2018-10-02 11:59:19 +00:00
|
|
|
opts := EngineOpts{
|
2020-01-27 22:29:44 +00:00
|
|
|
Logger: nil,
|
|
|
|
Reg: nil,
|
|
|
|
MaxSamples: 10,
|
|
|
|
Timeout: 100 * time.Second,
|
|
|
|
ActiveQueryTracker: queryTracker,
|
2018-10-02 11:59:19 +00:00
|
|
|
}
|
2018-01-09 16:44:23 +00:00
|
|
|
|
2018-10-02 11:59:19 +00:00
|
|
|
engine := NewEngine(opts)
|
2016-09-15 22:58:06 +00:00
|
|
|
ctx, cancelCtx := context.WithCancel(context.Background())
|
|
|
|
defer cancelCtx()
|
2015-04-30 22:49:19 +00:00
|
|
|
|
|
|
|
block := make(chan struct{})
|
|
|
|
processing := make(chan struct{})
|
2015-08-10 12:21:24 +00:00
|
|
|
|
|
|
|
f := func(context.Context) error {
|
2015-04-30 22:49:19 +00:00
|
|
|
processing <- struct{}{}
|
|
|
|
<-block
|
|
|
|
return nil
|
2015-08-10 12:21:24 +00:00
|
|
|
}
|
2015-04-30 22:49:19 +00:00
|
|
|
|
2020-01-28 20:38:49 +00:00
|
|
|
for i := 0; i < maxConcurrency; i++ {
|
2015-08-10 12:21:24 +00:00
|
|
|
q := engine.newTestQuery(f)
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
2016-09-15 11:52:50 +00:00
|
|
|
go q.Exec(ctx)
|
2015-04-30 22:49:19 +00:00
|
|
|
select {
|
|
|
|
case <-processing:
|
|
|
|
// Expected.
|
2015-09-23 09:06:51 +00:00
|
|
|
case <-time.After(20 * time.Millisecond):
|
2015-04-30 22:49:19 +00:00
|
|
|
t.Fatalf("Query within concurrency threshold not being executed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-10 12:21:24 +00:00
|
|
|
q := engine.newTestQuery(f)
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
2016-09-15 11:52:50 +00:00
|
|
|
go q.Exec(ctx)
|
2015-04-30 22:49:19 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-processing:
|
2016-07-11 18:27:25 +00:00
|
|
|
t.Fatalf("Query above concurrency threshold being executed")
|
2015-09-23 09:06:51 +00:00
|
|
|
case <-time.After(20 * time.Millisecond):
|
2015-04-30 22:49:19 +00:00
|
|
|
// Expected.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Terminate a running query.
|
|
|
|
block <- struct{}{}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-processing:
|
|
|
|
// Expected.
|
2015-09-23 09:06:51 +00:00
|
|
|
case <-time.After(20 * time.Millisecond):
|
2015-04-30 22:49:19 +00:00
|
|
|
t.Fatalf("Query within concurrency threshold not being executed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Terminate remaining queries.
|
2020-01-28 20:38:49 +00:00
|
|
|
for i := 0; i < maxConcurrency; i++ {
|
2015-04-30 22:49:19 +00:00
|
|
|
block <- struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 17:13:36 +00:00
|
|
|
func TestQueryTimeout(t *testing.T) {
|
2018-10-02 11:59:19 +00:00
|
|
|
opts := EngineOpts{
|
2020-01-28 20:38:49 +00:00
|
|
|
Logger: nil,
|
|
|
|
Reg: nil,
|
|
|
|
MaxSamples: 10,
|
|
|
|
Timeout: 5 * time.Millisecond,
|
2018-10-02 11:59:19 +00:00
|
|
|
}
|
|
|
|
engine := NewEngine(opts)
|
2016-09-15 22:58:06 +00:00
|
|
|
ctx, cancelCtx := context.WithCancel(context.Background())
|
|
|
|
defer cancelCtx()
|
2015-03-30 17:13:36 +00:00
|
|
|
|
2015-08-10 12:21:24 +00:00
|
|
|
query := engine.newTestQuery(func(ctx context.Context) error {
|
2020-01-22 10:09:31 +00:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2015-08-10 12:21:24 +00:00
|
|
|
return contextDone(ctx, "test statement execution")
|
2015-03-30 17:13:36 +00:00
|
|
|
})
|
|
|
|
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
2016-09-15 11:52:50 +00:00
|
|
|
res := query.Exec(ctx)
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.Error(t, res.Err, "expected timeout error but got none")
|
2019-10-10 00:06:53 +00:00
|
|
|
|
|
|
|
var e ErrQueryTimeout
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.True(t, errors.As(res.Err, &e), "expected timeout error but got: %s", res.Err)
|
2015-03-30 17:13:36 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 00:06:53 +00:00
|
|
|
const errQueryCanceled = ErrQueryCanceled("test statement execution")
|
|
|
|
|
2015-03-30 17:13:36 +00:00
|
|
|
func TestQueryCancel(t *testing.T) {
|
2018-10-02 11:59:19 +00:00
|
|
|
opts := EngineOpts{
|
2020-01-28 20:38:49 +00:00
|
|
|
Logger: nil,
|
|
|
|
Reg: nil,
|
|
|
|
MaxSamples: 10,
|
|
|
|
Timeout: 10 * time.Second,
|
2018-10-02 11:59:19 +00:00
|
|
|
}
|
|
|
|
engine := NewEngine(opts)
|
2016-09-15 22:58:06 +00:00
|
|
|
ctx, cancelCtx := context.WithCancel(context.Background())
|
|
|
|
defer cancelCtx()
|
2015-03-30 17:13:36 +00:00
|
|
|
|
2015-08-10 12:21:24 +00:00
|
|
|
// Cancel a running query before it completes.
|
|
|
|
block := make(chan struct{})
|
|
|
|
processing := make(chan struct{})
|
2015-03-30 17:13:36 +00:00
|
|
|
|
2015-08-10 12:21:24 +00:00
|
|
|
query1 := engine.newTestQuery(func(ctx context.Context) error {
|
|
|
|
processing <- struct{}{}
|
|
|
|
<-block
|
|
|
|
return contextDone(ctx, "test statement execution")
|
|
|
|
})
|
2015-04-29 09:08:56 +00:00
|
|
|
|
2015-03-30 17:13:36 +00:00
|
|
|
var res *Result
|
|
|
|
|
|
|
|
go func() {
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
2016-09-15 11:52:50 +00:00
|
|
|
res = query1.Exec(ctx)
|
2015-08-10 12:21:24 +00:00
|
|
|
processing <- struct{}{}
|
2015-03-30 17:13:36 +00:00
|
|
|
}()
|
2015-08-10 12:21:24 +00:00
|
|
|
|
|
|
|
<-processing
|
2015-03-30 17:13:36 +00:00
|
|
|
query1.Cancel()
|
2015-08-10 12:21:24 +00:00
|
|
|
block <- struct{}{}
|
|
|
|
<-processing
|
2015-03-30 17:13:36 +00:00
|
|
|
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.Error(t, res.Err, "expected cancellation error for query1 but got none")
|
|
|
|
assert.Equal(t, errQueryCanceled, res.Err)
|
2015-03-30 17:13:36 +00:00
|
|
|
|
2015-08-10 12:21:24 +00:00
|
|
|
// Canceling a query before starting it must have no effect.
|
|
|
|
query2 := engine.newTestQuery(func(ctx context.Context) error {
|
|
|
|
return contextDone(ctx, "test statement execution")
|
|
|
|
})
|
|
|
|
|
2015-03-30 17:13:36 +00:00
|
|
|
query2.Cancel()
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
2016-09-15 11:52:50 +00:00
|
|
|
res = query2.Exec(ctx)
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.NoError(t, res.Err)
|
2015-03-30 17:13:36 +00:00
|
|
|
}
|
|
|
|
|
2018-02-15 12:08:00 +00:00
|
|
|
// errQuerier implements storage.Querier which always returns error.
|
|
|
|
type errQuerier struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2020-06-09 16:57:31 +00:00
|
|
|
func (q *errQuerier) Select(bool, *storage.SelectHints, ...*labels.Matcher) storage.SeriesSet {
|
|
|
|
return errSeriesSet{err: q.err}
|
2018-02-15 12:08:00 +00:00
|
|
|
}
|
2020-03-12 09:36:09 +00:00
|
|
|
func (*errQuerier) LabelValues(string) ([]string, storage.Warnings, error) { return nil, nil, nil }
|
|
|
|
func (*errQuerier) LabelNames() ([]string, storage.Warnings, error) { return nil, nil, nil }
|
|
|
|
func (*errQuerier) Close() error { return nil }
|
2018-02-15 12:08:00 +00:00
|
|
|
|
|
|
|
// errSeriesSet implements storage.SeriesSet which always returns error.
|
|
|
|
type errSeriesSet struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2020-06-09 16:57:31 +00:00
|
|
|
func (errSeriesSet) Next() bool { return false }
|
|
|
|
func (errSeriesSet) At() storage.Series { return nil }
|
|
|
|
func (e errSeriesSet) Err() error { return e.err }
|
|
|
|
func (e errSeriesSet) Warnings() storage.Warnings { return nil }
|
2018-02-15 12:08:00 +00:00
|
|
|
|
|
|
|
func TestQueryError(t *testing.T) {
|
2018-10-02 11:59:19 +00:00
|
|
|
opts := EngineOpts{
|
2020-01-28 20:38:49 +00:00
|
|
|
Logger: nil,
|
|
|
|
Reg: nil,
|
|
|
|
MaxSamples: 10,
|
|
|
|
Timeout: 10 * time.Second,
|
2018-10-02 11:59:19 +00:00
|
|
|
}
|
|
|
|
engine := NewEngine(opts)
|
2019-03-25 23:01:12 +00:00
|
|
|
errStorage := ErrStorage{errors.New("storage error")}
|
2018-02-15 12:08:00 +00:00
|
|
|
queryable := storage.QueryableFunc(func(ctx context.Context, mint, maxt int64) (storage.Querier, error) {
|
|
|
|
return &errQuerier{err: errStorage}, nil
|
|
|
|
})
|
|
|
|
ctx, cancelCtx := context.WithCancel(context.Background())
|
|
|
|
defer cancelCtx()
|
|
|
|
|
|
|
|
vectorQuery, err := engine.NewInstantQuery(queryable, "foo", time.Unix(1, 0))
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.NoError(t, err)
|
2019-10-10 00:06:53 +00:00
|
|
|
|
2018-02-15 12:08:00 +00:00
|
|
|
res := vectorQuery.Exec(ctx)
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.Error(t, res.Err, "expected error on failed select but got none")
|
|
|
|
assert.True(t, errors.Is(res.Err, errStorage), "expected error doesn't match")
|
2018-02-15 12:08:00 +00:00
|
|
|
|
|
|
|
matrixQuery, err := engine.NewInstantQuery(queryable, "foo[1m]", time.Unix(1, 0))
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.NoError(t, err)
|
2019-10-10 00:06:53 +00:00
|
|
|
|
2018-02-15 12:08:00 +00:00
|
|
|
res = matrixQuery.Exec(ctx)
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.Error(t, res.Err, "expected error on failed select but got none")
|
|
|
|
assert.True(t, errors.Is(res.Err, errStorage), "expected error doesn't match")
|
2018-02-15 12:08:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 22:05:43 +00:00
|
|
|
type noopHintRecordingQueryable struct {
|
|
|
|
hints []*storage.SelectHints
|
2019-04-17 12:52:41 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 22:05:43 +00:00
|
|
|
func (h *noopHintRecordingQueryable) Querier(context.Context, int64, int64) (storage.Querier, error) {
|
|
|
|
return &hintRecordingQuerier{Querier: &errQuerier{}, h: h}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type hintRecordingQuerier struct {
|
|
|
|
storage.Querier
|
2019-04-17 12:52:41 +00:00
|
|
|
|
2020-07-23 22:05:43 +00:00
|
|
|
h *noopHintRecordingQueryable
|
2019-04-17 12:52:41 +00:00
|
|
|
}
|
2020-07-23 22:05:43 +00:00
|
|
|
|
|
|
|
func (h *hintRecordingQuerier) Select(sortSeries bool, hints *storage.SelectHints, matchers ...*labels.Matcher) storage.SeriesSet {
|
|
|
|
h.h.hints = append(h.h.hints, hints)
|
|
|
|
return h.Querier.Select(sortSeries, hints, matchers...)
|
2019-06-17 07:31:17 +00:00
|
|
|
}
|
2019-04-17 12:52:41 +00:00
|
|
|
|
2020-07-23 22:05:43 +00:00
|
|
|
func TestSelectHintsSetCorrectly(t *testing.T) {
|
2019-04-17 12:52:41 +00:00
|
|
|
opts := EngineOpts{
|
2020-02-09 23:58:23 +00:00
|
|
|
Logger: nil,
|
|
|
|
Reg: nil,
|
|
|
|
MaxSamples: 10,
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
LookbackDelta: 5 * time.Second,
|
2019-04-17 12:52:41 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 22:05:43 +00:00
|
|
|
for _, tc := range []struct {
|
2019-04-17 12:52:41 +00:00
|
|
|
query string
|
|
|
|
|
2020-07-23 22:05:43 +00:00
|
|
|
// All times are in milliseconds.
|
2019-04-17 12:52:41 +00:00
|
|
|
start int64
|
|
|
|
end int64
|
|
|
|
|
2020-07-23 22:05:43 +00:00
|
|
|
// TODO(bwplotka): Add support for better hints when subquerying.
|
|
|
|
expected []*storage.SelectHints
|
2019-04-17 12:52:41 +00:00
|
|
|
}{{
|
2020-07-23 22:05:43 +00:00
|
|
|
query: "foo", start: 10000,
|
|
|
|
expected: []*storage.SelectHints{
|
|
|
|
{Start: 5000, End: 10000},
|
|
|
|
},
|
2019-04-17 12:52:41 +00:00
|
|
|
}, {
|
2020-07-23 22:05:43 +00:00
|
|
|
query: "foo[2m]", start: 200000,
|
|
|
|
expected: []*storage.SelectHints{
|
|
|
|
{Start: 80000, End: 200000, Range: 120000},
|
|
|
|
},
|
2019-04-17 12:52:41 +00:00
|
|
|
}, {
|
2020-07-23 22:05:43 +00:00
|
|
|
query: "foo[2m] offset 2m", start: 300000,
|
|
|
|
expected: []*storage.SelectHints{
|
|
|
|
{Start: 60000, End: 180000, Range: 120000},
|
|
|
|
},
|
2019-04-17 12:52:41 +00:00
|
|
|
}, {
|
2020-07-23 22:05:43 +00:00
|
|
|
query: "foo[2m:1s]", start: 300000,
|
|
|
|
expected: []*storage.SelectHints{
|
|
|
|
{Start: 175000, End: 300000},
|
|
|
|
},
|
2019-04-17 12:52:41 +00:00
|
|
|
}, {
|
2020-07-23 22:05:43 +00:00
|
|
|
query: "count_over_time(foo[2m:1s])", start: 300000,
|
|
|
|
expected: []*storage.SelectHints{
|
|
|
|
{Start: 175000, End: 300000, Func: "count_over_time"},
|
|
|
|
},
|
2019-04-17 12:52:41 +00:00
|
|
|
}, {
|
2020-07-23 22:05:43 +00:00
|
|
|
query: "count_over_time(foo[2m:1s] offset 10s)", start: 300000,
|
|
|
|
expected: []*storage.SelectHints{
|
2020-08-11 06:21:39 +00:00
|
|
|
{Start: 165000, End: 290000, Func: "count_over_time"},
|
2020-07-23 22:05:43 +00:00
|
|
|
},
|
2019-04-17 12:52:41 +00:00
|
|
|
}, {
|
2020-07-23 22:05:43 +00:00
|
|
|
query: "count_over_time((foo offset 10s)[2m:1s] offset 10s)", start: 300000,
|
|
|
|
expected: []*storage.SelectHints{
|
2020-08-11 06:21:39 +00:00
|
|
|
{Start: 155000, End: 280000, Func: "count_over_time"},
|
2020-07-23 22:05:43 +00:00
|
|
|
},
|
2019-04-17 12:52:41 +00:00
|
|
|
}, {
|
|
|
|
|
2020-07-23 22:05:43 +00:00
|
|
|
query: "foo", start: 10000, end: 20000,
|
|
|
|
expected: []*storage.SelectHints{
|
|
|
|
{Start: 5000, End: 20000, Step: 1000},
|
|
|
|
},
|
2019-04-17 12:52:41 +00:00
|
|
|
}, {
|
2020-07-23 22:05:43 +00:00
|
|
|
query: "rate(foo[2m])", start: 200000, end: 500000,
|
|
|
|
expected: []*storage.SelectHints{
|
|
|
|
{Start: 80000, End: 500000, Range: 120000, Func: "rate", Step: 1000},
|
|
|
|
},
|
2019-04-17 12:52:41 +00:00
|
|
|
}, {
|
2020-07-23 22:05:43 +00:00
|
|
|
query: "rate(foo[2m] offset 2m)", start: 300000, end: 500000,
|
|
|
|
expected: []*storage.SelectHints{
|
|
|
|
{Start: 60000, End: 380000, Range: 120000, Func: "rate", Step: 1000},
|
|
|
|
},
|
2019-04-17 12:52:41 +00:00
|
|
|
}, {
|
2020-07-23 22:05:43 +00:00
|
|
|
query: "rate(foo[2m:1s])", start: 300000, end: 500000,
|
|
|
|
expected: []*storage.SelectHints{
|
|
|
|
{Start: 175000, End: 500000, Func: "rate", Step: 1000},
|
|
|
|
},
|
2019-04-17 12:52:41 +00:00
|
|
|
}, {
|
2020-07-23 22:05:43 +00:00
|
|
|
query: "count_over_time(foo[2m:1s])", start: 300000, end: 500000,
|
|
|
|
expected: []*storage.SelectHints{
|
|
|
|
{Start: 175000, End: 500000, Func: "count_over_time", Step: 1000},
|
|
|
|
},
|
2019-04-17 12:52:41 +00:00
|
|
|
}, {
|
2020-07-23 22:05:43 +00:00
|
|
|
query: "count_over_time(foo[2m:1s] offset 10s)", start: 300000, end: 500000,
|
|
|
|
expected: []*storage.SelectHints{
|
2020-08-11 06:21:39 +00:00
|
|
|
{Start: 165000, End: 490000, Func: "count_over_time", Step: 1000},
|
2020-07-23 22:05:43 +00:00
|
|
|
},
|
2019-04-17 12:52:41 +00:00
|
|
|
}, {
|
2020-07-23 22:05:43 +00:00
|
|
|
query: "count_over_time((foo offset 10s)[2m:1s] offset 10s)", start: 300000, end: 500000,
|
|
|
|
expected: []*storage.SelectHints{
|
2020-08-11 06:21:39 +00:00
|
|
|
{Start: 155000, End: 480000, Func: "count_over_time", Step: 1000},
|
2020-07-23 22:05:43 +00:00
|
|
|
},
|
2019-12-05 14:06:28 +00:00
|
|
|
}, {
|
2020-07-23 22:05:43 +00:00
|
|
|
query: "sum by (dim1) (foo)", start: 10000,
|
|
|
|
expected: []*storage.SelectHints{
|
|
|
|
{Start: 5000, End: 10000, Func: "sum", By: true, Grouping: []string{"dim1"}},
|
|
|
|
},
|
2019-12-05 14:06:28 +00:00
|
|
|
}, {
|
2020-07-23 22:05:43 +00:00
|
|
|
query: "sum without (dim1) (foo)", start: 10000,
|
|
|
|
expected: []*storage.SelectHints{
|
|
|
|
{Start: 5000, End: 10000, Func: "sum", Grouping: []string{"dim1"}},
|
|
|
|
},
|
2019-12-05 14:06:28 +00:00
|
|
|
}, {
|
2020-07-23 22:05:43 +00:00
|
|
|
query: "sum by (dim1) (avg_over_time(foo[1s]))", start: 10000,
|
|
|
|
expected: []*storage.SelectHints{
|
|
|
|
{Start: 9000, End: 10000, Func: "avg_over_time", Range: 1000},
|
|
|
|
},
|
2019-12-05 14:06:28 +00:00
|
|
|
}, {
|
2020-07-23 22:05:43 +00:00
|
|
|
query: "sum by (dim1) (max by (dim2) (foo))", start: 10000,
|
|
|
|
expected: []*storage.SelectHints{
|
|
|
|
{Start: 5000, End: 10000, Func: "max", By: true, Grouping: []string{"dim2"}},
|
|
|
|
},
|
2019-12-05 14:06:28 +00:00
|
|
|
}, {
|
2020-07-23 22:05:43 +00:00
|
|
|
query: "(max by (dim1) (foo))[5s:1s]", start: 10000,
|
|
|
|
expected: []*storage.SelectHints{
|
|
|
|
{Start: 0, End: 10000, Func: "max", By: true, Grouping: []string{"dim1"}},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
query: "(sum(http_requests{group=~\"p.*\"})+max(http_requests{group=~\"c.*\"}))[20s:5s]", start: 120000,
|
|
|
|
expected: []*storage.SelectHints{
|
|
|
|
{Start: 95000, End: 120000, Func: "sum", By: true},
|
|
|
|
{Start: 95000, End: 120000, Func: "max", By: true},
|
|
|
|
},
|
|
|
|
}} {
|
|
|
|
t.Run(tc.query, func(t *testing.T) {
|
|
|
|
engine := NewEngine(opts)
|
|
|
|
hintsRecorder := &noopHintRecordingQueryable{}
|
|
|
|
|
|
|
|
var (
|
|
|
|
query Query
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if tc.end == 0 {
|
|
|
|
query, err = engine.NewInstantQuery(hintsRecorder, tc.query, timestamp.Time(tc.start))
|
|
|
|
} else {
|
|
|
|
query, err = engine.NewRangeQuery(hintsRecorder, tc.query, timestamp.Time(tc.start), timestamp.Time(tc.end), time.Second)
|
|
|
|
}
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.NoError(t, err)
|
2019-04-17 12:52:41 +00:00
|
|
|
|
2020-07-23 22:05:43 +00:00
|
|
|
res := query.Exec(context.Background())
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.NoError(t, res.Err)
|
2020-07-23 22:05:43 +00:00
|
|
|
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.Equal(t, tc.expected, hintsRecorder.hints)
|
2020-07-23 22:05:43 +00:00
|
|
|
})
|
2019-04-17 12:52:41 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 17:13:36 +00:00
|
|
|
func TestEngineShutdown(t *testing.T) {
|
2018-10-02 11:59:19 +00:00
|
|
|
opts := EngineOpts{
|
2020-01-28 20:38:49 +00:00
|
|
|
Logger: nil,
|
|
|
|
Reg: nil,
|
|
|
|
MaxSamples: 10,
|
|
|
|
Timeout: 10 * time.Second,
|
2018-10-02 11:59:19 +00:00
|
|
|
}
|
|
|
|
engine := NewEngine(opts)
|
2016-09-15 22:58:06 +00:00
|
|
|
ctx, cancelCtx := context.WithCancel(context.Background())
|
2015-03-30 17:13:36 +00:00
|
|
|
|
2015-08-10 12:21:24 +00:00
|
|
|
block := make(chan struct{})
|
|
|
|
processing := make(chan struct{})
|
|
|
|
|
2015-03-30 17:13:36 +00:00
|
|
|
// Shutdown engine on first handler execution. Should handler execution ever become
|
|
|
|
// concurrent this test has to be adjusted accordingly.
|
2015-08-10 12:21:24 +00:00
|
|
|
f := func(ctx context.Context) error {
|
|
|
|
processing <- struct{}{}
|
|
|
|
<-block
|
|
|
|
return contextDone(ctx, "test statement execution")
|
|
|
|
}
|
|
|
|
query1 := engine.newTestQuery(f)
|
2015-03-30 17:13:36 +00:00
|
|
|
|
2015-04-29 09:08:56 +00:00
|
|
|
// Stopping the engine must cancel the base context. While executing queries is
|
|
|
|
// still possible, their context is canceled from the beginning and execution should
|
2015-03-30 17:13:36 +00:00
|
|
|
// terminate immediately.
|
|
|
|
|
2015-08-10 12:21:24 +00:00
|
|
|
var res *Result
|
|
|
|
go func() {
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
2016-09-15 11:52:50 +00:00
|
|
|
res = query1.Exec(ctx)
|
2015-08-10 12:21:24 +00:00
|
|
|
processing <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-processing
|
2016-09-15 22:58:06 +00:00
|
|
|
cancelCtx()
|
2015-08-10 12:21:24 +00:00
|
|
|
block <- struct{}{}
|
|
|
|
<-processing
|
|
|
|
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.Error(t, res.Err, "expected error on shutdown during query but got none")
|
|
|
|
assert.Equal(t, errQueryCanceled, res.Err)
|
2015-03-30 17:13:36 +00:00
|
|
|
|
2015-08-10 12:21:24 +00:00
|
|
|
query2 := engine.newTestQuery(func(context.Context) error {
|
|
|
|
t.Fatalf("reached query execution unexpectedly")
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// The second query is started after the engine shut down. It must
|
|
|
|
// be canceled immediately.
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
2016-09-15 11:52:50 +00:00
|
|
|
res2 := query2.Exec(ctx)
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.Error(t, res2.Err, "expected error on querying with canceled context but got none")
|
2019-10-10 00:06:53 +00:00
|
|
|
|
|
|
|
var e ErrQueryCanceled
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.True(t, errors.As(res2.Err, &e), "expected cancellation error but got: %s", res2.Err)
|
2015-03-30 17:13:36 +00:00
|
|
|
}
|
2015-08-19 13:28:53 +00:00
|
|
|
|
2017-04-18 13:51:10 +00:00
|
|
|
func TestEngineEvalStmtTimestamps(t *testing.T) {
|
|
|
|
test, err := NewTest(t, `
|
|
|
|
load 10s
|
|
|
|
metric 1 2
|
|
|
|
`)
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.NoError(t, err)
|
2018-03-08 16:48:11 +00:00
|
|
|
defer test.Close()
|
|
|
|
|
2017-04-18 13:51:10 +00:00
|
|
|
err = test.Run()
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.NoError(t, err)
|
2017-04-18 13:51:10 +00:00
|
|
|
|
|
|
|
cases := []struct {
|
2018-09-13 09:57:36 +00:00
|
|
|
Query string
|
2020-02-03 16:32:23 +00:00
|
|
|
Result parser.Value
|
2018-09-13 09:57:36 +00:00
|
|
|
Start time.Time
|
|
|
|
End time.Time
|
|
|
|
Interval time.Duration
|
|
|
|
ShouldError bool
|
2017-04-18 13:51:10 +00:00
|
|
|
}{
|
|
|
|
// Instant queries.
|
|
|
|
{
|
|
|
|
Query: "1",
|
|
|
|
Result: Scalar{V: 1, T: 1000},
|
|
|
|
Start: time.Unix(1, 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: "metric",
|
|
|
|
Result: Vector{
|
|
|
|
Sample{Point: Point{V: 1, T: 1000},
|
|
|
|
Metric: labels.FromStrings("__name__", "metric")},
|
|
|
|
},
|
|
|
|
Start: time.Unix(1, 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: "metric[20s]",
|
|
|
|
Result: Matrix{Series{
|
|
|
|
Points: []Point{{V: 1, T: 0}, {V: 2, T: 10000}},
|
|
|
|
Metric: labels.FromStrings("__name__", "metric")},
|
|
|
|
},
|
|
|
|
Start: time.Unix(10, 0),
|
|
|
|
},
|
|
|
|
// Range queries.
|
|
|
|
{
|
|
|
|
Query: "1",
|
|
|
|
Result: Matrix{Series{
|
|
|
|
Points: []Point{{V: 1, T: 0}, {V: 1, T: 1000}, {V: 1, T: 2000}},
|
|
|
|
Metric: labels.FromStrings()},
|
|
|
|
},
|
|
|
|
Start: time.Unix(0, 0),
|
|
|
|
End: time.Unix(2, 0),
|
|
|
|
Interval: time.Second,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: "metric",
|
|
|
|
Result: Matrix{Series{
|
2017-06-13 05:22:27 +00:00
|
|
|
Points: []Point{{V: 1, T: 0}, {V: 1, T: 1000}, {V: 1, T: 2000}},
|
2017-04-18 13:51:10 +00:00
|
|
|
Metric: labels.FromStrings("__name__", "metric")},
|
|
|
|
},
|
|
|
|
Start: time.Unix(0, 0),
|
|
|
|
End: time.Unix(2, 0),
|
|
|
|
Interval: time.Second,
|
|
|
|
},
|
2017-06-13 05:22:27 +00:00
|
|
|
{
|
|
|
|
Query: "metric",
|
|
|
|
Result: Matrix{Series{
|
|
|
|
Points: []Point{{V: 1, T: 0}, {V: 1, T: 5000}, {V: 2, T: 10000}},
|
|
|
|
Metric: labels.FromStrings("__name__", "metric")},
|
|
|
|
},
|
|
|
|
Start: time.Unix(0, 0),
|
|
|
|
End: time.Unix(10, 0),
|
|
|
|
Interval: 5 * time.Second,
|
|
|
|
},
|
2018-09-13 09:57:36 +00:00
|
|
|
{
|
|
|
|
Query: `count_values("wrong label!", metric)`,
|
|
|
|
ShouldError: true,
|
|
|
|
},
|
2017-04-18 13:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
var err error
|
|
|
|
var qry Query
|
|
|
|
if c.Interval == 0 {
|
2018-01-09 16:44:23 +00:00
|
|
|
qry, err = test.QueryEngine().NewInstantQuery(test.Queryable(), c.Query, c.Start)
|
2017-04-18 13:51:10 +00:00
|
|
|
} else {
|
2018-01-09 16:44:23 +00:00
|
|
|
qry, err = test.QueryEngine().NewRangeQuery(test.Queryable(), c.Query, c.Start, c.End, c.Interval)
|
2017-04-18 13:51:10 +00:00
|
|
|
}
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.NoError(t, err)
|
2019-10-10 00:06:53 +00:00
|
|
|
|
2017-04-18 13:51:10 +00:00
|
|
|
res := qry.Exec(test.Context())
|
2018-09-13 09:57:36 +00:00
|
|
|
if c.ShouldError {
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.Error(t, res.Err, "expected error for the query %q", c.Query)
|
2018-09-13 09:57:36 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-10-10 00:06:53 +00:00
|
|
|
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.NoError(t, res.Err)
|
|
|
|
assert.Equal(t, c.Result, res.Value, "query %q failed", c.Query)
|
2017-04-18 13:51:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-02 11:59:19 +00:00
|
|
|
func TestMaxQuerySamples(t *testing.T) {
|
|
|
|
test, err := NewTest(t, `
|
|
|
|
load 10s
|
|
|
|
metric 1 2
|
2020-05-28 12:36:30 +00:00
|
|
|
bigmetric{a="1"} 1 2
|
|
|
|
bigmetric{a="2"} 1 2
|
2018-10-02 11:59:19 +00:00
|
|
|
`)
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.NoError(t, err)
|
2018-10-02 11:59:19 +00:00
|
|
|
defer test.Close()
|
|
|
|
|
|
|
|
err = test.Run()
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.NoError(t, err)
|
2018-10-02 11:59:19 +00:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
Query string
|
|
|
|
MaxSamples int
|
|
|
|
Result Result
|
|
|
|
Start time.Time
|
|
|
|
End time.Time
|
|
|
|
Interval time.Duration
|
|
|
|
}{
|
|
|
|
// Instant queries.
|
|
|
|
{
|
|
|
|
Query: "1",
|
|
|
|
MaxSamples: 1,
|
|
|
|
Result: Result{
|
|
|
|
nil,
|
2018-11-30 14:27:12 +00:00
|
|
|
Scalar{V: 1, T: 1000},
|
|
|
|
nil},
|
2018-10-02 11:59:19 +00:00
|
|
|
Start: time.Unix(1, 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: "1",
|
|
|
|
MaxSamples: 0,
|
|
|
|
Result: Result{
|
|
|
|
ErrTooManySamples(env),
|
|
|
|
nil,
|
2018-11-30 14:27:12 +00:00
|
|
|
nil,
|
2018-10-02 11:59:19 +00:00
|
|
|
},
|
|
|
|
Start: time.Unix(1, 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: "metric",
|
|
|
|
MaxSamples: 0,
|
|
|
|
Result: Result{
|
|
|
|
ErrTooManySamples(env),
|
|
|
|
nil,
|
2018-11-30 14:27:12 +00:00
|
|
|
nil,
|
2018-10-02 11:59:19 +00:00
|
|
|
},
|
|
|
|
Start: time.Unix(1, 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: "metric",
|
|
|
|
MaxSamples: 1,
|
|
|
|
Result: Result{
|
|
|
|
nil,
|
|
|
|
Vector{
|
|
|
|
Sample{Point: Point{V: 1, T: 1000},
|
|
|
|
Metric: labels.FromStrings("__name__", "metric")},
|
|
|
|
},
|
2018-11-30 14:27:12 +00:00
|
|
|
nil,
|
2018-10-02 11:59:19 +00:00
|
|
|
},
|
|
|
|
Start: time.Unix(1, 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: "metric[20s]",
|
|
|
|
MaxSamples: 2,
|
|
|
|
Result: Result{
|
|
|
|
nil,
|
|
|
|
Matrix{Series{
|
|
|
|
Points: []Point{{V: 1, T: 0}, {V: 2, T: 10000}},
|
|
|
|
Metric: labels.FromStrings("__name__", "metric")},
|
|
|
|
},
|
2018-11-30 14:27:12 +00:00
|
|
|
nil,
|
2018-10-02 11:59:19 +00:00
|
|
|
},
|
|
|
|
Start: time.Unix(10, 0),
|
|
|
|
},
|
2018-12-22 13:47:13 +00:00
|
|
|
{
|
|
|
|
Query: "rate(metric[20s])",
|
|
|
|
MaxSamples: 3,
|
|
|
|
Result: Result{
|
|
|
|
nil,
|
|
|
|
Vector{
|
|
|
|
Sample{
|
|
|
|
Point: Point{V: 0.1, T: 10000},
|
|
|
|
Metric: labels.Labels{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
Start: time.Unix(10, 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: "metric[20s:5s]",
|
|
|
|
MaxSamples: 3,
|
|
|
|
Result: Result{
|
|
|
|
nil,
|
|
|
|
Matrix{Series{
|
|
|
|
Points: []Point{{V: 1, T: 0}, {V: 1, T: 5000}, {V: 2, T: 10000}},
|
|
|
|
Metric: labels.FromStrings("__name__", "metric")},
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
Start: time.Unix(10, 0),
|
|
|
|
},
|
2018-10-02 11:59:19 +00:00
|
|
|
{
|
|
|
|
Query: "metric[20s]",
|
|
|
|
MaxSamples: 0,
|
|
|
|
Result: Result{
|
|
|
|
ErrTooManySamples(env),
|
|
|
|
nil,
|
2018-11-30 14:27:12 +00:00
|
|
|
nil,
|
2018-10-02 11:59:19 +00:00
|
|
|
},
|
|
|
|
Start: time.Unix(10, 0),
|
|
|
|
},
|
|
|
|
// Range queries.
|
|
|
|
{
|
|
|
|
Query: "1",
|
|
|
|
MaxSamples: 3,
|
|
|
|
Result: Result{
|
|
|
|
nil,
|
|
|
|
Matrix{Series{
|
|
|
|
Points: []Point{{V: 1, T: 0}, {V: 1, T: 1000}, {V: 1, T: 2000}},
|
|
|
|
Metric: labels.FromStrings()},
|
|
|
|
},
|
2018-11-30 14:27:12 +00:00
|
|
|
nil,
|
2018-10-02 11:59:19 +00:00
|
|
|
},
|
|
|
|
Start: time.Unix(0, 0),
|
|
|
|
End: time.Unix(2, 0),
|
|
|
|
Interval: time.Second,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: "1",
|
|
|
|
MaxSamples: 0,
|
|
|
|
Result: Result{
|
|
|
|
ErrTooManySamples(env),
|
|
|
|
nil,
|
2018-11-30 14:27:12 +00:00
|
|
|
nil,
|
2018-10-02 11:59:19 +00:00
|
|
|
},
|
|
|
|
Start: time.Unix(0, 0),
|
|
|
|
End: time.Unix(2, 0),
|
|
|
|
Interval: time.Second,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: "metric",
|
|
|
|
MaxSamples: 3,
|
|
|
|
Result: Result{
|
|
|
|
nil,
|
|
|
|
Matrix{Series{
|
|
|
|
Points: []Point{{V: 1, T: 0}, {V: 1, T: 1000}, {V: 1, T: 2000}},
|
|
|
|
Metric: labels.FromStrings("__name__", "metric")},
|
|
|
|
},
|
2018-11-30 14:27:12 +00:00
|
|
|
nil,
|
2018-10-02 11:59:19 +00:00
|
|
|
},
|
|
|
|
Start: time.Unix(0, 0),
|
|
|
|
End: time.Unix(2, 0),
|
|
|
|
Interval: time.Second,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: "metric",
|
|
|
|
MaxSamples: 2,
|
|
|
|
Result: Result{
|
|
|
|
ErrTooManySamples(env),
|
|
|
|
nil,
|
2018-11-30 14:27:12 +00:00
|
|
|
nil,
|
2018-10-02 11:59:19 +00:00
|
|
|
},
|
|
|
|
Start: time.Unix(0, 0),
|
|
|
|
End: time.Unix(2, 0),
|
|
|
|
Interval: time.Second,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: "metric",
|
|
|
|
MaxSamples: 3,
|
|
|
|
Result: Result{
|
|
|
|
nil,
|
|
|
|
Matrix{Series{
|
|
|
|
Points: []Point{{V: 1, T: 0}, {V: 1, T: 5000}, {V: 2, T: 10000}},
|
|
|
|
Metric: labels.FromStrings("__name__", "metric")},
|
|
|
|
},
|
2018-11-30 14:27:12 +00:00
|
|
|
nil,
|
2018-10-02 11:59:19 +00:00
|
|
|
},
|
|
|
|
Start: time.Unix(0, 0),
|
|
|
|
End: time.Unix(10, 0),
|
|
|
|
Interval: 5 * time.Second,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: "metric",
|
|
|
|
MaxSamples: 2,
|
|
|
|
Result: Result{
|
|
|
|
ErrTooManySamples(env),
|
|
|
|
nil,
|
2018-11-30 14:27:12 +00:00
|
|
|
nil,
|
2018-10-02 11:59:19 +00:00
|
|
|
},
|
|
|
|
Start: time.Unix(0, 0),
|
|
|
|
End: time.Unix(10, 0),
|
|
|
|
Interval: 5 * time.Second,
|
|
|
|
},
|
2020-05-28 12:36:30 +00:00
|
|
|
{
|
|
|
|
Query: "rate(bigmetric[1s])",
|
|
|
|
MaxSamples: 1,
|
|
|
|
Result: Result{
|
|
|
|
nil,
|
|
|
|
Matrix{},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
Start: time.Unix(0, 0),
|
|
|
|
End: time.Unix(10, 0),
|
|
|
|
Interval: 5 * time.Second,
|
|
|
|
},
|
2018-10-02 11:59:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
engine := test.QueryEngine()
|
|
|
|
for _, c := range cases {
|
|
|
|
var err error
|
|
|
|
var qry Query
|
|
|
|
|
|
|
|
engine.maxSamplesPerQuery = c.MaxSamples
|
|
|
|
|
|
|
|
if c.Interval == 0 {
|
|
|
|
qry, err = engine.NewInstantQuery(test.Queryable(), c.Query, c.Start)
|
|
|
|
} else {
|
|
|
|
qry, err = engine.NewRangeQuery(test.Queryable(), c.Query, c.Start, c.End, c.Interval)
|
|
|
|
}
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.NoError(t, err)
|
2019-10-10 00:06:53 +00:00
|
|
|
|
2018-10-02 11:59:19 +00:00
|
|
|
res := qry.Exec(test.Context())
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.Equal(t, c.Result.Err, res.Err)
|
|
|
|
assert.Equal(t, c.Result.Value, res.Value, "query %q failed", c.Query)
|
2018-10-02 11:59:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-19 13:28:53 +00:00
|
|
|
func TestRecoverEvaluatorRuntime(t *testing.T) {
|
2017-08-11 18:45:52 +00:00
|
|
|
ev := &evaluator{logger: log.NewNopLogger()}
|
|
|
|
|
2015-08-19 13:28:53 +00:00
|
|
|
var err error
|
2020-06-09 16:57:31 +00:00
|
|
|
defer ev.recover(nil, &err)
|
2015-08-19 13:28:53 +00:00
|
|
|
|
|
|
|
// Cause a runtime panic.
|
|
|
|
var a []int
|
2019-05-03 13:11:28 +00:00
|
|
|
//nolint:govet
|
2015-08-19 13:28:53 +00:00
|
|
|
a[123] = 1
|
|
|
|
|
|
|
|
if err.Error() != "unexpected error" {
|
|
|
|
t.Fatalf("wrong error message: %q, expected %q", err, "unexpected error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecoverEvaluatorError(t *testing.T) {
|
2017-08-11 18:45:52 +00:00
|
|
|
ev := &evaluator{logger: log.NewNopLogger()}
|
2015-08-19 13:28:53 +00:00
|
|
|
var err error
|
|
|
|
|
2019-03-25 23:01:12 +00:00
|
|
|
e := errors.New("custom error")
|
2015-08-19 13:28:53 +00:00
|
|
|
|
2015-08-26 00:04:01 +00:00
|
|
|
defer func() {
|
|
|
|
if err.Error() != e.Error() {
|
|
|
|
t.Fatalf("wrong error message: %q, expected %q", err, e)
|
|
|
|
}
|
|
|
|
}()
|
2020-06-09 16:57:31 +00:00
|
|
|
defer ev.recover(nil, &err)
|
|
|
|
|
|
|
|
panic(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecoverEvaluatorErrorWithWarnings(t *testing.T) {
|
|
|
|
ev := &evaluator{logger: log.NewNopLogger()}
|
|
|
|
var err error
|
|
|
|
var ws storage.Warnings
|
|
|
|
|
|
|
|
warnings := storage.Warnings{errors.New("custom warning")}
|
|
|
|
e := errWithWarnings{
|
|
|
|
err: errors.New("custom error"),
|
|
|
|
warnings: warnings,
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err.Error() != e.Error() {
|
|
|
|
t.Fatalf("wrong error message: %q, expected %q", err, e)
|
|
|
|
}
|
|
|
|
if len(ws) != len(warnings) && ws[0] != warnings[0] {
|
|
|
|
t.Fatalf("wrong warning message: %q, expected %q", ws[0], warnings[0])
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
defer ev.recover(&ws, &err)
|
2015-08-26 00:04:01 +00:00
|
|
|
|
|
|
|
panic(e)
|
2015-08-19 13:28:53 +00:00
|
|
|
}
|
2018-12-22 13:47:13 +00:00
|
|
|
|
|
|
|
func TestSubquerySelector(t *testing.T) {
|
2020-07-23 22:05:43 +00:00
|
|
|
type caseType struct {
|
|
|
|
Query string
|
|
|
|
Result Result
|
|
|
|
Start time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tst := range []struct {
|
2018-12-22 13:47:13 +00:00
|
|
|
loadString string
|
2020-07-23 22:05:43 +00:00
|
|
|
cases []caseType
|
2018-12-22 13:47:13 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
loadString: `load 10s
|
|
|
|
metric 1 2`,
|
2020-07-23 22:05:43 +00:00
|
|
|
cases: []caseType{
|
2018-12-22 13:47:13 +00:00
|
|
|
{
|
|
|
|
Query: "metric[20s:10s]",
|
|
|
|
Result: Result{
|
|
|
|
nil,
|
|
|
|
Matrix{Series{
|
|
|
|
Points: []Point{{V: 1, T: 0}, {V: 2, T: 10000}},
|
|
|
|
Metric: labels.FromStrings("__name__", "metric")},
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
Start: time.Unix(10, 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: "metric[20s:5s]",
|
|
|
|
Result: Result{
|
|
|
|
nil,
|
|
|
|
Matrix{Series{
|
|
|
|
Points: []Point{{V: 1, T: 0}, {V: 1, T: 5000}, {V: 2, T: 10000}},
|
|
|
|
Metric: labels.FromStrings("__name__", "metric")},
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
Start: time.Unix(10, 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: "metric[20s:5s] offset 2s",
|
|
|
|
Result: Result{
|
|
|
|
nil,
|
|
|
|
Matrix{Series{
|
|
|
|
Points: []Point{{V: 1, T: 0}, {V: 1, T: 5000}, {V: 2, T: 10000}},
|
|
|
|
Metric: labels.FromStrings("__name__", "metric")},
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
Start: time.Unix(12, 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: "metric[20s:5s] offset 6s",
|
|
|
|
Result: Result{
|
|
|
|
nil,
|
|
|
|
Matrix{Series{
|
|
|
|
Points: []Point{{V: 1, T: 0}, {V: 1, T: 5000}, {V: 2, T: 10000}},
|
|
|
|
Metric: labels.FromStrings("__name__", "metric")},
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
Start: time.Unix(20, 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: "metric[20s:5s] offset 4s",
|
|
|
|
Result: Result{
|
|
|
|
nil,
|
|
|
|
Matrix{Series{
|
|
|
|
Points: []Point{{V: 2, T: 15000}, {V: 2, T: 20000}, {V: 2, T: 25000}, {V: 2, T: 30000}},
|
|
|
|
Metric: labels.FromStrings("__name__", "metric")},
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
Start: time.Unix(35, 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: "metric[20s:5s] offset 5s",
|
|
|
|
Result: Result{
|
|
|
|
nil,
|
|
|
|
Matrix{Series{
|
|
|
|
Points: []Point{{V: 2, T: 10000}, {V: 2, T: 15000}, {V: 2, T: 20000}, {V: 2, T: 25000}, {V: 2, T: 30000}},
|
|
|
|
Metric: labels.FromStrings("__name__", "metric")},
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
Start: time.Unix(35, 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: "metric[20s:5s] offset 6s",
|
|
|
|
Result: Result{
|
|
|
|
nil,
|
|
|
|
Matrix{Series{
|
|
|
|
Points: []Point{{V: 2, T: 10000}, {V: 2, T: 15000}, {V: 2, T: 20000}, {V: 2, T: 25000}},
|
|
|
|
Metric: labels.FromStrings("__name__", "metric")},
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
Start: time.Unix(35, 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: "metric[20s:5s] offset 7s",
|
|
|
|
Result: Result{
|
|
|
|
nil,
|
|
|
|
Matrix{Series{
|
|
|
|
Points: []Point{{V: 2, T: 10000}, {V: 2, T: 15000}, {V: 2, T: 20000}, {V: 2, T: 25000}},
|
|
|
|
Metric: labels.FromStrings("__name__", "metric")},
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
Start: time.Unix(35, 0),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
loadString: `load 10s
|
|
|
|
http_requests{job="api-server", instance="0", group="production"} 0+10x1000 100+30x1000
|
|
|
|
http_requests{job="api-server", instance="1", group="production"} 0+20x1000 200+30x1000
|
|
|
|
http_requests{job="api-server", instance="0", group="canary"} 0+30x1000 300+80x1000
|
|
|
|
http_requests{job="api-server", instance="1", group="canary"} 0+40x2000`,
|
2020-07-23 22:05:43 +00:00
|
|
|
cases: []caseType{
|
2018-12-22 13:47:13 +00:00
|
|
|
{ // Normal selector.
|
|
|
|
Query: `http_requests{group=~"pro.*",instance="0"}[30s:10s]`,
|
|
|
|
Result: Result{
|
|
|
|
nil,
|
|
|
|
Matrix{Series{
|
|
|
|
Points: []Point{{V: 9990, T: 9990000}, {V: 10000, T: 10000000}, {V: 100, T: 10010000}, {V: 130, T: 10020000}},
|
|
|
|
Metric: labels.FromStrings("__name__", "http_requests", "job", "api-server", "instance", "0", "group", "production")},
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
Start: time.Unix(10020, 0),
|
|
|
|
},
|
|
|
|
{ // Default step.
|
|
|
|
Query: `http_requests{group=~"pro.*",instance="0"}[5m:]`,
|
|
|
|
Result: Result{
|
|
|
|
nil,
|
|
|
|
Matrix{Series{
|
|
|
|
Points: []Point{{V: 9840, T: 9840000}, {V: 9900, T: 9900000}, {V: 9960, T: 9960000}, {V: 130, T: 10020000}, {V: 310, T: 10080000}},
|
|
|
|
Metric: labels.FromStrings("__name__", "http_requests", "job", "api-server", "instance", "0", "group", "production")},
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
Start: time.Unix(10100, 0),
|
|
|
|
},
|
|
|
|
{ // Checking if high offset (>LookbackDelta) is being taken care of.
|
|
|
|
Query: `http_requests{group=~"pro.*",instance="0"}[5m:] offset 20m`,
|
|
|
|
Result: Result{
|
|
|
|
nil,
|
|
|
|
Matrix{Series{
|
|
|
|
Points: []Point{{V: 8640, T: 8640000}, {V: 8700, T: 8700000}, {V: 8760, T: 8760000}, {V: 8820, T: 8820000}, {V: 8880, T: 8880000}},
|
|
|
|
Metric: labels.FromStrings("__name__", "http_requests", "job", "api-server", "instance", "0", "group", "production")},
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
Start: time.Unix(10100, 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: `rate(http_requests[1m])[15s:5s]`,
|
|
|
|
Result: Result{
|
|
|
|
nil,
|
|
|
|
Matrix{
|
|
|
|
Series{
|
|
|
|
Points: []Point{{V: 3, T: 7985000}, {V: 3, T: 7990000}, {V: 3, T: 7995000}, {V: 3, T: 8000000}},
|
|
|
|
Metric: labels.FromStrings("job", "api-server", "instance", "0", "group", "canary"),
|
|
|
|
},
|
|
|
|
Series{
|
|
|
|
Points: []Point{{V: 4, T: 7985000}, {V: 4, T: 7990000}, {V: 4, T: 7995000}, {V: 4, T: 8000000}},
|
|
|
|
Metric: labels.FromStrings("job", "api-server", "instance", "1", "group", "canary"),
|
|
|
|
},
|
|
|
|
Series{
|
|
|
|
Points: []Point{{V: 1, T: 7985000}, {V: 1, T: 7990000}, {V: 1, T: 7995000}, {V: 1, T: 8000000}},
|
|
|
|
Metric: labels.FromStrings("job", "api-server", "instance", "0", "group", "production"),
|
|
|
|
},
|
|
|
|
Series{
|
|
|
|
Points: []Point{{V: 2, T: 7985000}, {V: 2, T: 7990000}, {V: 2, T: 7995000}, {V: 2, T: 8000000}},
|
|
|
|
Metric: labels.FromStrings("job", "api-server", "instance", "1", "group", "production"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
Start: time.Unix(8000, 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: `sum(http_requests{group=~"pro.*"})[30s:10s]`,
|
|
|
|
Result: Result{
|
|
|
|
nil,
|
|
|
|
Matrix{Series{
|
|
|
|
Points: []Point{{V: 270, T: 90000}, {V: 300, T: 100000}, {V: 330, T: 110000}, {V: 360, T: 120000}},
|
|
|
|
Metric: labels.Labels{}},
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
Start: time.Unix(120, 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: `sum(http_requests)[40s:10s]`,
|
|
|
|
Result: Result{
|
|
|
|
nil,
|
|
|
|
Matrix{Series{
|
|
|
|
Points: []Point{{V: 800, T: 80000}, {V: 900, T: 90000}, {V: 1000, T: 100000}, {V: 1100, T: 110000}, {V: 1200, T: 120000}},
|
|
|
|
Metric: labels.Labels{}},
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
Start: time.Unix(120, 0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: `(sum(http_requests{group=~"p.*"})+sum(http_requests{group=~"c.*"}))[20s:5s]`,
|
|
|
|
Result: Result{
|
|
|
|
nil,
|
|
|
|
Matrix{Series{
|
|
|
|
Points: []Point{{V: 1000, T: 100000}, {V: 1000, T: 105000}, {V: 1100, T: 110000}, {V: 1100, T: 115000}, {V: 1200, T: 120000}},
|
|
|
|
Metric: labels.Labels{}},
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
Start: time.Unix(120, 0),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-07-23 22:05:43 +00:00
|
|
|
} {
|
2020-07-22 13:39:51 +00:00
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
test, err := NewTest(t, tst.loadString)
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.NoError(t, err)
|
2020-07-22 13:39:51 +00:00
|
|
|
defer test.Close()
|
|
|
|
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.NoError(t, test.Run())
|
2020-07-22 13:39:51 +00:00
|
|
|
engine := test.QueryEngine()
|
|
|
|
for _, c := range tst.cases {
|
|
|
|
t.Run(c.Query, func(t *testing.T) {
|
|
|
|
qry, err := engine.NewInstantQuery(test.Queryable(), c.Query, c.Start)
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.NoError(t, err)
|
2020-07-22 13:39:51 +00:00
|
|
|
|
|
|
|
res := qry.Exec(test.Context())
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.Equal(t, c.Result.Err, res.Err)
|
2020-07-22 13:39:51 +00:00
|
|
|
mat := res.Value.(Matrix)
|
|
|
|
sort.Sort(mat)
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.Equal(t, c.Result.Value, mat)
|
2020-07-22 13:39:51 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2018-12-22 13:47:13 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-08 13:28:43 +00:00
|
|
|
|
|
|
|
type FakeQueryLogger struct {
|
|
|
|
closed bool
|
|
|
|
logs []interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFakeQueryLogger() *FakeQueryLogger {
|
|
|
|
return &FakeQueryLogger{
|
|
|
|
closed: false,
|
|
|
|
logs: make([]interface{}, 0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FakeQueryLogger) Close() error {
|
|
|
|
f.closed = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FakeQueryLogger) Log(l ...interface{}) error {
|
|
|
|
f.logs = append(f.logs, l...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQueryLogger_basic(t *testing.T) {
|
|
|
|
opts := EngineOpts{
|
2020-01-28 20:38:49 +00:00
|
|
|
Logger: nil,
|
|
|
|
Reg: nil,
|
|
|
|
MaxSamples: 10,
|
|
|
|
Timeout: 10 * time.Second,
|
2020-01-08 13:28:43 +00:00
|
|
|
}
|
|
|
|
engine := NewEngine(opts)
|
|
|
|
|
|
|
|
queryExec := func() {
|
|
|
|
ctx, cancelCtx := context.WithCancel(context.Background())
|
|
|
|
defer cancelCtx()
|
|
|
|
query := engine.newTestQuery(func(ctx context.Context) error {
|
|
|
|
return contextDone(ctx, "test statement execution")
|
|
|
|
})
|
|
|
|
res := query.Exec(ctx)
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.NoError(t, res.Err)
|
2020-01-08 13:28:43 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 11:45:11 +00:00
|
|
|
// Query works without query log initialized.
|
2020-01-08 13:28:43 +00:00
|
|
|
queryExec()
|
|
|
|
|
|
|
|
f1 := NewFakeQueryLogger()
|
|
|
|
engine.SetQueryLogger(f1)
|
|
|
|
queryExec()
|
2020-01-27 09:53:10 +00:00
|
|
|
for i, field := range []interface{}{"params", map[string]interface{}{"query": "test statement"}} {
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.Equal(t, field, f1.logs[i])
|
2020-01-08 13:28:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
l := len(f1.logs)
|
|
|
|
queryExec()
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.Equal(t, 2*l, len(f1.logs))
|
2020-01-08 13:28:43 +00:00
|
|
|
|
|
|
|
// Test that we close the query logger when unsetting it.
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.True(t, !f1.closed, "expected f1 to be open, got closed")
|
2020-01-08 13:28:43 +00:00
|
|
|
engine.SetQueryLogger(nil)
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.True(t, f1.closed, "expected f1 to be closed, got open")
|
2020-01-08 13:28:43 +00:00
|
|
|
queryExec()
|
|
|
|
|
|
|
|
// Test that we close the query logger when swapping.
|
|
|
|
f2 := NewFakeQueryLogger()
|
|
|
|
f3 := NewFakeQueryLogger()
|
|
|
|
engine.SetQueryLogger(f2)
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.True(t, !f2.closed, "expected f2 to be open, got closed")
|
2020-01-08 13:28:43 +00:00
|
|
|
queryExec()
|
|
|
|
engine.SetQueryLogger(f3)
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.True(t, f2.closed, "expected f2 to be closed, got open")
|
|
|
|
assert.True(t, !f3.closed, "expected f3 to be open, got closed")
|
2020-01-08 13:28:43 +00:00
|
|
|
queryExec()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQueryLogger_fields(t *testing.T) {
|
|
|
|
opts := EngineOpts{
|
2020-01-28 20:38:49 +00:00
|
|
|
Logger: nil,
|
|
|
|
Reg: nil,
|
|
|
|
MaxSamples: 10,
|
|
|
|
Timeout: 10 * time.Second,
|
2020-01-08 13:28:43 +00:00
|
|
|
}
|
|
|
|
engine := NewEngine(opts)
|
|
|
|
|
|
|
|
f1 := NewFakeQueryLogger()
|
|
|
|
engine.SetQueryLogger(f1)
|
|
|
|
|
|
|
|
ctx, cancelCtx := context.WithCancel(context.Background())
|
2020-01-27 09:53:10 +00:00
|
|
|
ctx = NewOriginContext(ctx, map[string]interface{}{"foo": "bar"})
|
2020-01-08 13:28:43 +00:00
|
|
|
defer cancelCtx()
|
|
|
|
query := engine.newTestQuery(func(ctx context.Context) error {
|
|
|
|
return contextDone(ctx, "test statement execution")
|
|
|
|
})
|
|
|
|
|
|
|
|
res := query.Exec(ctx)
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.NoError(t, res.Err)
|
2020-01-08 13:28:43 +00:00
|
|
|
|
|
|
|
expected := []string{"foo", "bar"}
|
|
|
|
for i, field := range expected {
|
|
|
|
v := f1.logs[len(f1.logs)-len(expected)+i].(string)
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.Equal(t, field, v)
|
2020-01-08 13:28:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQueryLogger_error(t *testing.T) {
|
|
|
|
opts := EngineOpts{
|
2020-01-28 20:38:49 +00:00
|
|
|
Logger: nil,
|
|
|
|
Reg: nil,
|
|
|
|
MaxSamples: 10,
|
|
|
|
Timeout: 10 * time.Second,
|
2020-01-08 13:28:43 +00:00
|
|
|
}
|
|
|
|
engine := NewEngine(opts)
|
|
|
|
|
|
|
|
f1 := NewFakeQueryLogger()
|
|
|
|
engine.SetQueryLogger(f1)
|
|
|
|
|
|
|
|
ctx, cancelCtx := context.WithCancel(context.Background())
|
2020-01-27 09:53:10 +00:00
|
|
|
ctx = NewOriginContext(ctx, map[string]interface{}{"foo": "bar"})
|
2020-01-08 13:28:43 +00:00
|
|
|
defer cancelCtx()
|
|
|
|
testErr := errors.New("failure")
|
|
|
|
query := engine.newTestQuery(func(ctx context.Context) error {
|
|
|
|
return testErr
|
|
|
|
})
|
|
|
|
|
|
|
|
res := query.Exec(ctx)
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.Error(t, res.Err, "query should have failed")
|
2020-01-08 13:28:43 +00:00
|
|
|
|
2020-01-27 09:53:10 +00:00
|
|
|
for i, field := range []interface{}{"params", map[string]interface{}{"query": "test statement"}, "error", testErr} {
|
2020-10-22 09:00:08 +00:00
|
|
|
assert.Equal(t, f1.logs[i], field)
|
2020-01-08 13:28:43 +00:00
|
|
|
}
|
|
|
|
}
|