Skip to content

Commit 4c8e82e

Browse files
authored
Merge pull request #87 from coroot/system_uuid_label
metrics: add `system_uuid` label
2 parents 15b2c76 + 6acd550 commit 4c8e82e

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

main.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"net/http"
66
_ "net/http/pprof"
77
"os"
8-
"path"
98
"runtime"
109
"strings"
1110

@@ -14,6 +13,7 @@ import (
1413
"github.com/coroot/coroot-node-agent/flags"
1514
"github.com/coroot/coroot-node-agent/logs"
1615
"github.com/coroot/coroot-node-agent/node"
16+
"github.com/coroot/coroot-node-agent/proc"
1717
"github.com/coroot/coroot-node-agent/profiling"
1818
"github.com/coroot/coroot-node-agent/prom"
1919
"github.com/coroot/coroot-node-agent/tracing"
@@ -65,8 +65,8 @@ func uname() (string, string, error) {
6565
}
6666

6767
func machineID() string {
68-
for _, p := range []string{"etc/machine-id", "var/lib/dbus/machine-id", "sys/devices/virtual/dmi/id/product_uuid"} {
69-
payload, err := os.ReadFile(path.Join("/proc/1/root", p))
68+
for _, p := range []string{"/etc/machine-id", "/var/lib/dbus/machine-id", "/sys/devices/virtual/dmi/id/product_uuid"} {
69+
payload, err := os.ReadFile(proc.HostPath(p))
7070
if err != nil {
7171
klog.Warningln("failed to read machine-id:", err)
7272
continue
@@ -78,6 +78,15 @@ func machineID() string {
7878
return ""
7979
}
8080

81+
func systemUUID() string {
82+
payload, err := os.ReadFile(proc.HostPath("/sys/devices/virtual/dmi/id/product_uuid"))
83+
if err != nil {
84+
klog.Warningln("failed to read system-uuid:", err)
85+
return ""
86+
}
87+
return strings.TrimSpace(string(payload))
88+
}
89+
8190
func whitelistNodeExternalNetworks() {
8291
netdevs, err := node.NetDevices()
8392
if err != nil {
@@ -119,11 +128,13 @@ func main() {
119128
whitelistNodeExternalNetworks()
120129

121130
machineId := machineID()
131+
systemUuid := systemUUID()
132+
122133
tracing.Init(machineId, hostname, version)
123134
logs.Init(machineId, hostname, version)
124135

125136
registry := prometheus.NewRegistry()
126-
registerer := prometheus.WrapRegistererWith(prometheus.Labels{"machine_id": machineId}, registry)
137+
registerer := prometheus.WrapRegistererWith(prometheus.Labels{"machine_id": machineId, "system_uuid": systemUuid}, registry)
127138

128139
registerer.MustRegister(info("node_agent_info", version))
129140

0 commit comments

Comments
 (0)