From 0f106589010bc45151f89cf8b7def64ad51fd640 Mon Sep 17 00:00:00 2001 From: "grafana-delivery-bot[bot]" <132647405+grafana-delivery-bot[bot]@users.noreply.github.com> Date: Tue, 21 May 2024 19:33:31 +0300 Subject: [PATCH] [v11.0.x] Alerting/Annotations: Return nothing from historian store if filtering by tags (#88140) Alerting/Annotations: Return nothing from historian store if filtering by tags and matchAny is false (#85488) * Return nothing from historian store if filtering by tag (cherry picked from commit cad8190a91a0ded2e5305b97761ffd4f945bdee1) Co-authored-by: William Wernert --- .../annotationsimpl/loki/historian_store.go | 8 +++++- .../loki/historian_store_test.go | 26 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/pkg/services/annotations/annotationsimpl/loki/historian_store.go b/pkg/services/annotations/annotationsimpl/loki/historian_store.go index 3d12623bd02..1c4cf9da98c 100644 --- a/pkg/services/annotations/annotationsimpl/loki/historian_store.go +++ b/pkg/services/annotations/annotationsimpl/loki/historian_store.go @@ -78,6 +78,12 @@ func (r *LokiHistorianStore) Get(ctx context.Context, query *annotations.ItemQue return make([]*annotations.ItemDTO, 0), nil } + // if the query is filtering on tags, but not on a specific dashboard, we shouldn't query loki + // since state history won't have tags for annotations + if len(query.Tags) > 0 && query.DashboardID == 0 && query.DashboardUID == "" { + return make([]*annotations.ItemDTO, 0), nil + } + rule := &ngmodels.AlertRule{} if query.AlertID != 0 { var err error @@ -173,7 +179,7 @@ func (r *LokiHistorianStore) annotationsFromStream(stream historian.Stream, ac a } func (r *LokiHistorianStore) GetTags(ctx context.Context, query *annotations.TagsQuery) (annotations.FindTagsResult, error) { - return annotations.FindTagsResult{}, nil + return annotations.FindTagsResult{Tags: []*annotations.TagsDTO{}}, nil } // util diff --git a/pkg/services/annotations/annotationsimpl/loki/historian_store_test.go b/pkg/services/annotations/annotationsimpl/loki/historian_store_test.go index 56b999da54f..ed0df107b37 100644 --- a/pkg/services/annotations/annotationsimpl/loki/historian_store_test.go +++ b/pkg/services/annotations/annotationsimpl/loki/historian_store_test.go @@ -254,6 +254,32 @@ func TestIntegrationAlertStateHistoryStore(t *testing.T) { lastTime = item.Time } }) + + t.Run("should return nothing if query is for tags only", func(t *testing.T) { + fakeLokiClient.rangeQueryRes = []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, + From: start.UnixMilli(), + To: start.Add(time.Second * time.Duration(numTransitions+1)).UnixMilli(), + Tags: []string{"tag1"}, + } + 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("Testing items from Loki stream", func(t *testing.T) {