From e74313e1713fa613ac11bcacde2541fa770d0581 Mon Sep 17 00:00:00 2001 From: William Wernert Date: Thu, 18 Jan 2024 11:39:33 -0500 Subject: [PATCH] Alerting/Annotations: Return nothing from Loki historian store if query type is `annotation` (#80742) * Return empty slice if query type is `annotation` * Add test + fix related test --- .../annotationsimpl/loki/historian_store.go | 4 +++ .../loki/historian_store_test.go | 31 ++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/pkg/services/annotations/annotationsimpl/loki/historian_store.go b/pkg/services/annotations/annotationsimpl/loki/historian_store.go index e347cb45754..88ea9f6eedd 100644 --- a/pkg/services/annotations/annotationsimpl/loki/historian_store.go +++ b/pkg/services/annotations/annotationsimpl/loki/historian_store.go @@ -70,6 +70,10 @@ func NewLokiHistorianStore(cfg setting.UnifiedAlertingStateHistorySettings, ft f } func (r *LokiHistorianStore) Get(ctx context.Context, query *annotations.ItemQuery, accessResources *accesscontrol.AccessResources) ([]*annotations.ItemDTO, error) { + if query.Type == "annotation" { + return make([]*annotations.ItemDTO, 0), nil + } + rule := &ngmodels.AlertRule{} if query.AlertID != 0 { var err error diff --git a/pkg/services/annotations/annotationsimpl/loki/historian_store_test.go b/pkg/services/annotations/annotationsimpl/loki/historian_store_test.go index f32fc933cc7..c69b3ff67a2 100644 --- a/pkg/services/annotations/annotationsimpl/loki/historian_store_test.go +++ b/pkg/services/annotations/annotationsimpl/loki/historian_store_test.go @@ -128,7 +128,36 @@ func TestIntegrationAlertStateHistoryStore(t *testing.T) { require.Len(t, res, 2*numTransitions) }) - t.Run("should not find any when history is outside time range", func(t *testing.T) { + t.Run("should return empty results when type is annotation", func(t *testing.T) { + fakeLokiClient.Response = []historian.Stream{ + historian.StatesToStream(ruleMetaFromRule(t, dashboardRules[dashboard1.UID][0]), transitions, map[string]string{}, log.NewNopLogger()), + historian.StatesToStream(ruleMetaFromRule(t, dashboardRules[dashboard1.UID][1]), transitions, map[string]string{}, log.NewNopLogger()), + } + + query := annotations.ItemQuery{ + OrgID: 1, + Type: "annotation", + } + res, err := store.Get( + context.Background(), + &query, + &annotation_ac.AccessResources{ + Dashboards: map[string]int64{ + dashboard1.UID: dashboard1.ID, + }, + CanAccessDashAnnotations: true, + }, + ) + require.NoError(t, err) + require.Empty(t, res) + }) + + t.Run("should return empty results when history is outside time range", func(t *testing.T) { + fakeLokiClient.Response = []historian.Stream{ + historian.StatesToStream(ruleMetaFromRule(t, dashboardRules[dashboard1.UID][0]), transitions, map[string]string{}, log.NewNopLogger()), + historian.StatesToStream(ruleMetaFromRule(t, dashboardRules[dashboard1.UID][1]), transitions, map[string]string{}, log.NewNopLogger()), + } + query := annotations.ItemQuery{ OrgID: 1, DashboardID: dashboard1.ID,