Skip to content

Commit

Permalink
Add "nomad.client.host.cpu.total_ticks" to metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
TrueBrain committed Jun 17, 2023
1 parent 80e1ad6 commit a7a62d7
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
5 changes: 4 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3039,7 +3039,10 @@ func (c *Client) setGaugeForCPUStats(nodeID string, hStats *stats.HostStats, bas
Value: cpu.CPU,
})

metrics.SetGaugeWithLabels([]string{"client", "host", "cpu", "total"}, float32(cpu.Total), labels)
// Keep "total" around to remain compatible with older consumers of the metrics
metrics.SetGaugeWithLabels([]string{"client", "host", "cpu", "total"}, float32(cpu.TotalPercent), labels)
metrics.SetGaugeWithLabels([]string{"client", "host", "cpu", "total_percent"}, float32(cpu.TotalPercent), labels)
metrics.SetGaugeWithLabels([]string{"client", "host", "cpu", "total_ticks"}, float32(cpu.TotalTicks), labels)
metrics.SetGaugeWithLabels([]string{"client", "host", "cpu", "user"}, float32(cpu.User), labels)
metrics.SetGaugeWithLabels([]string{"client", "host", "cpu", "idle"}, float32(cpu.Idle), labels)
metrics.SetGaugeWithLabels([]string{"client", "host", "cpu", "system"}, float32(cpu.System), labels)
Expand Down
14 changes: 8 additions & 6 deletions client/stats/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,16 @@ func (h *HostStatsCollector) collectCPUStats() (cpus []*CPUStats, totalTicks flo
h.statsCalculator[cpuStat.CPU] = percentCalculator
}
idle, user, system, total := percentCalculator.Calculate(cpuStat)
ticks := (total / 100.0) * (float64(shelpers.TotalTicksAvailable()) / float64(len(cpuStats)))
cs[idx] = &CPUStats{
CPU: cpuStat.CPU,
User: user,
System: system,
Idle: idle,
Total: total,
CPU: cpuStat.CPU,
User: user,
System: system,
Idle: idle,
TotalPercent: total,
TotalTicks: ticks,
}
ticksConsumed += (total / 100.0) * (float64(shelpers.TotalTicksAvailable()) / float64(len(cpuStats)))
ticksConsumed += ticks
}

return cs, ticksConsumed, nil
Expand Down
6 changes: 4 additions & 2 deletions client/stats/cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ func TestHostStats_CPU(t *testing.T) {

for _, cpu := range stats.CPU {
assert.False(math.IsNaN(cpu.Idle))
assert.False(math.IsNaN(cpu.Total))
assert.False(math.IsNaN(cpu.TotalPercent))
assert.False(math.IsNaN(cpu.TotalTicks))
assert.False(math.IsNaN(cpu.System))
assert.False(math.IsNaN(cpu.User))

assert.False(math.IsInf(cpu.Idle, 0))
assert.False(math.IsInf(cpu.Total, 0))
assert.False(math.IsInf(cpu.TotalPercent, 0))
assert.False(math.IsInf(cpu.TotalTicks, 0))
assert.False(math.IsInf(cpu.System, 0))
assert.False(math.IsInf(cpu.User, 0))
}
Expand Down
11 changes: 6 additions & 5 deletions client/stats/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ type MemoryStats struct {

// CPUStats represents stats related to cpu usage
type CPUStats struct {
CPU string
User float64
System float64
Idle float64
Total float64
CPU string
User float64
System float64
Idle float64
TotalPercent float64
TotalTicks float64
}

// DiskStats represents stats related to disk usage
Expand Down
3 changes: 2 additions & 1 deletion website/content/docs/operations/metrics-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ Nomad will emit [tagged metrics][tagged-metrics], in the below format:
| `nomad.client.allocs.oom_killed` | Number of allocations OOM killed | Integer | Gauge | datacenter, host, node_class, node_id, node_pool, node_scheduling_eligibility, node_status |
| `nomad.client.host.cpu.idle` | CPU utilization in idle state | Percentage | Gauge | cpu, datacenter, host, node_class, node_id, node_pool, node_scheduling_eligibility, node_status |
| `nomad.client.host.cpu.system` | CPU utilization in system space | Percentage | Gauge | cpu, datacenter, host, node_class, node_id, node_pool, node_scheduling_eligibility, node_status |
| `nomad.client.host.cpu.total` | Total CPU utilization | Percentage | Gauge | cpu, datacenter, host, node_class, node_id, node_pool, node_scheduling_eligibility, node_status |
| `nomad.client.host.cpu.total_percent` | Total CPU utilization in percentage | Percentage | Gauge | cpu, datacenter, host, node_class, node_id, node_pool, node_scheduling_eligibility, node_status |
| `nomad.client.host.cpu.total_ticks` | Total CPU utilization in ticks | Integer | Gauge | cpu, datacenter, host, node_class, node_id, node_pool, node_scheduling_eligibility, node_status |
| `nomad.client.host.cpu.user` | CPU utilization in user space | Percentage | Gauge | cpu, datacenter, host, node_class, node_id, node_pool, node_scheduling_eligibility, node_status |
| `nomad.client.host.disk.available` | Amount of space which is available | Bytes | Gauge | datacenter, disk, host, node_class, node_id, node_pool, node_scheduling_eligibility, node_status |
| `nomad.client.host.disk.inodes_percent` | Disk space consumed by the inodes | Percentage | Gauge | datacenter, disk, host, node_class, node_id, node_pool, node_scheduling_eligibility, node_status |
Expand Down

0 comments on commit a7a62d7

Please sign in to comment.