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