collector: use ByteSliceToString from golang.org/x/sys/unix

Use unix.ByteSliceToString to convert Utsname []byte fields to strings.

This also allows to drop the bytesToString helper which serves the same
purpose and matches ByteSliceToString's implementation.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
This commit is contained in:
Tobias Klauser 2022-05-23 15:44:16 +02:00
parent da8b0f694c
commit a8ebe3519e
No known key found for this signature in database
GPG Key ID: 6F5040074CCC0D04
5 changed files with 15 additions and 79 deletions

View File

@ -39,14 +39,14 @@ func (c *filesystemCollector) GetStats() ([]filesystemStats, error) {
} }
stats := []filesystemStats{} stats := []filesystemStats{}
for _, fs := range buf { for _, fs := range buf {
mountpoint := bytesToString(fs.Mntonname[:]) mountpoint := unix.ByteSliceToString(fs.Mntonname[:])
if c.excludedMountPointsPattern.MatchString(mountpoint) { if c.excludedMountPointsPattern.MatchString(mountpoint) {
level.Debug(c.logger).Log("msg", "Ignoring mount point", "mountpoint", mountpoint) level.Debug(c.logger).Log("msg", "Ignoring mount point", "mountpoint", mountpoint)
continue continue
} }
device := bytesToString(fs.Mntfromname[:]) device := unix.ByteSliceToString(fs.Mntfromname[:])
fstype := bytesToString(fs.Fstypename[:]) fstype := unix.ByteSliceToString(fs.Fstypename[:])
if c.excludedFSTypesPattern.MatchString(fstype) { if c.excludedFSTypesPattern.MatchString(fstype) {
level.Debug(c.logger).Log("msg", "Ignoring fs type", "type", fstype) level.Debug(c.logger).Log("msg", "Ignoring fs type", "type", fstype)
continue continue

View File

@ -14,7 +14,6 @@
package collector package collector
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"regexp" "regexp"
"strconv" "strconv"
@ -33,19 +32,6 @@ func readUintFromFile(path string) (uint64, error) {
return value, nil return value, nil
} }
// Take a []byte{} and return a string based on null termination.
// This is useful for situations where the OS has returned a null terminated
// string to use.
// If this function happens to receive a byteArray that contains no nulls, we
// simply convert the array to a string with no bounding.
func bytesToString(byteArray []byte) string {
n := bytes.IndexByte(byteArray, 0)
if n < 0 {
return string(byteArray)
}
return string(byteArray[:n])
}
var metricNameRegex = regexp.MustCompile(`_*[^0-9A-Za-z_]+_*`) var metricNameRegex = regexp.MustCompile(`_*[^0-9A-Za-z_]+_*`)
// SanitizeMetricName sanitize the given metric name by replacing invalid characters by underscores. // SanitizeMetricName sanitize the given metric name by replacing invalid characters by underscores.

View File

@ -17,51 +17,6 @@ import (
"testing" "testing"
) )
func TestBytesToString(t *testing.T) {
tests := []struct {
name string
b []byte
expected string
}{
{
"Single null byte",
[]byte{0},
"",
},
{
"Empty byte array",
[]byte{},
"",
},
{
"Not null terminated",
[]byte{65, 66, 67},
"ABC",
},
{
"Null randomly in array",
[]byte{65, 66, 67, 0, 65, 0, 65},
"ABC",
},
{
"Array starts with null and contains other valid bytes",
[]byte{0, 65, 66, 67, 0},
"",
},
}
for _, tt := range tests {
name := tt.name
b := tt.b
result := bytesToString(b)
expected := tt.expected
if result != expected {
t.Errorf("bytesToString(%#v): Name: %s, expected %#v, got %#v)", b, name, expected, result)
}
}
}
func TestSanitizeMetricName(t *testing.T) { func TestSanitizeMetricName(t *testing.T) {
testcases := map[string]string{ testcases := map[string]string{
"": "", "": "",

View File

@ -18,7 +18,6 @@
package collector package collector
import ( import (
"bytes"
"strings" "strings"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
@ -33,10 +32,10 @@ func getUname() (uname, error) {
nodeName, domainName := parseHostNameAndDomainName(utsname) nodeName, domainName := parseHostNameAndDomainName(utsname)
output := uname{ output := uname{
SysName: string(utsname.Sysname[:bytes.IndexByte(utsname.Sysname[:], 0)]), SysName: unix.ByteSliceToString(utsname.Sysname[:]),
Release: string(utsname.Release[:bytes.IndexByte(utsname.Release[:], 0)]), Release: unix.ByteSliceToString(utsname.Release[:]),
Version: string(utsname.Version[:bytes.IndexByte(utsname.Version[:], 0)]), Version: unix.ByteSliceToString(utsname.Version[:]),
Machine: string(utsname.Machine[:bytes.IndexByte(utsname.Machine[:], 0)]), Machine: unix.ByteSliceToString(utsname.Machine[:]),
NodeName: nodeName, NodeName: nodeName,
DomainName: domainName, DomainName: domainName,
} }
@ -47,7 +46,7 @@ func getUname() (uname, error) {
// parseHostNameAndDomainName for FreeBSD,OpenBSD,Darwin. // parseHostNameAndDomainName for FreeBSD,OpenBSD,Darwin.
// Attempts to emulate what happens in the Linux uname calls since these OS doesn't have a Domainname. // Attempts to emulate what happens in the Linux uname calls since these OS doesn't have a Domainname.
func parseHostNameAndDomainName(utsname unix.Utsname) (hostname string, domainname string) { func parseHostNameAndDomainName(utsname unix.Utsname) (hostname string, domainname string) {
nodename := string(utsname.Nodename[:bytes.IndexByte(utsname.Nodename[:], 0)]) nodename := unix.ByteSliceToString(utsname.Nodename[:])
split := strings.SplitN(nodename, ".", 2) split := strings.SplitN(nodename, ".", 2)
// We'll always have at least a single element in the array. We assume this // We'll always have at least a single element in the array. We assume this

View File

@ -16,11 +16,7 @@
package collector package collector
import ( import "golang.org/x/sys/unix"
"bytes"
"golang.org/x/sys/unix"
)
func getUname() (uname, error) { func getUname() (uname, error) {
var utsname unix.Utsname var utsname unix.Utsname
@ -29,12 +25,12 @@ func getUname() (uname, error) {
} }
output := uname{ output := uname{
SysName: string(utsname.Sysname[:bytes.IndexByte(utsname.Sysname[:], 0)]), SysName: unix.ByteSliceToString(utsname.Sysname[:]),
Release: string(utsname.Release[:bytes.IndexByte(utsname.Release[:], 0)]), Release: unix.ByteSliceToString(utsname.Release[:]),
Version: string(utsname.Version[:bytes.IndexByte(utsname.Version[:], 0)]), Version: unix.ByteSliceToString(utsname.Version[:]),
Machine: string(utsname.Machine[:bytes.IndexByte(utsname.Machine[:], 0)]), Machine: unix.ByteSliceToString(utsname.Machine[:]),
NodeName: string(utsname.Nodename[:bytes.IndexByte(utsname.Nodename[:], 0)]), NodeName: unix.ByteSliceToString(utsname.Nodename[:]),
DomainName: string(utsname.Domainname[:bytes.IndexByte(utsname.Domainname[:], 0)]), DomainName: unix.ByteSliceToString(utsname.Domainname[:]),
} }
return output, nil return output, nil