metrics.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "sync"
  4. "github.com/docker/docker/errdefs"
  5. "github.com/docker/docker/pkg/plugingetter"
  6. "github.com/docker/docker/pkg/plugins"
  7. metrics "github.com/docker/go-metrics"
  8. "github.com/pkg/errors"
  9. "github.com/prometheus/client_golang/prometheus"
  10. "github.com/sirupsen/logrus"
  11. )
  12. const metricsPluginType = "MetricsCollector"
  13. var (
  14. metricsNS = metrics.NewNamespace("engine", "daemon", nil)
  15. containerActions = metricsNS.NewLabeledTimer("container_actions", "The number of seconds it takes to process each container action", "action")
  16. networkActions = metricsNS.NewLabeledTimer("network_actions", "The number of seconds it takes to process each network action", "action")
  17. hostInfoFunctions = metricsNS.NewLabeledTimer("host_info_functions", "The number of seconds it takes to call functions gathering info about the host", "function")
  18. engineInfo = metricsNS.NewLabeledGauge("engine", "The information related to the engine and the OS it is running on", metrics.Unit("info"),
  19. "version",
  20. "commit",
  21. "architecture",
  22. "graphdriver",
  23. "kernel",
  24. "os",
  25. "os_type",
  26. "os_version",
  27. "daemon_id", // ID is a randomly generated unique identifier (e.g. UUID4)
  28. )
  29. engineCpus = metricsNS.NewGauge("engine_cpus", "The number of cpus that the host system of the engine has", metrics.Unit("cpus"))
  30. engineMemory = metricsNS.NewGauge("engine_memory", "The number of bytes of memory that the host system of the engine has", metrics.Bytes)
  31. healthChecksCounter = metricsNS.NewCounter("health_checks", "The total number of health checks")
  32. healthChecksFailedCounter = metricsNS.NewCounter("health_checks_failed", "The total number of failed health checks")
  33. stateCtr = newStateCounter(metricsNS, metricsNS.NewDesc("container_states", "The count of containers in various states", metrics.Unit("containers"), "state"))
  34. )
  35. func init() {
  36. for _, a := range []string{
  37. "start",
  38. "changes",
  39. "commit",
  40. "create",
  41. "delete",
  42. } {
  43. containerActions.WithValues(a).Update(0)
  44. }
  45. metrics.Register(metricsNS)
  46. }
  47. type stateCounter struct {
  48. mu sync.RWMutex
  49. states map[string]string
  50. desc *prometheus.Desc
  51. }
  52. func newStateCounter(ns *metrics.Namespace, desc *prometheus.Desc) *stateCounter {
  53. c := &stateCounter{
  54. states: make(map[string]string),
  55. desc: desc,
  56. }
  57. ns.Add(c)
  58. return c
  59. }
  60. func (ctr *stateCounter) get() (running int, paused int, stopped int) {
  61. ctr.mu.RLock()
  62. defer ctr.mu.RUnlock()
  63. states := map[string]int{
  64. "running": 0,
  65. "paused": 0,
  66. "stopped": 0,
  67. }
  68. for _, state := range ctr.states {
  69. states[state]++
  70. }
  71. return states["running"], states["paused"], states["stopped"]
  72. }
  73. func (ctr *stateCounter) set(id, label string) {
  74. ctr.mu.Lock()
  75. ctr.states[id] = label
  76. ctr.mu.Unlock()
  77. }
  78. func (ctr *stateCounter) del(id string) {
  79. ctr.mu.Lock()
  80. delete(ctr.states, id)
  81. ctr.mu.Unlock()
  82. }
  83. func (ctr *stateCounter) Describe(ch chan<- *prometheus.Desc) {
  84. ch <- ctr.desc
  85. }
  86. func (ctr *stateCounter) Collect(ch chan<- prometheus.Metric) {
  87. running, paused, stopped := ctr.get()
  88. ch <- prometheus.MustNewConstMetric(ctr.desc, prometheus.GaugeValue, float64(running), "running")
  89. ch <- prometheus.MustNewConstMetric(ctr.desc, prometheus.GaugeValue, float64(paused), "paused")
  90. ch <- prometheus.MustNewConstMetric(ctr.desc, prometheus.GaugeValue, float64(stopped), "stopped")
  91. }
  92. func (daemon *Daemon) cleanupMetricsPlugins() {
  93. ls := daemon.PluginStore.GetAllManagedPluginsByCap(metricsPluginType)
  94. var wg sync.WaitGroup
  95. wg.Add(len(ls))
  96. for _, plugin := range ls {
  97. p := plugin
  98. go func() {
  99. defer wg.Done()
  100. adapter, err := makePluginAdapter(p)
  101. if err != nil {
  102. logrus.WithError(err).WithField("plugin", p.Name()).Error("Error creating metrics plugin adapter")
  103. return
  104. }
  105. if err := adapter.StopMetrics(); err != nil {
  106. logrus.WithError(err).WithField("plugin", p.Name()).Error("Error stopping plugin metrics collection")
  107. }
  108. }()
  109. }
  110. wg.Wait()
  111. if daemon.metricsPluginListener != nil {
  112. daemon.metricsPluginListener.Close()
  113. }
  114. }
  115. type metricsPlugin interface {
  116. StartMetrics() error
  117. StopMetrics() error
  118. }
  119. func makePluginAdapter(p plugingetter.CompatPlugin) (metricsPlugin, error) {
  120. if pc, ok := p.(plugingetter.PluginWithV1Client); ok {
  121. return &metricsPluginAdapter{pc.Client(), p.Name()}, nil
  122. }
  123. pa, ok := p.(plugingetter.PluginAddr)
  124. if !ok {
  125. return nil, errdefs.System(errors.Errorf("got unknown plugin type %T", p))
  126. }
  127. if pa.Protocol() != plugins.ProtocolSchemeHTTPV1 {
  128. return nil, errors.Errorf("plugin protocol not supported: %s", pa.Protocol())
  129. }
  130. addr := pa.Addr()
  131. client, err := plugins.NewClientWithTimeout(addr.Network()+"://"+addr.String(), nil, pa.Timeout())
  132. if err != nil {
  133. return nil, errors.Wrap(err, "error creating metrics plugin client")
  134. }
  135. return &metricsPluginAdapter{client, p.Name()}, nil
  136. }
  137. type metricsPluginAdapter struct {
  138. c *plugins.Client
  139. name string
  140. }
  141. func (a *metricsPluginAdapter) StartMetrics() error {
  142. type metricsPluginResponse struct {
  143. Err string
  144. }
  145. var res metricsPluginResponse
  146. if err := a.c.Call(metricsPluginType+".StartMetrics", nil, &res); err != nil {
  147. return errors.Wrap(err, "could not start metrics plugin")
  148. }
  149. if res.Err != "" {
  150. return errors.New(res.Err)
  151. }
  152. return nil
  153. }
  154. func (a *metricsPluginAdapter) StopMetrics() error {
  155. if err := a.c.Call(metricsPluginType+".StopMetrics", nil, nil); err != nil {
  156. return errors.Wrap(err, "error stopping metrics collector")
  157. }
  158. return nil
  159. }