Skip to content

Commit

Permalink
Updated metrics.go to add listener add unit tests
Browse files Browse the repository at this point in the history
Issue: COSI-65
  • Loading branch information
anurag4DSB committed Dec 19, 2024
1 parent bdb2b78 commit 7c0fc42
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package metrics

import (
"net"
"net/http"

c "github.com/scality/cosi-driver/pkg/constants"
Expand All @@ -25,17 +26,24 @@ func init() {
}

func StartMetricsServer(addr string) (*http.Server, error) {
listener, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
return StartMetricsServerWithListener(listener)
}

func StartMetricsServerWithListener(listener net.Listener) (*http.Server, error) {
mux := http.NewServeMux()
mux.Handle(c.MetricsPath, promhttp.Handler())

srv := &http.Server{
Addr: addr,
Handler: mux,
}

go func() {
klog.InfoS("Starting Prometheus metrics server", "address", addr)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
klog.InfoS("Starting Prometheus metrics server", "address", listener.Addr().String())
if err := srv.Serve(listener); err != nil && err != http.ErrServerClosed {
klog.ErrorS(err, "Failed to start metrics server")
}
}()
Expand Down

0 comments on commit 7c0fc42

Please sign in to comment.