2017-04-14 01:56:50 +00:00
|
|
|
// +build !windows
|
|
|
|
|
2018-02-05 21:05:59 +00:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2017-04-14 01:56:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/plugingetter"
|
|
|
|
"github.com/docker/docker/pkg/plugins"
|
2017-12-13 20:24:51 +00:00
|
|
|
"github.com/docker/docker/plugin"
|
2018-05-19 11:38:54 +00:00
|
|
|
"github.com/docker/go-metrics"
|
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
2017-04-14 01:56:50 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 21:42:13 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-05-23 14:22:32 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2017-04-14 01:56:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (daemon *Daemon) listenMetricsSock() (string, error) {
|
|
|
|
path := filepath.Join(daemon.configStore.ExecRoot, "metrics.sock")
|
2017-05-23 14:22:32 +00:00
|
|
|
unix.Unlink(path)
|
2017-04-14 01:56:50 +00: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 20:24:51 +00: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-14 01:56:50 +00:00
|
|
|
// Use lookup since nothing in the system can really reference it, no need
|
|
|
|
// to protect against removal
|
2017-12-13 20:24:51 +00:00
|
|
|
p, err := store.Get(name, metricsPluginType, plugingetter.Lookup)
|
2017-04-14 01:56:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-25 01:45:00 +00:00
|
|
|
adapter, err := makePluginAdapter(p)
|
|
|
|
if err != nil {
|
2018-10-08 11:15:38 +00:00
|
|
|
logrus.WithError(err).WithField("plugin", p.Name()).Error("Error creating plugin adapter")
|
2018-04-25 01:45:00 +00:00
|
|
|
}
|
|
|
|
if err := adapter.StartMetrics(); err != nil {
|
|
|
|
logrus.WithError(err).WithField("plugin", p.Name()).Error("Error starting metrics collector plugin")
|
2017-04-14 01:56:50 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|