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

Fix a race condition on add_host_metadata #8653

Merged
merged 8 commits into from
Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ https://github.com/elastic/beats/compare/v6.4.0...master[Check the HEAD diff]
- Replace index patterns in TSVB visualizations. {pull}7929[7929]
- Fixed Support `add_docker_metadata` in Windows by identifying systems' path separator. {issue}7797[7797]
- Add backoff support to x-pack monitoring outputs. {issue}7966[7966]
- Fix a race condition with the `add_host_metadata` and the event serialization. {pull}8223[8223]
- Fix a race condition with the `add_host_metadata` and the event serialization. {pull}8223[8223] {pull}8653[8653]
- Enforce that data used by k8s or docker doesn't use any reference. {pull}8240[8240]
- Switch to different UUID lib due to to non-random generated UUIDs. {pull}8485[8485]

Expand Down
41 changes: 22 additions & 19 deletions libbeat/processors/add_host_metadata/add_host_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func init() {
type addHostMetadata struct {
info types.HostInfo
lastUpdate time.Time
data common.MapStr
data common.MapStrPointer
config Config
}

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

// Run enriches the given event with the host meta data
func (p *addHostMetadata) Run(event *beat.Event) (*beat.Event, error) {
p.loadData()
event.Fields.DeepUpdate(p.data.Clone())
event.Fields.DeepUpdate(p.data.Get().Clone())
return event, nil
}

func (p *addHostMetadata) loadData() {

// Check if cache is expired
if p.lastUpdate.Add(cacheExpiration).Before(time.Now()) {
p.data = host.MapHostInfo(p.info)

if p.config.NetInfoEnabled {
// IP-address and MAC-address
var ipList, hwList, err = p.getNetInfo()
if err != nil {
logp.Info("Error when getting network information %v", err)
}
if p.lastUpdate.Add(cacheExpiration).After(time.Now()) {
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
return
}

if len(ipList) > 0 {
p.data.Put("host.ip", ipList)
}
if len(hwList) > 0 {
p.data.Put("host.mac", hwList)
}
data := host.MapHostInfo(p.info)
if p.config.NetInfoEnabled {
// IP-address and MAC-address
var ipList, hwList, err = p.getNetInfo()
if err != nil {
logp.Info("Error when getting network information %v", err)
}

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

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

func (p addHostMetadata) getNetInfo() ([]string, []string, error) {
Expand Down