From 90d4704cd752b12aef4f0932855e5f1c11bc6cd4 Mon Sep 17 00:00:00 2001 From: Alexander Weaver Date: Thu, 4 Jan 2024 12:40:21 -0600 Subject: [PATCH] Alerting: Fix URL timestamp conversion in historian API in annotation mode (#80026) Fix timestamp conversion when calling annotation store --- .../ngalert/state/historian/annotation.go | 4 +- .../state/historian/annotation_test.go | 42 +++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/pkg/services/ngalert/state/historian/annotation.go b/pkg/services/ngalert/state/historian/annotation.go index e4ae3b9f8d3..79fbe298bbb 100644 --- a/pkg/services/ngalert/state/historian/annotation.go +++ b/pkg/services/ngalert/state/historian/annotation.go @@ -111,8 +111,8 @@ func (h *AnnotationBackend) Query(ctx context.Context, query ngmodels.HistoryQue q := annotations.ItemQuery{ AlertID: rule.ID, OrgID: query.OrgID, - From: query.From.Unix(), - To: query.To.Unix(), + From: query.From.UnixMilli(), + To: query.To.UnixMilli(), SignedInUser: query.SignedInUser, } items, err := h.store.Find(ctx, &q) diff --git a/pkg/services/ngalert/state/historian/annotation_test.go b/pkg/services/ngalert/state/historian/annotation_test.go index 4108323cfed..0192d321659 100644 --- a/pkg/services/ngalert/state/historian/annotation_test.go +++ b/pkg/services/ngalert/state/historian/annotation_test.go @@ -47,6 +47,25 @@ func TestAnnotationHistorian(t *testing.T) { } }) + t.Run("annotation queries send expected item query", func(t *testing.T) { + store := &interceptingAnnotationStore{} + anns := createTestAnnotationSutWithStore(t, store) + now := time.Now().UTC() + + q := models.HistoryQuery{ + RuleUID: "my-rule", + OrgID: 1, + From: now.Add(-10 * time.Second), + To: now, + } + _, err := anns.Query(context.Background(), q) + + require.NoError(t, err) + query := store.lastQuery + require.Equal(t, now.UnixMilli(), query.To) + require.Equal(t, now.Add(-10*time.Second).UnixMilli(), query.From) + }) + t.Run("writing state transitions as annotations succeeds", func(t *testing.T) { anns := createTestAnnotationBackendSut(t) rule := createTestRule() @@ -104,6 +123,16 @@ func createTestAnnotationBackendSut(t *testing.T) *AnnotationBackend { return createTestAnnotationBackendSutWithMetrics(t, metrics.NewHistorianMetrics(prometheus.NewRegistry(), metrics.Subsystem)) } +func createTestAnnotationSutWithStore(t *testing.T, annotations AnnotationStore) *AnnotationBackend { + t.Helper() + met := metrics.NewHistorianMetrics(prometheus.NewRegistry(), metrics.Subsystem) + rules := fakes.NewRuleStore(t) + rules.Rules[1] = []*models.AlertRule{ + models.AlertRuleGen(withOrgID(1), withUID("my-rule"))(), + } + return NewAnnotationBackend(annotations, rules, met) +} + func createTestAnnotationBackendSutWithMetrics(t *testing.T, met *metrics.Historian) *AnnotationBackend { t.Helper() fakeAnnoRepo := annotationstest.NewFakeAnnotationsRepo() @@ -213,3 +242,16 @@ func assertValidJSON(t *testing.T, j *simplejson.Json) string { require.NoError(t, err) return string(ser) } + +type interceptingAnnotationStore struct { + lastQuery *annotations.ItemQuery +} + +func (i *interceptingAnnotationStore) Find(ctx context.Context, query *annotations.ItemQuery) ([]*annotations.ItemDTO, error) { + i.lastQuery = query + return []*annotations.ItemDTO{}, nil +} + +func (i *interceptingAnnotationStore) Save(ctx context.Context, panel *PanelKey, annotations []annotations.Item, orgID int64, logger log.Logger) error { + return nil +}