Skip to content

Commit

Permalink
Add type for CPU metrics (#226)
Browse files Browse the repository at this point in the history
to include the CPU times and load stats

Also introduce following additional fields in MemInfo:
- Used
- Free
- Shared
- Cache
- Buffer

So that these can be used in upcoming resource metrics feature.
  • Loading branch information
anjalshireesh authored Sep 15, 2023
1 parent 92f22be commit abd696d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
10 changes: 10 additions & 0 deletions health.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,12 @@ type MemInfo struct {
NodeCommon

Total uint64 `json:"total,omitempty"`
Used uint64 `json:"used,omitempty"`
Free uint64 `json:"free,omitempty"`
Available uint64 `json:"available,omitempty"`
Shared uint64 `json:"shared,omitempty"`
Cache uint64 `json:"cache,omitempty"`
Buffers uint64 `json:"buffer,omitempty"`
SwapSpaceTotal uint64 `json:"swap_space_total,omitempty"`
SwapSpaceFree uint64 `json:"swap_space_free,omitempty"`
// Limit will store cgroup limit if configured and
Expand Down Expand Up @@ -593,7 +598,12 @@ func GetMemInfo(ctx context.Context, addr string) MemInfo {
return MemInfo{
NodeCommon: NodeCommon{Addr: addr},
Total: meminfo.Total,
Used: meminfo.Used,
Free: meminfo.Free,
Available: meminfo.Available,
Shared: meminfo.Shared,
Cache: meminfo.Cached,
Buffers: meminfo.Buffers,
SwapSpaceTotal: swapinfo.Total,
SwapSpaceFree: swapinfo.Free,
Limit: getMemoryLimit(meminfo.Total),
Expand Down
57 changes: 57 additions & 0 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
"time"

"github.com/prometheus/procfs"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/load"
)

// MetricType is a bitfield representation of different metric types.
Expand All @@ -48,6 +50,8 @@ const (
MetricsBatchJobs
MetricsSiteResync
MetricNet
MetricsMem
MetricsCPU

// MetricsAll must be last.
// Enables all metrics.
Expand Down Expand Up @@ -150,6 +154,8 @@ type Metrics struct {
BatchJobs *BatchJobMetrics `json:"batchJobs,omitempty"`
SiteResync *SiteResyncMetrics `json:"siteResync,omitempty"`
Net *NetMetrics `json:"net,omitempty"`
Mem *MemMetrics `json:"mem,omitempty"`
CPU *CPUMetrics `json:"cpu,omitempty"`
}

// Merge other into r.
Expand Down Expand Up @@ -606,3 +612,54 @@ func (n *NetMetrics) Merge(other *NetMetrics) {
n.NetStats.TxCarrier += other.NetStats.TxCarrier
n.NetStats.TxCompressed += other.NetStats.TxCompressed
}

type MemMetrics struct {
// Time these metrics were collected
CollectedAt time.Time `json:"collected"`

Info MemInfo `json:"memInfo"`
}

// Merge other into 'm'.
func (m *MemMetrics) Merge(other *MemMetrics) {
if m.CollectedAt.Before(other.CollectedAt) {
// Use latest timestamp
m.CollectedAt = other.CollectedAt
}

m.Info.Total += other.Info.Total
m.Info.Available += other.Info.Available
m.Info.SwapSpaceTotal += other.Info.SwapSpaceTotal
m.Info.SwapSpaceFree += other.Info.SwapSpaceFree
m.Info.Limit += other.Info.Limit
}

type CPUMetrics struct {
// Time these metrics were collected
CollectedAt time.Time `json:"collected"`

TimesStat *cpu.TimesStat `json:"timesStat"`
LoadStat *load.AvgStat `json:"loadStat"`
}

// Merge other into 'm'.
func (m *CPUMetrics) Merge(other *CPUMetrics) {
if m.CollectedAt.Before(other.CollectedAt) {
// Use latest timestamp
m.CollectedAt = other.CollectedAt
}
m.TimesStat.User += other.TimesStat.User
m.TimesStat.System += other.TimesStat.System
m.TimesStat.Idle += other.TimesStat.Idle
m.TimesStat.Nice += other.TimesStat.Nice
m.TimesStat.Iowait += other.TimesStat.Iowait
m.TimesStat.Irq += other.TimesStat.Irq
m.TimesStat.Softirq += other.TimesStat.Softirq
m.TimesStat.Steal += other.TimesStat.Steal
m.TimesStat.Guest += other.TimesStat.Guest
m.TimesStat.GuestNice += other.TimesStat.GuestNice

m.LoadStat.Load1 += other.LoadStat.Load1
m.LoadStat.Load5 += other.LoadStat.Load5
m.LoadStat.Load15 += other.LoadStat.Load15
}

0 comments on commit abd696d

Please sign in to comment.