| 1 |
// Copyright 2019 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 |
//go:build !windows && !js && !wasip1 && !darwin |
| 15 |
// +build !windows,!js,!wasip1,!darwin |
| 16 |
|
| 17 |
package prometheus |
| 18 |
|
| 19 |
import ( |
| 20 |
"github.com/prometheus/procfs" |
| 21 |
) |
| 22 |
|
| 23 |
func canCollectProcess() bool { |
| 24 |
_, err := procfs.NewDefaultFS() |
| 25 |
return err == nil |
| 26 |
} |
| 27 |
|
| 28 |
func (c *processCollector) processCollect(ch chan<- Metric) { |
| 29 |
pid, err := c.pidFn() |
| 30 |
if err != nil { |
| 31 |
c.reportError(ch, nil, err) |
| 32 |
return |
| 33 |
} |
| 34 |
|
| 35 |
p, err := procfs.NewProc(pid) |
| 36 |
if err != nil { |
| 37 |
c.reportError(ch, nil, err) |
| 38 |
return |
| 39 |
} |
| 40 |
|
| 41 |
if stat, err := p.Stat(); err == nil { |
| 42 |
ch <- MustNewConstMetric(c.cpuTotal, CounterValue, stat.CPUTime()) |
| 43 |
ch <- MustNewConstMetric(c.vsize, GaugeValue, float64(stat.VirtualMemory())) |
| 44 |
ch <- MustNewConstMetric(c.rss, GaugeValue, float64(stat.ResidentMemory())) |
| 45 |
if startTime, err := stat.StartTime(); err == nil { |
| 46 |
ch <- MustNewConstMetric(c.startTime, GaugeValue, startTime) |
| 47 |
} else { |
| 48 |
c.reportError(ch, c.startTime, err) |
| 49 |
} |
| 50 |
} else { |
| 51 |
c.reportError(ch, nil, err) |
| 52 |
} |
| 53 |
|
| 54 |
if fds, err := p.FileDescriptorsLen(); err == nil { |
| 55 |
ch <- MustNewConstMetric(c.openFDs, GaugeValue, float64(fds)) |
| 56 |
} else { |
| 57 |
c.reportError(ch, c.openFDs, err) |
| 58 |
} |
| 59 |
|
| 60 |
if limits, err := p.Limits(); err == nil { |
| 61 |
ch <- MustNewConstMetric(c.maxFDs, GaugeValue, float64(limits.OpenFiles)) |
| 62 |
ch <- MustNewConstMetric(c.maxVsize, GaugeValue, float64(limits.AddressSpace)) |
| 63 |
} else { |
| 64 |
c.reportError(ch, nil, err) |
| 65 |
} |
| 66 |
|
| 67 |
if netstat, err := p.Netstat(); err == nil { |
| 68 |
var inOctets, outOctets float64 |
| 69 |
if netstat.InOctets != nil { |
| 70 |
inOctets = *netstat.InOctets |
| 71 |
} |
| 72 |
if netstat.OutOctets != nil { |
| 73 |
outOctets = *netstat.OutOctets |
| 74 |
} |
| 75 |
ch <- MustNewConstMetric(c.inBytes, CounterValue, inOctets) |
| 76 |
ch <- MustNewConstMetric(c.outBytes, CounterValue, outOctets) |
| 77 |
} else { |
| 78 |
c.reportError(ch, nil, err) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
// describe returns all descriptions of the collector for others than windows, js, wasip1 and darwin. |
| 83 |
// Ensure that this list of descriptors is kept in sync with the metrics collected |
| 84 |
// in the processCollect method. Any changes to the metrics in processCollect |
| 85 |
// (such as adding or removing metrics) should be reflected in this list of descriptors. |
| 86 |
func (c *processCollector) describe(ch chan<- *Desc) { |
| 87 |
ch <- c.cpuTotal |
| 88 |
ch <- c.openFDs |
| 89 |
ch <- c.maxFDs |
| 90 |
ch <- c.vsize |
| 91 |
ch <- c.maxVsize |
| 92 |
ch <- c.rss |
| 93 |
ch <- c.startTime |
| 94 |
ch <- c.inBytes |
| 95 |
ch <- c.outBytes |
| 96 |
} |