37 lines
1.1 KiB
Go
37 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"runtime"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type agentHealthMetrics struct {
|
|
RSSBytes *uint64 `json:"rss_bytes,omitempty"`
|
|
HeapAllocBytes uint64 `json:"go_heap_alloc_bytes"`
|
|
Goroutines int `json:"goroutines"`
|
|
OpenFDs *int `json:"open_fds,omitempty"`
|
|
}
|
|
|
|
// agentDiagnostics is an authenticated, on-demand snapshot. No background
|
|
// goroutine, accumulating history, or extra public health endpoint is needed.
|
|
func agentDiagnostics() agentHealthMetrics {
|
|
var memory runtime.MemStats
|
|
runtime.ReadMemStats(&memory)
|
|
result := agentHealthMetrics{HeapAllocBytes: memory.HeapAlloc, Goroutines: runtime.NumGoroutine()}
|
|
// Linux agents expose RSS and descriptor counts in /proc. On unsupported
|
|
// platforms omit these fields instead of incorrectly calling heap RSS.
|
|
if fields := strings.Fields(readTrimmed("/proc/self/statm")); len(fields) >= 2 {
|
|
if resident, err := strconv.ParseUint(fields[1], 10, 64); err == nil {
|
|
rss := resident * uint64(os.Getpagesize())
|
|
result.RSSBytes = &rss
|
|
}
|
|
}
|
|
if entries, err := os.ReadDir("/proc/self/fd"); err == nil {
|
|
count := len(entries)
|
|
result.OpenFDs = &count
|
|
}
|
|
return result
|
|
}
|