Skip to content

Commit

Permalink
Use event logger instead of "trace level"
Browse files Browse the repository at this point in the history
Use the event logger instead of "trace level" for debug logs
containing events, both under Elastic-Agent and standalone Beat.
  • Loading branch information
belimawr committed May 8, 2024
1 parent 7cea22c commit 76d1ee0
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 39 deletions.
6 changes: 2 additions & 4 deletions libbeat/processors/actions/append.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 2 additions & 4 deletions libbeat/processors/actions/copy_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
Expand Down
6 changes: 2 additions & 4 deletions libbeat/processors/actions/decode_base64_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
Expand Down
6 changes: 2 additions & 4 deletions libbeat/processors/actions/decompress_gzip_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
Expand Down
6 changes: 2 additions & 4 deletions libbeat/processors/actions/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
Expand Down
6 changes: 2 additions & 4 deletions libbeat/processors/actions/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
Expand Down
6 changes: 2 additions & 4 deletions libbeat/processors/urldecode/urldecode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
Expand Down
19 changes: 8 additions & 11 deletions libbeat/publisher/processing/processors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
})
}
Expand Down

0 comments on commit 76d1ee0

Please sign in to comment.