Alerting: Pass logger into NewAnnotationBackend. (#87812)

* Alerting: Pass logger into NewAnnotationBackend.

Make it possible to pass loggers into more places for code reuse.

* Mistake in passing logger
This commit is contained in:
Steve Simpson 2024-05-14 15:51:27 +02:00 committed by GitHub
parent 5678012f0f
commit 67fa96f88d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 15 additions and 9 deletions

View File

@ -472,7 +472,8 @@ func configureHistorianBackend(ctx context.Context, cfg setting.UnifiedAlertingS
}
if backend == historian.BackendTypeAnnotations {
store := historian.NewAnnotationStore(ar, ds, met)
return historian.NewAnnotationBackend(store, rs, met), nil
annotationBackendLogger := log.New("ngalert.state.historian", "backend", "annotations")
return historian.NewAnnotationBackend(annotationBackendLogger, store, rs, met), nil
}
if backend == historian.BackendTypeLoki {
lcfg, err := historian.NewLokiConfig(cfg)

View File

@ -41,8 +41,7 @@ type AnnotationStore interface {
Save(ctx context.Context, panel *PanelKey, annotations []annotations.Item, orgID int64, logger log.Logger) error
}
func NewAnnotationBackend(annotations AnnotationStore, rules RuleStore, metrics *metrics.Historian) *AnnotationBackend {
logger := log.New("ngalert.state.historian", "backend", "annotations")
func NewAnnotationBackend(logger log.Logger, annotations AnnotationStore, rules RuleStore, metrics *metrics.Historian) *AnnotationBackend {
return &AnnotationBackend{
store: annotations,
rules: rules,

View File

@ -131,7 +131,8 @@ func createTestAnnotationSutWithStore(t *testing.T, annotations AnnotationStore)
rules.Rules[1] = []*models.AlertRule{
models.RuleGen.With(models.RuleMuts.WithOrgID(1), withUID("my-rule")).GenerateRef(),
}
return NewAnnotationBackend(annotations, rules, met)
annotationBackendLogger := log.New("ngalert.state.historian", "backend", "annotations")
return NewAnnotationBackend(annotationBackendLogger, annotations, rules, met)
}
func createTestAnnotationBackendSutWithMetrics(t *testing.T, met *metrics.Historian) *AnnotationBackend {
@ -144,7 +145,8 @@ func createTestAnnotationBackendSutWithMetrics(t *testing.T, met *metrics.Histor
dbs := &dashboards.FakeDashboardService{}
dbs.On("GetDashboard", mock.Anything, mock.Anything).Return(&dashboards.Dashboard{}, nil)
store := NewAnnotationStore(fakeAnnoRepo, dbs, met)
return NewAnnotationBackend(store, rules, met)
annotationBackendLogger := log.New("ngalert.state.historian", "backend", "annotations")
return NewAnnotationBackend(annotationBackendLogger, store, rules, met)
}
func createFailingAnnotationSut(t *testing.T, met *metrics.Historian) *AnnotationBackend {
@ -155,8 +157,9 @@ func createFailingAnnotationSut(t *testing.T, met *metrics.Historian) *Annotatio
}
dbs := &dashboards.FakeDashboardService{}
dbs.On("GetDashboard", mock.Anything, mock.Anything).Return(&dashboards.Dashboard{}, nil)
annotationBackendLogger := log.New("ngalert.state.historian", "backend", "annotations")
store := NewAnnotationStore(fakeAnnoRepo, dbs, met)
return NewAnnotationBackend(store, rules, met)
return NewAnnotationBackend(annotationBackendLogger, store, rules, met)
}
func createAnnotation() annotations.Item {

View File

@ -24,7 +24,8 @@ func BenchmarkProcessEvalResults(b *testing.B) {
as.On("SaveMany", mock.Anything, mock.Anything).Return(nil)
metrics := metrics.NewHistorianMetrics(prometheus.NewRegistry(), metrics.Subsystem)
store := historian.NewAnnotationStore(&as, nil, metrics)
hist := historian.NewAnnotationBackend(store, nil, metrics)
annotationBackendLogger := log.New("ngalert.state.historian", "backend", "annotations")
hist := historian.NewAnnotationBackend(annotationBackendLogger, store, nil, metrics)
cfg := state.ManagerCfg{
Historian: hist,
Tracer: tracing.InitializeTracerForTest(),

View File

@ -234,7 +234,8 @@ func TestDashboardAnnotations(t *testing.T) {
fakeAnnoRepo := annotationstest.NewFakeAnnotationsRepo()
historianMetrics := metrics.NewHistorianMetrics(prometheus.NewRegistry(), metrics.Subsystem)
store := historian.NewAnnotationStore(fakeAnnoRepo, &dashboards.FakeDashboardService{}, historianMetrics)
hist := historian.NewAnnotationBackend(store, nil, historianMetrics)
annotationBackendLogger := log.New("ngalert.state.historian", "backend", "annotations")
hist := historian.NewAnnotationBackend(annotationBackendLogger, store, nil, historianMetrics)
cfg := state.ManagerCfg{
Metrics: metrics.NewNGAlert(prometheus.NewPedanticRegistry()).GetStateMetrics(),
ExternalURL: nil,
@ -1280,7 +1281,8 @@ func TestProcessEvalResults(t *testing.T) {
stateMetrics := metrics.NewStateMetrics(reg)
m := metrics.NewHistorianMetrics(prometheus.NewRegistry(), metrics.Subsystem)
store := historian.NewAnnotationStore(fakeAnnoRepo, &dashboards.FakeDashboardService{}, m)
hist := historian.NewAnnotationBackend(store, nil, m)
annotationBackendLogger := log.New("ngalert.state.historian", "backend", "annotations")
hist := historian.NewAnnotationBackend(annotationBackendLogger, store, nil, m)
clk := clock.NewMock()
cfg := state.ManagerCfg{
Metrics: stateMetrics,