Skip to content

Commit

Permalink
Use atomic pointers to set data
Browse files Browse the repository at this point in the history
  • Loading branch information
jsoriano committed Oct 19, 2018
1 parent 77578ba commit 813d222
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions libbeat/processors/add_host_metadata/add_host_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package add_host_metadata
import (
"fmt"
"net"
"sync"
"time"

"github.com/joeshaw/multierror"
Expand All @@ -40,11 +39,9 @@ func init() {
}

type addHostMetadata struct {
sync.Mutex

info types.HostInfo
lastUpdate time.Time
data common.MapStr
data common.MapStrPointer
config Config
}

Expand All @@ -66,18 +63,15 @@ func newHostMetadataProcessor(cfg *common.Config) (processors.Processor, error)
p := &addHostMetadata{
info: h.Info(),
config: config,
data: common.NewMapStrPointer(nil),
}
p.loadData()
return p, nil
}

// Run enriches the given event with the host meta data
func (p *addHostMetadata) Run(event *beat.Event) (*beat.Event, error) {
p.Lock()
defer p.Unlock()

p.loadData()
event.Fields.DeepUpdate(p.data)
event.Fields.DeepUpdate(p.data.Get())
return event, nil
}

Expand All @@ -87,8 +81,7 @@ func (p *addHostMetadata) loadData() {
return
}

p.data = host.MapHostInfo(p.info)

data := host.MapHostInfo(p.info)
if p.config.NetInfoEnabled {
// IP-address and MAC-address
var ipList, hwList, err = p.getNetInfo()
Expand All @@ -97,16 +90,18 @@ func (p *addHostMetadata) loadData() {
}

if len(ipList) > 0 {
p.data.Put("host.ip", ipList)
data.Put("host.ip", ipList)
}
if len(hwList) > 0 {
p.data.Put("host.mac", hwList)
data.Put("host.mac", hwList)
}
}

p.data.Set(data)
p.lastUpdate = time.Now()
}

func (p *addHostMetadata) getNetInfo() ([]string, []string, error) {
func (p addHostMetadata) getNetInfo() ([]string, []string, error) {
var ipList []string
var hwList []string

Expand Down Expand Up @@ -151,7 +146,7 @@ func (p *addHostMetadata) getNetInfo() ([]string, []string, error) {
return ipList, hwList, errs.Err()
}

func (p *addHostMetadata) String() string {
func (p addHostMetadata) String() string {
return fmt.Sprintf("%v=[netinfo.enabled=[%v]]",
processorName, p.config.NetInfoEnabled)
}

0 comments on commit 813d222

Please sign in to comment.