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 <zhao.weng@shopee.com>
This commit is contained in:
weng zhao 2021-01-26 16:12:04 +08:00 committed by Aliaksandr Valialkin
parent fdced59278
commit 2a8a34ea05
4 changed files with 11 additions and 4 deletions

View File

@ -24,6 +24,8 @@ var (
lookBack = flag.Duration("datasource.lookback", 0, "Lookback defines how far to look into past when evaluating queries. "+ 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.") "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."+ 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.") "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 tr.MaxIdleConns = *maxIdleConnections
c := &http.Client{Transport: tr} c := &http.Client{Transport: tr}
return NewVMStorage(*addr, *basicAuthUsername, *basicAuthPassword, *lookBack, c), nil return NewVMStorage(*addr, *basicAuthUsername, *basicAuthPassword, *lookBack, *queryStep, c), nil
} }

View File

@ -53,18 +53,20 @@ type VMStorage struct {
basicAuthUser string basicAuthUser string
basicAuthPass string basicAuthPass string
lookBack time.Duration lookBack time.Duration
queryStep time.Duration
} }
const queryPath = "/api/v1/query?query=" const queryPath = "/api/v1/query?query="
// NewVMStorage is a constructor for VMStorage // 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{ return &VMStorage{
c: c, c: c,
basicAuthUser: basicAuthUser, basicAuthUser: basicAuthUser,
basicAuthPass: basicAuthPass, basicAuthPass: basicAuthPass,
queryURL: strings.TrimSuffix(baseURL, "/") + queryPath, queryURL: strings.TrimSuffix(baseURL, "/") + queryPath,
lookBack: lookBack, 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) lookBack := time.Now().Add(-s.lookBack)
q += fmt.Sprintf("&time=%d", lookBack.Unix()) 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) req, err := http.NewRequest("POST", q, nil)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -61,7 +61,7 @@ func TestVMSelectQuery(t *testing.T) {
srv := httptest.NewServer(mux) srv := httptest.NewServer(mux)
defer srv.Close() 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 { if _, err := am.Query(ctx, query); err == nil {
t.Fatalf("expected connection error got nil") t.Fatalf("expected connection error got nil")
} }

View File

@ -35,5 +35,5 @@ func Init() (datasource.Querier, error) {
return nil, fmt.Errorf("failed to create transport: %w", err) return nil, fmt.Errorf("failed to create transport: %w", err)
} }
c := &http.Client{Transport: tr} c := &http.Client{Transport: tr}
return datasource.NewVMStorage(*addr, *basicAuthUsername, *basicAuthPassword, 0, c), nil return datasource.NewVMStorage(*addr, *basicAuthUsername, *basicAuthPassword, 0, 0, c), nil
} }