Skip to content
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

[exporter/signalfx] Don't use syscall to avoid compilation errors #7062

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- `zookeeperreceiver`: Fix issue where receiver could panic during shutdown (#7020)
- `prometheusreceiver`: Fix metadata fetching when metrics differ by trimmable suffixes (#6932)
- Sanitize URLs being logged (#7021)
- `signalfxexporter`: Don't use syscall to avoid compilation errors on some platforms (#7062)

## 💡 Enhancements 💡

Expand Down
10 changes: 0 additions & 10 deletions exporter/signalfxexporter/internal/hostmetadata/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package hostmetadata // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/hostmetadata"

import (
"bytes"
"context"
"errors"
"io/ioutil"
Expand Down Expand Up @@ -129,15 +128,6 @@ func (o *hostOS) toStringMap() map[string]string {
}
}

// int8ArrayToByteArray converts an []int8 to []byte
func int8ArrayToByteArray(in []int8) []byte {
bts := make([]byte, len(in))
for i, c := range in {
bts[i] = byte(c)
}
return bytes.Trim(bts, "\x00")
}

// getOS returns a struct with information about the host os
func getOS() (info *hostOS, err error) {
info = &hostOS{}
Expand Down
17 changes: 10 additions & 7 deletions exporter/signalfxexporter/internal/hostmetadata/host_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,39 @@
//go:build linux
// +build linux

// Taken from https://github.com/signalfx/golib/blob/master/metadata/hostmetadata/host-linux.go as is.
// Taken from https://github.com/signalfx/golib/blob/master/metadata/hostmetadata/host-linux.go
// with minor modifications.

package hostmetadata // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/hostmetadata"

import (
"syscall"
"bytes"

"golang.org/x/sys/unix"
)

// syscallUname maps to the golib system call, but can be modified for testing
var syscallUname = syscall.Uname
var syscallUname = unix.Uname

func fillPlatformSpecificOSData(info *hostOS) error {
info.HostLinuxVersion, _ = getLinuxVersion()

uname := &syscall.Utsname{}
uname := &unix.Utsname{}
if err := syscallUname(uname); err != nil {
return err
}

info.HostKernelVersion = string(int8ArrayToByteArray(uname.Version[:]))
info.HostKernelVersion = string(bytes.Trim(uname.Version[:], "\x00"))
return nil
}

func fillPlatformSpecificCPUData(info *hostCPU) error {
uname := &syscall.Utsname{}
uname := &unix.Utsname{}
if err := syscallUname(uname); err != nil {
return err
}

info.HostMachine = string(int8ArrayToByteArray(uname.Machine[:]))
info.HostMachine = string(bytes.Trim(uname.Machine[:], "\x00"))

// according to the python doc platform.Processor usually returns the same
// value as platform.Machine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ import (
"errors"
"os"
"reflect"
"syscall"
"testing"

"golang.org/x/sys/unix"
)

func TestFillOSSpecificData(t *testing.T) {
type args struct {
syscallUname func(*syscall.Utsname) error
syscallUname func(*unix.Utsname) error
etc string
}
tests := []struct {
Expand All @@ -42,8 +43,8 @@ func TestFillOSSpecificData(t *testing.T) {
name: "get uname os information",
args: args{
etc: "./testdata/lsb-release",
syscallUname: func(in *syscall.Utsname) error {
in.Version = [65]int8{35, 57, 45, 85, 98, 117, 110, 116,
syscallUname: func(in *unix.Utsname) error {
in.Version = [65]byte{35, 57, 45, 85, 98, 117, 110, 116,
117, 32, 83, 77, 80, 32, 87, 101, 100,
32, 77, 97, 121, 32, 49, 54, 32, 49,
53, 58, 50, 50, 58, 53, 52, 32, 85,
Expand All @@ -60,8 +61,8 @@ func TestFillOSSpecificData(t *testing.T) {
name: "get uname os information uname call fails",
args: args{
etc: "./testdata/lsb-release",
syscallUname: func(in *syscall.Utsname) error {
in.Version = [65]int8{}
syscallUname: func(in *unix.Utsname) error {
in.Version = [65]byte{}
return errors.New("shouldn't work")
},
},
Expand All @@ -88,13 +89,13 @@ func TestFillOSSpecificData(t *testing.T) {
}
})
os.Unsetenv("HOST_ETC")
syscallUname = syscall.Uname
syscallUname = unix.Uname
}
}

func TestFillPlatformSpecificCPUData(t *testing.T) {
type args struct {
syscallUname func(*syscall.Utsname) error
syscallUname func(*unix.Utsname) error
}
tests := []struct {
name string
Expand All @@ -105,8 +106,8 @@ func TestFillPlatformSpecificCPUData(t *testing.T) {
{
name: "get uname cpu information",
args: args{
syscallUname: func(in *syscall.Utsname) error {
in.Machine = [65]int8{120, 56, 54, 95, 54, 52}
syscallUname: func(in *unix.Utsname) error {
in.Machine = [65]byte{120, 56, 54, 95, 54, 52}
return nil
},
},
Expand All @@ -118,8 +119,8 @@ func TestFillPlatformSpecificCPUData(t *testing.T) {
{
name: "get uname cpu information and the call to uname fails",
args: args{
syscallUname: func(in *syscall.Utsname) error {
in.Machine = [65]int8{}
syscallUname: func(in *unix.Utsname) error {
in.Machine = [65]byte{}
return errors.New("shouldn't work")
},
},
Expand All @@ -142,6 +143,6 @@ func TestFillPlatformSpecificCPUData(t *testing.T) {
}
})
os.Unsetenv("HOST_ETC")
syscallUname = syscall.Uname
syscallUname = unix.Uname
}
}
26 changes: 0 additions & 26 deletions exporter/signalfxexporter/internal/hostmetadata/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,29 +318,3 @@ func TestEtcPath(t *testing.T) {
}

}

func TestInt8ArrayToByteArray(t *testing.T) {
type args struct {
in []int8
}
tests := []struct {
name string
args args
want []byte
}{
{
name: "convert int8 array to byte array",
args: args{
in: []int8{72, 69, 76, 76, 79, 32, 87, 79, 82, 76, 68},
},
want: []byte{72, 69, 76, 76, 79, 32, 87, 79, 82, 76, 68},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := int8ArrayToByteArray(tt.args.in); !reflect.DeepEqual(got, tt.want) {
t.Errorf("int8ArrayToByteArray() = %v, want %v", got, tt.want)
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

package hostmetadata

import "syscall"
import "golang.org/x/sys/unix"

func mockSyscallUname() {
syscallUname = func(in *syscall.Utsname) error {
in.Machine = [65]int8{}
syscallUname = func(in *unix.Utsname) error {
in.Machine = [65]byte{}
return nil
}
}