Skip to content

Commit

Permalink
Fix error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
treydock committed Feb 20, 2020
1 parent 903b0fd commit a58b20c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
21 changes: 13 additions & 8 deletions cgroup_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type CgroupMetric struct {
uid string
username string
jobid string
err bool
}

type Exporter struct {
Expand Down Expand Up @@ -237,25 +238,27 @@ func NewExporter(paths []string) *Exporter {
info: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "info"),
"User slice information", []string{"cgroup", "username", "uid", "jobid"}, nil),
collectError: prometheus.NewDesc(prometheus.BuildFQName(namespace, "exporter", "collect_error"),
"Indicates collection error, 0=no error, 1=error", []string{"path"}, nil),
"Indicates collection error, 0=no error, 1=error", []string{"cgroup"}, nil),
}
}

func (e *Exporter) collect(ch chan<- prometheus.Metric) ([]CgroupMetric, error) {
func (e *Exporter) collect() ([]CgroupMetric, error) {
var names []string
var metrics []CgroupMetric
for _, path := range e.paths {
log.Debugf("Loading cgroup path %v", path)
control, err := cgroups.Load(subsystem, cgroups.StaticPath(path))
if err != nil {
log.Errorf("Error loading cgroup subsystem path %s: %s", path, err.Error())
ch <- prometheus.MustNewConstMetric(e.collectError, prometheus.GaugeValue, 1, path)
metric := CgroupMetric{name: path, err: true}
metrics = append(metrics, metric)
continue
}
processes, err := control.Processes(cgroups.Cpuacct, true)
if err != nil {
log.Errorf("Error loading cgroup processes for path %s: %s", path, err.Error())
ch <- prometheus.MustNewConstMetric(e.collectError, prometheus.GaugeValue, 1, path)
metric := CgroupMetric{name: path, err: true}
metrics = append(metrics, metric)
continue
}
log.Debugf("Found %d processes", len(processes))
Expand All @@ -276,7 +279,8 @@ func (e *Exporter) collect(ch chan<- prometheus.Metric) ([]CgroupMetric, error)
})
if err != nil {
log.Errorf("Failed to load cgroups for %s: %s", name, err.Error())
ch <- prometheus.MustNewConstMetric(e.collectError, prometheus.GaugeValue, 1, name)
metric.err = true
metrics = append(metrics, metric)
continue
}
stats, _ := ctrl.Stat(cgroups.IgnoreNotExist)
Expand All @@ -295,7 +299,6 @@ func (e *Exporter) collect(ch chan<- prometheus.Metric) ([]CgroupMetric, error)
getInfo(name, &metric)
metrics = append(metrics, metric)
}
ch <- prometheus.MustNewConstMetric(e.collectError, prometheus.GaugeValue, 0, path)
}

return metrics, nil
Expand All @@ -312,12 +315,14 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- e.swapUsed
ch <- e.swapTotal
ch <- e.swapFailCount
ch <- e.collectError
}

func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
metrics, _ := e.collect(ch)
metrics, _ := e.collect()
for _, m := range metrics {
if m.err {
ch <- prometheus.MustNewConstMetric(e.collectError, prometheus.GaugeValue, 1, m.name)
}
ch <- prometheus.MustNewConstMetric(e.cpuUser, prometheus.GaugeValue, m.cpuUser, m.name)
ch <- prometheus.MustNewConstMetric(e.cpuSystem, prometheus.GaugeValue, m.cpuSystem, m.name)
ch <- prometheus.MustNewConstMetric(e.cpuTotal, prometheus.GaugeValue, m.cpuTotal, m.name)
Expand Down
11 changes: 3 additions & 8 deletions cgroup_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,13 @@
package main

import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
kingpin "gopkg.in/alecthomas/kingpin.v2"
"path/filepath"
"runtime"
"testing"
)

var (
ch = make(chan prometheus.Metric)
)

func TestParseCpuSet(t *testing.T) {
if cpus, err := parseCpuSet("0-2"); err != nil {
t.Errorf("Unexpected error: %s", err.Error())
Expand Down Expand Up @@ -54,7 +49,7 @@ func TestCollectUserSlice(t *testing.T) {
cgroupRoot = &fixture

exporter := NewExporter([]string{"/user.slice"})
metrics, err := exporter.collect(ch)
metrics, err := exporter.collect()
if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
return
Expand Down Expand Up @@ -109,7 +104,7 @@ func TestCollectSLURM(t *testing.T) {
cgroupRoot = &fixture

exporter := NewExporter([]string{"/slurm"})
metrics, err := exporter.collect(ch)
metrics, err := exporter.collect()
if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
return
Expand Down Expand Up @@ -167,7 +162,7 @@ func TestCollectTorque(t *testing.T) {
cgroupRoot = &fixture

exporter := NewExporter([]string{"/torque"})
metrics, err := exporter.collect(ch)
metrics, err := exporter.collect()
if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
return
Expand Down

0 comments on commit a58b20c

Please sign in to comment.