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

Allow to choose hostname and FQDN to be lowercased or not #236

Closed
wants to merge 5 commits into from
Closed
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
3 changes: 3 additions & 0 deletions .changelog/236.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
Allow to choose hostname and FQDN to be lowercased or not
```
3 changes: 2 additions & 1 deletion internal/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ type ProcessProvider interface {
}

type ProviderOptions struct {
Hostfs string
Hostfs string
LowerHostname bool
}

var (
Expand Down
42 changes: 31 additions & 11 deletions providers/aix/host_aix_ppc64.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"errors"
"fmt"
"os"
"strings"
"time"

"github.com/elastic/go-sysinfo/internal/registry"
Expand All @@ -45,18 +46,27 @@ import (
// As cgo will return some psinfo's fields with *byte, binary.Read will refuse this type.

func init() {
registry.Register(aixSystem{})
// register wrappers that implement the HostFS versions of the ProcessProvider and HostProvider
registry.Register(func(opts registry.ProviderOptions) registry.HostProvider {
return aixSystem{lowerHostname: opts.LowerHostname}
})
registry.Register(func(opts registry.ProviderOptions) registry.ProcessProvider {
return aixSystem{lowerHostname: opts.LowerHostname}
})
}

type aixSystem struct{}
type aixSystem struct {
lowerHostname bool
}

// Host returns a new AIX host.
func (aixSystem) Host() (types.Host, error) {
return newHost()
func (a aixSystem) Host() (types.Host, error) {
return newHost(a.lowerHostname)
}

type host struct {
info types.HostInfo
info types.HostInfo
lowerHostname bool
}

// Architecture returns the architecture of the host
Expand All @@ -69,7 +79,7 @@ func (h *host) Info() types.HostInfo {
return h.info
}

// Info returns the current CPU usage of the host.
// CPUTime returns the current CPU usage of the host.
func (*host) CPUTime() (types.CPUTimes, error) {
clock := uint64(C.sysconf(C._SC_CLK_TCK))
tick2nsec := func(val uint64) uint64 {
Expand Down Expand Up @@ -127,16 +137,21 @@ func (*host) Memory() (*types.HostMemoryInfo, error) {
}

func (h *host) FQDNWithContext(ctx context.Context) (string, error) {
return shared.FQDNWithContext(ctx)
fqdn, err := shared.FQDNWithContext(ctx)
if h.lowerHostname {
fqdn = strings.ToLower(fqdn)
}

return fqdn, err
}

func (h *host) FQDN() (string, error) {
return h.FQDNWithContext(context.Background())
}

func newHost() (*host, error) {
h := &host{}
r := &reader{}
func newHost(lowerHostname bool) (*host, error) {
h := &host{lowerHostname: lowerHostname}
r := &reader{lowerHostname: lowerHostname}
r.architecture(h)
r.bootTime(h)
r.hostname(h)
Expand All @@ -149,7 +164,8 @@ func newHost() (*host, error) {
}

type reader struct {
errs []error
errs []error
lowerHostname bool
}

func (r *reader) addErr(err error) bool {
Expand Down Expand Up @@ -190,6 +206,10 @@ func (r *reader) hostname(h *host) {
if r.addErr(err) {
return
}

if r.lowerHostname {
v = strings.ToLower(v)
}
h.info.Hostname = v
}

Expand Down
5 changes: 2 additions & 3 deletions providers/aix/process_aix_ppc64.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -77,8 +76,8 @@ func (aixSystem) Process(pid int) (types.Process, error) {
}

// Self returns the current process.
func (s aixSystem) Self() (types.Process, error) {
return s.Process(os.Getpid())
func (a aixSystem) Self() (types.Process, error) {
return a.Process(os.Getpid())
}

type process struct {
Expand Down
38 changes: 29 additions & 9 deletions providers/darwin/host_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"errors"
"fmt"
"os"
"strings"
"time"

"github.com/elastic/go-sysinfo/internal/registry"
Expand All @@ -32,17 +33,26 @@ import (
)

func init() {
registry.Register(darwinSystem{})
// register wrappers that implement the HostFS versions of the ProcessProvider and HostProvider
registry.Register(func(opts registry.ProviderOptions) registry.HostProvider {
return darwinSystem{lowerHostname: opts.LowerHostname}
})
registry.Register(func(opts registry.ProviderOptions) registry.ProcessProvider {
return darwinSystem{lowerHostname: opts.LowerHostname}
})
}

type darwinSystem struct{}
type darwinSystem struct {
lowerHostname bool
}

func (s darwinSystem) Host() (types.Host, error) {
return newHost()
return newHost(s.lowerHostname)
}

type host struct {
info types.HostInfo
info types.HostInfo
lowerHostname bool
}

func (h *host) Info() types.HostInfo {
Expand Down Expand Up @@ -138,7 +148,12 @@ func (h *host) Memory() (*types.HostMemoryInfo, error) {
}

func (h *host) FQDNWithContext(ctx context.Context) (string, error) {
return shared.FQDNWithContext(ctx)
fqdn, err := shared.FQDNWithContext(ctx)
if h.lowerHostname {
fqdn = strings.ToLower(fqdn)
}

return fqdn, err
}

func (h *host) FQDN() (string, error) {
Expand All @@ -160,9 +175,9 @@ func (h *host) LoadAverage() (*types.LoadAverageInfo, error) {
}, nil
}

func newHost() (*host, error) {
h := &host{}
r := &reader{}
func newHost(lowerHostname bool) (*host, error) {
h := &host{lowerHostname: lowerHostname}
r := &reader{lowerHostname: lowerHostname}
r.architecture(h)
r.nativeArchitecture(h)
r.bootTime(h)
Expand All @@ -176,7 +191,8 @@ func newHost() (*host, error) {
}

type reader struct {
errs []error
errs []error
lowerHostname bool
}

func (r *reader) addErr(err error) bool {
Expand Down Expand Up @@ -225,6 +241,10 @@ func (r *reader) hostname(h *host) {
if r.addErr(err) {
return
}

if r.lowerHostname {
v = strings.ToLower(v)
}
h.info.Hostname = v
}

Expand Down
37 changes: 35 additions & 2 deletions providers/linux/host_fqdn_integration_docker_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,41 @@ import (
"github.com/stretchr/testify/require"
)

func TestHost_hostname(t *testing.T) {
testCases := []struct {
name string
want string
lowercase bool
}{
{
name: "hostname",
want: "hostName",
lowercase: false,
},
{
name: "lowercase hostname",
want: "hostname",
lowercase: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
host, err := newLinuxSystem("", tc.lowercase).Host()
if err != nil {
t.Fatalf("could not get host info: %v\n", err)
}

got := host.Info()
if got.Hostname != tc.want {
t.Errorf("got hostname %q; want hostname %q",
got.Hostname, tc.want)
}
})
}
}
func TestHost_FQDN_set(t *testing.T) {
host, err := newLinuxSystem("").Host()
host, err := newLinuxSystem("", false).Host()
if err != nil {
t.Fatal(fmt.Errorf("could not get host information: %w", err))
}
Expand All @@ -45,7 +78,7 @@ func TestHost_FQDN_set(t *testing.T) {
}

func TestHost_FQDN_not_set(t *testing.T) {
host, err := newLinuxSystem("").Host()
host, err := newLinuxSystem("", false).Host()
if err != nil {
t.Fatal(fmt.Errorf("could not get host information: %w", err))
}
Expand Down
19 changes: 15 additions & 4 deletions providers/linux/host_fqdn_integration_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ import (
)

const (
wantHostname = "hostname"
wantDomain = "some.domain"
wantFQDN = wantHostname + "." + wantDomain
wantHostnameMixedCase = "hostName"
wantHostname = "hostname"
wantDomain = "some.domain"
wantFQDN = wantHostname + "." + wantDomain
)

func TestHost_FQDN(t *testing.T) {
func TestHost_Docker(t *testing.T) {
if _, err := execabs.LookPath("docker"); err != nil {
t.Skipf("Skipping because docker was not found: %v", err)
}
Expand Down Expand Up @@ -77,6 +78,16 @@ func TestHost_FQDN(t *testing.T) {
"./providers/linux",
},
},
{
name: "TestHost_hostname",
Hostname: wantHostnameMixedCase,
Cmd: []string{
"go", "test", "-v", "-count", "1",
"-tags", "integration,docker",
"-run", "^TestHost_FQDN_not_set$",
"./providers/linux",
},
},
}

for _, tc := range tcs {
Expand Down
Loading
Loading