metrics_unix.go 2.0 KB

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