diff --git a/pkg/services/ngalert/state/historian/loki_http.go b/pkg/services/ngalert/state/historian/loki_http.go index ff482397154..003f9ae05d3 100644 --- a/pkg/services/ngalert/state/historian/loki_http.go +++ b/pkg/services/ngalert/state/historian/loki_http.go @@ -17,6 +17,8 @@ import ( "github.com/weaveworks/common/http/client" ) +const defaultPageSize = 5000 + func NewRequester() client.Requester { return &http.Client{} } @@ -222,6 +224,7 @@ func (c *httpLokiClient) setAuthAndTenantHeaders(req *http.Request) { req.Header.Add("X-Scope-OrgID", c.cfg.TenantID) } } + func (c *httpLokiClient) rangeQuery(ctx context.Context, logQL string, start, end int64) (queryRes, error) { // Run the pre-flight checks for the query. if start > end { @@ -234,6 +237,7 @@ func (c *httpLokiClient) rangeQuery(ctx context.Context, logQL string, start, en values.Set("query", logQL) values.Set("start", fmt.Sprintf("%d", start)) values.Set("end", fmt.Sprintf("%d", end)) + values.Set("limit", fmt.Sprintf("%d", defaultPageSize)) queryURL.RawQuery = values.Encode() diff --git a/pkg/services/ngalert/state/historian/loki_http_test.go b/pkg/services/ngalert/state/historian/loki_http_test.go index 25e010227f2..02ebe4cf741 100644 --- a/pkg/services/ngalert/state/historian/loki_http_test.go +++ b/pkg/services/ngalert/state/historian/loki_http_test.go @@ -1,6 +1,7 @@ package historian import ( + "bytes" "context" "encoding/json" "fmt" @@ -116,6 +117,28 @@ func TestLokiHTTPClient(t *testing.T) { exp := fmt.Sprintf(`{"streams": [{"stream": {}, "values": [["%d", "some line"]]}]}`, now.UnixNano()) require.JSONEq(t, exp, sent) }) + + t.Run("range query", func(t *testing.T) { + t.Run("passes along page size", func(t *testing.T) { + req := NewFakeRequester().WithResponse(&http.Response{ + Status: "200 OK", + StatusCode: 200, + Body: io.NopCloser(bytes.NewBufferString(`{}`)), + ContentLength: int64(0), + Header: make(http.Header, 0), + }) + client := createTestLokiClient(req) + now := time.Now().UTC().UnixNano() + q := `{from="state-history"}` + + _, err := client.rangeQuery(context.Background(), q, now-100, now) + + require.NoError(t, err) + params := req.lastRequest.URL.Query() + require.True(t, params.Has("limit"), "query params did not contain 'limit': %#v", params) + require.Equal(t, fmt.Sprint(defaultPageSize), params.Get("limit")) + }) + }) } // This function can be used for local testing, just remove the skip call.