From 7cca491c35f2335a1871cbc0158fa59dcaa7e063 Mon Sep 17 00:00:00 2001 From: SamerJ Date: Fri, 25 Oct 2024 02:41:51 +0300 Subject: [PATCH] [pkg/stanza][operators] Retain Operator should proagate unreleated fields of the original log entry (#35832) #### Description The retain operator should propagate the severity field like it does with timestamps. This fix will ensure that is the case. #### Testing Added UT to cover the use case. --- .chloggen/stanza-operators-retain.yaml | 28 +++++++++++++ .../transformer/retain/transformer.go | 18 +++++---- .../transformer/retain/transformer_test.go | 40 +++++++++++++++++++ 3 files changed, 78 insertions(+), 8 deletions(-) create mode 100644 .chloggen/stanza-operators-retain.yaml diff --git a/.chloggen/stanza-operators-retain.yaml b/.chloggen/stanza-operators-retain.yaml new file mode 100644 index 000000000000..4b596e618d35 --- /dev/null +++ b/.chloggen/stanza-operators-retain.yaml @@ -0,0 +1,28 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: pkg/stanza/operator + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Retain Operator should propagate the severity field + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [35832] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: | + The retain operator should propagate the severity field like it does with timestamps. + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/pkg/stanza/operator/transformer/retain/transformer.go b/pkg/stanza/operator/transformer/retain/transformer.go index 14ed45da293d..db4e27c42882 100644 --- a/pkg/stanza/operator/transformer/retain/transformer.go +++ b/pkg/stanza/operator/transformer/retain/transformer.go @@ -26,18 +26,16 @@ func (t *Transformer) Process(ctx context.Context, entry *entry.Entry) error { // Transform will apply the retain operation to an entry func (t *Transformer) Transform(e *entry.Entry) error { - newEntry := entry.New() - newEntry.ObservedTimestamp = e.ObservedTimestamp - newEntry.Timestamp = e.Timestamp + retainedEntryFields := entry.New() if !t.AllResourceFields { - newEntry.Resource = e.Resource + retainedEntryFields.Resource = e.Resource } if !t.AllAttributeFields { - newEntry.Attributes = e.Attributes + retainedEntryFields.Attributes = e.Attributes } if !t.AllBodyFields { - newEntry.Body = e.Body + retainedEntryFields.Body = e.Body } for _, field := range t.Fields { @@ -45,12 +43,16 @@ func (t *Transformer) Transform(e *entry.Entry) error { if !ok { continue } - err := newEntry.Set(field, val) + err := retainedEntryFields.Set(field, val) if err != nil { return err } } - *e = *newEntry + // The entry's Resource, Attributes & Body are modified. + // All other fields are left untouched (Ex: Timestamp, TraceID, ..) + e.Resource = retainedEntryFields.Resource + e.Attributes = retainedEntryFields.Attributes + e.Body = retainedEntryFields.Body return nil } diff --git a/pkg/stanza/operator/transformer/retain/transformer_test.go b/pkg/stanza/operator/transformer/retain/transformer_test.go index d0ca8692acfb..5f610a404958 100644 --- a/pkg/stanza/operator/transformer/retain/transformer_test.go +++ b/pkg/stanza/operator/transformer/retain/transformer_test.go @@ -57,6 +57,46 @@ func TestBuildAndProcess(t *testing.T) { return e }, }, + { + "retain_unrelated_fields", + false, + func() *Config { + cfg := NewConfig() + cfg.Fields = append(cfg.Fields, entry.NewBodyField("key")) + return cfg + }(), + func() *entry.Entry { + e := newTestEntry() + + e.Severity = entry.Debug3 + e.SeverityText = "debug" + e.Timestamp = time.Unix(1000, 1000) + e.ObservedTimestamp = time.Unix(2000, 2000) + e.TraceID = []byte{0x01} + e.SpanID = []byte{0x01} + e.TraceFlags = []byte{0x01} + e.ScopeName = "scope" + + return e + }, + func() *entry.Entry { + e := newTestEntry() + + e.Severity = entry.Debug3 + e.SeverityText = "debug" + e.Timestamp = time.Unix(1000, 1000) + e.ObservedTimestamp = time.Unix(2000, 2000) + e.TraceID = []byte{0x01} + e.SpanID = []byte{0x01} + e.TraceFlags = []byte{0x01} + e.ScopeName = "scope" + + e.Body = map[string]any{ + "key": "val", + } + return e + }, + }, { "retain_multi", false,