-
Notifications
You must be signed in to change notification settings - Fork 435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
internal/osinfo: setup for kernel info #2933
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,54 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2022 Datadog, Inc. | ||
// Copyright 2024 Datadog, Inc. | ||
|
||
// Package osinfo provides information about the current operating system release | ||
package osinfo | ||
|
||
import ( | ||
"runtime" | ||
) | ||
|
||
// Modified in init functions to provide OS-specific information | ||
var ( | ||
osName = runtime.GOOS | ||
osVersion = "unknown" | ||
arch = runtime.GOARCH | ||
kernelName = "unknown" | ||
kernelRelease = "unknown" | ||
kernelVersion = "unknown" | ||
) | ||
|
||
// OSName returns the name of the operating system, including the distribution | ||
// for Linux when possible. | ||
func OSName() string { | ||
// call out to OS-specific implementation | ||
return osName() | ||
return osName | ||
} | ||
|
||
// OSVersion returns the operating system release, e.g. major/minor version | ||
// number and build ID. | ||
func OSVersion() string { | ||
// call out to OS-specific implementation | ||
return osVersion() | ||
return osVersion | ||
} | ||
|
||
// Architecture returns the architecture of the operating system. | ||
func Architecture() string { | ||
return arch | ||
} | ||
|
||
// KernelName returns the name of the kernel. | ||
func KernelName() string { | ||
return kernelName | ||
} | ||
|
||
// KernelRelease returns the release of the kernel. | ||
func KernelRelease() string { | ||
return kernelRelease | ||
} | ||
|
||
// KernelVersion returns the version of the kernel. | ||
func KernelVersion() string { | ||
return kernelVersion | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2024 Datadog, Inc. | ||
|
||
package osinfo_test | ||
|
||
import ( | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"golang.org/x/mod/semver" | ||
|
||
"gopkg.in/DataDog/dd-trace-go.v1/internal/osinfo" | ||
) | ||
|
||
func Test(t *testing.T) { | ||
var ( | ||
osName = osinfo.OSName() | ||
osVersion = osinfo.OSVersion() | ||
arch = osinfo.Architecture() | ||
kernelName = osinfo.KernelName() | ||
kernelRelease = osinfo.KernelRelease() | ||
kernelVersion = osinfo.KernelVersion() | ||
) | ||
|
||
t.Logf("OS Name: %s\n", osName) | ||
t.Logf("OS Version: %s\n", osVersion) | ||
t.Logf("Architecture: %s\n", arch) | ||
t.Logf("Kernel Name: %s\n", kernelName) | ||
t.Logf("Kernel Release: %s\n", kernelRelease) | ||
t.Logf("Kernel Version: %s\n", kernelVersion) | ||
|
||
switch runtime.GOOS { | ||
case "linux": | ||
require.Equal(t, "Linux", kernelName) | ||
require.Truef(t, semver.IsValid("v"+kernelRelease), "invalid kernel version: %s", kernelRelease) | ||
case "darwin": | ||
require.Equal(t, "Darwin", kernelName) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2024 Datadog, Inc. | ||
|
||
//go:build unix | ||
|
||
package osinfo | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"os" | ||
"os/exec" | ||
"runtime" | ||
"strings" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func init() { | ||
// Change the default values for backwards compatibility on scenarios | ||
if runtime.GOOS == "linux" { | ||
osName = "Linux (Unknown Distribution)" | ||
kernelName = "Linux" | ||
} | ||
|
||
if runtime.GOOS == "darwin" { | ||
kernelName = "Darwin" | ||
out, err := exec.Command("sw_vers", "-productVersion").Output() | ||
if err != nil { | ||
return | ||
} | ||
|
||
osVersion = string(bytes.Trim(out, "\n")) | ||
} | ||
|
||
var uts unix.Utsname | ||
if err := unix.Uname(&uts); err == nil { | ||
kernelName = string(bytes.TrimRight(uts.Sysname[:], "\x00")) | ||
kernelVersion = string(bytes.TrimRight(uts.Version[:], "\x00")) | ||
kernelRelease = strings.SplitN(strings.TrimRight(string(uts.Release[:]), "\x00"), "-", 2)[0] | ||
|
||
// Backwards compatibility on how data is reported for freebsd | ||
if runtime.GOOS == "freebsd" { | ||
osVersion = kernelRelease | ||
} | ||
} | ||
|
||
f, err := os.Open("/etc/os-release") | ||
if err != nil { | ||
return | ||
} | ||
|
||
defer f.Close() | ||
scanner := bufio.NewScanner(f) | ||
for scanner.Scan() { | ||
parts := strings.SplitN(scanner.Text(), "=", 2) | ||
switch parts[0] { | ||
case "NAME": | ||
osName = strings.Trim(parts[1], "\"") | ||
case "VERSION": | ||
osVersion = strings.Trim(parts[1], "\"") | ||
case "VERSION_ID": | ||
if osVersion == "" { // Fallback to VERSION_ID if VERSION is not set | ||
osVersion = strings.Trim(parts[1], "\"") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have to add the go:build line there because of golang/go#20322 so I decided to add go:build to all the files that need it in this package