Skip to content

Commit

Permalink
telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Dec 10, 2024
1 parent 59bfdd9 commit 9db84ba
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions server/v2/api/telemetry/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
}
Expand Down

0 comments on commit 9db84ba

Please sign in to comment.