diff --git a/pkg/alertbackfill/rule_test.go b/pkg/alertbackfill/rule_test.go index 503c53868..dff1d6c1f 100644 --- a/pkg/alertbackfill/rule_test.go +++ b/pkg/alertbackfill/rule_test.go @@ -7,6 +7,7 @@ import ( "time" "github.com/prometheus/common/model" + "github.com/prometheus/common/promslog" "github.com/prometheus/prometheus/model/labels" "github.com/prometheus/prometheus/rules" ) @@ -21,43 +22,46 @@ func TestFindGroupAndAlert(t *testing.T) { "testalert", // name nil, // expression time.Hour, // hold + 0, labels.Labels{ labels.Label{"labelkey", "labelvalue"}, }, // labels - nil, // annotations - nil, // externalLabels - "", // externalURL - false, // restored - nil, // logger + nil, // annotations + nil, // externalLabels + "", // externalURL + false, // restored + promslog.NewNopLogger(), // logger ), rules.NewAlertingRule( "alertWithLabels", // name nil, // expression time.Hour, // hold + 0, labels.Labels{ labels.Label{"labelkey", "labelvalue"}, }, // labels - nil, // annotations - nil, // externalLabels - "", // externalURL - false, // restored - nil, // logger + nil, // annotations + nil, // externalLabels + "", // externalURL + false, // restored + promslog.NewNopLogger(), // logger ), rules.NewAlertingRule( "alertWithLabels", // name nil, // expression time.Hour, // hold + 0, labels.Labels{ labels.Label{"labelkey", "labelvalue2"}, }, // labels - nil, // annotations - nil, // externalLabels - "", // externalURL - false, // restored - nil, // logger + nil, // annotations + nil, // externalLabels + "", // externalURL + false, // restored + promslog.NewNopLogger(), // logger ), }, - Opts: &rules.ManagerOptions{}, + Opts: &rules.ManagerOptions{Logger: promslog.NewNopLogger()}, }), } diff --git a/pkg/promclient/engine_test.go b/pkg/promclient/engine_test.go index 83ea6d7a5..35090ebde 100644 --- a/pkg/promclient/engine_test.go +++ b/pkg/promclient/engine_test.go @@ -9,6 +9,8 @@ import ( v1 "github.com/prometheus/client_golang/api/prometheus/v1" "github.com/prometheus/common/model" "github.com/prometheus/prometheus/promql" + "github.com/prometheus/prometheus/promql/promqltest" + "github.com/prometheus/prometheus/storage" ) func TestEngineAPI(t *testing.T) { @@ -18,15 +20,32 @@ func TestEngineAPI(t *testing.T) { t.Fatal(err) } - test, err := promql.NewTest(t, string(content)) + test, err := promqltest.NewTest(t, string(content)) if err != nil { t.Fatal(err) } - if err := test.Run(); err != nil { + + engineOpts := promql.EngineOpts{ + Logger: nil, + Reg: nil, + MaxSamples: 50000000, + Timeout: 10 * time.Minute, + ActiveQueryTracker: nil, + LookbackDelta: 0, + NoStepSubqueryIntervalFn: func(int64) int64 { return (1 * time.Minute).Milliseconds() }, + EnableAtModifier: true, + EnableNegativeOffset: false, + EnablePerStepStats: false, + EnableDelayedNameRemoval: false, + } + + queryEngine := promql.NewEngine(engineOpts) + + if err := test.Run(queryEngine); err != nil { t.Fatal(err) } - api, err := NewEngineAPI(test.QueryEngine(), test.Queryable()) + api, err := NewEngineAPI(queryEngine, test.Storage().(storage.SampleAndChunkQueryable)) if err != nil { t.Fatal(err) } diff --git a/pkg/promclient/test_server.go b/pkg/promclient/test_server.go index 5ecde88b9..ac3c54da6 100644 --- a/pkg/promclient/test_server.go +++ b/pkg/promclient/test_server.go @@ -1,87 +1,118 @@ package promclient +import ( + "net" + "net/http" + "os" + "testing" + "time" + + "github.com/prometheus/client_golang/api" + clientv1 "github.com/prometheus/client_golang/api/prometheus/v1" + "github.com/prometheus/common/route" + "github.com/prometheus/prometheus/config" + "github.com/prometheus/prometheus/promql" + "github.com/prometheus/prometheus/promql/promqltest" + "github.com/prometheus/prometheus/storage" + v1 "github.com/prometheus/prometheus/web/api/v1" +) + // CreateTestServer simply will create a test HTTP server on the path defined and return // an API client, a clode method, and any error when creating -//TODO Fix it later -//func CreateTestServer(t *testing.T, path string) (API, func(), error) { -// var close func() -// content, err := os.ReadFile(path) -// if err != nil { -// return nil, close, err -// } -// -// test, err := promqltest.NewTestEngine(t, string(content)) -// if err != nil { -// return nil, nil, err -// } -// close = test.Close -// -// // Load the data -// if err := test.Run(); err != nil { -// return nil, close, err -// } -// -// ln, err := net.Listen("tcp", "") -// if err != nil { -// return nil, close, err -// } -// -// // Start up API server for engine -// cfgFunc := func() config.Config { return config.DefaultConfig } -// // Return 503 until ready (for us there isn't much startup, so this might not need to be implemented -// readyFunc := func(f http.HandlerFunc) http.HandlerFunc { return f } -// -// apiRouter := route.New() -// webv1.NewAPI( -// test.QueryEngine(), // Query Engine -// test.Storage().(storage.SampleAndChunkQueryable), // SampleAndChunkQueryable -// nil, //appendable -// nil, // exemplarQueryable -// nil, //factoryTr -// nil, //factoryAr -// nil, -// cfgFunc, -// nil, // flags -// webv1.GlobalURLOptions{ -// ListenAddress: ln.Addr().String(), -// Host: "localhost", -// Scheme: "http", -// }, // global URL options -// readyFunc, // ready -// nil, // local storage -// "", // tsdb dir -// false, // enable admin API -// nil, // logger -// nil, // FactoryRr -// 50000000, // RemoteReadSampleLimit -// 1000, // RemoteReadConcurrencyLimit -// 1048576, // RemoteReadBytesInFrame -// false, // isAgent -// nil, // CORSOrigin -// nil, // runtimeInfo -// nil, // buildInfo -// nil, -// nil, -// nil, // gatherer -// nil, // registerer -// nil, // statsRenderer -// false, -// nil, -// false, -// ).Register(apiRouter.WithPrefix("/api/v1")) -// -// srv := &http.Server{Handler: apiRouter} -// go srv.Serve(ln) // TODO: cancel/stop ability -// close = func() { -// //test.Close() -// srv.Close() -// } -// -// client, err := api.NewClient(api.Config{Address: "http://" + ln.Addr().String()}) -// if err != nil { -// return nil, close, err -// } -// -// return &PromAPIV1{clientv1.NewAPI(client)}, close, nil -//} +func CreateTestServer(t *testing.T, path string) (API, func(), error) { + var close func() + content, err := os.ReadFile(path) + if err != nil { + return nil, close, err + } + + test, err := promqltest.NewTest(t, string(content)) + if err != nil { + return nil, nil, err + } + close = test.Close + + engineOpts := promql.EngineOpts{ + Logger: nil, + Reg: nil, + MaxSamples: 50000000, + Timeout: 10 * time.Minute, + ActiveQueryTracker: nil, + LookbackDelta: 0, + NoStepSubqueryIntervalFn: func(int64) int64 { return (1 * time.Minute).Milliseconds() }, + EnableAtModifier: true, + EnableNegativeOffset: false, + EnablePerStepStats: false, + EnableDelayedNameRemoval: false, + } + + queryEngine := promql.NewEngine(engineOpts) + + // Load the data + if err := test.Run(queryEngine); err != nil { + return nil, close, err + } + + ln, err := net.Listen("tcp", "") + if err != nil { + return nil, close, err + } + + // Start up API server for engine + cfgFunc := func() config.Config { return config.DefaultConfig } + // Return 503 until ready (for us there isn't much startup, so this might not need to be implemented + readyFunc := func(f http.HandlerFunc) http.HandlerFunc { return f } + apiRouter := route.New() + v1.NewAPI( + queryEngine, // Query Engine + test.Storage().(storage.SampleAndChunkQueryable), // SampleAndChunkQueryable + nil, //appendable + nil, // exemplarQueryable + nil, //factoryTr + nil, //factoryAr + nil, + cfgFunc, + nil, // flags + v1.GlobalURLOptions{ + ListenAddress: ln.Addr().String(), + Host: "localhost", + Scheme: "http", + }, // global URL options + readyFunc, // ready + nil, // local storage + "", // tsdb dir + false, // enable admin API + nil, // logger + nil, // FactoryRr + 50000000, // RemoteReadSampleLimit + 1000, // RemoteReadConcurrencyLimit + 1048576, // RemoteReadBytesInFrame + false, // isAgent + nil, // CORSOrigin + nil, // runtimeInfo + nil, // buildInfo + nil, + nil, + nil, // gatherer + nil, // registerer + nil, // statsRenderer + false, + nil, + false, + ).Register(apiRouter.WithPrefix("/api/v1")) + + srv := &http.Server{Handler: apiRouter} + go srv.Serve(ln) // TODO: cancel/stop ability + close = func() { + //test.Close() + srv.Close() + } + + client, err := api.NewClient(api.Config{Address: "http://" + ln.Addr().String()}) + if err != nil { + return nil, close, err + } + + return &PromAPIV1{clientv1.NewAPI(client)}, close, nil +} diff --git a/pkg/promhttputil/merge.go b/pkg/promhttputil/merge.go index df33d14b3..8090b5d9f 100644 --- a/pkg/promhttputil/merge.go +++ b/pkg/promhttputil/merge.go @@ -15,7 +15,7 @@ import ( func WarningsConvert(ws v1.Warnings) annotations.Annotations { w := make(annotations.Annotations, len(ws)) for i, item := range ws { - w[string(i)] = errors.New(item) + w[fmt.Sprint(i)] = errors.New(item) } return w } diff --git a/pkg/proxystorage/proxy_test.go b/pkg/proxystorage/proxy_test.go index 862ab9db6..62386c447 100644 --- a/pkg/proxystorage/proxy_test.go +++ b/pkg/proxystorage/proxy_test.go @@ -114,7 +114,7 @@ func TestNodeReplacer(t *testing.T) { // count_values is both a query downstream *AND* a tree replacement { in: "count_values(\"label\", foo)", - out: "sum by(label) ()", + out: "sum by (label) ()", queries: []string{ "count_values(\"label\", foo) @ 10000", }, diff --git a/pkg/remote/read_test.go b/pkg/remote/read_test.go index f9d5f584a..e521e2f06 100644 --- a/pkg/remote/read_test.go +++ b/pkg/remote/read_test.go @@ -42,7 +42,7 @@ func TestExternalLabelsQuerierSelect(t *testing.T) { externalLabels: model.LabelSet{"region": "europe"}, } want := newSeriesSetFilter(mockSeriesSet{}, q.externalLabels) - have := q.Select(false, nil, matchers...) + have := q.Select(context.TODO(), false, nil, matchers...) if err := have.Err(); err != nil { t.Error(err) } @@ -164,7 +164,7 @@ func (m mockSeriesSet) Err() error { return nil } -func (mockQuerier) Select(bool, *storage.SelectHints, ...*labels.Matcher) storage.SeriesSet { +func (mockQuerier) Select(context.Context, bool, *storage.SelectHints, ...*labels.Matcher) storage.SeriesSet { return mockSeriesSet{} } @@ -199,13 +199,13 @@ func TestPreferLocalStorageFilter(t *testing.T) { for i, test := range tests { f := PreferLocalStorageFilter( - storage.QueryableFunc(func(ctx context.Context, mint, maxt int64) (storage.Querier, error) { + storage.QueryableFunc(func(mint, maxt int64) (storage.Querier, error) { return mockQuerier{ctx: ctx, mint: mint, maxt: maxt}, nil }), func() (int64, error) { return test.localStartTime, nil }, ) - q, err := f.Querier(ctx, test.mint, test.maxt) + q, err := f.Querier(test.mint, test.maxt) if err != nil { t.Fatal(err) } @@ -220,7 +220,7 @@ func TestRequiredMatchersFilter(t *testing.T) { ctx := context.Background() f := RequiredMatchersFilter( - storage.QueryableFunc(func(ctx context.Context, mint, maxt int64) (storage.Querier, error) { + storage.QueryableFunc(func(mint, maxt int64) (storage.Querier, error) { return mockQuerier{ctx: ctx, mint: mint, maxt: maxt}, nil }), []*labels.Matcher{mustNewLabelMatcher(labels.MatchEqual, "special", "label")}, @@ -230,7 +230,7 @@ func TestRequiredMatchersFilter(t *testing.T) { Querier: mockQuerier{ctx: ctx, mint: 0, maxt: 50}, requiredMatchers: []*labels.Matcher{mustNewLabelMatcher(labels.MatchEqual, "special", "label")}, } - have, err := f.Querier(ctx, 0, 50) + have, err := f.Querier(0, 50) if err != nil { t.Fatal(err) } @@ -320,7 +320,7 @@ func TestRequiredLabelsQuerierSelect(t *testing.T) { requiredMatchers: test.requiredMatchers, } - have := q.Select(false, nil, test.matchers...) + have := q.Select(context.TODO(), false, nil, test.matchers...) if err := have.Err(); err != nil { t.Error(err) }