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