From 0ab48694702ec52a866d93c1d0f054c1dc4e1e53 Mon Sep 17 00:00:00 2001 From: Panos Koutsovasilis Date: Wed, 3 Apr 2024 05:52:36 +0300 Subject: [PATCH] fix(fim/ebpf): utilise OnceValues to declutter the code --- libbeat/ebpf/sys/sys.go | 35 ++++++++++------------------------- libbeat/ebpf/sys/time.go | 30 +++++++----------------------- 2 files changed, 17 insertions(+), 48 deletions(-) diff --git a/libbeat/ebpf/sys/sys.go b/libbeat/ebpf/sys/sys.go index ca0351fada63..2d1d101e66b9 100644 --- a/libbeat/ebpf/sys/sys.go +++ b/libbeat/ebpf/sys/sys.go @@ -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 } diff --git a/libbeat/ebpf/sys/time.go b/libbeat/ebpf/sys/time.go index 630b0a563178..7dca6454c328 100644 --- a/libbeat/ebpf/sys/time.go +++ b/libbeat/ebpf/sys/time.go @@ -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 } @@ -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 } @@ -73,7 +57,7 @@ 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 @@ -81,7 +65,7 @@ func TimeFromNsSinceBoot(ns uint64) (time.Time, error) { // 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 }