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