process_collector.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright 2015 The Prometheus Authors
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package prometheus
  14. import (
  15. "errors"
  16. "os"
  17. "github.com/prometheus/procfs"
  18. )
  19. type processCollector struct {
  20. collectFn func(chan<- Metric)
  21. pidFn func() (int, error)
  22. reportErrors bool
  23. cpuTotal *Desc
  24. openFDs, maxFDs *Desc
  25. vsize, maxVsize *Desc
  26. rss *Desc
  27. startTime *Desc
  28. }
  29. // ProcessCollectorOpts defines the behavior of a process metrics collector
  30. // created with NewProcessCollector.
  31. type ProcessCollectorOpts struct {
  32. // PidFn returns the PID of the process the collector collects metrics
  33. // for. It is called upon each collection. By default, the PID of the
  34. // current process is used, as determined on construction time by
  35. // calling os.Getpid().
  36. PidFn func() (int, error)
  37. // If non-empty, each of the collected metrics is prefixed by the
  38. // provided string and an underscore ("_").
  39. Namespace string
  40. // If true, any error encountered during collection is reported as an
  41. // invalid metric (see NewInvalidMetric). Otherwise, errors are ignored
  42. // and the collected metrics will be incomplete. (Possibly, no metrics
  43. // will be collected at all.) While that's usually not desired, it is
  44. // appropriate for the common "mix-in" of process metrics, where process
  45. // metrics are nice to have, but failing to collect them should not
  46. // disrupt the collection of the remaining metrics.
  47. ReportErrors bool
  48. }
  49. // NewProcessCollector returns a collector which exports the current state of
  50. // process metrics including CPU, memory and file descriptor usage as well as
  51. // the process start time. The detailed behavior is defined by the provided
  52. // ProcessCollectorOpts. The zero value of ProcessCollectorOpts creates a
  53. // collector for the current process with an empty namespace string and no error
  54. // reporting.
  55. //
  56. // Currently, the collector depends on a Linux-style proc filesystem and
  57. // therefore only exports metrics for Linux.
  58. //
  59. // Note: An older version of this function had the following signature:
  60. //
  61. // NewProcessCollector(pid int, namespace string) Collector
  62. //
  63. // Most commonly, it was called as
  64. //
  65. // NewProcessCollector(os.Getpid(), "")
  66. //
  67. // The following call of the current version is equivalent to the above:
  68. //
  69. // NewProcessCollector(ProcessCollectorOpts{})
  70. func NewProcessCollector(opts ProcessCollectorOpts) Collector {
  71. ns := ""
  72. if len(opts.Namespace) > 0 {
  73. ns = opts.Namespace + "_"
  74. }
  75. c := &processCollector{
  76. reportErrors: opts.ReportErrors,
  77. cpuTotal: NewDesc(
  78. ns+"process_cpu_seconds_total",
  79. "Total user and system CPU time spent in seconds.",
  80. nil, nil,
  81. ),
  82. openFDs: NewDesc(
  83. ns+"process_open_fds",
  84. "Number of open file descriptors.",
  85. nil, nil,
  86. ),
  87. maxFDs: NewDesc(
  88. ns+"process_max_fds",
  89. "Maximum number of open file descriptors.",
  90. nil, nil,
  91. ),
  92. vsize: NewDesc(
  93. ns+"process_virtual_memory_bytes",
  94. "Virtual memory size in bytes.",
  95. nil, nil,
  96. ),
  97. maxVsize: NewDesc(
  98. ns+"process_virtual_memory_max_bytes",
  99. "Maximum amount of virtual memory available in bytes.",
  100. nil, nil,
  101. ),
  102. rss: NewDesc(
  103. ns+"process_resident_memory_bytes",
  104. "Resident memory size in bytes.",
  105. nil, nil,
  106. ),
  107. startTime: NewDesc(
  108. ns+"process_start_time_seconds",
  109. "Start time of the process since unix epoch in seconds.",
  110. nil, nil,
  111. ),
  112. }
  113. if opts.PidFn == nil {
  114. pid := os.Getpid()
  115. c.pidFn = func() (int, error) { return pid, nil }
  116. } else {
  117. c.pidFn = opts.PidFn
  118. }
  119. // Set up process metric collection if supported by the runtime.
  120. if _, err := procfs.NewStat(); err == nil {
  121. c.collectFn = c.processCollect
  122. } else {
  123. c.collectFn = func(ch chan<- Metric) {
  124. c.reportError(ch, nil, errors.New("process metrics not supported on this platform"))
  125. }
  126. }
  127. return c
  128. }
  129. // Describe returns all descriptions of the collector.
  130. func (c *processCollector) Describe(ch chan<- *Desc) {
  131. ch <- c.cpuTotal
  132. ch <- c.openFDs
  133. ch <- c.maxFDs
  134. ch <- c.vsize
  135. ch <- c.maxVsize
  136. ch <- c.rss
  137. ch <- c.startTime
  138. }
  139. // Collect returns the current state of all metrics of the collector.
  140. func (c *processCollector) Collect(ch chan<- Metric) {
  141. c.collectFn(ch)
  142. }
  143. func (c *processCollector) processCollect(ch chan<- Metric) {
  144. pid, err := c.pidFn()
  145. if err != nil {
  146. c.reportError(ch, nil, err)
  147. return
  148. }
  149. p, err := procfs.NewProc(pid)
  150. if err != nil {
  151. c.reportError(ch, nil, err)
  152. return
  153. }
  154. if stat, err := p.NewStat(); err == nil {
  155. ch <- MustNewConstMetric(c.cpuTotal, CounterValue, stat.CPUTime())
  156. ch <- MustNewConstMetric(c.vsize, GaugeValue, float64(stat.VirtualMemory()))
  157. ch <- MustNewConstMetric(c.rss, GaugeValue, float64(stat.ResidentMemory()))
  158. if startTime, err := stat.StartTime(); err == nil {
  159. ch <- MustNewConstMetric(c.startTime, GaugeValue, startTime)
  160. } else {
  161. c.reportError(ch, c.startTime, err)
  162. }
  163. } else {
  164. c.reportError(ch, nil, err)
  165. }
  166. if fds, err := p.FileDescriptorsLen(); err == nil {
  167. ch <- MustNewConstMetric(c.openFDs, GaugeValue, float64(fds))
  168. } else {
  169. c.reportError(ch, c.openFDs, err)
  170. }
  171. if limits, err := p.NewLimits(); err == nil {
  172. ch <- MustNewConstMetric(c.maxFDs, GaugeValue, float64(limits.OpenFiles))
  173. ch <- MustNewConstMetric(c.maxVsize, GaugeValue, float64(limits.AddressSpace))
  174. } else {
  175. c.reportError(ch, nil, err)
  176. }
  177. }
  178. func (c *processCollector) reportError(ch chan<- Metric, desc *Desc, err error) {
  179. if !c.reportErrors {
  180. return
  181. }
  182. if desc == nil {
  183. desc = NewInvalidDesc(err)
  184. }
  185. ch <- NewInvalidMetric(desc, err)
  186. }