...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package resource
16
17 import (
18 "context"
19 "errors"
20 "strings"
21
22 semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
23 )
24
25 type hostIDProvider func() (string, error)
26
27 var defaultHostIDProvider hostIDProvider = platformHostIDReader.read
28
29 var hostID = defaultHostIDProvider
30
31 type hostIDReader interface {
32 read() (string, error)
33 }
34
35 type fileReader func(string) (string, error)
36
37 type commandExecutor func(string, ...string) (string, error)
38
39
40 type hostIDReaderBSD struct {
41 execCommand commandExecutor
42 readFile fileReader
43 }
44
45
46
47
48 func (r *hostIDReaderBSD) read() (string, error) {
49 if result, err := r.readFile("/etc/hostid"); err == nil {
50 return strings.TrimSpace(result), nil
51 }
52
53 if result, err := r.execCommand("kenv", "-q", "smbios.system.uuid"); err == nil {
54 return strings.TrimSpace(result), nil
55 }
56
57 return "", errors.New("host id not found in: /etc/hostid or kenv")
58 }
59
60
61 type hostIDReaderDarwin struct {
62 execCommand commandExecutor
63 }
64
65
66
67
68 func (r *hostIDReaderDarwin) read() (string, error) {
69 result, err := r.execCommand("ioreg", "-rd1", "-c", "IOPlatformExpertDevice")
70 if err != nil {
71 return "", err
72 }
73
74 lines := strings.Split(result, "\n")
75 for _, line := range lines {
76 if strings.Contains(line, "IOPlatformUUID") {
77 parts := strings.Split(line, " = ")
78 if len(parts) == 2 {
79 return strings.Trim(parts[1], "\""), nil
80 }
81 break
82 }
83 }
84
85 return "", errors.New("could not parse IOPlatformUUID")
86 }
87
88 type hostIDReaderLinux struct {
89 readFile fileReader
90 }
91
92
93
94
95 func (r *hostIDReaderLinux) read() (string, error) {
96 if result, err := r.readFile("/etc/machine-id"); err == nil {
97 return strings.TrimSpace(result), nil
98 }
99
100 if result, err := r.readFile("/var/lib/dbus/machine-id"); err == nil {
101 return strings.TrimSpace(result), nil
102 }
103
104 return "", errors.New("host id not found in: /etc/machine-id or /var/lib/dbus/machine-id")
105 }
106
107 type hostIDDetector struct{}
108
109
110 func (hostIDDetector) Detect(ctx context.Context) (*Resource, error) {
111 hostID, err := hostID()
112 if err != nil {
113 return nil, err
114 }
115
116 return NewWithAttributes(
117 semconv.SchemaURL,
118 semconv.HostID(hostID),
119 ), nil
120 }
121
View as plain text