github.com/devdivbcp/moby@v17.12.0-ce-rc1.0.20200726071732-2d4bfdc789ad+incompatible/daemon/metrics_unix.go (about)

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