metrics.go 5.0 KB

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