Skip to content

Commit

Permalink
update metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
jay-mckay committed Sep 13, 2024
1 parent 4acb95b commit e556078
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ var namespace = "systemd_unit"
var labels = []string{"unit"}
var procLabels = []string{"unit", "proc"}

const NSPerSec = 1000000000 // billion

type Collector struct {
pattern string
collectProc bool
Expand All @@ -43,15 +45,15 @@ type Collector struct {
}

type Metric struct {
memoryAccounting bool
memoryMax uint64
memoryMin uint64
memoryHigh uint64
memoryLow uint64
memoryMax int64
memoryMin int64
memoryHigh int64
memoryLow int64
memoryCurrent uint64
cpuQuota int64
memoryAccounting bool
cpuAccounting bool
cpuUsage uint64
cpuQuota uint64
unit string
processes map[string]*Process
}
Expand Down Expand Up @@ -82,11 +84,11 @@ func NewCollector(pattern string, collectProc bool) *Collector {
"Whether CPU accounting is enabled", labels, nil),
cpuUsage: prometheus.NewDesc(prometheus.BuildFQName(namespace, "cpu", "usage_ns"),
"Total CPU usage", labels, nil),
cpuQuota: prometheus.NewDesc(prometheus.BuildFQName(namespace, "cpu", "quota_ns_per_s"),
cpuQuota: prometheus.NewDesc(prometheus.BuildFQName(namespace, "cpu", "quota_us_per_s"),
"CPU Quota", labels, nil),
procCPU: prometheus.NewDesc(prometheus.BuildFQName(namespace, "proc", "cpu_seconds"),
procCPU: prometheus.NewDesc(prometheus.BuildFQName(namespace, "proc", "cpu_usage_ns"),
"Aggregate CPU usage for this process", procLabels, nil),
procMemory: prometheus.NewDesc(prometheus.BuildFQName(namespace, "proc", "memory_bytes"),
procMemory: prometheus.NewDesc(prometheus.BuildFQName(namespace, "proc", "memory_current_bytes"),
"Aggregate memory usage for this process", procLabels, nil),
procCount: prometheus.NewDesc(prometheus.BuildFQName(namespace, "proc", "count"),
"Instance count of this process", procLabels, nil),
Expand Down Expand Up @@ -156,15 +158,16 @@ func (c *Collector) collectMetrics() []Metric {
continue
}
metric := Metric{
// we cast the 'limits' as int64 so -1 is exported properly
memoryAccounting: props["MemoryAccounting"].(bool),
memoryMax: props["MemoryMax"].(uint64),
memoryMin: props["MemoryMin"].(uint64),
memoryHigh: props["MemoryHigh"].(uint64),
memoryLow: props["MemoryLow"].(uint64),
memoryMax: int64(props["MemoryMax"].(uint64)),
memoryMin: int64(props["MemoryMin"].(uint64)),
memoryHigh: int64(props["MemoryHigh"].(uint64)),
memoryLow: int64(props["MemoryLow"].(uint64)),
memoryCurrent: props["MemoryCurrent"].(uint64),
cpuAccounting: props["CPUAccounting"].(bool),
cpuUsage: props["CPUUsageNSec"].(uint64),
cpuQuota: props["CPUQuotaPerSecUSec"].(uint64),
cpuQuota: int64(props["CPUQuotaPerSecUSec"].(uint64)),
unit: unit.Name,
}
if c.collectProc {
Expand Down Expand Up @@ -219,9 +222,9 @@ func collectProcesses(conn *systemd.Conn, ctx context.Context, unit string) (map

val, ok := processes[comm]
if !ok {
processes[comm] = &Process{cpu: stat.CPUTime(), memory: smaps.Pss, count: 1}
processes[comm] = &Process{cpu: stat.CPUTime() * NSPerSec, memory: smaps.Pss, count: 1}
} else {
val.cpu += stat.CPUTime()
val.cpu += (stat.CPUTime() * NSPerSec)
val.memory += smaps.Pss
val.count += 1
}
Expand Down

0 comments on commit e556078

Please sign in to comment.