os_unix.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. "fmt"
  19. "os"
  20. "golang.org/x/sys/unix"
  21. )
  22. type unameProvider func(buf *unix.Utsname) (err error)
  23. var defaultUnameProvider unameProvider = unix.Uname
  24. var currentUnameProvider = defaultUnameProvider
  25. func setDefaultUnameProvider() {
  26. setUnameProvider(defaultUnameProvider)
  27. }
  28. func setUnameProvider(unameProvider unameProvider) {
  29. currentUnameProvider = unameProvider
  30. }
  31. // platformOSDescription returns a human readable OS version information string.
  32. // The final string combines OS release information (where available) and the
  33. // result of the `uname` system call.
  34. func platformOSDescription() (string, error) {
  35. uname, err := uname()
  36. if err != nil {
  37. return "", err
  38. }
  39. osRelease := osRelease()
  40. if osRelease != "" {
  41. return fmt.Sprintf("%s (%s)", osRelease, uname), nil
  42. }
  43. return uname, nil
  44. }
  45. // uname issues a uname(2) system call (or equivalent on systems which doesn't
  46. // have one) and formats the output in a single string, similar to the output
  47. // of the `uname` commandline program. The final string resembles the one
  48. // obtained with a call to `uname -snrvm`.
  49. func uname() (string, error) {
  50. var utsName unix.Utsname
  51. err := currentUnameProvider(&utsName)
  52. if err != nil {
  53. return "", err
  54. }
  55. return fmt.Sprintf("%s %s %s %s %s",
  56. unix.ByteSliceToString(utsName.Sysname[:]),
  57. unix.ByteSliceToString(utsName.Nodename[:]),
  58. unix.ByteSliceToString(utsName.Release[:]),
  59. unix.ByteSliceToString(utsName.Version[:]),
  60. unix.ByteSliceToString(utsName.Machine[:]),
  61. ), nil
  62. }
  63. // getFirstAvailableFile returns an *os.File of the first available
  64. // file from a list of candidate file paths.
  65. func getFirstAvailableFile(candidates []string) (*os.File, error) {
  66. for _, c := range candidates {
  67. file, err := os.Open(c)
  68. if err == nil {
  69. return file, nil
  70. }
  71. }
  72. return nil, fmt.Errorf("no candidate file available: %v", candidates)
  73. }