Skip to content

Commit

Permalink
fix(fim/ebpf): utilise OnceValues to declutter the code
Browse files Browse the repository at this point in the history
  • Loading branch information
pkoutsovasilis committed Apr 3, 2024
1 parent 38b27f5 commit 0ab4869
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 48 deletions.
35 changes: 10 additions & 25 deletions libbeat/ebpf/sys/sys.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,47 +26,32 @@ import (

"github.com/elastic/beats/v7/x-pack/auditbeat/module/system"
"github.com/elastic/go-sysinfo"
"github.com/elastic/go-sysinfo/types"
)

type hostID struct {
uniqueID string
bootTime time.Time
err error
}

var (
hostInfo = sync.OnceValue(func() hostID {
info, err := sysinfo.Host()
hostInfoOnce = sync.OnceValues(func() (types.HostInfo, error) {
host, err := sysinfo.Host()

if info == nil {
return hostID{
err: err,
}
if host == nil {
return types.HostInfo{}, err
}

return hostID{
uniqueID: info.Info().UniqueID,
bootTime: info.Info().BootTime,
err: err,
}
return host.Info(), err
})
)

func HostInfo() (string, time.Time, error) {
hID := hostInfo()

return hID.uniqueID, hID.bootTime, hID.err
}

// EntityID creates an ID that uniquely identifies this process across machines.
func EntityID(pid uint32, start time.Time) (string, error) {
hid, _, err := HostInfo()
info, err := hostInfoOnce()
if err != nil {
return "", err
}

h := system.NewEntityHash()
h.Write([]byte(hid))
if _, err := h.Write([]byte(info.UniqueID)); err != nil {
return "", err
}
if err := binary.Write(h, binary.LittleEndian, int64(pid)); err != nil {
return "", err
}
Expand Down
30 changes: 7 additions & 23 deletions libbeat/ebpf/sys/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,19 @@ import (
"github.com/tklauser/go-sysconf"
)

type ticksPerSecond struct {
value uint64
err error
}

var (
tps = sync.OnceValue(func() ticksPerSecond {
ticksPerSecondOnce = sync.OnceValues(func() (uint64, error) {
ticks, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
if err != nil {
return ticksPerSecond{
value: 0,
err: err,
}
return 0, err
}

return ticksPerSecond{
value: uint64(ticks),
err: err,
}
return uint64(ticks), err
})
)

func TicksPerSecond() (uint64, error) {
ticks := tps()
return ticks.value, ticks.err
}

func TicksToNs(ticks uint64) (uint64, error) {
tps, err := TicksPerSecond()
tps, err := ticksPerSecondOnce()
if err != nil {
return 0, err
}
Expand All @@ -63,7 +47,7 @@ func TicksToNs(ticks uint64) (uint64, error) {
}

func TimeFromNsSinceBoot(ns uint64) (time.Time, error) {
_, bt, err := HostInfo()
info, err := hostInfoOnce()
if err != nil {
return time.Time{}, err
}
Expand All @@ -73,15 +57,15 @@ func TimeFromNsSinceBoot(ns uint64) (time.Time, error) {
return time.Time{}, err
}

return bt.Add(time.Duration(reduced)), nil
return info.BootTime.Add(time.Duration(reduced)), nil
}

// When generating an `entity_id` in ECS we need to reduce the precision of a
// process's start time to that of procfs. Process start times can come from either
// eBPF (high precision) or other sources. We must reduce them all to the
// lowest common denominator such that entity ID's generated are always consistent.
func reduceTimestampPrecision(ns uint64) (uint64, error) {
tps, err := TicksPerSecond()
tps, err := ticksPerSecondOnce()
if err != nil {
return 0, err
}
Expand Down

0 comments on commit 0ab4869

Please sign in to comment.