From b211e82ed2940856d7e8a486ba736ee6b13db02d Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Sat, 6 Mar 2021 07:23:09 -0700 Subject: [PATCH] Fix logged hostport for metrics endpoint This commit fixes a bug where the bind address is not reported correctly in the log if a hostname is specified. Previously it would always report the host as "localhost" even if a host was specified (such as "0.0.0.0:9090"). This commit also adds validation to require the port to be specified and only specifying a hostname will return an error. --- cmd/litestream/replicate.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cmd/litestream/replicate.go b/cmd/litestream/replicate.go index 0dfb1ae..f748ae1 100644 --- a/cmd/litestream/replicate.go +++ b/cmd/litestream/replicate.go @@ -115,8 +115,14 @@ func (c *ReplicateCommand) Run(ctx context.Context) (err error) { // Serve metrics over HTTP if enabled. if c.Config.Addr != "" { - _, port, _ := net.SplitHostPort(c.Config.Addr) - log.Printf("serving metrics on http://localhost:%s/metrics", port) + hostport := c.Config.Addr + if host, port, _ := net.SplitHostPort(c.Config.Addr); port == "" { + return fmt.Errorf("must specify port for bind address: %q", c.Config.Addr) + } else if host == "" { + hostport = net.JoinHostPort("localhost", port) + } + + log.Printf("serving metrics on http://%s/metrics", hostport) go func() { http.Handle("/metrics", promhttp.Handler()) if err := http.ListenAndServe(c.Config.Addr, nil); err != nil {