Skip to content

Commit

Permalink
feat: use new NewServeMux instead of Default and ctx for running metr…
Browse files Browse the repository at this point in the history
…ics server (#253)

* add mux and register metrics handler

* added context based cancelling

* server closed

* minor logger ting
  • Loading branch information
arora-anmol authored May 26, 2024
1 parent bf1671a commit b2bcaf1
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions metrics/eigenmetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,38 @@ func (m *EigenMetrics) SetPerformanceScore(score float64) {
// reg needs to be the prometheus registry that was passed in the NewEigenMetrics constructor
func (m *EigenMetrics) Start(ctx context.Context, reg prometheus.Gatherer) <-chan error {
m.logger.Infof("Starting metrics server at port %v", m.ipPortAddress)
errC := make(chan error, 1)
errChan := make(chan error, 1)
mux := http.NewServeMux()
httpServer := http.Server{
Addr: m.ipPortAddress,
Handler: mux,
}
mux.Handle("/metrics", promhttp.HandlerFor(
reg,
promhttp.HandlerOpts{},
))

// shutdown server on context done
go func() {
<-ctx.Done()
m.logger.Info("shutdown signal received")
defer func() {
close(errChan)
}()

if err := httpServer.Shutdown(context.Background()); err != nil {
errChan <- err
}
m.logger.Info("shutdown completed")
}()

go func() {
http.Handle("/metrics", promhttp.HandlerFor(
reg,
promhttp.HandlerOpts{},
))
err := http.ListenAndServe(m.ipPortAddress, nil)
if err != nil {
errC <- utils.WrapError("Prometheus server failed", err)
err := httpServer.ListenAndServe()
if err == http.ErrServerClosed {
m.logger.Info("server closed")
} else {
errC <- nil
errChan <- utils.WrapError("Prometheus server failed", err)
}
}()
return errC
return errChan
}

0 comments on commit b2bcaf1

Please sign in to comment.