Skip to content

Export CPU utilization in observe.Snapshot #97

@FumingPower3925

Description

@FumingPower3925

Problem

The overload middleware needs CPU utilization to make shedding decisions. CPU monitoring exists in internal/cpumon (Linux: /proc/stat, other: runtime/metrics) but is not exposed through the public API.

Unblocks

Change

// observe/collector.go — add to Snapshot:
CPUUtilization float64 // 0.0–1.0; -1 if unavailable

// observe/collector.go — add CPU monitor support to Collector:
func (c *Collector) SetCPUMonitor(m CPUMonitor) { c.cpuMon = m }

// observe/cpumon.go — public interface + constructor:
type CPUMonitor interface {
    Sample() (float64, error)  // returns 0.0–1.0
}

// NewCPUMonitor() CPUMonitor — returns platform-appropriate implementation
// (linux: /proc/stat, other: runtime/metrics)
// Thin wrapper around internal/cpumon to keep the internal package internal.

In Snapshot(), if cpuMon != nil, call Sample() and populate CPUUtilization. Otherwise set to -1.

Performance

CPU sampling only happens during Snapshot() calls (Prometheus scrape, ~every 15s). Zero cost on the hot path. The cpuMon field is a nil-checked interface on Collector, not on Context.

Files

  • observe/collector.go
  • observe/cpumon.go (new)
  • internal/cpumon/ (keep internal, wrap)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions