Skip to content

Commit

Permalink
change unit to cgroup label
Browse files Browse the repository at this point in the history
  • Loading branch information
jay-mckay committed Dec 3, 2024
1 parent 969e837 commit 0f7604c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 32 deletions.
28 changes: 8 additions & 20 deletions metrics/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const (

var (
namespace = "cgroup_warden"
labels = []string{"username", "unit"}
procLabels = []string{"username", "unit", "proc"}
labels = []string{"cgroup", "username"}
procLabels = []string{"cgroup", "username", "proc"}
lock = sync.RWMutex{}
)

Expand All @@ -50,7 +50,7 @@ type Collector struct {
}

type Metric struct {
unit string
cgroup string
username string
memoryUsage uint64
cpuUsage float64
Expand All @@ -68,12 +68,12 @@ func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
func (c *Collector) Collect(ch chan<- prometheus.Metric) {
stats := c.CollectMetrics()
for _, s := range stats {
ch <- prometheus.MustNewConstMetric(c.memoryUsage, prometheus.GaugeValue, float64(s.memoryUsage), s.username, s.unit)
ch <- prometheus.MustNewConstMetric(c.cpuUsage, prometheus.CounterValue, s.cpuUsage, s.username, s.unit)
ch <- prometheus.MustNewConstMetric(c.memoryUsage, prometheus.GaugeValue, float64(s.memoryUsage), s.cgroup, s.username)
ch <- prometheus.MustNewConstMetric(c.cpuUsage, prometheus.CounterValue, s.cpuUsage, s.cgroup, s.username)
for name, p := range s.processes {
ch <- prometheus.MustNewConstMetric(c.procCPU, prometheus.CounterValue, float64(p.cpu), s.username, s.unit, name)
ch <- prometheus.MustNewConstMetric(c.procMemory, prometheus.GaugeValue, float64(p.memory), s.username, s.unit, name)
ch <- prometheus.MustNewConstMetric(c.procCount, prometheus.GaugeValue, float64(p.count), s.username, s.unit, name)
ch <- prometheus.MustNewConstMetric(c.procCPU, prometheus.CounterValue, float64(p.cpu), s.cgroup, s.username, name)
ch <- prometheus.MustNewConstMetric(c.procMemory, prometheus.GaugeValue, float64(p.memory), s.cgroup, s.username, name)
ch <- prometheus.MustNewConstMetric(c.procCount, prometheus.GaugeValue, float64(p.count), s.cgroup, s.username, name)
}
}
}
Expand Down Expand Up @@ -194,18 +194,6 @@ func ProcInfo(pids map[uint64]bool) map[string]Process {
return processes
}

var unitRe = regexp.MustCompile(`(user-\d+\.slice)`)

func unitName(cgroup string) (string, error) {
match := unitRe.FindStringSubmatch(cgroup)

if len(match) < 1 {
return "", fmt.Errorf("cannot determine slice from '%s'", cgroup)
}

return match[0], nil
}

var uidRe = regexp.MustCompile(`user-(\d+)\.slice`)

// Looks up the username associated with a user slice cgroup.
Expand Down
7 changes: 1 addition & 6 deletions metrics/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,7 @@ func (l *legacy) CreateMetric(group string, pids pidSet) *Metric {

metric.processes = ProcInfo(pids)

unit, err := unitName(group)
if err != nil {
log.Println(err)
return nil
}
metric.unit = unit
metric.cgroup = group

username, err := lookupUsername(group)
if err != nil {
Expand Down
7 changes: 1 addition & 6 deletions metrics/unified.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,7 @@ func (u *unified) CreateMetric(group string, pids pidSet) *Metric {

metric.processes = ProcInfo(pids)

unit, err := unitName(group)
if err != nil {
log.Println(err)
return nil
}
metric.unit = unit
metric.cgroup = group

username, err := lookupUsername(group)
if err != nil {
Expand Down

0 comments on commit 0f7604c

Please sign in to comment.