From 4f34a57ce259d9aedebfee6a2e53ae4acaf146fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20=C5=A0tibran=C3=BD?= Date: Fri, 27 Jun 2025 16:22:28 +0200 Subject: [PATCH] Don't use prometheus.DefaultRegisterer in annotationsimpl/loki. (#107311) * Don't use prometheus.DefaultRegisterer in annotationsimpl/loki. --- pkg/services/annotations/annotationsimpl/annotations.go | 5 ++++- pkg/services/annotations/annotationsimpl/annotations_test.go | 5 +++-- .../annotations/annotationsimpl/loki/historian_store.go | 4 ++-- pkg/services/publicdashboards/service/common_test.go | 4 +++- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/pkg/services/annotations/annotationsimpl/annotations.go b/pkg/services/annotations/annotationsimpl/annotations.go index 07dff48325a..87f5a9a13ce 100644 --- a/pkg/services/annotations/annotationsimpl/annotations.go +++ b/pkg/services/annotations/annotationsimpl/annotations.go @@ -3,6 +3,8 @@ package annotationsimpl import ( "context" + "github.com/prometheus/client_golang/prometheus" + "github.com/grafana/grafana/pkg/services/annotations/accesscontrol" "github.com/grafana/grafana/pkg/services/annotations/annotationsimpl/loki" "github.com/grafana/grafana/pkg/services/dashboards" @@ -33,6 +35,7 @@ func ProvideService( tracer tracing.Tracer, ruleStore *alertingStore.DBstore, dashSvc dashboards.DashboardService, + reg prometheus.Registerer, ) *RepositoryImpl { l := log.New("annotations") l.Debug("Initializing annotations service") @@ -41,7 +44,7 @@ func ProvideService( write := xormStore var read readStore - historianStore := loki.NewLokiHistorianStore(cfg.UnifiedAlerting.StateHistory, db, ruleStore, log.New("annotations.loki"), tracer) + historianStore := loki.NewLokiHistorianStore(cfg.UnifiedAlerting.StateHistory, db, ruleStore, log.New("annotations.loki"), tracer, reg) if historianStore != nil { l.Debug("Using composite read store") read = NewCompositeStore(log.New("annotations.composite"), xormStore, historianStore) diff --git a/pkg/services/annotations/annotationsimpl/annotations_test.go b/pkg/services/annotations/annotationsimpl/annotations_test.go index 600e41ce269..9cb5a099812 100644 --- a/pkg/services/annotations/annotationsimpl/annotations_test.go +++ b/pkg/services/annotations/annotationsimpl/annotations_test.go @@ -6,6 +6,7 @@ import ( "fmt" "testing" + "github.com/prometheus/client_golang/prometheus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -70,7 +71,7 @@ func TestIntegrationAnnotationListingWithRBAC(t *testing.T) { kvstore.NewFakeKVStore()) require.NoError(t, err) dashSvc.RegisterDashboardPermissions(accesscontrolmock.NewMockedPermissionsService()) - repo := ProvideService(sql, cfg, features, tagService, tracing.InitializeTracerForTest(), ruleStore, dashSvc) + repo := ProvideService(sql, cfg, features, tagService, tracing.InitializeTracerForTest(), ruleStore, dashSvc, prometheus.NewPedanticRegistry()) dashboard1 := testutil.CreateDashboard(t, sql, cfg, features, dashboards.SaveDashboardCommand{ UserID: 1, @@ -344,7 +345,7 @@ func TestIntegrationAnnotationListingWithInheritedRBAC(t *testing.T) { cfg := setting.NewCfg() cfg.AnnotationMaximumTagsLength = 60 ruleStore := alertingStore.SetupStoreForTesting(t, sql) - repo := ProvideService(sql, cfg, tc.features, tagimpl.ProvideService(sql), tracing.InitializeTracerForTest(), ruleStore, dashSvc) + repo := ProvideService(sql, cfg, tc.features, tagimpl.ProvideService(sql), tracing.InitializeTracerForTest(), ruleStore, dashSvc, prometheus.NewPedanticRegistry()) usr.Permissions = map[int64]map[string][]string{1: tc.permissions} testutil.SetupRBACPermission(t, sql, role, usr) diff --git a/pkg/services/annotations/annotationsimpl/loki/historian_store.go b/pkg/services/annotations/annotationsimpl/loki/historian_store.go index 933b7426ab1..8ab1095cdec 100644 --- a/pkg/services/annotations/annotationsimpl/loki/historian_store.go +++ b/pkg/services/annotations/annotationsimpl/loki/historian_store.go @@ -56,7 +56,7 @@ type LokiHistorianStore struct { ruleStore RuleStore } -func NewLokiHistorianStore(cfg setting.UnifiedAlertingStateHistorySettings, db db.DB, ruleStore RuleStore, log log.Logger, tracer tracing.Tracer) *LokiHistorianStore { +func NewLokiHistorianStore(cfg setting.UnifiedAlertingStateHistorySettings, db db.DB, ruleStore RuleStore, log log.Logger, tracer tracing.Tracer, reg prometheus.Registerer) *LokiHistorianStore { if !useStore(cfg) { return nil } @@ -67,7 +67,7 @@ func NewLokiHistorianStore(cfg setting.UnifiedAlertingStateHistorySettings, db d } return &LokiHistorianStore{ - client: historian.NewLokiClient(lokiCfg, historian.NewRequester(), ngmetrics.NewHistorianMetrics(prometheus.DefaultRegisterer, subsystem), log, tracer), + client: historian.NewLokiClient(lokiCfg, historian.NewRequester(), ngmetrics.NewHistorianMetrics(reg, subsystem), log, tracer), db: db, log: log, ruleStore: ruleStore, diff --git a/pkg/services/publicdashboards/service/common_test.go b/pkg/services/publicdashboards/service/common_test.go index 82992ed3622..777104d4526 100644 --- a/pkg/services/publicdashboards/service/common_test.go +++ b/pkg/services/publicdashboards/service/common_test.go @@ -3,6 +3,8 @@ package service import ( "testing" + "github.com/prometheus/client_golang/prometheus" + "github.com/grafana/grafana/pkg/infra/db" "github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/tracing" @@ -35,7 +37,7 @@ func newPublicDashboardServiceImpl( } tagService := tagimpl.ProvideService(store) if annotationsRepo == nil { - annotationsRepo = annotationsimpl.ProvideService(store, cfg, featuremgmt.WithFeatures(), tagService, tracing.InitializeTracerForTest(), nil, dashboardService) + annotationsRepo = annotationsimpl.ProvideService(store, cfg, featuremgmt.WithFeatures(), tagService, tracing.InitializeTracerForTest(), nil, dashboardService, prometheus.NewPedanticRegistry()) } if publicDashboardStore == nil {