metrics.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. healthCheckStartDuration = metricsNS.NewTimer("health_check_start_duration", "The number of seconds it takes to prepare to run health checks")
  34. stateCtr = newStateCounter(metricsNS, metricsNS.NewDesc("container_states", "The count of containers in various states", metrics.Unit("containers"), "state"))
  35. )
  36. func init() {
  37. for _, a := range []string{
  38. "start",
  39. "changes",
  40. "commit",
  41. "create",
  42. "delete",
  43. } {
  44. containerActions.WithValues(a).Update(0)
  45. }
  46. metrics.Register(metricsNS)
  47. }
  48. type stateCounter struct {
  49. mu sync.RWMutex
  50. states map[string]string
  51. desc *prometheus.Desc
  52. }
  53. func newStateCounter(ns *metrics.Namespace, desc *prometheus.Desc) *stateCounter {
  54. c := &stateCounter{
  55. states: make(map[string]string),
  56. desc: desc,
  57. }
  58. ns.Add(c)
  59. return c
  60. }
  61. func (ctr *stateCounter) get() (running int, paused int, stopped int) {
  62. ctr.mu.RLock()
  63. defer ctr.mu.RUnlock()
  64. states := map[string]int{
  65. "running": 0,
  66. "paused": 0,
  67. "stopped": 0,
  68. }
  69. for _, state := range ctr.states {
  70. states[state]++
  71. }
  72. return states["running"], states["paused"], states["stopped"]
  73. }
  74. func (ctr *stateCounter) set(id, label string) {
  75. ctr.mu.Lock()
  76. ctr.states[id] = label
  77. ctr.mu.Unlock()
  78. }
  79. func (ctr *stateCounter) del(id string) {
  80. ctr.mu.Lock()
  81. delete(ctr.states, id)
  82. ctr.mu.Unlock()
  83. }
  84. func (ctr *stateCounter) Describe(ch chan<- *prometheus.Desc) {
  85. ch <- ctr.desc
  86. }
  87. func (ctr *stateCounter) Collect(ch chan<- prometheus.Metric) {
  88. running, paused, stopped := ctr.get()
  89. ch <- prometheus.MustNewConstMetric(ctr.desc, prometheus.GaugeValue, float64(running), "running")
  90. ch <- prometheus.MustNewConstMetric(ctr.desc, prometheus.GaugeValue, float64(paused), "paused")
  91. ch <- prometheus.MustNewConstMetric(ctr.desc, prometheus.GaugeValue, float64(stopped), "stopped")
  92. }
  93. func (daemon *Daemon) cleanupMetricsPlugins() {
  94. ls := daemon.PluginStore.GetAllManagedPluginsByCap(metricsPluginType)
  95. var wg sync.WaitGroup
  96. wg.Add(len(ls))
  97. for _, plugin := range ls {
  98. p := plugin
  99. go func() {
  100. defer wg.Done()
  101. adapter, err := makePluginAdapter(p)
  102. if err != nil {
  103. logrus.WithError(err).WithField("plugin", p.Name()).Error("Error creating metrics plugin adapter")
  104. return
  105. }
  106. if err := adapter.StopMetrics(); err != nil {
  107. logrus.WithError(err).WithField("plugin", p.Name()).Error("Error stopping plugin metrics collection")
  108. }
  109. }()
  110. }
  111. wg.Wait()
  112. if daemon.metricsPluginListener != nil {
  113. daemon.metricsPluginListener.Close()
  114. }
  115. }
  116. type metricsPlugin interface {
  117. StartMetrics() error
  118. StopMetrics() error
  119. }
  120. func makePluginAdapter(p plugingetter.CompatPlugin) (metricsPlugin, error) {
  121. if pc, ok := p.(plugingetter.PluginWithV1Client); ok {
  122. return &metricsPluginAdapter{pc.Client(), p.Name()}, nil
  123. }
  124. pa, ok := p.(plugingetter.PluginAddr)
  125. if !ok {
  126. return nil, errdefs.System(errors.Errorf("got unknown plugin type %T", p))
  127. }
  128. if pa.Protocol() != plugins.ProtocolSchemeHTTPV1 {
  129. return nil, errors.Errorf("plugin protocol not supported: %s", pa.Protocol())
  130. }
  131. addr := pa.Addr()
  132. client, err := plugins.NewClientWithTimeout(addr.Network()+"://"+addr.String(), nil, pa.Timeout())
  133. if err != nil {
  134. return nil, errors.Wrap(err, "error creating metrics plugin client")
  135. }
  136. return &metricsPluginAdapter{client, p.Name()}, nil
  137. }
  138. type metricsPluginAdapter struct {
  139. c *plugins.Client
  140. name string
  141. }
  142. func (a *metricsPluginAdapter) StartMetrics() error {
  143. type metricsPluginResponse struct {
  144. Err string
  145. }
  146. var res metricsPluginResponse
  147. if err := a.c.Call(metricsPluginType+".StartMetrics", nil, &res); err != nil {
  148. return errors.Wrap(err, "could not start metrics plugin")
  149. }
  150. if res.Err != "" {
  151. return errors.New(res.Err)
  152. }
  153. return nil
  154. }
  155. func (a *metricsPluginAdapter) StopMetrics() error {
  156. if err := a.c.Call(metricsPluginType+".StopMetrics", nil, nil); err != nil {
  157. return errors.Wrap(err, "error stopping metrics collector")
  158. }
  159. return nil
  160. }