K8s: Refactor metrics to share k8s registry (#77957)

Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
This commit is contained in:
Todd Treece
2023-12-04 10:54:59 -08:00
committed by GitHub
co-authored by Ryan McKinley
parent cd584e9261
commit 38bc41651a
13 changed files with 197 additions and 39 deletions
+8 -2
View File
@@ -13,6 +13,8 @@ import (
"golang.org/x/sync/errgroup"
"github.com/prometheus/client_golang/prometheus"
"github.com/grafana/grafana/pkg/api"
_ "github.com/grafana/grafana/pkg/extensions"
"github.com/grafana/grafana/pkg/infra/log"
@@ -38,9 +40,10 @@ type Options struct {
func New(opts Options, cfg *setting.Cfg, httpServer *api.HTTPServer, roleRegistry accesscontrol.RoleRegistry,
provisioningService provisioning.ProvisioningService, backgroundServiceProvider registry.BackgroundServiceRegistry,
usageStatsProvidersRegistry registry.UsageStatsProvidersRegistry, statsCollectorService *statscollector.Service,
promReg prometheus.Registerer,
) (*Server, error) {
statsCollectorService.RegisterProviders(usageStatsProvidersRegistry.GetServices())
s, err := newServer(opts, cfg, httpServer, roleRegistry, provisioningService, backgroundServiceProvider)
s, err := newServer(opts, cfg, httpServer, roleRegistry, provisioningService, backgroundServiceProvider, promReg)
if err != nil {
return nil, err
}
@@ -54,11 +57,13 @@ func New(opts Options, cfg *setting.Cfg, httpServer *api.HTTPServer, roleRegistr
func newServer(opts Options, cfg *setting.Cfg, httpServer *api.HTTPServer, roleRegistry accesscontrol.RoleRegistry,
provisioningService provisioning.ProvisioningService, backgroundServiceProvider registry.BackgroundServiceRegistry,
promReg prometheus.Registerer,
) (*Server, error) {
rootCtx, shutdownFn := context.WithCancel(context.Background())
childRoutines, childCtx := errgroup.WithContext(rootCtx)
s := &Server{
promReg: promReg,
context: childCtx,
childRoutines: childRoutines,
HTTPServer: httpServer,
@@ -101,6 +106,7 @@ type Server struct {
HTTPServer *api.HTTPServer
roleRegistry accesscontrol.RoleRegistry
provisioningService provisioning.ProvisioningService
promReg prometheus.Registerer
}
// Init initializes the server and its services.
@@ -117,7 +123,7 @@ func (s *Server) Init() error {
return err
}
if err := metrics.SetEnvironmentInformation(s.cfg.MetricsGrafanaEnvironmentInfo); err != nil {
if err := metrics.SetEnvironmentInformation(s.promReg, s.cfg.MetricsGrafanaEnvironmentInfo); err != nil {
return err
}
+2 -1
View File
@@ -7,6 +7,7 @@ import (
"testing"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/registry"
@@ -48,7 +49,7 @@ func (s *testService) IsDisabled() bool {
func testServer(t *testing.T, services ...registry.BackgroundService) *Server {
t.Helper()
s, err := newServer(Options{}, setting.NewCfg(), nil, &acimpl.Service{}, nil, backgroundsvcs.NewBackgroundServiceRegistry(services...))
s, err := newServer(Options{}, setting.NewCfg(), nil, &acimpl.Service{}, nil, backgroundsvcs.NewBackgroundServiceRegistry(services...), prometheus.NewRegistry())
require.NoError(t, err)
// Required to skip configuration initialization that causes
// DI errors in this test.
+3 -3
View File
@@ -253,7 +253,6 @@ var wireBasicSet = wire.NewSet(
notifications.ProvideSmtpService,
tracing.ProvideService,
wire.Bind(new(tracing.Tracer), new(*tracing.TracingService)),
metrics.ProvideService,
testdatasource.ProvideService,
ldapapi.ProvideService,
opentsdb.ProvideService,
@@ -386,7 +385,7 @@ var wireBasicSet = wire.NewSet(
var wireSet = wire.NewSet(
wireBasicSet,
metrics.ProvideRegisterer,
metrics.WireSet,
sqlstore.ProvideService,
ngmetrics.ProvideService,
wire.Bind(new(notifications.Service), new(*notifications.NotificationService)),
@@ -401,6 +400,7 @@ var wireSet = wire.NewSet(
var wireCLISet = wire.NewSet(
NewRunner,
wireBasicSet,
metrics.WireSet,
sqlstore.ProvideService,
ngmetrics.ProvideService,
wire.Bind(new(notifications.Service), new(*notifications.NotificationService)),
@@ -415,7 +415,7 @@ var wireCLISet = wire.NewSet(
var wireTestSet = wire.NewSet(
wireBasicSet,
ProvideTestEnv,
metrics.ProvideRegistererForTest,
metrics.WireSetForTest,
sqlstore.ProvideServiceForTests,
ngmetrics.ProvideServiceForTest,
notifications.MockNotificationService,
+2
View File
@@ -7,6 +7,7 @@ package server
import (
"github.com/google/wire"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/plugins/manager"
"github.com/grafana/grafana/pkg/registry"
@@ -123,6 +124,7 @@ var wireExtsTestSet = wire.NewSet(
var wireExtsBaseCLISet = wire.NewSet(
NewModuleRunner,
metrics.WireSet,
featuremgmt.ProvideManagerService,
featuremgmt.ProvideToggles,
hooks.ProvideService,