Debian Codegraph @

sdk/resource/host_id.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package resource // import "go.opentelemetry.io/otel/sdk/resource"
5
6 import (
7 "context"
8 "errors"
9 "strings"
10
11 semconv "go.opentelemetry.io/otel/semconv/v1.40.0"
12 )
13
14 type hostIDProvider func() (string, error)
15
16 var defaultHostIDProvider hostIDProvider = platformHostIDReader.read
17
18 var hostID = defaultHostIDProvider
19
20 type hostIDReader interface {
21 read() (string, error)
22 }
23
24 type fileReader func(string) (string, error)
25
26 type commandExecutor func(string, ...string) (string, error)
27
28 // hostIDReaderBSD implements hostIDReader.
29 type hostIDReaderBSD struct {
30 execCommand commandExecutor
31 readFile fileReader
32 }
33
34 // read attempts to read the machine-id from /etc/hostid.
35 // If not found it will execute: /bin/kenv -q smbios.system.uuid.
36 // If neither location yields an id an error will be returned.
37 func (r *hostIDReaderBSD) read() (string, error) {
38 if result, err := r.readFile("/etc/hostid"); err == nil {
39 return strings.TrimSpace(result), nil
40 }
41
42 if result, err := r.execCommand("/bin/kenv", "-q", "smbios.system.uuid"); err == nil {
43 return strings.TrimSpace(result), nil
44 }
45
46 return "", errors.New("host id not found in: /etc/hostid or /bin/kenv")
47 }
48
49 // hostIDReaderDarwin implements hostIDReader.
50 type hostIDReaderDarwin struct {
51 execCommand commandExecutor
52 }
53
54 // read executes `/usr/sbin/ioreg -rd1 -c "IOPlatformExpertDevice"` and parses host id
55 // from the IOPlatformUUID line. If the command fails or the uuid cannot be
56 // parsed an error will be returned.
57 func (r *hostIDReaderDarwin) read() (string, error) {
58 result, err := r.execCommand("/usr/sbin/ioreg", "-rd1", "-c", "IOPlatformExpertDevice")
59 if err != nil {
60 return "", err
61 }
62
63 for line := range strings.SplitSeq(result, "\n") {
64 if strings.Contains(line, "IOPlatformUUID") {
65 parts := strings.Split(line, " = ")
66 if len(parts) == 2 {
67 return strings.Trim(parts[1], "\""), nil
68 }
69 break
70 }
71 }
72
73 return "", errors.New("could not parse IOPlatformUUID")
74 }
75
76 type hostIDReaderLinux struct {
77 readFile fileReader
78 }
79
80 // read attempts to read the machine-id from /etc/machine-id followed by
81 // /var/lib/dbus/machine-id. If neither location yields an ID an error will
82 // be returned.
83 func (r *hostIDReaderLinux) read() (string, error) {
84 if result, err := r.readFile("/etc/machine-id"); err == nil {
85 return strings.TrimSpace(result), nil
86 }
87
88 if result, err := r.readFile("/var/lib/dbus/machine-id"); err == nil {
89 return strings.TrimSpace(result), nil
90 }
91
92 return "", errors.New("host id not found in: /etc/machine-id or /var/lib/dbus/machine-id")
93 }
94
95 type hostIDDetector struct{}
96
97 // Detect returns a *Resource containing the platform specific host id.
98 func (hostIDDetector) Detect(context.Context) (*Resource, error) {
99 hostID, err := hostID()
100 if err != nil {
101 return nil, err
102 }
103
104 return NewWithAttributes(
105 semconv.SchemaURL,
106 semconv.HostID(hostID),
107 ), nil
108 }