Alerting: Use default page size of 5000 when querying Loki for state history (#66315)

Always specify limit of 5000
This commit is contained in:
Alexander Weaver
2023-04-18 14:31:29 -05:00
committed by GitHub
parent cf7157f683
commit a384194e15
2 changed files with 27 additions and 0 deletions

View File

@ -17,6 +17,8 @@ import (
"github.com/weaveworks/common/http/client" "github.com/weaveworks/common/http/client"
) )
const defaultPageSize = 5000
func NewRequester() client.Requester { func NewRequester() client.Requester {
return &http.Client{} return &http.Client{}
} }
@ -222,6 +224,7 @@ func (c *httpLokiClient) setAuthAndTenantHeaders(req *http.Request) {
req.Header.Add("X-Scope-OrgID", c.cfg.TenantID) req.Header.Add("X-Scope-OrgID", c.cfg.TenantID)
} }
} }
func (c *httpLokiClient) rangeQuery(ctx context.Context, logQL string, start, end int64) (queryRes, error) { func (c *httpLokiClient) rangeQuery(ctx context.Context, logQL string, start, end int64) (queryRes, error) {
// Run the pre-flight checks for the query. // Run the pre-flight checks for the query.
if start > end { if start > end {
@ -234,6 +237,7 @@ func (c *httpLokiClient) rangeQuery(ctx context.Context, logQL string, start, en
values.Set("query", logQL) values.Set("query", logQL)
values.Set("start", fmt.Sprintf("%d", start)) values.Set("start", fmt.Sprintf("%d", start))
values.Set("end", fmt.Sprintf("%d", end)) values.Set("end", fmt.Sprintf("%d", end))
values.Set("limit", fmt.Sprintf("%d", defaultPageSize))
queryURL.RawQuery = values.Encode() queryURL.RawQuery = values.Encode()

View File

@ -1,6 +1,7 @@
package historian package historian
import ( import (
"bytes"
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
@ -116,6 +117,28 @@ func TestLokiHTTPClient(t *testing.T) {
exp := fmt.Sprintf(`{"streams": [{"stream": {}, "values": [["%d", "some line"]]}]}`, now.UnixNano()) exp := fmt.Sprintf(`{"streams": [{"stream": {}, "values": [["%d", "some line"]]}]}`, now.UnixNano())
require.JSONEq(t, exp, sent) 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. // This function can be used for local testing, just remove the skip call.