Skip to content

[processor/transform] Fix context inferrer so it takes into consideration global conditions #39463

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

Merged
27 changes: 27 additions & 0 deletions .chloggen/fix-global-conditions-ctx-inference.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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: transformprocessor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix the context inferrer to also take into consideration the global OTTL conditions configuration.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [39455]

# (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:

# 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: [user, api]
3 changes: 1 addition & 2 deletions processor/transformprocessor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ type Config struct {

// Unmarshal is used internally by mapstructure to parse the transformprocessor configuration (Config),
// adding support to structured and flat configuration styles.
// When the flat configuration style is used, each statement becomes a new common.ContextStatements
// When the flat configuration style is used, all statements are grouped into a common.ContextStatements
// object, with empty [common.ContextStatements.Context] value.
// On the other hand, structured configurations are parsed following the mapstructure Config format.
// Mixed configuration styles are also supported.
//
// Example of flat configuration:
//
Expand Down
2 changes: 1 addition & 1 deletion processor/transformprocessor/internal/common/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,5 @@ func (lpc *LogParserCollection) ParseContextStatements(contextStatements Context
if contextStatements.Context != "" {
return pc.ParseStatementsWithContext(string(contextStatements.Context), contextStatements, true)
}
return pc.ParseStatements(contextStatements)
return pc.ParseStatements(contextStatements, ottl.WithContextInferenceConditions(contextStatements.Conditions))
}
2 changes: 1 addition & 1 deletion processor/transformprocessor/internal/common/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,5 +257,5 @@ func (mpc *MetricParserCollection) ParseContextStatements(contextStatements Cont
if contextStatements.Context != "" {
return pc.ParseStatementsWithContext(string(contextStatements.Context), contextStatements, true)
}
return pc.ParseStatements(contextStatements)
return pc.ParseStatements(contextStatements, ottl.WithContextInferenceConditions(contextStatements.Conditions))
}
2 changes: 1 addition & 1 deletion processor/transformprocessor/internal/common/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,5 @@ func (tpc *TraceParserCollection) ParseContextStatements(contextStatements Conte
if contextStatements.Context != "" {
return pc.ParseStatementsWithContext(string(contextStatements.Context), contextStatements, true)
}
return pc.ParseStatements(contextStatements)
return pc.ParseStatements(contextStatements, ottl.WithContextInferenceConditions(contextStatements.Conditions))
}
50 changes: 50 additions & 0 deletions processor/transformprocessor/internal/logs/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,56 @@ func Test_ProcessLogs_CacheAccess(t *testing.T) {
}
}

func Test_ProcessLogs_InferredContextFromConditions(t *testing.T) {
tests := []struct {
name string
contextStatements []common.ContextStatements
want func(td plog.Logs)
}{
{
name: "inferring from statements",
contextStatements: []common.ContextStatements{
{
Conditions: []string{`resource.attributes["test"] == nil`},
Statements: []string{`set(log.attributes["test"], "pass")`},
},
},
want: func(td plog.Logs) {
td.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0).Attributes().PutStr("test", "pass")
td.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(1).Attributes().PutStr("test", "pass")
},
},
{
name: "inferring from conditions",
contextStatements: []common.ContextStatements{
{
Conditions: []string{`log.attributes["test"] == nil`},
Statements: []string{`set(resource.attributes["test"], "pass")`},
},
},
want: func(td plog.Logs) {
td.ResourceLogs().At(0).Resource().Attributes().PutStr("test", "pass")
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
td := constructLogs()
processor, err := NewProcessor(tt.contextStatements, ottl.IgnoreError, false, componenttest.NewNopTelemetrySettings())
assert.NoError(t, err)

_, err = processor.ProcessLogs(context.Background(), td)
assert.NoError(t, err)

exTd := constructLogs()
tt.want(exTd)

assert.Equal(t, exTd, td)
})
}
}

func Test_NewProcessor_ConditionsParse(t *testing.T) {
type testCase struct {
name string
Expand Down
57 changes: 57 additions & 0 deletions processor/transformprocessor/internal/metrics/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1880,6 +1880,63 @@ func Test_ProcessMetrics_CacheAccess(t *testing.T) {
}
}

func Test_ProcessMetrics_InferredContextFromConditions(t *testing.T) {
tests := []struct {
name string
contextStatements []common.ContextStatements
want func(td pmetric.Metrics)
}{
{
name: "inferring from statements",
contextStatements: []common.ContextStatements{
{
Conditions: []string{`resource.attributes["test"] == nil`},
Statements: []string{`set(metric.name, Concat([metric.name, "pass"], "-"))`},
},
},
want: func(td pmetric.Metrics) {
for _, v := range td.ResourceMetrics().All() {
for _, m := range v.ScopeMetrics().All() {
for _, mm := range m.Metrics().All() {
mm.SetName(mm.Name() + "-pass")
}
}
}
},
},
{
name: "inferring from conditions",
contextStatements: []common.ContextStatements{
{
Conditions: []string{`metric.name != nil`},
Statements: []string{`set(resource.attributes["test"], "pass")`},
},
},
want: func(td pmetric.Metrics) {
for _, v := range td.ResourceMetrics().All() {
v.Resource().Attributes().PutStr("test", "pass")
}
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
td := constructMetrics()
processor, err := NewProcessor(tt.contextStatements, ottl.IgnoreError, componenttest.NewNopTelemetrySettings())
assert.NoError(t, err)

_, err = processor.ProcessMetrics(context.Background(), td)
assert.NoError(t, err)

exTd := constructMetrics()
tt.want(exTd)

assert.Equal(t, exTd, td)
})
}
}

func Test_NewProcessor_ConditionsParse(t *testing.T) {
type testCase struct {
name string
Expand Down
57 changes: 57 additions & 0 deletions processor/transformprocessor/internal/traces/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,63 @@ func Test_ProcessTraces_CacheAccess(t *testing.T) {
}
}

func Test_ProcessTraces_InferredContextFromConditions(t *testing.T) {
tests := []struct {
name string
contextStatements []common.ContextStatements
want func(td ptrace.Traces)
}{
{
name: "inferring from statements",
contextStatements: []common.ContextStatements{
{
Conditions: []string{`resource.attributes["test"] == nil`},
Statements: []string{`set(span.name, Concat([span.name, "pass"], "-"))`},
},
},
want: func(td ptrace.Traces) {
for _, v := range td.ResourceSpans().All() {
for _, m := range v.ScopeSpans().All() {
for _, mm := range m.Spans().All() {
mm.SetName(mm.Name() + "-pass")
}
}
}
},
},
{
name: "inferring from conditions",
contextStatements: []common.ContextStatements{
{
Conditions: []string{`span.name != nil`},
Statements: []string{`set(resource.attributes["test"], "pass")`},
},
},
want: func(td ptrace.Traces) {
for _, v := range td.ResourceSpans().All() {
v.Resource().Attributes().PutStr("test", "pass")
}
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
td := constructTraces()
processor, err := NewProcessor(tt.contextStatements, ottl.IgnoreError, componenttest.NewNopTelemetrySettings())
assert.NoError(t, err)

_, err = processor.ProcessTraces(context.Background(), td)
assert.NoError(t, err)

exTd := constructTraces()
tt.want(exTd)

assert.Equal(t, exTd, td)
})
}
}

func Test_NewProcessor_ConditionsParse(t *testing.T) {
type testCase struct {
name string
Expand Down
Loading