Alerting: Add max limit for Loki query size in state history API (#89646)

* add setting for query limit

* update BuildLogQuery to return error if limit is exceeded

* move tests for BuildLogQuery to separate suite
This commit is contained in:
Yuri Tseretyan
2024-06-25 09:20:38 -04:00
committed by GitHub
parent 9a3477dd11
commit 4a5aab54a5
8 changed files with 159 additions and 70 deletions
@@ -45,6 +45,7 @@ var (
type lokiQueryClient interface {
RangeQuery(ctx context.Context, query string, start, end, limit int64) (historian.QueryRes, error)
MaxQuerySize() int
}
// LokiHistorianStore is a read store that queries Loki for alert state history.
@@ -98,8 +99,12 @@ func (r *LokiHistorianStore) Get(ctx context.Context, query *annotations.ItemQue
}
}
logQL, err := historian.BuildLogQuery(buildHistoryQuery(query, accessResources.Dashboards, rule.UID))
logQL, err := historian.BuildLogQuery(buildHistoryQuery(query, accessResources.Dashboards, rule.UID), r.client.MaxQuerySize())
if err != nil {
grafanaErr := errutil.Error{}
if errors.As(err, &grafanaErr) {
return make([]*annotations.ItemDTO, 0), err
}
return make([]*annotations.ItemDTO, 0), ErrLokiStoreInternal.Errorf("failed to build loki query: %w", err)
}
@@ -778,6 +778,7 @@ func NewFakeLokiClient() *FakeLokiClient {
ReadPathURL: url,
Encoder: historian.JsonEncoder{},
MaxQueryLength: 721 * time.Hour,
MaxQuerySize: 65536,
},
metrics: metrics,
log: log.New("ngalert.state.historian", "backend", "loki"),
@@ -812,6 +813,10 @@ func (c *FakeLokiClient) RangeQuery(ctx context.Context, query string, from, to,
return res, nil
}
func (c *FakeLokiClient) MaxQuerySize() int {
return c.cfg.MaxQuerySize
}
func TestUseStore(t *testing.T) {
t.Run("false if state history disabled", func(t *testing.T) {
cfg := setting.UnifiedAlertingStateHistorySettings{