khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame^] | 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 | // +build !windows |
| 15 | |
| 16 | package prometheus |
| 17 | |
| 18 | import ( |
| 19 | "github.com/prometheus/procfs" |
| 20 | ) |
| 21 | |
| 22 | func canCollectProcess() bool { |
| 23 | _, err := procfs.NewDefaultFS() |
| 24 | return err == nil |
| 25 | } |
| 26 | |
| 27 | func (c *processCollector) processCollect(ch chan<- Metric) { |
| 28 | pid, err := c.pidFn() |
| 29 | if err != nil { |
| 30 | c.reportError(ch, nil, err) |
| 31 | return |
| 32 | } |
| 33 | |
| 34 | p, err := procfs.NewProc(pid) |
| 35 | if err != nil { |
| 36 | c.reportError(ch, nil, err) |
| 37 | return |
| 38 | } |
| 39 | |
| 40 | if stat, err := p.Stat(); err == nil { |
| 41 | ch <- MustNewConstMetric(c.cpuTotal, CounterValue, stat.CPUTime()) |
| 42 | ch <- MustNewConstMetric(c.vsize, GaugeValue, float64(stat.VirtualMemory())) |
| 43 | ch <- MustNewConstMetric(c.rss, GaugeValue, float64(stat.ResidentMemory())) |
| 44 | if startTime, err := stat.StartTime(); err == nil { |
| 45 | ch <- MustNewConstMetric(c.startTime, GaugeValue, startTime) |
| 46 | } else { |
| 47 | c.reportError(ch, c.startTime, err) |
| 48 | } |
| 49 | } else { |
| 50 | c.reportError(ch, nil, err) |
| 51 | } |
| 52 | |
| 53 | if fds, err := p.FileDescriptorsLen(); err == nil { |
| 54 | ch <- MustNewConstMetric(c.openFDs, GaugeValue, float64(fds)) |
| 55 | } else { |
| 56 | c.reportError(ch, c.openFDs, err) |
| 57 | } |
| 58 | |
| 59 | if limits, err := p.Limits(); err == nil { |
| 60 | ch <- MustNewConstMetric(c.maxFDs, GaugeValue, float64(limits.OpenFiles)) |
| 61 | ch <- MustNewConstMetric(c.maxVsize, GaugeValue, float64(limits.AddressSpace)) |
| 62 | } else { |
| 63 | c.reportError(ch, nil, err) |
| 64 | } |
| 65 | } |