host_id.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright The OpenTelemetry Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package resource // import "go.opentelemetry.io/otel/sdk/resource"
  15. import (
  16. "context"
  17. "errors"
  18. "strings"
  19. semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
  20. )
  21. type hostIDProvider func() (string, error)
  22. var defaultHostIDProvider hostIDProvider = platformHostIDReader.read
  23. var hostID = defaultHostIDProvider
  24. type hostIDReader interface {
  25. read() (string, error)
  26. }
  27. type fileReader func(string) (string, error)
  28. type commandExecutor func(string, ...string) (string, error)
  29. // hostIDReaderBSD implements hostIDReader.
  30. type hostIDReaderBSD struct {
  31. execCommand commandExecutor
  32. readFile fileReader
  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.
  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. if result, err := r.execCommand("kenv", "-q", "smbios.system.uuid"); err == nil {
  42. return strings.TrimSpace(result), nil
  43. }
  44. return "", errors.New("host id not found in: /etc/hostid or kenv")
  45. }
  46. // hostIDReaderDarwin implements hostIDReader.
  47. type hostIDReaderDarwin struct {
  48. execCommand commandExecutor
  49. }
  50. // read executes `ioreg -rd1 -c "IOPlatformExpertDevice"` and parses host id
  51. // from the IOPlatformUUID line. If the command fails or the uuid cannot be
  52. // parsed an error will be returned.
  53. func (r *hostIDReaderDarwin) read() (string, error) {
  54. result, err := r.execCommand("ioreg", "-rd1", "-c", "IOPlatformExpertDevice")
  55. if err != nil {
  56. return "", err
  57. }
  58. lines := strings.Split(result, "\n")
  59. for _, line := range lines {
  60. if strings.Contains(line, "IOPlatformUUID") {
  61. parts := strings.Split(line, " = ")
  62. if len(parts) == 2 {
  63. return strings.Trim(parts[1], "\""), nil
  64. }
  65. break
  66. }
  67. }
  68. return "", errors.New("could not parse IOPlatformUUID")
  69. }
  70. type hostIDReaderLinux struct {
  71. readFile fileReader
  72. }
  73. // read attempts to read the machine-id from /etc/machine-id followed by
  74. // /var/lib/dbus/machine-id. If neither location yields an ID an error will
  75. // be returned.
  76. func (r *hostIDReaderLinux) read() (string, error) {
  77. if result, err := r.readFile("/etc/machine-id"); err == nil {
  78. return strings.TrimSpace(result), nil
  79. }
  80. if result, err := r.readFile("/var/lib/dbus/machine-id"); err == nil {
  81. return strings.TrimSpace(result), nil
  82. }
  83. return "", errors.New("host id not found in: /etc/machine-id or /var/lib/dbus/machine-id")
  84. }
  85. type hostIDDetector struct{}
  86. // Detect returns a *Resource containing the platform specific host id.
  87. func (hostIDDetector) Detect(ctx context.Context) (*Resource, error) {
  88. hostID, err := hostID()
  89. if err != nil {
  90. return nil, err
  91. }
  92. return NewWithAttributes(
  93. semconv.SchemaURL,
  94. semconv.HostID(hostID),
  95. ), nil
  96. }