mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2024-12-14 10:13:10 +02:00
484258eb36
* Added Linux-specific detector for the os.description attribute * Generalized OS description detector with placeholder function for unimplemented OSes * Extended osDescription function to *nix OSes based on golang.org/x/sys/unix * Added WithOS resource configuration function to configure all of the OS resource attributes * Implemented osDescription funtion for Windows OS * Improved documentation header for *nix version of the osDescription function * Added support for reading os-release file * Added/updated documentation headers for *nix implementation of osDescription and related functions * Changelog update * Added support for reading macOS version information * Mock approach to test OS description attribute * Extracted common function getFirstAvailableFile to read the first available file from a list of candidates * Upgraded golang.org/x/sys * Changelog update * Fixed wrong function name in documentation header for WithOSDescription * Updated documentation header for platformOSDescription function * Renamed restoreProcessAttributesProviders test helper function The function restoreProcessAttributesProviders was renamed to simply restoreAttributesProviders to better reflect its broader scope, which not only applies to process attribute's providers. * Fixed os_linux.go overriding build tags defined inside the file The suffix on os_linux.go was overriding the build tags already defined in that file. The file was renamed to os_release_unix.go, reflecting the main function defined in the file. For consistency, os_darwin.go was renamed to os_release_darwing.go, as its primary purpose is to also define the osRelease function. * Removed use of discontinued function resource.WithoutBuiltin * Added PR number to changelog entries * Updated go.sum files after run of make lint * Linux implementation: ignore lines with an empty key * Linux implementation: avoid unquoting strings less than two chars * WIP: added tests for Linux support functions * WIP: added tests for charsToString and getFirstAvailableFile functions * Replaced os.CreateTemp with ioutil.TempFile as the former only exists in Go 1.16 * Added unameProvider type to decouple direct reference to unix.Uname function inside Uname() * Added tests for Uname() function * Replaced *os.File with io.Reader in parseOSReleaseFile to ease testing * Added tests for parseOSReleaseFile function * Darwin implementation: added tests for buildOSRelease function * Replaced *os.File with io.Reader in parsePlistFile to ease testing * Darwin implementation: added tests for parsePlistFile function * Type in documentation header for Linux osRelease function * Extracted logic for reading specific registry values into helper functions * Added basic tests for Windows version of platformOSDescription and helper functions * Manually formatted uint64 to strings to have an uniform interface for test assertions * Asserts there's no error when opening registry key for testing Co-authored-by: Robert Pająk <pellared@hotmail.com> * Simplified subtests by using a single test with multiple asserts * go.sum update after running make * Fix typo Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * WIP: added placeholder implementation of platformOSDescription for unsupported OSes * Fixed typo on osRelease documentation header Co-authored-by: Chris Bandy <bandy.chris@gmail.com> * Fixed typo on test case name for ParsePlistFile tests Co-authored-by: Chris Bandy <bandy.chris@gmail.com> * Linter fix in changelog * go.sum updates after running make * Used strings.Replacer instead of multiple strings.ReplaceAll calls * Optimized implementation of charsToString * Safer temporary file deletion with t.TempDir() * Used t.Cleanup() for safer mocking of runtime providers in OS resource tests * Handled optionality of DisplayVersion registry key. For example, CI machine runs on: Windows Server 2019 Datacenter (1809) [Version 10.0.17763.1999] So, to not add an extra white space due to missing DisplayVersion, this value is checked to be not empty, and only in such case a trailing space is added for that component. * Workaround to handle the case of DisplayVersion registry key not present * Excluded unsupported GOOSes by negation of supported ones * go.sum update after running make Co-authored-by: Anthony Mirabella <a9@aneurysm9.com> Co-authored-by: Robert Pająk <pellared@hotmail.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> Co-authored-by: Chris Bandy <bandy.chris@gmail.com>
154 lines
4.6 KiB
Go
154 lines
4.6 KiB
Go
// Copyright The OpenTelemetry Authors
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
// +build aix dragonfly freebsd linux netbsd openbsd solaris zos
|
|
|
|
package resource // import "go.opentelemetry.io/otel/sdk/resource"
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// osRelease builds a string describing the operating system release based on the
|
|
// properties of the os-release file. If no os-release file is found, or if the
|
|
// required properties to build the release description string are missing, an empty
|
|
// string is returned instead. For more information about os-release files, see:
|
|
// https://www.freedesktop.org/software/systemd/man/os-release.html
|
|
func osRelease() string {
|
|
file, err := getOSReleaseFile()
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
values := parseOSReleaseFile(file)
|
|
|
|
return buildOSRelease(values)
|
|
}
|
|
|
|
// getOSReleaseFile returns a *os.File pointing to one of the well-known os-release
|
|
// files, according to their order of preference. If no file can be opened, it
|
|
// returns an error.
|
|
func getOSReleaseFile() (*os.File, error) {
|
|
return getFirstAvailableFile([]string{"/etc/os-release", "/usr/lib/os-release"})
|
|
}
|
|
|
|
// parseOSReleaseFile process the file pointed by `file` as an os-release file and
|
|
// returns a map with the key-values contained in it. Empty lines or lines starting
|
|
// with a '#' character are ignored, as well as lines with the missing key=value
|
|
// separator. Values are unquoted and unescaped.
|
|
func parseOSReleaseFile(file io.Reader) map[string]string {
|
|
values := make(map[string]string)
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
|
|
if skip(line) {
|
|
continue
|
|
}
|
|
|
|
key, value, ok := parse(line)
|
|
if ok {
|
|
values[key] = value
|
|
}
|
|
}
|
|
|
|
return values
|
|
}
|
|
|
|
// skip returns true if the line is blank or starts with a '#' character, and
|
|
// therefore should be skipped from processing.
|
|
func skip(line string) bool {
|
|
line = strings.TrimSpace(line)
|
|
|
|
return len(line) == 0 || strings.HasPrefix(line, "#")
|
|
}
|
|
|
|
// parse attempts to split the provided line on the first '=' character, and then
|
|
// sanitize each side of the split before returning them as a key-value pair.
|
|
func parse(line string) (string, string, bool) {
|
|
parts := strings.SplitN(line, "=", 2)
|
|
|
|
if len(parts) != 2 || len(parts[0]) == 0 {
|
|
return "", "", false
|
|
}
|
|
|
|
key := strings.TrimSpace(parts[0])
|
|
value := unescape(unquote(strings.TrimSpace(parts[1])))
|
|
|
|
return key, value, true
|
|
}
|
|
|
|
// unquote checks whether the string `s` is quoted with double or single quotes
|
|
// and, if so, returns a version of the string without them. Otherwise it returns
|
|
// the provided string unchanged.
|
|
func unquote(s string) string {
|
|
if len(s) < 2 {
|
|
return s
|
|
}
|
|
|
|
if (s[0] == '"' || s[0] == '\'') && s[0] == s[len(s)-1] {
|
|
return s[1 : len(s)-1]
|
|
}
|
|
|
|
return s
|
|
}
|
|
|
|
// unescape removes the `\` prefix from some characters that are expected
|
|
// to have it added in front of them for escaping purposes.
|
|
func unescape(s string) string {
|
|
return strings.NewReplacer(
|
|
`\$`, `$`,
|
|
`\"`, `"`,
|
|
`\'`, `'`,
|
|
`\\`, `\`,
|
|
"\\`", "`",
|
|
).Replace(s)
|
|
}
|
|
|
|
// buildOSRelease builds a string describing the OS release based on the properties
|
|
// available on the provided map. It favors a combination of the `NAME` and `VERSION`
|
|
// properties as first option (falling back to `VERSION_ID` if `VERSION` isn't
|
|
// found), and using `PRETTY_NAME` alone if some of the previous are not present. If
|
|
// none of these properties are found, it returns an empty string.
|
|
//
|
|
// The rationale behind not using `PRETTY_NAME` as first choice was that, for some
|
|
// Linux distributions, it doesn't include the same detail that can be found on the
|
|
// individual `NAME` and `VERSION` properties, and combining `PRETTY_NAME` with
|
|
// other properties can produce "pretty" redundant strings in some cases.
|
|
func buildOSRelease(values map[string]string) string {
|
|
var osRelease string
|
|
|
|
name := values["NAME"]
|
|
version := values["VERSION"]
|
|
|
|
if version == "" {
|
|
version = values["VERSION_ID"]
|
|
}
|
|
|
|
if name != "" && version != "" {
|
|
osRelease = fmt.Sprintf("%s %s", name, version)
|
|
} else {
|
|
osRelease = values["PRETTY_NAME"]
|
|
}
|
|
|
|
return osRelease
|
|
}
|