From a428349583575d5554f15c4224e18bca9a6fc556 Mon Sep 17 00:00:00 2001 From: Mauri de Souza Meneguzzo Date: Wed, 20 Aug 2025 16:42:08 -0300 Subject: [PATCH] chore: fix formatting issues in logp printf-style calls (#45944) * fix go vet errors for logp formatting issues * chore: fix formatting for logp calls * revert elastic-agent-libs bump * Apply suggestions from code review Co-authored-by: Tiago Queiroz --------- Co-authored-by: Tiago Queiroz Co-authored-by: Blake Rouse (cherry picked from commit a5be2a856ddcdc635060129adea6ac0a328cc491) # Conflicts: # libbeat/processors/add_host_metadata/add_host_metadata.go --- .../add_host_metadata/add_host_metadata.go | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/libbeat/processors/add_host_metadata/add_host_metadata.go b/libbeat/processors/add_host_metadata/add_host_metadata.go index 5c962dac01ae..e79cc76e14ef 100644 --- a/libbeat/processors/add_host_metadata/add_host_metadata.go +++ b/libbeat/processors/add_host_metadata/add_host_metadata.go @@ -112,6 +112,31 @@ func New(cfg *config.C, log *logp.Logger) (beat.Processor, error) { p.geoData = mapstr.M{"host": mapstr.M{"geo": geoFields}} } +<<<<<<< HEAD +======= + // create a unique ID for this instance of the processor + var cbIDStr string + cbID, err := uuid.NewV4() + // 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)) return p, nil } @@ -245,6 +270,63 @@ func (p *addHostMetadata) String() string { processorName, p.config.NetInfoEnabled, p.config.CacheTTL) } +<<<<<<< HEAD +======= +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)) 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")