2016-07-20 23:11:28 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2020-01-29 16:51:01 +00:00
|
|
|
"strings"
|
2016-07-20 23:11:28 +00:00
|
|
|
|
2019-08-05 14:37:47 +00:00
|
|
|
metrics "github.com/docker/go-metrics"
|
2017-07-26 21:42:13 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-07-20 23:11:28 +00:00
|
|
|
)
|
|
|
|
|
2020-01-29 16:51:01 +00:00
|
|
|
func startMetricsServer(addr string) error {
|
2019-09-11 11:40:11 +00:00
|
|
|
if addr == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-20 23:11:28 +00:00
|
|
|
if err := allocateDaemonPort(addr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
l, err := net.Listen("tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.Handle("/metrics", metrics.Handler())
|
|
|
|
go func() {
|
2020-01-29 16:51:01 +00:00
|
|
|
logrus.Infof("metrics API listening on %s", l.Addr())
|
|
|
|
if err := http.Serve(l, mux); err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
|
|
|
|
logrus.WithError(err).Error("error serving metrics API")
|
2016-07-20 23:11:28 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|