fix: normalize CPU by core count in metric alerts (#4754)

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Amir Raminfar
2026-05-28 17:55:29 -07:00
committed by GitHub
parent 7bbddd90cb
commit 6cdf81e4bb
2 changed files with 18 additions and 8 deletions
+5 -5
View File
@@ -176,11 +176,11 @@ Metric alerts fire when a container's CPU or memory usage crosses a threshold. T
Available properties:
| Property | Type | Description |
| ------------- | ------ | ----------------------------------------------- |
| `cpu` | number | CPU usage percentage (0100, per core-adjusted) |
| `memory` | number | Memory usage percentage (0100) |
| `memoryUsage` | number | Memory usage in bytes |
| Property | Type | Description |
| ------------- | ------ | --------------------------------------------- |
| `cpu` | number | CPU usage percentage (0100), matching the UI |
| `memory` | number | Memory usage percentage (0100) |
| `memoryUsage` | number | Memory usage in bytes |
### Cooldown & Sample Window
+13 -3
View File
@@ -114,8 +114,18 @@ func (m *Manager) processStatEvents() {
// processStatEvent processes a single stat event and sends notifications for matching metric subscriptions
func (m *Manager) processStatEvent(event *ContainerStatEvent) {
// Normalize CPU by core count so alerts report overall load (0-100%),
// matching the UI. Stat.CPUPercent is per-core (100% = one full core).
cores := event.Container.CPULimit
if cores <= 0 {
cores = float64(event.Host.NCPU)
}
if cores <= 0 {
cores = 1
}
notificationStat := types.NotificationStat{
CPUPercent: event.Stat.CPUPercent,
CPUPercent: event.Stat.CPUPercent / cores,
MemoryPercent: event.Stat.MemoryPercent,
MemoryUsage: event.Stat.MemoryUsage,
Mounts: FromContainerMounts(event.Container),
@@ -154,8 +164,8 @@ func (m *Manager) processStatEvent(event *ContainerStatEvent) {
log.Debug().
Str("containerID", event.Stat.ID).
Float64("cpu", event.Stat.CPUPercent).
Float64("memory", event.Stat.MemoryPercent).
Float64("cpu", notificationStat.CPUPercent).
Float64("memory", notificationStat.MemoryPercent).
Str("subscription", sub.Name).
Msg("Metric alert triggered")