Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions libbeat/processors/add_host_metadata/add_host_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,31 @@
p.geoData = mapstr.M{"host": mapstr.M{"geo": geoFields}}
}

<<<<<<< HEAD

Check failure on line 115 in libbeat/processors/add_host_metadata/add_host_metadata.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

syntax error: unexpected <<, expected }

Check failure on line 115 in libbeat/processors/add_host_metadata/add_host_metadata.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

syntax error: unexpected <<, expected }
=======
// create a unique ID for this instance of the processor
var cbIDStr string
cbID, err := uuid.NewV4()

Check failure on line 119 in libbeat/processors/add_host_metadata/add_host_metadata.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

syntax error: non-declaration statement outside function body

Check failure on line 119 in libbeat/processors/add_host_metadata/add_host_metadata.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

syntax error: non-declaration statement outside function body
// if we fail, fall back to the processor name, hope for the best.
if err != nil {
p.logger.Errorf("error generating ID for FQDN callback, reverting to processor name: %v", err)
cbIDStr = processorName
} else {
cbIDStr = cbID.String()
}

// this is safe as New() returns a pointer, not the actual object.
// This matters as other pieces of code in libbeat, like libbeat/processors/processor.go,
// will do weird stuff like copy the entire list of global processors.
err = features.AddFQDNOnChangeCallback(p.handleFQDNReportingChange, cbIDStr)
if err != nil {
return nil, fmt.Errorf(
"could not register callback for FQDN reporting onChange from %s processor: %w",
processorName, err,
)
}

>>>>>>> a5be2a856 (chore: fix formatting issues in logp printf-style calls (#45944))

Check failure on line 139 in libbeat/processors/add_host_metadata/add_host_metadata.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

invalid character U+0023 '#'

Check failure on line 139 in libbeat/processors/add_host_metadata/add_host_metadata.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

invalid character U+0023 '#'
return p, nil
}

Expand Down Expand Up @@ -245,6 +270,63 @@
processorName, p.config.NetInfoEnabled, p.config.CacheTTL)
}

<<<<<<< HEAD

Check failure on line 273 in libbeat/processors/add_host_metadata/add_host_metadata.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

syntax error: non-declaration statement outside function body
=======
func (p *addHostMetadata) handleFQDNReportingChange(new, old bool) {
if new == old {
// Nothing to do
return
}

// update the data for the processor
p.updateOrExpire(new)
}

// updateOrExpire will attempt to update the data for the processor, or expire the cache
// if the config update fails, or times out
func (p *addHostMetadata) updateOrExpire(useFQDN bool) {
if p.config.CacheTTL <= 0 {
return
}

p.lastUpdate.Lock()
defer p.lastUpdate.Unlock()

// while holding the mutex, attempt to update loadData()
// doing this with the mutex means other events must wait until we have the correct host data, as we assume that
// a call to this function means something else wants to force an update, and thus all events must sync.

updateChanSuccess := make(chan bool)
timeout := time.After(p.config.ExpireUpdateTimeout)
go func() {
err := p.loadData(false, useFQDN)
if err != nil {
p.logger.Errorf("error updating data for processor: %v", err)
updateChanSuccess <- false
return
}
updateChanSuccess <- true
}()

// this additional timeout check is paranoid, but when it's method is called from handleFQDNReportingChange(),
// it's blocking, which means we can hold a mutex in features. In addition, we don't want to break the processor by
// having all the events wait for too long.
select {
case <-timeout:
p.logger.Errorf("got timeout while trying to update metadata")
p.lastUpdate.Time = time.Time{}
case success := <-updateChanSuccess:
// only expire the cache if update was failed
if !success {
p.lastUpdate.Time = time.Time{}
} else {
p.lastUpdate.Time = time.Now()
}
}

}

>>>>>>> a5be2a856 (chore: fix formatting issues in logp printf-style calls (#45944))

Check failure on line 329 in libbeat/processors/add_host_metadata/add_host_metadata.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

invalid character U+0023 '#') (typecheck)

Check failure on line 329 in libbeat/processors/add_host_metadata/add_host_metadata.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

syntax error: non-declaration statement outside function body
func skipAddingHostMetadata(event *beat.Event) bool {
// If host fields exist(besides host.name added by libbeat) in event, skip add_host_metadata.
hostFields, err := event.Fields.GetValue("host")
Expand Down
Loading