metrics_unix.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. adapter, err := makePluginAdapter(p)
  46. if err != nil {
  47. logrus.WithError(err).WithField("plugin", p.Name()).Error("Error creating plugin adapater")
  48. }
  49. if err := adapter.StartMetrics(); err != nil {
  50. logrus.WithError(err).WithField("plugin", p.Name()).Error("Error starting metrics collector plugin")
  51. }
  52. })
  53. }