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