Skip to content

Commit

Permalink
[pkg/stanza][operators] Retain Operator should proagate unreleated fi…
Browse files Browse the repository at this point in the history
…elds of the original log entry (#35832)

<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description
The retain operator should propagate the severity field like it does
with timestamps.
This fix will ensure that is the case.

<!--Describe what testing was performed and which tests were added.-->
#### Testing
Added UT to cover the use case.
  • Loading branch information
SamerJ authored Oct 24, 2024
1 parent 0e0e581 commit 7cca491
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 8 deletions.
28 changes: 28 additions & 0 deletions .chloggen/stanza-operators-retain.yaml
Original file line number Diff line number Diff line change
@@ -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: []
18 changes: 10 additions & 8 deletions pkg/stanza/operator/transformer/retain/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,33 @@ 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 {
val, ok := e.Get(field)
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
}
40 changes: 40 additions & 0 deletions pkg/stanza/operator/transformer/retain/transformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 7cca491

Please sign in to comment.