os_windows.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. "fmt"
  17. "strconv"
  18. "golang.org/x/sys/windows/registry"
  19. )
  20. // platformOSDescription returns a human readable OS version information string.
  21. // It does so by querying registry values under the
  22. // `SOFTWARE\Microsoft\Windows NT\CurrentVersion` key. The final string
  23. // resembles the one displayed by the Version Reporter Applet (winver.exe).
  24. func platformOSDescription() (string, error) {
  25. k, err := registry.OpenKey(
  26. registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
  27. if err != nil {
  28. return "", err
  29. }
  30. defer k.Close()
  31. var (
  32. productName = readProductName(k)
  33. displayVersion = readDisplayVersion(k)
  34. releaseID = readReleaseID(k)
  35. currentMajorVersionNumber = readCurrentMajorVersionNumber(k)
  36. currentMinorVersionNumber = readCurrentMinorVersionNumber(k)
  37. currentBuildNumber = readCurrentBuildNumber(k)
  38. ubr = readUBR(k)
  39. )
  40. if displayVersion != "" {
  41. displayVersion += " "
  42. }
  43. return fmt.Sprintf("%s %s(%s) [Version %s.%s.%s.%s]",
  44. productName,
  45. displayVersion,
  46. releaseID,
  47. currentMajorVersionNumber,
  48. currentMinorVersionNumber,
  49. currentBuildNumber,
  50. ubr,
  51. ), nil
  52. }
  53. func getStringValue(name string, k registry.Key) string {
  54. value, _, _ := k.GetStringValue(name)
  55. return value
  56. }
  57. func getIntegerValue(name string, k registry.Key) uint64 {
  58. value, _, _ := k.GetIntegerValue(name)
  59. return value
  60. }
  61. func readProductName(k registry.Key) string {
  62. return getStringValue("ProductName", k)
  63. }
  64. func readDisplayVersion(k registry.Key) string {
  65. return getStringValue("DisplayVersion", k)
  66. }
  67. func readReleaseID(k registry.Key) string {
  68. return getStringValue("ReleaseID", k)
  69. }
  70. func readCurrentMajorVersionNumber(k registry.Key) string {
  71. return strconv.FormatUint(getIntegerValue("CurrentMajorVersionNumber", k), 10)
  72. }
  73. func readCurrentMinorVersionNumber(k registry.Key) string {
  74. return strconv.FormatUint(getIntegerValue("CurrentMinorVersionNumber", k), 10)
  75. }
  76. func readCurrentBuildNumber(k registry.Key) string {
  77. return getStringValue("CurrentBuildNumber", k)
  78. }
  79. func readUBR(k registry.Key) string {
  80. return strconv.FormatUint(getIntegerValue("UBR", k), 10)
  81. }