Skip to content

Commit

Permalink
Metrics on 127.0.0.1 on macOS, 0.0.0.0 otheriwse
Browse files Browse the repository at this point in the history
since macOS complains about any non-signed binary that wants to listen
on non-loopback address, this keeps this warning quiet. However,
when deploying to Kubernetes for example, 0.0.0.0 is what we want
  • Loading branch information
mkuratczyk committed Feb 5, 2024
1 parent 50c7546 commit 3d2f0da
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net/http"
"runtime"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -68,7 +69,7 @@ func GetMetricsServer() *MetricsServer {
metricsServer =
&MetricsServer{
httpServer: &http.Server{
Addr: "127.0.0.1:8080",
Addr: get_metrics_ip() + ":8080",
Handler: promhttp.Handler(),
},
}
Expand All @@ -92,7 +93,7 @@ func (m MetricsServer) Start() {
err := m.httpServer.ListenAndServe()
if errors.Is(err, syscall.EADDRINUSE) {
port, _ := strconv.Atoi(strings.Split(m.httpServer.Addr, ":")[1])
m.httpServer.Addr = "127.0.0.1:" + fmt.Sprint(port+1)
m.httpServer.Addr = get_metrics_ip() + ":" + fmt.Sprint(port+1)
log.Info("Prometheus metrics: port already in use, trying the next one", "port", m.httpServer.Addr)
}
}
Expand Down Expand Up @@ -128,3 +129,12 @@ func (m MetricsServer) PrintMetrics() {
}

}

func get_metrics_ip() string {
// on macoOS, return 127.0.0.1, otherwise 0.0.0.0
if runtime.GOOS == "darwin" {
return "127.0.0.1"
} else {
return "0.0.0.0"
}
}

0 comments on commit 3d2f0da

Please sign in to comment.