From 0b12feb953bec40a55d5b539d08fdb2dd8a2573a Mon Sep 17 00:00:00 2001 From: Todd Date: Fri, 15 Apr 2022 09:30:23 -0700 Subject: [PATCH] Do not register the build_info collectors multiple times. (#2014) Since we call base.NewServer multiple times when building the Commands map this was returning an error "duplicate metrics collector registration attempted". This PR only creates a single server which is used for both the "server" command as well as "dev". --- internal/cmd/base/servers.go | 8 +++----- internal/cmd/commands.go | 10 +++++----- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/internal/cmd/base/servers.go b/internal/cmd/base/servers.go index 7bad4e1a4e..ee2a55a687 100644 --- a/internal/cmd/base/servers.go +++ b/internal/cmd/base/servers.go @@ -134,12 +134,10 @@ type Server struct { StatusGracePeriodDuration time.Duration } -// The only option used here is WithPrometheusRegisterer; all others are ignored. +// NewServer creates a new Server. +// The WithPrometheusRegisterer Option uses the passed in registry to initialize +// the build info collectors. All other Options are ignored. func NewServer(cmd *Command, opt ...Option) *Server { - // Create a new prometheus registry here to avoid "duplicate metrics collector - // registration" panics in tests where new servers are called consecutively. - // prometheus.DefaultRegisterer and prometheus.DefaultGatherer vars need to be - // assigned for promhttp package to work correctly. opts := getOpts(opt...) metric.InitializeBuildInfo(opts.withPrometheusRegisterer) diff --git a/internal/cmd/commands.go b/internal/cmd/commands.go index 554ba22c29..0f25f88b6f 100644 --- a/internal/cmd/commands.go +++ b/internal/cmd/commands.go @@ -34,19 +34,19 @@ import ( var Commands map[string]cli.CommandFactory func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { + srv := base.NewServer(base.NewCommand(serverCmdUi), + base.WithPrometheusRegisterer(prometheus.DefaultRegisterer)) Commands = map[string]cli.CommandFactory{ "server": func() (cli.Command, error) { return &server.Command{ - Server: base.NewServer(base.NewCommand(serverCmdUi), - base.WithPrometheusRegisterer(prometheus.DefaultRegisterer)), + Server: srv, SighupCh: base.MakeSighupCh(), SigUSR2Ch: MakeSigUSR2Ch(), }, nil }, "dev": func() (cli.Command, error) { return &dev.Command{ - Server: base.NewServer(base.NewCommand(serverCmdUi), - base.WithPrometheusRegisterer(prometheus.DefaultRegisterer)), + Server: srv, SighupCh: base.MakeSighupCh(), SigUSR2Ch: MakeSigUSR2Ch(), }, nil @@ -321,7 +321,7 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) { }, "database init": func() (cli.Command, error) { return &database.InitCommand{ - Server: base.NewServer(base.NewCommand(ui), base.WithPrometheusRegisterer(prometheus.DefaultRegisterer)), + Server: base.NewServer(base.NewCommand(ui)), }, nil }, "database migrate": func() (cli.Command, error) {