2017-04-13 21:56:50 -04:00
|
|
|
// +build !windows
|
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2017-04-13 21:56:50 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/plugingetter"
|
|
|
|
"github.com/docker/docker/pkg/plugins"
|
2017-12-13 15:24:51 -05:00
|
|
|
"github.com/docker/docker/plugin"
|
2017-04-13 21:56:50 -04:00
|
|
|
metrics "github.com/docker/go-metrics"
|
2017-12-13 15:24:51 -05:00
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
2017-04-13 21:56:50 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-05-23 10:22:32 -04:00
|
|
|
"golang.org/x/sys/unix"
|
2017-04-13 21:56:50 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func (daemon *Daemon) listenMetricsSock() (string, error) {
|
|
|
|
path := filepath.Join(daemon.configStore.ExecRoot, "metrics.sock")
|
2017-05-23 10:22:32 -04:00
|
|
|
unix.Unlink(path)
|
2017-04-13 21:56:50 -04:00
|
|
|
l, err := net.Listen("unix", path)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "error setting up metrics plugin listener")
|
|
|
|
}
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.Handle("/metrics", metrics.Handler())
|
|
|
|
go func() {
|
|
|
|
http.Serve(l, mux)
|
|
|
|
}()
|
|
|
|
daemon.metricsPluginListener = l
|
|
|
|
return path, nil
|
|
|
|
}
|
|
|
|
|
2017-12-13 15:24:51 -05:00
|
|
|
func registerMetricsPluginCallback(store *plugin.Store, sockPath string) {
|
|
|
|
store.RegisterRuntimeOpt(metricsPluginType, func(s *specs.Spec) {
|
|
|
|
f := plugin.WithSpecMounts([]specs.Mount{
|
|
|
|
{Type: "bind", Source: sockPath, Destination: "/run/docker/metrics.sock", Options: []string{"bind", "ro"}},
|
|
|
|
})
|
|
|
|
f(s)
|
|
|
|
})
|
|
|
|
store.Handle(metricsPluginType, func(name string, client *plugins.Client) {
|
2017-04-13 21:56:50 -04:00
|
|
|
// Use lookup since nothing in the system can really reference it, no need
|
|
|
|
// to protect against removal
|
2017-12-13 15:24:51 -05:00
|
|
|
p, err := store.Get(name, metricsPluginType, plugingetter.Lookup)
|
2017-04-13 21:56:50 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := pluginStartMetricsCollection(p); err != nil {
|
|
|
|
logrus.WithError(err).WithField("name", name).Error("error while initializing metrics plugin")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|