github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/daemon/metrics_unix.go (about) 1 // +build !windows 2 3 package daemon // import "github.com/docker/docker/daemon" 4 5 import ( 6 "net" 7 "net/http" 8 "path/filepath" 9 "strings" 10 11 "github.com/docker/docker/pkg/plugingetter" 12 "github.com/docker/docker/pkg/plugins" 13 "github.com/docker/docker/plugin" 14 metrics "github.com/docker/go-metrics" 15 specs "github.com/opencontainers/runtime-spec/specs-go" 16 "github.com/pkg/errors" 17 "github.com/sirupsen/logrus" 18 "golang.org/x/sys/unix" 19 ) 20 21 func (daemon *Daemon) listenMetricsSock() (string, error) { 22 path := filepath.Join(daemon.configStore.ExecRoot, "metrics.sock") 23 unix.Unlink(path) 24 l, err := net.Listen("unix", path) 25 if err != nil { 26 return "", errors.Wrap(err, "error setting up metrics plugin listener") 27 } 28 29 mux := http.NewServeMux() 30 mux.Handle("/metrics", metrics.Handler()) 31 go func() { 32 logrus.Debugf("metrics API listening on %s", l.Addr()) 33 if err := http.Serve(l, mux); err != nil && !strings.Contains(err.Error(), "use of closed network connection") { 34 logrus.WithError(err).Error("error serving metrics API") 35 } 36 }() 37 daemon.metricsPluginListener = l 38 return path, nil 39 } 40 41 func registerMetricsPluginCallback(store *plugin.Store, sockPath string) { 42 store.RegisterRuntimeOpt(metricsPluginType, func(s *specs.Spec) { 43 f := plugin.WithSpecMounts([]specs.Mount{ 44 {Type: "bind", Source: sockPath, Destination: "/run/docker/metrics.sock", Options: []string{"bind", "ro"}}, 45 }) 46 f(s) 47 }) 48 store.Handle(metricsPluginType, func(name string, client *plugins.Client) { 49 // Use lookup since nothing in the system can really reference it, no need 50 // to protect against removal 51 p, err := store.Get(name, metricsPluginType, plugingetter.Lookup) 52 if err != nil { 53 return 54 } 55 56 adapter, err := makePluginAdapter(p) 57 if err != nil { 58 logrus.WithError(err).WithField("plugin", p.Name()).Error("Error creating plugin adapter") 59 } 60 if err := adapter.StartMetrics(); err != nil { 61 logrus.WithError(err).WithField("plugin", p.Name()).Error("Error starting metrics collector plugin") 62 } 63 }) 64 }