Skip to content

Commit

Permalink
Add cgroup version
Browse files Browse the repository at this point in the history
  • Loading branch information
corest committed Apr 16, 2024
1 parent 18dd528 commit 0aa66d9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ func Run(startTime time.Time, serviceName string, license string) (stats *Stats,
log.Warn().Err(err).Msg("Failed to get OS release")
}

cgroupVersion, err := getCgroupVersion()
if err != nil {
log.Warn().Err(err).Msg("Failed to get cgroup version")
cgroupVersion = "none"
}

cpuUsage := getCPUUsage()
cpuNum := getCPUNum()
memAlloc, memSys := getMemoryUsage()
Expand All @@ -103,6 +109,7 @@ func Run(startTime time.Time, serviceName string, license string) (stats *Stats,
Hostname: strings.TrimSpace(hostname),
KernelDetails: kernelDetails,
OSRelease: osRelease,
CgroupVersion: cgroupVersion,
}

err = emitMetrics(stats, serviceName, license)
Expand Down
1 change: 1 addition & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ type Stats struct {
Hostname string `json:"hostname"`
KernelDetails string `json:"kernelDetails"`
OSRelease string `json:"osRelease"`
CgroupVersion string `json:"cgroupVersion"`
}
20 changes: 20 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"os/exec"
"runtime"
"strings"

"github.com/struCoder/pidusage"
)
Expand Down Expand Up @@ -58,3 +59,22 @@ func getOSRelease() (string, error) {

return string(output), nil
}

func getCgroupVersion() (string, error) {
data, err := os.ReadFile("/proc/mounts")
if err != nil {
return "", err
}

lines := strings.Split(string(data), "\n")
for _, line := range lines {
fields := strings.Fields(line)
if len(fields) >= 2 && strings.Contains(fields[1], "cgroup2") {
return "v2", nil
} else if len(fields) >= 2 && strings.Contains(fields[1], "cgroup") {
return "v1", nil
}
}

return "", nil
}

0 comments on commit 0aa66d9

Please sign in to comment.