From f80bf11782382685d82d33b07e014eadb005bbe8 Mon Sep 17 00:00:00 2001 From: Alexander Weaver Date: Tue, 7 Feb 2023 14:26:43 -0600 Subject: [PATCH] Alerting: Make time range query parameters not required when querying Loki (#62985) * Make from and to not required * Move default range calculation up to loki.go --- pkg/services/ngalert/state/historian/loki.go | 15 +++++++++++++-- pkg/services/ngalert/state/historian/loki_http.go | 2 +- .../ngalert/state/historian/loki_http_test.go | 4 ++-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/pkg/services/ngalert/state/historian/loki.go b/pkg/services/ngalert/state/historian/loki.go index 4158887aa1d..a4a63d063a9 100644 --- a/pkg/services/ngalert/state/historian/loki.go +++ b/pkg/services/ngalert/state/historian/loki.go @@ -34,10 +34,12 @@ const ( StateHistoryLabelValue = "state-history" ) +const defaultQueryRange = 6 * time.Hour + type remoteLokiClient interface { ping(context.Context) error push(context.Context, []stream) error - query(ctx context.Context, selectors []Selector, start, end int64) (QueryRes, error) + rangeQuery(ctx context.Context, selectors []Selector, start, end int64) (QueryRes, error) } type RemoteLokiBackend struct { @@ -78,8 +80,17 @@ func (h *RemoteLokiBackend) QueryStates(ctx context.Context, query models.Histor if err != nil { return nil, fmt.Errorf("failed to build the provided selectors: %w", err) } + + now := time.Now().UTC() + if query.To.IsZero() { + query.To = now + } + if query.From.IsZero() { + query.From = now.Add(-defaultQueryRange) + } + // Timestamps are expected in RFC3339Nano. - res, err := h.client.query(ctx, selectors, query.From.UnixNano(), query.To.UnixNano()) + res, err := h.client.rangeQuery(ctx, selectors, query.From.UnixNano(), query.To.UnixNano()) if err != nil { return nil, err } diff --git a/pkg/services/ngalert/state/historian/loki_http.go b/pkg/services/ngalert/state/historian/loki_http.go index 37a87e73add..459a08d64c1 100644 --- a/pkg/services/ngalert/state/historian/loki_http.go +++ b/pkg/services/ngalert/state/historian/loki_http.go @@ -186,7 +186,7 @@ func (c *httpLokiClient) setAuthAndTenantHeaders(req *http.Request) { req.Header.Add("X-Scope-OrgID", c.cfg.TenantID) } } -func (c *httpLokiClient) query(ctx context.Context, selectors []Selector, start, end int64) (QueryRes, error) { +func (c *httpLokiClient) rangeQuery(ctx context.Context, selectors []Selector, start, end int64) (QueryRes, error) { // Run the pre-flight checks for the query. if len(selectors) == 0 { return QueryRes{}, fmt.Errorf("at least one selector required to query") diff --git a/pkg/services/ngalert/state/historian/loki_http_test.go b/pkg/services/ngalert/state/historian/loki_http_test.go index 86b856f3a4e..1c862abc633 100644 --- a/pkg/services/ngalert/state/historian/loki_http_test.go +++ b/pkg/services/ngalert/state/historian/loki_http_test.go @@ -101,7 +101,7 @@ func TestLokiHTTPClient(t *testing.T) { require.NoError(t, err) }) - t.Run("smoke test querying Loki", func(t *testing.T) { + t.Run("smoke test range querying Loki", func(t *testing.T) { url, err := url.Parse("https://logs-prod-eu-west-0.grafana.net") require.NoError(t, err) @@ -127,7 +127,7 @@ func TestLokiHTTPClient(t *testing.T) { end := time.Now().UnixNano() // Authorized request should not fail against Grafana Cloud. - res, err := client.query(context.Background(), selectors, start, end) + res, err := client.rangeQuery(context.Background(), selectors, start, end) require.NoError(t, err) require.NotNil(t, res) })