metrics_unix.go 2.0 KB

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