// Copyright The OpenTelemetry Authors// SPDX-License-Identifier: Apache-2.0package resource // import "go.opentelemetry.io/otel/sdk/resource"import (semconv)type hostIDProvider func() (string, error)var defaultHostIDProvider hostIDProvider = platformHostIDReader.readvar hostID = defaultHostIDProvidertype hostIDReader interface { read() (string, error)}type fileReader func(string) (string, error)type commandExecutor func(string, ...string) (string, error)// hostIDReaderBSD implements hostIDReader.type hostIDReaderBSD struct { execCommand commandExecutor readFile fileReader}// read attempts to read the machine-id from /etc/hostid. If not found it will// execute `kenv -q smbios.system.uuid`. If neither location yields an id an// error will be returned.func ( *hostIDReaderBSD) () (string, error) {if , := .readFile("/etc/hostid"); == nil {returnstrings.TrimSpace(), nil }if , := .execCommand("kenv", "-q", "smbios.system.uuid"); == nil {returnstrings.TrimSpace(), nil }return"", errors.New("host id not found in: /etc/hostid or kenv")}// hostIDReaderDarwin implements hostIDReader.type hostIDReaderDarwin struct { execCommand commandExecutor}// read executes `ioreg -rd1 -c "IOPlatformExpertDevice"` and parses host id// from the IOPlatformUUID line. If the command fails or the uuid cannot be// parsed an error will be returned.func ( *hostIDReaderDarwin) () (string, error) { , := .execCommand("ioreg", "-rd1", "-c", "IOPlatformExpertDevice")if != nil {return"", } := strings.Split(, "\n")for , := range {ifstrings.Contains(, "IOPlatformUUID") { := strings.Split(, " = ")iflen() == 2 {returnstrings.Trim([1], "\""), nil }break } }return"", errors.New("could not parse IOPlatformUUID")}type hostIDReaderLinux struct { readFile fileReader}// read attempts to read the machine-id from /etc/machine-id followed by// /var/lib/dbus/machine-id. If neither location yields an ID an error will// be returned.func ( *hostIDReaderLinux) () (string, error) {if , := .readFile("/etc/machine-id"); == nil {returnstrings.TrimSpace(), nil }if , := .readFile("/var/lib/dbus/machine-id"); == nil {returnstrings.TrimSpace(), nil }return"", errors.New("host id not found in: /etc/machine-id or /var/lib/dbus/machine-id")}type hostIDDetector struct{}// Detect returns a *Resource containing the platform specific host id.func (hostIDDetector) (context.Context) (*Resource, error) { , := hostID()if != nil {returnnil, }returnNewWithAttributes(semconv.SchemaURL,semconv.HostID(), ), nil}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.