fix(unified-storage): update instrumentation_server metric gatherer (#102399)
* update instrumentation_server used by the module server to use prometheus gatherer from wire instead of DefaultRegisterer
This commit is contained in:
@@ -9,19 +9,21 @@ import (
|
||||
"github.com/grafana/dskit/services"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
type instrumentationService struct {
|
||||
*services.BasicService
|
||||
cfg *setting.Cfg
|
||||
httpServ *http.Server
|
||||
log log.Logger
|
||||
errChan chan error
|
||||
cfg *setting.Cfg
|
||||
httpServ *http.Server
|
||||
log log.Logger
|
||||
errChan chan error
|
||||
promGatherer prometheus.Gatherer
|
||||
}
|
||||
|
||||
func NewInstrumentationService(log log.Logger, cfg *setting.Cfg) (*instrumentationService, error) {
|
||||
s := &instrumentationService{log: log, cfg: cfg}
|
||||
func NewInstrumentationService(log log.Logger, cfg *setting.Cfg, promGatherer prometheus.Gatherer) (*instrumentationService, error) {
|
||||
s := &instrumentationService{log: log, cfg: cfg, promGatherer: promGatherer}
|
||||
s.BasicService = services.NewBasicService(s.start, s.running, s.stop)
|
||||
return s, nil
|
||||
}
|
||||
@@ -56,7 +58,7 @@ func (s *instrumentationService) stop(failureReason error) error {
|
||||
|
||||
func (s *instrumentationService) newInstrumentationServer(ctx context.Context) *http.Server {
|
||||
router := http.NewServeMux()
|
||||
router.Handle("/metrics", promhttp.Handler())
|
||||
router.Handle("/metrics", promhttp.HandlerFor(s.promGatherer, promhttp.HandlerOpts{EnableOpenMetrics: true}))
|
||||
|
||||
srv := &http.Server{
|
||||
// 5s timeout for header reads to avoid Slowloris attacks (https://thetooth.io/blog/slowloris-attack/)
|
||||
|
||||
@@ -18,7 +18,7 @@ import (
|
||||
func TestRunInstrumentationService(t *testing.T) {
|
||||
cfg := setting.NewCfg()
|
||||
cfg.HTTPPort = "3001"
|
||||
s, err := NewInstrumentationService(log.New("test-logger"), cfg)
|
||||
s, err := NewInstrumentationService(log.New("test-logger"), cfg, prometheus.DefaultGatherer)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Second)
|
||||
|
||||
@@ -18,12 +18,13 @@ import (
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/storage/unified/resource"
|
||||
"github.com/grafana/grafana/pkg/storage/unified/sql"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
// NewModule returns an instance of a ModuleServer, responsible for managing
|
||||
// dskit modules (services).
|
||||
func NewModule(opts Options, apiOpts api.ServerOptions, features featuremgmt.FeatureToggles, cfg *setting.Cfg, storageMetrics *resource.StorageMetrics, indexMetrics *resource.BleveIndexMetrics) (*ModuleServer, error) {
|
||||
s, err := newModuleServer(opts, apiOpts, features, cfg, storageMetrics, indexMetrics)
|
||||
func NewModule(opts Options, apiOpts api.ServerOptions, features featuremgmt.FeatureToggles, cfg *setting.Cfg, storageMetrics *resource.StorageMetrics, indexMetrics *resource.BleveIndexMetrics, promGatherer prometheus.Gatherer) (*ModuleServer, error) {
|
||||
s, err := newModuleServer(opts, apiOpts, features, cfg, storageMetrics, indexMetrics, promGatherer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -35,7 +36,7 @@ func NewModule(opts Options, apiOpts api.ServerOptions, features featuremgmt.Fea
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func newModuleServer(opts Options, apiOpts api.ServerOptions, features featuremgmt.FeatureToggles, cfg *setting.Cfg, storageMetrics *resource.StorageMetrics, indexMetrics *resource.BleveIndexMetrics) (*ModuleServer, error) {
|
||||
func newModuleServer(opts Options, apiOpts api.ServerOptions, features featuremgmt.FeatureToggles, cfg *setting.Cfg, storageMetrics *resource.StorageMetrics, indexMetrics *resource.BleveIndexMetrics, promGatherer prometheus.Gatherer) (*ModuleServer, error) {
|
||||
rootCtx, shutdownFn := context.WithCancel(context.Background())
|
||||
|
||||
s := &ModuleServer{
|
||||
@@ -53,6 +54,7 @@ func newModuleServer(opts Options, apiOpts api.ServerOptions, features featuremg
|
||||
buildBranch: opts.BuildBranch,
|
||||
storageMetrics: storageMetrics,
|
||||
indexMetrics: indexMetrics,
|
||||
promGatherer: promGatherer,
|
||||
}
|
||||
|
||||
return s, nil
|
||||
@@ -81,6 +83,8 @@ type ModuleServer struct {
|
||||
version string
|
||||
commit string
|
||||
buildBranch string
|
||||
|
||||
promGatherer prometheus.Gatherer
|
||||
}
|
||||
|
||||
// init initializes the server and its services.
|
||||
@@ -119,7 +123,7 @@ func (s *ModuleServer) Run() error {
|
||||
if m.IsModuleEnabled(modules.All) || m.IsModuleEnabled(modules.Core) {
|
||||
return services.NewBasicService(nil, nil, nil).WithName(modules.InstrumentationServer), nil
|
||||
}
|
||||
return NewInstrumentationService(s.log, s.cfg)
|
||||
return NewInstrumentationService(s.log, s.cfg, s.promGatherer)
|
||||
})
|
||||
|
||||
m.RegisterModule(modules.Core, func() (services.Service, error) {
|
||||
|
||||
Reference in New Issue
Block a user