os_unix.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || zos
  15. // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris zos
  16. package resource // import "go.opentelemetry.io/otel/sdk/resource"
  17. import (
  18. "bytes"
  19. "fmt"
  20. "os"
  21. "golang.org/x/sys/unix"
  22. )
  23. type unameProvider func(buf *unix.Utsname) (err error)
  24. var defaultUnameProvider unameProvider = unix.Uname
  25. var currentUnameProvider = defaultUnameProvider
  26. func setDefaultUnameProvider() {
  27. setUnameProvider(defaultUnameProvider)
  28. }
  29. func setUnameProvider(unameProvider unameProvider) {
  30. currentUnameProvider = unameProvider
  31. }
  32. // platformOSDescription returns a human readable OS version information string.
  33. // The final string combines OS release information (where available) and the
  34. // result of the `uname` system call.
  35. func platformOSDescription() (string, error) {
  36. uname, err := uname()
  37. if err != nil {
  38. return "", err
  39. }
  40. osRelease := osRelease()
  41. if osRelease != "" {
  42. return fmt.Sprintf("%s (%s)", osRelease, uname), nil
  43. }
  44. return uname, nil
  45. }
  46. // uname issues a uname(2) system call (or equivalent on systems which doesn't
  47. // have one) and formats the output in a single string, similar to the output
  48. // of the `uname` commandline program. The final string resembles the one
  49. // obtained with a call to `uname -snrvm`.
  50. func uname() (string, error) {
  51. var utsName unix.Utsname
  52. err := currentUnameProvider(&utsName)
  53. if err != nil {
  54. return "", err
  55. }
  56. return fmt.Sprintf("%s %s %s %s %s",
  57. charsToString(utsName.Sysname[:]),
  58. charsToString(utsName.Nodename[:]),
  59. charsToString(utsName.Release[:]),
  60. charsToString(utsName.Version[:]),
  61. charsToString(utsName.Machine[:]),
  62. ), nil
  63. }
  64. // charsToString converts a C-like null-terminated char array to a Go string.
  65. func charsToString(charArray []byte) string {
  66. if i := bytes.IndexByte(charArray, 0); i >= 0 {
  67. charArray = charArray[:i]
  68. }
  69. return string(charArray)
  70. }
  71. // getFirstAvailableFile returns an *os.File of the first available
  72. // file from a list of candidate file paths.
  73. func getFirstAvailableFile(candidates []string) (*os.File, error) {
  74. for _, c := range candidates {
  75. file, err := os.Open(c)
  76. if err == nil {
  77. return file, nil
  78. }
  79. }
  80. return nil, fmt.Errorf("no candidate file available: %v", candidates)
  81. }