Skip to content

Commit

Permalink
internal/osinfo: setup for kernel info
Browse files Browse the repository at this point in the history
Signed-off-by: Eliott Bouhana <[email protected]>
  • Loading branch information
eliottness committed Oct 21, 2024
1 parent 2cae09f commit a91276e
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 168 deletions.
41 changes: 37 additions & 4 deletions internal/osinfo/osinfo.go
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
}
24 changes: 0 additions & 24 deletions internal/osinfo/osinfo_darwin.go

This file was deleted.

21 changes: 0 additions & 21 deletions internal/osinfo/osinfo_default.go

This file was deleted.

24 changes: 0 additions & 24 deletions internal/osinfo/osinfo_freebsd.go

This file was deleted.

52 changes: 0 additions & 52 deletions internal/osinfo/osinfo_linux.go

This file was deleted.

42 changes: 42 additions & 0 deletions internal/osinfo/osinfo_test.go
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)
}
}
70 changes: 70 additions & 0 deletions internal/osinfo/osinfo_unix.go
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], "\"")
}
}
}
}
12 changes: 5 additions & 7 deletions internal/osinfo/osinfo_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.

//go:build windows

package osinfo

import (
"fmt"
"runtime"
"strings"

"golang.org/x/sys/windows/registry"
)

func osName() string {
return runtime.GOOS
}

func osVersion() string {
func init() {
k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
if err != nil {
return "unknown"
Expand Down Expand Up @@ -50,5 +47,6 @@ func osVersion() string {
} else {
version.WriteString(" Unknown Build")
}
return version.String()

osVersion = version.String()
}
Loading

0 comments on commit a91276e

Please sign in to comment.