Alerting: Fix URL timestamp conversion in historian API in annotation mode (#80026)

Fix timestamp conversion when calling annotation store
This commit is contained in:
Alexander Weaver 2024-01-04 12:40:21 -06:00 committed by GitHub
parent fc8e2472e1
commit 90d4704cd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 2 deletions

View File

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

View File

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