mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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
This commit is contained in:
parent
42be0e106f
commit
f80bf11782
@ -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
|
||||
}
|
||||
|
@ -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")
|
||||
|
@ -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)
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user