metrics_unix.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // +build !windows
  2. package daemon // import "github.com/docker/docker/daemon"
  3. import (
  4. "net"
  5. "net/http"
  6. "path/filepath"
  7. "github.com/docker/docker/pkg/plugingetter"
  8. "github.com/docker/docker/pkg/plugins"
  9. "github.com/docker/docker/plugin"
  10. "github.com/docker/go-metrics"
  11. "github.com/opencontainers/runtime-spec/specs-go"
  12. "github.com/pkg/errors"
  13. "github.com/sirupsen/logrus"
  14. "golang.org/x/sys/unix"
  15. )
  16. func (daemon *Daemon) listenMetricsSock() (string, error) {
  17. path := filepath.Join(daemon.configStore.ExecRoot, "metrics.sock")
  18. unix.Unlink(path)
  19. l, err := net.Listen("unix", path)
  20. if err != nil {
  21. return "", errors.Wrap(err, "error setting up metrics plugin listener")
  22. }
  23. mux := http.NewServeMux()
  24. mux.Handle("/metrics", metrics.Handler())
  25. go func() {
  26. http.Serve(l, mux)
  27. }()
  28. daemon.metricsPluginListener = l
  29. return path, nil
  30. }
  31. func registerMetricsPluginCallback(store *plugin.Store, sockPath string) {
  32. store.RegisterRuntimeOpt(metricsPluginType, func(s *specs.Spec) {
  33. f := plugin.WithSpecMounts([]specs.Mount{
  34. {Type: "bind", Source: sockPath, Destination: "/run/docker/metrics.sock", Options: []string{"bind", "ro"}},
  35. })
  36. f(s)
  37. })
  38. store.Handle(metricsPluginType, func(name string, client *plugins.Client) {
  39. // Use lookup since nothing in the system can really reference it, no need
  40. // to protect against removal
  41. p, err := store.Get(name, metricsPluginType, plugingetter.Lookup)
  42. if err != nil {
  43. return
  44. }
  45. if err := pluginStartMetricsCollection(p); err != nil {
  46. logrus.WithError(err).WithField("name", name).Error("error while initializing metrics plugin")
  47. }
  48. })
  49. }