| 1 |
// Copyright 2021 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 collectors |
| 15 |
|
| 16 |
import "github.com/prometheus/client_golang/prometheus" |
| 17 |
|
| 18 |
// ProcessCollectorOpts defines the behavior of a process metrics collector |
| 19 |
// created with NewProcessCollector. |
| 20 |
type ProcessCollectorOpts struct { |
| 21 |
// PidFn returns the PID of the process the collector collects metrics |
| 22 |
// for. It is called upon each collection. By default, the PID of the |
| 23 |
// current process is used, as determined on construction time by |
| 24 |
// calling os.Getpid(). |
| 25 |
PidFn func() (int, error) |
| 26 |
// If non-empty, each of the collected metrics is prefixed by the |
| 27 |
// provided string and an underscore ("_"). |
| 28 |
Namespace string |
| 29 |
// If true, any error encountered during collection is reported as an |
| 30 |
// invalid metric (see NewInvalidMetric). Otherwise, errors are ignored |
| 31 |
// and the collected metrics will be incomplete. (Possibly, no metrics |
| 32 |
// will be collected at all.) While that's usually not desired, it is |
| 33 |
// appropriate for the common "mix-in" of process metrics, where process |
| 34 |
// metrics are nice to have, but failing to collect them should not |
| 35 |
// disrupt the collection of the remaining metrics. |
| 36 |
ReportErrors bool |
| 37 |
} |
| 38 |
|
| 39 |
// NewProcessCollector returns a collector which exports the current state of |
| 40 |
// process metrics including CPU, memory and file descriptor usage as well as |
| 41 |
// the process start time. The detailed behavior is defined by the provided |
| 42 |
// ProcessCollectorOpts. The zero value of ProcessCollectorOpts creates a |
| 43 |
// collector for the current process with an empty namespace string and no error |
| 44 |
// reporting. |
| 45 |
// |
| 46 |
// The collector only works on operating systems with a Linux-style proc |
| 47 |
// filesystem and on Microsoft Windows. On other operating systems, it will not |
| 48 |
// collect any metrics. |
| 49 |
func NewProcessCollector(opts ProcessCollectorOpts) prometheus.Collector { |
| 50 |
//nolint:staticcheck // Ignore SA1019 until v2. |
| 51 |
return prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{ |
| 52 |
PidFn: opts.PidFn, |
| 53 |
Namespace: opts.Namespace, |
| 54 |
ReportErrors: opts.ReportErrors, |
| 55 |
}) |
| 56 |
} |