From 94f039f41952f9cb337ab210f4848daa97124883 Mon Sep 17 00:00:00 2001 From: mayankpande88 Date: Thu, 8 Oct 2026 13:11:00 +0530 Subject: [PATCH 1/2] fix: honor LISTEN when metrics are pushed With --metrics-endpoint set, the listen address was forced to 127.0.0.1:10300 even when LISTEN or --listen was given, and a taken port made the agent exit, so two pushing agents could not share a host and a port conflict stopped all collection. LISTEN now wins whenever it is set; unset, the defaults stay as before (0.0.0.0:80, or 127.0.0.1:10300 when pushing). When pushing, a failed bind is logged and the agent keeps running, since nothing scrapes the endpoint. --- flags/flags.go | 11 ++++++++--- main.go | 9 ++++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/flags/flags.go b/flags/flags.go index 10fca964..ad95f9a9 100644 --- a/flags/flags.go +++ b/flags/flags.go @@ -9,7 +9,7 @@ import ( ) var ( - ListenAddress = kingpin.Flag("listen", "Listen address - ip:port or :port").Default("0.0.0.0:80").Envar("LISTEN").String() + ListenAddress = kingpin.Flag("listen", "Listen address - ip:port or :port (default 0.0.0.0:80, or 127.0.0.1:10300 when --metrics-endpoint is set)").Envar("LISTEN").String() CgroupRoot = kingpin.Flag("cgroupfs-root", "The mount point of the host cgroupfs root").Default("/sys/fs/cgroup").Envar("CGROUPFS_ROOT").String() DisableLogParsing = kingpin.Flag("disable-log-parsing", "Disable container log parsing").Default("false").Envar("DISABLE_LOG_PARSING").Bool() DisablePinger = kingpin.Flag("disable-pinger", "Don't ping upstreams").Default("true").Envar("DISABLE_PINGER").Bool() @@ -135,7 +135,12 @@ func init() { } } - if *MetricsEndpoint != nil { - *ListenAddress = "127.0.0.1:10300" + if *ListenAddress == "" { + *ListenAddress = "0.0.0.0:80" + if *MetricsEndpoint != nil { + // Metrics are pushed, so nothing needs to scrape this endpoint: + // keep it off the network and off port 80. + *ListenAddress = "127.0.0.1:10300" + } } } diff --git a/main.go b/main.go index 0d6b9fb3..fa24827b 100644 --- a/main.go +++ b/main.go @@ -284,7 +284,14 @@ func main() { klog.Infoln("listening on:", *flags.ListenAddress) if err := srv.ListenAndServe(); err != http.ErrServerClosed { - klog.Errorln(err) + if *flags.MetricsEndpoint == nil { + klog.Errorln(err) + } else { + // Metrics are pushed: the local endpoint is optional, so a + // taken port must not stop the agent. + klog.Errorln("local metrics endpoint unavailable, metrics are still pushed:", err) + <-ctx.Done() + } } klog.Infoln("shutdown complete") } From bf5fc20af8abae4de2c8c4e5c05bd76e7a9c9010 Mon Sep 17 00:00:00 2001 From: mayankpande88 Date: Thu, 8 Oct 2026 13:27:07 +0530 Subject: [PATCH 2/2] fix: exit non-zero when the scrape endpoint can't be bound In scrape mode a failed bind made main return, so the agent exited 0 and a service manager restarting only on failure would leave it down. --- main.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index fa24827b..e5f04149 100644 --- a/main.go +++ b/main.go @@ -285,7 +285,9 @@ func main() { klog.Infoln("listening on:", *flags.ListenAddress) if err := srv.ListenAndServe(); err != http.ErrServerClosed { if *flags.MetricsEndpoint == nil { - klog.Errorln(err) + // Nothing can scrape the agent: exit non-zero so the service + // manager restarts it. + klog.Exitln(err) } else { // Metrics are pushed: the local endpoint is optional, so a // taken port must not stop the agent.