Skip to content

Commit 1dbb5b5

Browse files
committed
[exporter/signalfx] Don't use syscall to avoid compilation errors
syscall is deprecated and doesn't guarantee compatibility with all platforms. This change replaces usages of syscall with sys/unix package to avoid compilation errors on some specific platforms.
1 parent eaff83a commit 1dbb5b5

File tree

6 files changed

+28
-59
lines changed

6 files changed

+28
-59
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
- `zookeeperreceiver`: Fix issue where receiver could panic during shutdown (#7020)
3636
- `prometheusreceiver`: Fix metadata fetching when metrics differ by trimmable suffixes (#6932)
3737
- Sanitize URLs being logged (#7021)
38+
- `signalfxexporter`: Don't use syscall to avoid compilation errors on some platforms (#7062)
3839

3940
## 💡 Enhancements 💡
4041

exporter/signalfxexporter/internal/hostmetadata/host.go

-10
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package hostmetadata // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/hostmetadata"
1919

2020
import (
21-
"bytes"
2221
"context"
2322
"errors"
2423
"io/ioutil"
@@ -129,15 +128,6 @@ func (o *hostOS) toStringMap() map[string]string {
129128
}
130129
}
131130

132-
// int8ArrayToByteArray converts an []int8 to []byte
133-
func int8ArrayToByteArray(in []int8) []byte {
134-
bts := make([]byte, len(in))
135-
for i, c := range in {
136-
bts[i] = byte(c)
137-
}
138-
return bytes.Trim(bts, "\x00")
139-
}
140-
141131
// getOS returns a struct with information about the host os
142132
func getOS() (info *hostOS, err error) {
143133
info = &hostOS{}

exporter/signalfxexporter/internal/hostmetadata/host_linux.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,39 @@
1515
//go:build linux
1616
// +build linux
1717

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

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

2223
import (
23-
"syscall"
24+
"bytes"
25+
26+
"golang.org/x/sys/unix"
2427
)
2528

2629
// syscallUname maps to the golib system call, but can be modified for testing
27-
var syscallUname = syscall.Uname
30+
var syscallUname = unix.Uname
2831

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

32-
uname := &syscall.Utsname{}
35+
uname := &unix.Utsname{}
3336
if err := syscallUname(uname); err != nil {
3437
return err
3538
}
3639

37-
info.HostKernelVersion = string(int8ArrayToByteArray(uname.Version[:]))
40+
info.HostKernelVersion = string(bytes.Trim(uname.Version[:], "\x00"))
3841
return nil
3942
}
4043

4144
func fillPlatformSpecificCPUData(info *hostCPU) error {
42-
uname := &syscall.Utsname{}
45+
uname := &unix.Utsname{}
4346
if err := syscallUname(uname); err != nil {
4447
return err
4548
}
4649

47-
info.HostMachine = string(int8ArrayToByteArray(uname.Machine[:]))
50+
info.HostMachine = string(bytes.Trim(uname.Machine[:], "\x00"))
4851

4952
// according to the python doc platform.Processor usually returns the same
5053
// value as platform.Machine

exporter/signalfxexporter/internal/hostmetadata/host_linux_test.go

+14-13
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ import (
2323
"errors"
2424
"os"
2525
"reflect"
26-
"syscall"
2726
"testing"
27+
28+
"golang.org/x/sys/unix"
2829
)
2930

3031
func TestFillOSSpecificData(t *testing.T) {
3132
type args struct {
32-
syscallUname func(*syscall.Utsname) error
33+
syscallUname func(*unix.Utsname) error
3334
etc string
3435
}
3536
tests := []struct {
@@ -42,8 +43,8 @@ func TestFillOSSpecificData(t *testing.T) {
4243
name: "get uname os information",
4344
args: args{
4445
etc: "./testdata/lsb-release",
45-
syscallUname: func(in *syscall.Utsname) error {
46-
in.Version = [65]int8{35, 57, 45, 85, 98, 117, 110, 116,
46+
syscallUname: func(in *unix.Utsname) error {
47+
in.Version = [65]byte{35, 57, 45, 85, 98, 117, 110, 116,
4748
117, 32, 83, 77, 80, 32, 87, 101, 100,
4849
32, 77, 97, 121, 32, 49, 54, 32, 49,
4950
53, 58, 50, 50, 58, 53, 52, 32, 85,
@@ -60,8 +61,8 @@ func TestFillOSSpecificData(t *testing.T) {
6061
name: "get uname os information uname call fails",
6162
args: args{
6263
etc: "./testdata/lsb-release",
63-
syscallUname: func(in *syscall.Utsname) error {
64-
in.Version = [65]int8{}
64+
syscallUname: func(in *unix.Utsname) error {
65+
in.Version = [65]byte{}
6566
return errors.New("shouldn't work")
6667
},
6768
},
@@ -88,13 +89,13 @@ func TestFillOSSpecificData(t *testing.T) {
8889
}
8990
})
9091
os.Unsetenv("HOST_ETC")
91-
syscallUname = syscall.Uname
92+
syscallUname = unix.Uname
9293
}
9394
}
9495

9596
func TestFillPlatformSpecificCPUData(t *testing.T) {
9697
type args struct {
97-
syscallUname func(*syscall.Utsname) error
98+
syscallUname func(*unix.Utsname) error
9899
}
99100
tests := []struct {
100101
name string
@@ -105,8 +106,8 @@ func TestFillPlatformSpecificCPUData(t *testing.T) {
105106
{
106107
name: "get uname cpu information",
107108
args: args{
108-
syscallUname: func(in *syscall.Utsname) error {
109-
in.Machine = [65]int8{120, 56, 54, 95, 54, 52}
109+
syscallUname: func(in *unix.Utsname) error {
110+
in.Machine = [65]byte{120, 56, 54, 95, 54, 52}
110111
return nil
111112
},
112113
},
@@ -118,8 +119,8 @@ func TestFillPlatformSpecificCPUData(t *testing.T) {
118119
{
119120
name: "get uname cpu information and the call to uname fails",
120121
args: args{
121-
syscallUname: func(in *syscall.Utsname) error {
122-
in.Machine = [65]int8{}
122+
syscallUname: func(in *unix.Utsname) error {
123+
in.Machine = [65]byte{}
123124
return errors.New("shouldn't work")
124125
},
125126
},
@@ -142,6 +143,6 @@ func TestFillPlatformSpecificCPUData(t *testing.T) {
142143
}
143144
})
144145
os.Unsetenv("HOST_ETC")
145-
syscallUname = syscall.Uname
146+
syscallUname = unix.Uname
146147
}
147148
}

exporter/signalfxexporter/internal/hostmetadata/host_test.go

-26
Original file line numberDiff line numberDiff line change
@@ -318,29 +318,3 @@ func TestEtcPath(t *testing.T) {
318318
}
319319

320320
}
321-
322-
func TestInt8ArrayToByteArray(t *testing.T) {
323-
type args struct {
324-
in []int8
325-
}
326-
tests := []struct {
327-
name string
328-
args args
329-
want []byte
330-
}{
331-
{
332-
name: "convert int8 array to byte array",
333-
args: args{
334-
in: []int8{72, 69, 76, 76, 79, 32, 87, 79, 82, 76, 68},
335-
},
336-
want: []byte{72, 69, 76, 76, 79, 32, 87, 79, 82, 76, 68},
337-
},
338-
}
339-
for _, tt := range tests {
340-
t.Run(tt.name, func(t *testing.T) {
341-
if got := int8ArrayToByteArray(tt.args.in); !reflect.DeepEqual(got, tt.want) {
342-
t.Errorf("int8ArrayToByteArray() = %v, want %v", got, tt.want)
343-
}
344-
})
345-
}
346-
}

exporter/signalfxexporter/internal/hostmetadata/metadata_linux_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414

1515
package hostmetadata
1616

17-
import "syscall"
17+
import "golang.org/x/sys/unix"
1818

1919
func mockSyscallUname() {
20-
syscallUname = func(in *syscall.Utsname) error {
21-
in.Machine = [65]int8{}
20+
syscallUname = func(in *unix.Utsname) error {
21+
in.Machine = [65]byte{}
2222
return nil
2323
}
2424
}

0 commit comments

Comments
 (0)