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
This commit is contained in:
William Wernert 2024-01-18 11:39:33 -05:00 committed by GitHub
parent ded941eb84
commit e74313e171
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 1 deletions

View File

@ -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

View File

@ -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,