From 76d1ee0c34ae7f8a06fbb7e3dd067793820a4f1a Mon Sep 17 00:00:00 2001 From: Tiago Queiroz Date: Wed, 8 May 2024 10:06:44 -0400 Subject: [PATCH] Use event logger instead of "trace level" Use the event logger instead of "trace level" for debug logs containing events, both under Elastic-Agent and standalone Beat. --- libbeat/processors/actions/append.go | 6 ++---- libbeat/processors/actions/copy_fields.go | 6 ++---- .../processors/actions/decode_base64_field.go | 6 ++---- .../actions/decompress_gzip_field.go | 6 ++---- libbeat/processors/actions/rename.go | 6 ++---- libbeat/processors/actions/replace.go | 6 ++---- libbeat/processors/urldecode/urldecode.go | 6 ++---- libbeat/publisher/processing/processors.go | 19 ++++++++----------- 8 files changed, 22 insertions(+), 39 deletions(-) diff --git a/libbeat/processors/actions/append.go b/libbeat/processors/actions/append.go index 1bf2caad45fb..fd15eeb2e587 100644 --- a/libbeat/processors/actions/append.go +++ b/libbeat/processors/actions/append.go @@ -21,7 +21,6 @@ import ( "fmt" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/management" "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/checks" jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor" @@ -82,9 +81,8 @@ func (f *appendProcessor) Run(event *beat.Event) (*beat.Event, error) { err := f.appendValues(f.config.TargetField, f.config.Fields, f.config.Values, event) if err != nil { errMsg := fmt.Errorf("failed to append fields in append processor: %w", err) - if management.TraceLevelEnabled() { - f.logger.Debug(errMsg.Error()) - } + f.logger.Debugw(errMsg.Error(), logp.TypeKey, logp.EventType) + if f.config.FailOnError { event = backup if _, err := event.PutValue("error.message", errMsg.Error()); err != nil { diff --git a/libbeat/processors/actions/copy_fields.go b/libbeat/processors/actions/copy_fields.go index 0f4fab309a36..f0d6cbe775aa 100644 --- a/libbeat/processors/actions/copy_fields.go +++ b/libbeat/processors/actions/copy_fields.go @@ -22,7 +22,6 @@ import ( "fmt" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/management" "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/checks" jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor" @@ -79,9 +78,8 @@ func (f *copyFields) Run(event *beat.Event) (*beat.Event, error) { err := f.copyField(field.From, field.To, event) if err != nil { errMsg := fmt.Errorf("Failed to copy fields in copy_fields processor: %w", err) - if management.TraceLevelEnabled() { - f.logger.Debug(errMsg.Error()) - } + f.logger.Debugw(errMsg.Error(), logp.TypeKey, logp.EventType) + if f.config.FailOnError { event = backup _, _ = event.PutValue("error.message", errMsg.Error()) diff --git a/libbeat/processors/actions/decode_base64_field.go b/libbeat/processors/actions/decode_base64_field.go index c45166beb111..3ec5e0a8d7a3 100644 --- a/libbeat/processors/actions/decode_base64_field.go +++ b/libbeat/processors/actions/decode_base64_field.go @@ -24,7 +24,6 @@ import ( "strings" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/management" "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/checks" jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor" @@ -84,9 +83,8 @@ func (f *decodeBase64Field) Run(event *beat.Event) (*beat.Event, error) { err := f.decodeField(event) if err != nil { errMsg := fmt.Errorf("failed to decode base64 fields in processor: %w", err) - if management.TraceLevelEnabled() { - f.log.Debug(errMsg.Error()) - } + f.log.Debugw(errMsg.Error(), logp.TypeKey, logp.EventType) + if f.config.FailOnError { event = backup _, _ = event.PutValue("error.message", errMsg.Error()) diff --git a/libbeat/processors/actions/decompress_gzip_field.go b/libbeat/processors/actions/decompress_gzip_field.go index 8d463600c210..993dbf3e8210 100644 --- a/libbeat/processors/actions/decompress_gzip_field.go +++ b/libbeat/processors/actions/decompress_gzip_field.go @@ -25,7 +25,6 @@ import ( "io" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/management" "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/checks" conf "github.com/elastic/elastic-agent-libs/config" @@ -76,9 +75,8 @@ func (f *decompressGzipField) Run(event *beat.Event) (*beat.Event, error) { err := f.decompressGzipField(event) if err != nil { errMsg := fmt.Errorf("Failed to decompress field in decompress_gzip_field processor: %w", err) - if management.TraceLevelEnabled() { - f.log.Debug(errMsg.Error()) - } + f.log.Debugw(errMsg.Error(), logp.EventType, logp.TypeKey) + if f.config.FailOnError { event = backup _, _ = event.PutValue("error.message", errMsg.Error()) diff --git a/libbeat/processors/actions/rename.go b/libbeat/processors/actions/rename.go index 4c49174bf54e..7503127e1032 100644 --- a/libbeat/processors/actions/rename.go +++ b/libbeat/processors/actions/rename.go @@ -22,7 +22,6 @@ import ( "fmt" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/management" "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/checks" jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor" @@ -84,9 +83,8 @@ func (f *renameFields) Run(event *beat.Event) (*beat.Event, error) { err := f.renameField(field.From, field.To, event) if err != nil { errMsg := fmt.Errorf("Failed to rename fields in processor: %w", err) - if management.TraceLevelEnabled() { - f.logger.Debug(errMsg.Error()) - } + f.logger.Debugw(errMsg.Error(), logp.TypeKey, logp.EventType) + if f.config.FailOnError { event = backup _, _ = event.PutValue("error.message", errMsg.Error()) diff --git a/libbeat/processors/actions/replace.go b/libbeat/processors/actions/replace.go index df4aa03fc86e..b242b9f35796 100644 --- a/libbeat/processors/actions/replace.go +++ b/libbeat/processors/actions/replace.go @@ -23,7 +23,6 @@ import ( "regexp" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/management" "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/checks" jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor" @@ -86,9 +85,8 @@ func (f *replaceString) Run(event *beat.Event) (*beat.Event, error) { err := f.replaceField(field.Field, field.Pattern, field.Replacement, event) if err != nil { errMsg := fmt.Errorf("Failed to replace fields in processor: %w", err) - if management.TraceLevelEnabled() { - f.log.Debug(errMsg.Error()) - } + f.log.Debugw(errMsg.Error(), logp.TypeKey, logp.EventType) + if f.config.FailOnError { event = backup _, _ = event.PutValue("error.message", errMsg.Error()) diff --git a/libbeat/processors/urldecode/urldecode.go b/libbeat/processors/urldecode/urldecode.go index 59ed552e2ae1..c9aac0cdef1d 100644 --- a/libbeat/processors/urldecode/urldecode.go +++ b/libbeat/processors/urldecode/urldecode.go @@ -23,7 +23,6 @@ import ( "net/url" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/management" "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/checks" jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor" @@ -83,9 +82,8 @@ func (p *urlDecode) Run(event *beat.Event) (*beat.Event, error) { err := p.decodeField(field.From, field.To, event) if err != nil { errMsg := fmt.Errorf("failed to decode fields in urldecode processor: %w", err) - if management.TraceLevelEnabled() { - p.log.Debug(errMsg.Error()) - } + p.log.Debugw(errMsg.Error(), logp.TypeKey, logp.EventType) + if p.config.FailOnError { event = backup _, _ = event.PutValue("error.message", errMsg.Error()) diff --git a/libbeat/publisher/processing/processors.go b/libbeat/publisher/processing/processors.go index 69fb5090e4ce..e90202401a71 100644 --- a/libbeat/publisher/processing/processors.go +++ b/libbeat/publisher/processing/processors.go @@ -27,7 +27,6 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" - "github.com/elastic/beats/v7/libbeat/management" "github.com/elastic/beats/v7/libbeat/outputs/codec/json" "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/elastic-agent-libs/logp" @@ -200,18 +199,16 @@ func debugPrintProcessor(info beat.Info, log *logp.Logger) *processorFn { EscapeHTML: false, }) return newProcessor("debugPrint", func(event *beat.Event) (*beat.Event, error) { - if management.TraceLevelEnabled() { - mux.Lock() - defer mux.Unlock() + mux.Lock() + defer mux.Unlock() - b, err := encoder.Encode(info.Beat, event) - if err != nil { - //nolint:nilerr // encoder failure is not considered an error by this processor [why not?] - return event, nil - } - - log.Debugf("Publish event: %s", b) + b, err := encoder.Encode(info.Beat, event) + if err != nil { + //nolint:nilerr // encoder failure is not considered an error by this processor [why not?] + return event, nil } + + log.Debugw(fmt.Sprintf("Publish event: %s", b), logp.TypeKey, logp.EventType) return event, nil }) }