Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 84 additions & 2 deletions libbeat/cmd/instance/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/elastic/beats/v7/libbeat/metric/system/cpu"
"github.com/elastic/beats/v7/libbeat/metric/system/process"
"github.com/elastic/beats/v7/libbeat/monitoring"
"github.com/elastic/gosigar/cgroup"
)

var (
Expand Down Expand Up @@ -65,10 +66,15 @@ func setupMetrics(name string) error {
}

func setupPlatformSpecificMetrics() {
switch runtime.GOOS {
case "linux":
monitoring.NewFunc(beatMetrics, "cgroup", reportBeatCgroups, monitoring.Report)
case "windows":
setupWindowsHandlesMetrics()
}

if runtime.GOOS != "windows" {
monitoring.NewFunc(systemMetrics, "load", reportSystemLoadAverage, monitoring.Report)
} else {
setupWindowsHandlesMetrics()
}

setupLinuxBSDFDMetrics()
Expand Down Expand Up @@ -254,3 +260,79 @@ func reportRuntime(_ monitoring.Mode, V monitoring.Visitor) {

monitoring.ReportInt(V, "goroutines", int64(runtime.NumGoroutine()))
}

func reportBeatCgroups(_ monitoring.Mode, V monitoring.Visitor) {
V.OnRegistryStart()
defer V.OnRegistryFinished()

pid, err := process.GetSelfPid()
if err != nil {
logp.Err("error getting PID for self process: %v", err)
return
}

cgroups, err := cgroup.NewReader("", true)
if err != nil {
if err == cgroup.ErrCgroupsMissing {
logp.Warn("cgroup data collection disabled: %v", err)
} else {
logp.Err("cgroup data collection disabled: %v", err)
}
return
}
selfStats, err := cgroups.GetStatsForProcess(pid)
if err != nil {
logp.Err("error getting group status: %v", err)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't that be "error getting cgroup stats"? The error message puzzled me initially..

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed :)
Thanks for pointing that out, will be fixed by #23413

return
}

if cpu := selfStats.CPU; cpu != nil {
monitoring.ReportNamespace(V, "cpu", func() {
if cpu.ID != "" {
monitoring.ReportString(V, "id", cpu.ID)
}
monitoring.ReportNamespace(V, "cfs", func() {
monitoring.ReportNamespace(V, "period", func() {
monitoring.ReportInt(V, "us", int64(cpu.CFS.PeriodMicros))
})
monitoring.ReportNamespace(V, "quota", func() {
monitoring.ReportInt(V, "us", int64(cpu.CFS.QuotaMicros))
})
})
monitoring.ReportNamespace(V, "stats", func() {
monitoring.ReportInt(V, "periods", int64(cpu.Stats.Periods))
monitoring.ReportNamespace(V, "throttled", func() {
monitoring.ReportInt(V, "periods", int64(cpu.Stats.ThrottledPeriods))
monitoring.ReportInt(V, "ns", int64(cpu.Stats.ThrottledTimeNanos))
})
})
})
}

if cpuacct := selfStats.CPUAccounting; cpuacct != nil {
monitoring.ReportNamespace(V, "cpuacct", func() {
if cpuacct.ID != "" {
monitoring.ReportString(V, "id", cpuacct.ID)
}
monitoring.ReportNamespace(V, "total", func() {
monitoring.ReportInt(V, "ns", int64(cpuacct.TotalNanos))
})
})
}

if memory := selfStats.Memory; memory != nil {
monitoring.ReportNamespace(V, "memory", func() {
if memory.ID != "" {
monitoring.ReportString(V, "id", memory.ID)
}
monitoring.ReportNamespace(V, "mem", func() {
monitoring.ReportNamespace(V, "limit", func() {
monitoring.ReportInt(V, "bytes", int64(memory.Mem.Limit))
})
monitoring.ReportNamespace(V, "usage", func() {
monitoring.ReportInt(V, "bytes", int64(memory.Mem.Usage))
})
})
})
}
}