Update process-exporter methods

This commit is contained in:
Manuel Alejandro de Brito Fontes 2018-12-05 13:28:10 -03:00
parent 2fa55eabf6
commit 1fed943b3d
3 changed files with 25 additions and 20 deletions

View file

@ -70,8 +70,7 @@ func TestFlagConflict(t *testing.T) {
defer func() { os.Args = oldArgs }()
os.Args = []string{"cmd", "--publish-service", "namespace/test", "--http-port", "0", "--https-port", "0", "--publish-status-address", "1.1.1.1"}
_, c, err := parseFlags()
t.Logf("%v", c)
_, _, err := parseFlags()
if err == nil {
t.Fatalf("Expected an error parsing flags but none returned")
}

View file

@ -24,9 +24,9 @@ import (
"syscall"
"time"
"github.com/golang/glog"
ps "github.com/mitchellh/go-ps"
"github.com/ncabatoff/process-exporter/proc"
"k8s.io/klog"
)
// IsRespawnIfRequired checks if error type is exec.ExitError or not
@ -37,7 +37,7 @@ func IsRespawnIfRequired(err error) bool {
}
waitStatus := exitError.Sys().(syscall.WaitStatus)
glog.Warningf(`
klog.Warningf(`
-------------------------------------------------------------------------------
NGINX master process died (%v): %v
-------------------------------------------------------------------------------
@ -56,9 +56,9 @@ func WaitUntilPortIsAvailable(port int) {
}
conn.Close()
// kill nginx worker processes
fs, err := proc.NewFS("/proc")
fs, err := proc.NewFS("/proc", false)
if err != nil {
glog.Errorf("unexpected error reading /proc information: %v", err)
klog.Errorf("unexpected error reading /proc information: %v", err)
continue
}
@ -66,14 +66,14 @@ func WaitUntilPortIsAvailable(port int) {
for _, p := range procs {
pn, err := p.Comm()
if err != nil {
glog.Errorf("unexpected error obtaining process information: %v", err)
klog.Errorf("unexpected error obtaining process information: %v", err)
continue
}
if pn == "nginx" {
osp, err := os.FindProcess(p.PID)
if err != nil {
glog.Errorf("unexpected error obtaining process information: %v", err)
klog.Errorf("unexpected error obtaining process information: %v", err)
continue
}
osp.Signal(syscall.SIGQUIT)

View file

@ -17,9 +17,10 @@ limitations under the License.
package collectors
import (
"fmt"
"path/filepath"
"github.com/golang/glog"
"k8s.io/klog"
common "github.com/ncabatoff/process-exporter"
"github.com/ncabatoff/process-exporter/proc"
@ -37,7 +38,7 @@ type Stopable interface {
Stop()
}
// BinaryNameMatcher ...
// BinaryNameMatcher define a namer using the binary name
type BinaryNameMatcher struct {
Name string
Binary string
@ -45,7 +46,7 @@ type BinaryNameMatcher struct {
// MatchAndName returns false if the match failed, otherwise
// true and the resulting name.
func (em BinaryNameMatcher) MatchAndName(nacl common.NameAndCmdline) (bool, string) {
func (em BinaryNameMatcher) MatchAndName(nacl common.ProcAttributes) (bool, string) {
if len(nacl.Cmdline) == 0 {
return false, ""
}
@ -53,6 +54,11 @@ func (em BinaryNameMatcher) MatchAndName(nacl common.NameAndCmdline) (bool, stri
return em.Name == cmd, ""
}
// String returns the name of the binary to match
func (m BinaryNameMatcher) String() string {
return fmt.Sprintf("%+v", m.Binary)
}
type namedProcessData struct {
numProcs *prometheus.Desc
cpuSecs *prometheus.Desc
@ -86,7 +92,7 @@ var binary = "/usr/bin/nginx"
// NewNGINXProcess returns a new prometheus collector for the nginx process
func NewNGINXProcess(pod, namespace, ingressClass string) (NGINXProcessCollector, error) {
fs, err := proc.NewFS("/proc")
fs, err := proc.NewFS("/proc", false)
if err != nil {
return nil, err
}
@ -98,11 +104,11 @@ func NewNGINXProcess(pod, namespace, ingressClass string) (NGINXProcessCollector
p := namedProcess{
scrapeChan: make(chan scrapeRequest),
Grouper: proc.NewGrouper(true, nm),
Grouper: proc.NewGrouper(nm, true, false, false),
fs: fs,
}
_, err = p.Update(p.fs.AllProcs())
_, _, err = p.Update(p.fs.AllProcs())
if err != nil {
return nil, err
}
@ -184,23 +190,23 @@ func (p namedProcess) Stop() {
}
func (p namedProcess) scrape(ch chan<- prometheus.Metric) {
_, err := p.Update(p.fs.AllProcs())
_, groups, err := p.Update(p.fs.AllProcs())
if err != nil {
glog.Warningf("unexpected error obtaining nginx process info: %v", err)
klog.Warningf("unexpected error obtaining nginx process info: %v", err)
return
}
for _, gcounts := range p.Groups() {
for _, gcounts := range groups {
ch <- prometheus.MustNewConstMetric(p.data.numProcs,
prometheus.GaugeValue, float64(gcounts.Procs))
ch <- prometheus.MustNewConstMetric(p.data.memResidentbytes,
prometheus.GaugeValue, float64(gcounts.Memresident))
prometheus.GaugeValue, float64(gcounts.Memory.ResidentBytes))
ch <- prometheus.MustNewConstMetric(p.data.memVirtualbytes,
prometheus.GaugeValue, float64(gcounts.Memvirtual))
prometheus.GaugeValue, float64(gcounts.Memory.VirtualBytes))
ch <- prometheus.MustNewConstMetric(p.data.startTime,
prometheus.GaugeValue, float64(gcounts.OldestStartTime.Unix()))
ch <- prometheus.MustNewConstMetric(p.data.cpuSecs,
prometheus.CounterValue, gcounts.Cpu)
prometheus.CounterValue, gcounts.CPUSystemTime)
ch <- prometheus.MustNewConstMetric(p.data.readBytes,
prometheus.CounterValue, float64(gcounts.ReadBytes))
ch <- prometheus.MustNewConstMetric(p.data.writeBytes,