forked from mtusi2026/mtusi2026lab6
125 lines
3.1 KiB
Go
125 lines
3.1 KiB
Go
package telemetry
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"go.opentelemetry.io/otel"
|
|
"go.opentelemetry.io/otel/metric"
|
|
)
|
|
|
|
// RegisterProcessMetrics exposes this process/container CPU and memory. It
|
|
// uses cgroup v2 when available and /proc as a fallback. The metrics are
|
|
// intentionally process-scoped: no Docker socket or host root is required.
|
|
func RegisterProcessMetrics() error {
|
|
initMetrics()
|
|
_, err := otel.Meter("messenger").RegisterCallback(func(_ context.Context, o metric.Observer) error {
|
|
cpu, memory, limit := readProcessStats()
|
|
o.ObserveFloat64(processCPU, cpu)
|
|
o.ObserveInt64(processMemory, memory)
|
|
o.ObserveInt64(processMemLimit, limit)
|
|
return nil
|
|
}, processCPU, processMemory, processMemLimit)
|
|
return err
|
|
}
|
|
|
|
func readProcessStats() (cpuSeconds float64, memoryBytes, memoryLimit int64) {
|
|
if value, ok := readCgroupCPU(); ok {
|
|
cpuSeconds = value
|
|
} else {
|
|
cpuSeconds = readProcCPU()
|
|
}
|
|
memoryBytes = readCgroupMemory()
|
|
if memoryBytes == 0 {
|
|
memoryBytes = readProcRSS()
|
|
}
|
|
memoryLimit = readCgroupMemoryLimit()
|
|
return cpuSeconds, memoryBytes, memoryLimit
|
|
}
|
|
|
|
func readCgroupCPU() (float64, bool) {
|
|
raw, err := os.ReadFile("/sys/fs/cgroup/cpu.stat")
|
|
if err != nil {
|
|
return 0, false
|
|
}
|
|
for _, line := range strings.Split(string(raw), "\n") {
|
|
fields := strings.Fields(line)
|
|
if len(fields) == 2 && fields[0] == "usage_usec" {
|
|
usec, err := strconv.ParseFloat(fields[1], 64)
|
|
if err == nil {
|
|
return usec / 1e6, true
|
|
}
|
|
}
|
|
}
|
|
return 0, false
|
|
}
|
|
|
|
func readProcCPU() float64 {
|
|
raw, err := os.ReadFile("/proc/self/stat")
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
// comm may contain spaces; fields after the closing parenthesis are stable.
|
|
end := strings.LastIndexByte(string(raw), ')')
|
|
if end < 0 {
|
|
return 0
|
|
}
|
|
fields := strings.Fields(string(raw)[end+1:])
|
|
if len(fields) < 15 {
|
|
return 0
|
|
}
|
|
utime, _ := strconv.ParseFloat(fields[11], 64)
|
|
stime, _ := strconv.ParseFloat(fields[12], 64)
|
|
return (utime + stime) / 100 // USER_HZ on Linux is 100 in the images used.
|
|
}
|
|
|
|
func readCgroupMemory() int64 {
|
|
for _, path := range []string{"/sys/fs/cgroup/memory.current", "/sys/fs/cgroup/memory/memory.usage_in_bytes"} {
|
|
if raw, err := os.ReadFile(path); err == nil {
|
|
if value, err := strconv.ParseInt(strings.TrimSpace(string(raw)), 10, 64); err == nil {
|
|
return value
|
|
}
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func readCgroupMemoryLimit() int64 {
|
|
for _, path := range []string{"/sys/fs/cgroup/memory.max", "/sys/fs/cgroup/memory/memory.limit_in_bytes"} {
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
value := strings.TrimSpace(string(raw))
|
|
if value == "max" {
|
|
return 0
|
|
}
|
|
if parsed, err := strconv.ParseInt(value, 10, 64); err == nil {
|
|
return parsed
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func readProcRSS() int64 {
|
|
file, err := os.Open("/proc/self/status")
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
defer file.Close()
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
for scanner.Scan() {
|
|
fields := strings.Fields(scanner.Text())
|
|
if len(fields) >= 2 && fields[0] == "VmRSS:" {
|
|
if kb, err := strconv.ParseInt(fields[1], 10, 64); err == nil {
|
|
return kb * 1024
|
|
}
|
|
}
|
|
}
|
|
return 0
|
|
}
|