Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pkg/stanza][operators] Retain Operator should proagate unreleated fields of the original log entry #35832

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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