From 2a8a34ea0528b63b891e19ef2beab33040cbd2d9 Mon Sep 17 00:00:00 2001 From: weng zhao Date: Tue, 26 Jan 2021 16:12:04 +0800 Subject: [PATCH] vmalert: add option datasource.queryStep to allow user to address the inconsistency between grafana dashboards(query_range with step 15s usually) and ALERTS (#1027) Co-authored-by: zhao.weng --- app/vmalert/datasource/init.go | 4 +++- app/vmalert/datasource/vm.go | 7 ++++++- app/vmalert/datasource/vm_test.go | 2 +- app/vmalert/remoteread/init.go | 2 +- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/app/vmalert/datasource/init.go b/app/vmalert/datasource/init.go index 6658e3d281..9c9546dea9 100644 --- a/app/vmalert/datasource/init.go +++ b/app/vmalert/datasource/init.go @@ -24,6 +24,8 @@ var ( lookBack = flag.Duration("datasource.lookback", 0, "Lookback defines how far to look into past when evaluating queries. "+ "For example, if datasource.lookback=5m then param \"time\" with value now()-5m will be added to every query.") + queryStep = flag.Duration("datasource.queryStep", 0, "queryStep defines how far a value can fallback to when evaluating queries. "+ + "For example, if datasource.queryStep=15s then param \"step\" with value \"15s\" will be added to every query.") maxIdleConnections = flag.Int("datasource.maxIdleConnections", 100, "Defines the number of idle (keep-alive connections) to configured datasource."+ "Consider to set this value equal to the value: groups_total * group.concurrency. Too low value may result into high number of sockets in TIME_WAIT state.") ) @@ -39,5 +41,5 @@ func Init() (Querier, error) { } tr.MaxIdleConns = *maxIdleConnections c := &http.Client{Transport: tr} - return NewVMStorage(*addr, *basicAuthUsername, *basicAuthPassword, *lookBack, c), nil + return NewVMStorage(*addr, *basicAuthUsername, *basicAuthPassword, *lookBack, *queryStep, c), nil } diff --git a/app/vmalert/datasource/vm.go b/app/vmalert/datasource/vm.go index 7e0bc4ce4b..ae99df121f 100644 --- a/app/vmalert/datasource/vm.go +++ b/app/vmalert/datasource/vm.go @@ -53,18 +53,20 @@ type VMStorage struct { basicAuthUser string basicAuthPass string lookBack time.Duration + queryStep time.Duration } const queryPath = "/api/v1/query?query=" // NewVMStorage is a constructor for VMStorage -func NewVMStorage(baseURL, basicAuthUser, basicAuthPass string, lookBack time.Duration, c *http.Client) *VMStorage { +func NewVMStorage(baseURL, basicAuthUser, basicAuthPass string, lookBack time.Duration, queryStep time.Duration, c *http.Client) *VMStorage { return &VMStorage{ c: c, basicAuthUser: basicAuthUser, basicAuthPass: basicAuthPass, queryURL: strings.TrimSuffix(baseURL, "/") + queryPath, lookBack: lookBack, + queryStep: queryStep, } } @@ -78,6 +80,9 @@ func (s *VMStorage) Query(ctx context.Context, query string) ([]Metric, error) { lookBack := time.Now().Add(-s.lookBack) q += fmt.Sprintf("&time=%d", lookBack.Unix()) } + if s.queryStep > 0 { + q += fmt.Sprintf("&step=%s", s.queryStep.String()) + } req, err := http.NewRequest("POST", q, nil) if err != nil { return nil, err diff --git a/app/vmalert/datasource/vm_test.go b/app/vmalert/datasource/vm_test.go index 4c59129ae6..af99c8421a 100644 --- a/app/vmalert/datasource/vm_test.go +++ b/app/vmalert/datasource/vm_test.go @@ -61,7 +61,7 @@ func TestVMSelectQuery(t *testing.T) { srv := httptest.NewServer(mux) defer srv.Close() - am := NewVMStorage(srv.URL, basicAuthName, basicAuthPass, time.Minute, srv.Client()) + am := NewVMStorage(srv.URL, basicAuthName, basicAuthPass, time.Minute, 0, srv.Client()) if _, err := am.Query(ctx, query); err == nil { t.Fatalf("expected connection error got nil") } diff --git a/app/vmalert/remoteread/init.go b/app/vmalert/remoteread/init.go index dfbcba8f6a..7a0aa32ac5 100644 --- a/app/vmalert/remoteread/init.go +++ b/app/vmalert/remoteread/init.go @@ -35,5 +35,5 @@ func Init() (datasource.Querier, error) { return nil, fmt.Errorf("failed to create transport: %w", err) } c := &http.Client{Transport: tr} - return datasource.NewVMStorage(*addr, *basicAuthUsername, *basicAuthPassword, 0, c), nil + return datasource.NewVMStorage(*addr, *basicAuthUsername, *basicAuthPassword, 0, 0, c), nil }