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.
This commit is contained in:
@@ -115,8 +115,14 @@ func (c *ReplicateCommand) Run(ctx context.Context) (err error) {
|
|||||||
|
|
||||||
// Serve metrics over HTTP if enabled.
|
// Serve metrics over HTTP if enabled.
|
||||||
if c.Config.Addr != "" {
|
if c.Config.Addr != "" {
|
||||||
_, port, _ := net.SplitHostPort(c.Config.Addr)
|
hostport := c.Config.Addr
|
||||||
log.Printf("serving metrics on http://localhost:%s/metrics", port)
|
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() {
|
go func() {
|
||||||
http.Handle("/metrics", promhttp.Handler())
|
http.Handle("/metrics", promhttp.Handler())
|
||||||
if err := http.ListenAndServe(c.Config.Addr, nil); err != nil {
|
if err := http.ListenAndServe(c.Config.Addr, nil); err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user