diff --git a/server/v2/api/telemetry/server.go b/server/v2/api/telemetry/server.go index f612964a8476..7efdeae16b79 100644 --- a/server/v2/api/telemetry/server.go +++ b/server/v2/api/telemetry/server.go @@ -52,9 +52,24 @@ func New[T transaction.Tx](cfg server.ConfigMap, logger log.Logger, enableTeleme return nil, fmt.Errorf("failed to initialize metrics: %w", err) } srv.metrics = metrics + srv.init() return srv, nil } +func (s *Server[T]) init() { + mux := http.NewServeMux() + // /metrics is the default standard path for Prometheus metrics. + mux.HandleFunc("/metrics", s.metricsHandler) + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, "/metrics", http.StatusMovedPermanently) + }) + + s.server = &http.Server{ + Addr: s.config.Address, + Handler: mux, + } +} + // Name returns the server name. func (s *Server[T]) Name() string { return ServerName @@ -74,18 +89,6 @@ func (s *Server[T]) Start(ctx context.Context) error { return nil } - mux := http.NewServeMux() - // /metrics is the default standard path for Prometheus metrics. - mux.HandleFunc("/metrics", s.metricsHandler) - mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, "/metrics", http.StatusMovedPermanently) - }) - - s.server = &http.Server{ - Addr: s.config.Address, - Handler: mux, - } - s.logger.Info("starting telemetry server...", "address", s.config.Address) if err := s.server.ListenAndServe(); err != nil && err != http.ErrServerClosed { return fmt.Errorf("failed to start telemetry server: %w", err) @@ -99,6 +102,7 @@ func (s *Server[T]) Stop(ctx context.Context) error { return nil } + defer s.init() s.logger.Info("stopping telemetry server...", "address", s.config.Address) return s.server.Shutdown(ctx) }