25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 

165 satır
4.6 KiB

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