2016-07-20 19:11:28 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
|
2019-08-05 10:37:47 -04:00
|
|
|
metrics "github.com/docker/go-metrics"
|
2019-09-11 07:40:11 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-07-20 19:11:28 -04:00
|
|
|
)
|
|
|
|
|
2019-09-11 07:40:11 -04:00
|
|
|
func (cli *DaemonCli) startMetricsServer(addr string) error {
|
|
|
|
if addr == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cli.d.HasExperimental() {
|
|
|
|
return errors.New("metrics-addr is only supported when experimental is enabled")
|
|
|
|
}
|
|
|
|
|
2016-07-20 19:11:28 -04: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() {
|
|
|
|
if err := http.Serve(l, mux); err != nil {
|
|
|
|
logrus.Errorf("serve metrics api: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|