Skip to content

Commit

Permalink
fix: do not register storage metric collectors if it is not enabled
Browse files Browse the repository at this point in the history
If discovery service storage is not enabled, we were still wrongly registering it as prometheus metrics collector, because the nil check was done against the interface. It had to be done on the concrete type instead.

Signed-off-by: Utku Ozdemir <[email protected]>
  • Loading branch information
utkuozdemir committed Nov 28, 2024
1 parent b8da986 commit 2bb245a
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func Run(ctx context.Context, options Options, logger *zap.Logger) error {
return fmt.Errorf("failed to configure server: %w", err)
}

if options.SnapshotsEnabled {
if stateStorage != nil {
eg.Go(func() error {
return stateStorage.Start(ctx, clockwork.NewRealClock(), options.SnapshotInterval)
})
Expand Down Expand Up @@ -295,7 +295,11 @@ func Run(ctx context.Context, options Options, logger *zap.Logger) error {
}

if options.MetricsRegisterer != nil {
collectors := []prom.Collector{state, srv, metrics, stateStorage}
collectors := []prom.Collector{state, srv, metrics}

if stateStorage != nil {
collectors = append(collectors, stateStorage)
}

defer unregisterCollectors(options.MetricsRegisterer, collectors...)

Expand All @@ -319,20 +323,12 @@ func recoveryHandler(logger *zap.Logger) grpc_recovery.RecoveryHandlerFunc {

func unregisterCollectors(registerer prom.Registerer, collectors ...prom.Collector) {
for _, collector := range collectors {
if collector == nil {
continue
}

registerer.Unregister(collector)
}
}

func registerCollectors(registerer prom.Registerer, collectors ...prom.Collector) (err error) {
for _, collector := range collectors {
if collector == nil {
continue
}

if err = registerer.Register(collector); err != nil {
return fmt.Errorf("failed to register collector: %w", err)
}
Expand Down

0 comments on commit 2bb245a

Please sign in to comment.