process_collector.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. )
  18. type processCollector struct {
  19. collectFn func(chan<- Metric)
  20. pidFn func() (int, error)
  21. reportErrors bool
  22. cpuTotal *Desc
  23. openFDs, maxFDs *Desc
  24. vsize, maxVsize *Desc
  25. rss *Desc
  26. startTime *Desc
  27. }
  28. // ProcessCollectorOpts defines the behavior of a process metrics collector
  29. // created with NewProcessCollector.
  30. type ProcessCollectorOpts struct {
  31. // PidFn returns the PID of the process the collector collects metrics
  32. // for. It is called upon each collection. By default, the PID of the
  33. // current process is used, as determined on construction time by
  34. // calling os.Getpid().
  35. PidFn func() (int, error)
  36. // If non-empty, each of the collected metrics is prefixed by the
  37. // provided string and an underscore ("_").
  38. Namespace string
  39. // If true, any error encountered during collection is reported as an
  40. // invalid metric (see NewInvalidMetric). Otherwise, errors are ignored
  41. // and the collected metrics will be incomplete. (Possibly, no metrics
  42. // will be collected at all.) While that's usually not desired, it is
  43. // appropriate for the common "mix-in" of process metrics, where process
  44. // metrics are nice to have, but failing to collect them should not
  45. // disrupt the collection of the remaining metrics.
  46. ReportErrors bool
  47. }
  48. // NewProcessCollector returns a collector which exports the current state of
  49. // process metrics including CPU, memory and file descriptor usage as well as
  50. // the process start time. The detailed behavior is defined by the provided
  51. // ProcessCollectorOpts. The zero value of ProcessCollectorOpts creates a
  52. // collector for the current process with an empty namespace string and no error
  53. // reporting.
  54. //
  55. // The collector only works on operating systems with a Linux-style proc
  56. // filesystem and on Microsoft Windows. On other operating systems, it will not
  57. // collect any metrics.
  58. func NewProcessCollector(opts ProcessCollectorOpts) Collector {
  59. ns := ""
  60. if len(opts.Namespace) > 0 {
  61. ns = opts.Namespace + "_"
  62. }
  63. c := &processCollector{
  64. reportErrors: opts.ReportErrors,
  65. cpuTotal: NewDesc(
  66. ns+"process_cpu_seconds_total",
  67. "Total user and system CPU time spent in seconds.",
  68. nil, nil,
  69. ),
  70. openFDs: NewDesc(
  71. ns+"process_open_fds",
  72. "Number of open file descriptors.",
  73. nil, nil,
  74. ),
  75. maxFDs: NewDesc(
  76. ns+"process_max_fds",
  77. "Maximum number of open file descriptors.",
  78. nil, nil,
  79. ),
  80. vsize: NewDesc(
  81. ns+"process_virtual_memory_bytes",
  82. "Virtual memory size in bytes.",
  83. nil, nil,
  84. ),
  85. maxVsize: NewDesc(
  86. ns+"process_virtual_memory_max_bytes",
  87. "Maximum amount of virtual memory available in bytes.",
  88. nil, nil,
  89. ),
  90. rss: NewDesc(
  91. ns+"process_resident_memory_bytes",
  92. "Resident memory size in bytes.",
  93. nil, nil,
  94. ),
  95. startTime: NewDesc(
  96. ns+"process_start_time_seconds",
  97. "Start time of the process since unix epoch in seconds.",
  98. nil, nil,
  99. ),
  100. }
  101. if opts.PidFn == nil {
  102. pid := os.Getpid()
  103. c.pidFn = func() (int, error) { return pid, nil }
  104. } else {
  105. c.pidFn = opts.PidFn
  106. }
  107. // Set up process metric collection if supported by the runtime.
  108. if canCollectProcess() {
  109. c.collectFn = c.processCollect
  110. } else {
  111. c.collectFn = func(ch chan<- Metric) {
  112. c.reportError(ch, nil, errors.New("process metrics not supported on this platform"))
  113. }
  114. }
  115. return c
  116. }
  117. // Describe returns all descriptions of the collector.
  118. func (c *processCollector) Describe(ch chan<- *Desc) {
  119. ch <- c.cpuTotal
  120. ch <- c.openFDs
  121. ch <- c.maxFDs
  122. ch <- c.vsize
  123. ch <- c.maxVsize
  124. ch <- c.rss
  125. ch <- c.startTime
  126. }
  127. // Collect returns the current state of all metrics of the collector.
  128. func (c *processCollector) Collect(ch chan<- Metric) {
  129. c.collectFn(ch)
  130. }
  131. func (c *processCollector) reportError(ch chan<- Metric, desc *Desc, err error) {
  132. if !c.reportErrors {
  133. return
  134. }
  135. if desc == nil {
  136. desc = NewInvalidDesc(err)
  137. }
  138. ch <- NewInvalidMetric(desc, err)
  139. }