Skip to content

Commit

Permalink
another approach
Browse files Browse the repository at this point in the history
  • Loading branch information
AndersonQ committed Aug 23, 2024
1 parent 6ed87ca commit 421e72c
Show file tree
Hide file tree
Showing 14 changed files with 144 additions and 152 deletions.
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
40 changes: 27 additions & 13 deletions providers/aix/host_aix_ppc64.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (
"time"

"github.com/elastic/go-sysinfo/internal/registry"
"github.com/elastic/go-sysinfo/providers"
"github.com/elastic/go-sysinfo/providers/shared"
"github.com/elastic/go-sysinfo/types"
)
Expand All @@ -47,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 @@ -71,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 @@ -129,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 @@ -151,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 @@ -193,7 +207,7 @@ func (r *reader) hostname(h *host) {
return
}

if providers.LowercaseHostname() {
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
36 changes: 25 additions & 11 deletions providers/darwin/host_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,31 @@ import (
"time"

"github.com/elastic/go-sysinfo/internal/registry"
"github.com/elastic/go-sysinfo/providers"
"github.com/elastic/go-sysinfo/providers/shared"
"github.com/elastic/go-sysinfo/types"
)

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 @@ -140,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 @@ -162,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 @@ -178,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 @@ -228,7 +242,7 @@ func (r *reader) hostname(h *host) {
return
}

if providers.LowercaseHostname() {
if r.lowerHostname {
v = strings.ToLower(v)
}
h.info.Hostname = v
Expand Down
30 changes: 0 additions & 30 deletions providers/doc.go

This file was deleted.

22 changes: 20 additions & 2 deletions providers/linux/host_fqdn_integration_docker_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ package linux
import (
"context"
"fmt"
"strings"
"testing"
"time"

"github.com/stretchr/testify/require"
)

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 @@ -44,8 +45,25 @@ func TestHost_FQDN_set(t *testing.T) {
}
}

func TestHost_FQDN_set_lowerHostname(t *testing.T) {
want := strings.ToLower(wantFQDN)
host, err := newLinuxSystem("", true).Host()
if err != nil {
t.Fatal(fmt.Errorf("could not get host information: %w", err))
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

gotFQDN, err := host.FQDNWithContext(ctx)
require.NoError(t, err)
if gotFQDN != want {
t.Errorf("got FQDN %q, want: %q", gotFQDN, want)
}
}

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
4 changes: 2 additions & 2 deletions providers/linux/host_fqdn_integration_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import (
)

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

Expand Down
41 changes: 26 additions & 15 deletions providers/linux/host_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,43 @@ import (
"github.com/prometheus/procfs"

"github.com/elastic/go-sysinfo/internal/registry"
"github.com/elastic/go-sysinfo/providers"
"github.com/elastic/go-sysinfo/providers/shared"
"github.com/elastic/go-sysinfo/types"
)

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

type linuxSystem struct {
procFS procFS
procFS procFS
lowerHostname bool
}

func newLinuxSystem(hostFS string) linuxSystem {
func newLinuxSystem(hostFS string, lowerHostname bool) linuxSystem {
mountPoint := filepath.Join(hostFS, procfs.DefaultMountPoint)
fs, _ := procfs.NewFS(mountPoint)
return linuxSystem{
procFS: procFS{FS: fs, mountPoint: mountPoint, baseMount: hostFS},
procFS: procFS{FS: fs, mountPoint: mountPoint, baseMount: hostFS},
lowerHostname: lowerHostname,
}
}

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

type host struct {
procFS procFS
stat procfs.Stat
info types.HostInfo
procFS procFS
stat procfs.Stat
info types.HostInfo
lowerHostname bool
}

// Info returns host info
Expand All @@ -79,7 +85,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 Down Expand Up @@ -155,14 +166,14 @@ func (h *host) CPUTime() (types.CPUTimes, error) {
}, nil
}

func newHost(fs procFS) (*host, error) {
func newHost(fs procFS, lowerHostname bool) (*host, error) {
stat, err := fs.Stat()
if err != nil {
return nil, fmt.Errorf("failed to read proc stat: %w", err)
}

h := &host{stat: stat, procFS: fs}
r := &reader{}
h := &host{stat: stat, procFS: fs, lowerHostname: lowerHostname}
r := &reader{lowerHostname: lowerHostname}
r.architecture(h)
r.nativeArchitecture(h)
r.bootTime(h)
Expand Down Expand Up @@ -237,7 +248,7 @@ func (r *reader) hostname(h *host) {
return
}

if providers.LowercaseHostname() {
if r.lowerHostname {
v = strings.ToLower(v)
}
h.info.Hostname = v
Expand Down
Loading

0 comments on commit 421e72c

Please sign in to comment.