ingress-nginx-helm/internal/ingress/metric/collectors/process.go

220 lines
5.8 KiB
Go
Raw Normal View History

/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2018-07-07 17:46:18 +00:00
package collectors
import (
2018-12-05 16:28:10 +00:00
"fmt"
"path/filepath"
2020-08-08 23:31:02 +00:00
"k8s.io/klog/v2"
2017-10-06 22:47:46 +00:00
common "github.com/ncabatoff/process-exporter"
"github.com/ncabatoff/process-exporter/proc"
"github.com/prometheus/client_golang/prometheus"
)
type scrapeRequest struct {
results chan<- prometheus.Metric
done chan struct{}
}
// Stopable defines a prometheus collector that can be stopped
type Stopable interface {
prometheus.Collector
Stop()
}
2020-02-06 12:50:13 +00:00
func newBinaryNameMatcher(name, binary string) common.MatchNamer {
return BinaryNameMatcher{
Name: name,
Binary: binary,
}
}
2018-12-05 16:28:10 +00:00
// BinaryNameMatcher define a namer using the binary name
type BinaryNameMatcher struct {
2017-03-12 15:27:05 +00:00
Name string
Binary string
}
2017-03-12 15:27:05 +00:00
// MatchAndName returns false if the match failed, otherwise
// true and the resulting name.
2018-12-05 16:28:10 +00:00
func (em BinaryNameMatcher) MatchAndName(nacl common.ProcAttributes) (bool, string) {
if len(nacl.Cmdline) == 0 {
return false, ""
}
2017-03-12 15:27:05 +00:00
cmd := filepath.Base(em.Binary)
return em.Name == cmd, ""
}
2018-12-05 16:28:10 +00:00
// String returns the name of the binary to match
2018-12-05 16:28:28 +00:00
func (em BinaryNameMatcher) String() string {
return fmt.Sprintf("%+v", em.Binary)
2018-12-05 16:28:10 +00:00
}
2017-03-12 15:27:05 +00:00
type namedProcessData struct {
numProcs *prometheus.Desc
cpuSecs *prometheus.Desc
readBytes *prometheus.Desc
writeBytes *prometheus.Desc
memResidentbytes *prometheus.Desc
memVirtualbytes *prometheus.Desc
startTime *prometheus.Desc
}
type namedProcess struct {
*proc.Grouper
2017-03-12 15:27:05 +00:00
scrapeChan chan scrapeRequest
fs *proc.FS
data namedProcessData
}
2018-07-07 17:46:18 +00:00
const subSystem = "nginx_process"
// NGINXProcessCollector defines a process collector interface
type NGINXProcessCollector interface {
prometheus.Collector
Start()
Stop()
}
var name = "nginx"
var binary = "/usr/bin/nginx"
// NewNGINXProcess returns a new prometheus collector for the nginx process
func NewNGINXProcess(pod, namespace, ingressClass string) (NGINXProcessCollector, error) {
2018-12-05 16:28:10 +00:00
fs, err := proc.NewFS("/proc", false)
if err != nil {
return nil, err
}
2018-07-07 17:46:18 +00:00
2020-02-06 12:50:13 +00:00
nm := newBinaryNameMatcher(name, binary)
2018-07-07 17:46:18 +00:00
p := namedProcess{
scrapeChan: make(chan scrapeRequest),
2020-02-06 12:50:13 +00:00
Grouper: proc.NewGrouper(nm, true, false, false, false),
fs: fs,
}
2018-07-07 17:46:18 +00:00
2018-12-05 16:28:10 +00:00
_, _, err = p.Update(p.fs.AllProcs())
if err != nil {
return nil, err
}
2018-07-07 17:46:18 +00:00
constLabels := prometheus.Labels{
"controller_namespace": namespace,
"controller_class": ingressClass,
"controller_pod": pod,
}
2017-03-12 15:27:05 +00:00
p.data = namedProcessData{
numProcs: prometheus.NewDesc(
2018-07-07 17:46:18 +00:00
prometheus.BuildFQName(PrometheusNamespace, subSystem, "num_procs"),
2017-03-12 15:27:05 +00:00
"number of processes",
2018-07-07 17:46:18 +00:00
nil, constLabels),
2017-03-12 15:27:05 +00:00
cpuSecs: prometheus.NewDesc(
2018-07-07 17:46:18 +00:00
prometheus.BuildFQName(PrometheusNamespace, subSystem, "cpu_seconds_total"),
2017-03-12 15:27:05 +00:00
"Cpu usage in seconds",
2018-07-07 17:46:18 +00:00
nil, constLabels),
2017-03-12 15:27:05 +00:00
readBytes: prometheus.NewDesc(
2018-07-07 17:46:18 +00:00
prometheus.BuildFQName(PrometheusNamespace, subSystem, "read_bytes_total"),
2017-03-12 15:27:05 +00:00
"number of bytes read",
2018-07-07 17:46:18 +00:00
nil, constLabels),
2017-03-12 15:27:05 +00:00
writeBytes: prometheus.NewDesc(
2018-07-07 17:46:18 +00:00
prometheus.BuildFQName(PrometheusNamespace, subSystem, "write_bytes_total"),
2017-03-12 15:27:05 +00:00
"number of bytes written",
2018-07-07 17:46:18 +00:00
nil, constLabels),
2017-03-12 15:27:05 +00:00
memResidentbytes: prometheus.NewDesc(
2018-07-07 17:46:18 +00:00
prometheus.BuildFQName(PrometheusNamespace, subSystem, "resident_memory_bytes"),
2017-03-12 15:27:05 +00:00
"number of bytes of memory in use",
2018-07-07 17:46:18 +00:00
nil, constLabels),
2017-03-12 15:27:05 +00:00
memVirtualbytes: prometheus.NewDesc(
2018-07-07 17:46:18 +00:00
prometheus.BuildFQName(PrometheusNamespace, subSystem, "virtual_memory_bytes"),
2017-03-12 15:27:05 +00:00
"number of bytes of memory in use",
2018-07-07 17:46:18 +00:00
nil, constLabels),
2017-03-12 15:27:05 +00:00
startTime: prometheus.NewDesc(
2018-07-07 17:46:18 +00:00
prometheus.BuildFQName(PrometheusNamespace, subSystem, "oldest_start_time_seconds"),
2017-03-12 15:27:05 +00:00
"start time in seconds since 1970/01/01",
2018-07-07 17:46:18 +00:00
nil, constLabels),
2017-03-12 15:27:05 +00:00
}
return p, nil
}
// Describe implements prometheus.Collector.
func (p namedProcess) Describe(ch chan<- *prometheus.Desc) {
2017-03-12 15:27:05 +00:00
ch <- p.data.cpuSecs
ch <- p.data.numProcs
ch <- p.data.readBytes
ch <- p.data.writeBytes
ch <- p.data.memResidentbytes
ch <- p.data.memVirtualbytes
ch <- p.data.startTime
}
// Collect implements prometheus.Collector.
func (p namedProcess) Collect(ch chan<- prometheus.Metric) {
req := scrapeRequest{results: ch, done: make(chan struct{})}
p.scrapeChan <- req
<-req.done
}
2018-07-07 17:46:18 +00:00
func (p namedProcess) Start() {
for req := range p.scrapeChan {
ch := req.results
p.scrape(ch)
req.done <- struct{}{}
}
}
func (p namedProcess) Stop() {
close(p.scrapeChan)
}
func (p namedProcess) scrape(ch chan<- prometheus.Metric) {
2018-12-05 16:28:10 +00:00
_, groups, err := p.Update(p.fs.AllProcs())
if err != nil {
2018-12-05 16:28:10 +00:00
klog.Warningf("unexpected error obtaining nginx process info: %v", err)
return
}
2018-12-05 16:28:10 +00:00
for _, gcounts := range groups {
2017-03-12 15:27:05 +00:00
ch <- prometheus.MustNewConstMetric(p.data.numProcs,
prometheus.GaugeValue, float64(gcounts.Procs))
2017-03-12 15:27:05 +00:00
ch <- prometheus.MustNewConstMetric(p.data.memResidentbytes,
2018-12-05 16:28:10 +00:00
prometheus.GaugeValue, float64(gcounts.Memory.ResidentBytes))
2017-03-12 15:27:05 +00:00
ch <- prometheus.MustNewConstMetric(p.data.memVirtualbytes,
2018-12-05 16:28:10 +00:00
prometheus.GaugeValue, float64(gcounts.Memory.VirtualBytes))
2017-03-12 15:27:05 +00:00
ch <- prometheus.MustNewConstMetric(p.data.startTime,
prometheus.GaugeValue, float64(gcounts.OldestStartTime.Unix()))
2017-03-12 15:27:05 +00:00
ch <- prometheus.MustNewConstMetric(p.data.cpuSecs,
2018-12-05 16:28:10 +00:00
prometheus.CounterValue, gcounts.CPUSystemTime)
2017-03-12 15:27:05 +00:00
ch <- prometheus.MustNewConstMetric(p.data.readBytes,
prometheus.CounterValue, float64(gcounts.ReadBytes))
2017-03-12 15:27:05 +00:00
ch <- prometheus.MustNewConstMetric(p.data.writeBytes,
prometheus.CounterValue, float64(gcounts.WriteBytes))
}
}