Skip to content

[pkg/ottl] Fix the context inference order to prioritize scope over resource #39307

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
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
27 changes: 27 additions & 0 deletions .chloggen/fix-context-inferrer-priority.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: pkg/ottl

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix OTTL context inference order to prioritize the `scope` context over `resource`.

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

# (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: []
2 changes: 1 addition & 1 deletion pkg/ottl/context_inferrer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ var defaultContextInferPriority = []string{
"metric",
"spanevent",
"span",
"resource",
"scope",
"instrumentation_scope",
"resource",
}

// contextInferrer is an interface used to infer the OTTL context from statements.
Expand Down
190 changes: 188 additions & 2 deletions pkg/ottl/context_inferrer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,16 +282,16 @@ func Test_NewPriorityContextInferrer_InvalidStatement(t *testing.T) {
require.ErrorContains(t, err, "unexpected token")
}

func Test_DefaultPriorityContextInferrer(t *testing.T) {
func Test_NewPriorityContextInferrer_DefaultPriorityList(t *testing.T) {
expectedPriority := []string{
"log",
"datapoint",
"metric",
"spanevent",
"span",
"resource",
"scope",
"instrumentation_scope",
"resource",
}

inferrer := newPriorityContextInferrer(componenttest.NewNopTelemetrySettings(), map[string]*priorityContextInferrerCandidate{}).(*priorityContextInferrer)
Expand All @@ -301,3 +301,189 @@ func Test_DefaultPriorityContextInferrer(t *testing.T) {
require.Equal(t, pri, inferrer.contextPriority[ctx])
}
}

func Test_NewPriorityContextInferrer_InferStatements_DefaultContextsOrder(t *testing.T) {
inferrer := newPriorityContextInferrer(componenttest.NewNopTelemetrySettings(), map[string]*priorityContextInferrerCandidate{
"log": newDummyPriorityContextInferrerCandidate(true, true, []string{"scope", "instrumentation_scope", "resource"}),
"metric": newDummyPriorityContextInferrerCandidate(true, true, []string{"datapoint", "scope", "instrumentation_scope", "resource"}),
"datapoint": newDummyPriorityContextInferrerCandidate(true, true, []string{"scope", "instrumentation_scope", "resource"}),
"span": newDummyPriorityContextInferrerCandidate(true, true, []string{"spanevent", "scope", "instrumentation_scope", "resource"}),
"spanevent": newDummyPriorityContextInferrerCandidate(true, true, []string{"scope", "instrumentation_scope", "resource"}),
"scope": newDummyPriorityContextInferrerCandidate(true, true, []string{"resource"}),
"instrumentation_scope": newDummyPriorityContextInferrerCandidate(true, true, []string{"resource"}),
"resource": newDummyPriorityContextInferrerCandidate(true, true, []string{}),
})

tests := []struct {
name string
statement string
expected string
}{
{
name: "log,instrumentation_scope,resource",
statement: `set(log.attributes["foo"], true) where instrumentation_scope.attributes["foo"] == resource.attributes["foo"]`,
expected: "log",
},
{
name: "log,scope,resource",
statement: `set(log.attributes["foo"], true) where scope.attributes["foo"] == resource.attributes["foo"]`,
expected: "log",
},
{
name: "instrumentation_scope,resource",
statement: `set(instrumentation_scope.attributes["foo"], true) where resource.attributes["foo"] != nil`,
expected: "instrumentation_scope",
},
{
name: "scope,resource",
statement: `set(scope.attributes["foo"], true) where resource.attributes["foo"] != nil`,
expected: "scope",
},
{
name: "metric,instrumentation_scope,resource",
statement: `set(metric.name, "foo") where instrumentation_scope.name != nil and resource.attributes["foo"] != nil`,
expected: "metric",
},
{
name: "metric,scope,resource",
statement: `set(metric.name, "foo") where scope.name != nil and resource.attributes["foo"] != nil`,
expected: "metric",
},
{
name: "datapoint,metric,instrumentation_scope,resource",
statement: `set(metric.name, "foo") where datapoint.double_value > 0 and instrumentation_scope.name != nil and resource.attributes["foo"] != nil`,
expected: "datapoint",
},
{
name: "datapoint,metric,scope,resource",
statement: `set(metric.name, "foo") where datapoint.double_value > 0 and scope.name != nil and resource.attributes["foo"] != nil`,
expected: "datapoint",
},
{
name: "span,instrumentation_scope,resource",
statement: `set(span.name, "foo") where instrumentation_scope.name != nil and resource.attributes["foo"] != nil`,
expected: "span",
},
{
name: "span,scope,resource",
statement: `set(span.name, "foo") where scope.name != nil and resource.attributes["foo"] != nil`,
expected: "span",
},
{
name: "spanevent,span,instrumentation_scope,resource",
statement: `set(span.name, "foo") where spanevent.name != nil and instrumentation_scope.name != nil and resource.attributes["foo"] != nil`,
expected: "spanevent",
},
{
name: "spanevent,span,scope,resource",
statement: `set(span.name, "foo") where spanevent.name != nil and scope.name != nil and resource.attributes["foo"] != nil`,
expected: "spanevent",
},
{
name: "resource",
statement: `set(resource.attributes["bar"], "foo") where dummy.attributes["foo"] != nil`,
expected: "resource",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
inferred, err := inferrer.inferFromStatements([]string{tt.statement})
require.NoError(t, err)
assert.Equal(t, tt.expected, inferred)
})
}
}

func Test_NewPriorityContextInferrer_InferConditions_DefaultContextsOrder(t *testing.T) {
inferrer := newPriorityContextInferrer(componenttest.NewNopTelemetrySettings(), map[string]*priorityContextInferrerCandidate{
"log": newDummyPriorityContextInferrerCandidate(true, true, []string{"scope", "instrumentation_scope", "resource"}),
"metric": newDummyPriorityContextInferrerCandidate(true, true, []string{"datapoint", "scope", "instrumentation_scope", "resource"}),
"datapoint": newDummyPriorityContextInferrerCandidate(true, true, []string{"scope", "instrumentation_scope", "resource"}),
"span": newDummyPriorityContextInferrerCandidate(true, true, []string{"spanevent", "scope", "instrumentation_scope", "resource"}),
"spanevent": newDummyPriorityContextInferrerCandidate(true, true, []string{"scope", "instrumentation_scope", "resource"}),
"scope": newDummyPriorityContextInferrerCandidate(true, true, []string{"resource"}),
"instrumentation_scope": newDummyPriorityContextInferrerCandidate(true, true, []string{"resource"}),
"resource": newDummyPriorityContextInferrerCandidate(true, true, []string{}),
})

tests := []struct {
name string
condition string
expected string
}{
{
name: "log,instrumentation_scope,resource",
condition: `log.attributes["foo"] !=nil and instrumentation_scope.attributes["foo"] == resource.attributes["foo"]`,
expected: "log",
},
{
name: "log,scope,resource",
condition: `log.attributes["foo"] != nil and scope.attributes["foo"] == resource.attributes["foo"]`,
expected: "log",
},
{
name: "instrumentation_scope,resource",
condition: `instrumentation_scope.attributes["foo"] != nil and resource.attributes["foo"] != nil`,
expected: "instrumentation_scope",
},
{
name: "scope,resource",
condition: `scope.attributes["foo"] != nil and resource.attributes["foo"] != nil`,
expected: "scope",
},
{
name: "metric,instrumentation_scope,resource",
condition: `metric.name != nil and instrumentation_scope.name != nil and resource.attributes["foo"] != nil`,
expected: "metric",
},
{
name: "metric,scope,resource",
condition: `metric.name != nil and scope.name != nil and resource.attributes["foo"] != nil`,
expected: "metric",
},
{
name: "datapoint,metric,instrumentation_scope,resource",
condition: `metric.name != nil and datapoint.double_value > 0 and instrumentation_scope.name != nil and resource.attributes["foo"] != nil`,
expected: "datapoint",
},
{
name: "datapoint,metric,scope,resource",
condition: `metric.name != nil and datapoint.double_value > 0 and scope.name != nil and resource.attributes["foo"] != nil`,
expected: "datapoint",
},
{
name: "span,instrumentation_scope,resource",
condition: `span.name != nil and instrumentation_scope.name != nil and resource.attributes["foo"] != nil`,
expected: "span",
},
{
name: "span,scope,resource",
condition: `span.name != nil and scope.name != nil and resource.attributes["foo"] != nil`,
expected: "span",
},
{
name: "spanevent,span,instrumentation_scope,resource",
condition: `span.name != nil and spanevent.name != nil and instrumentation_scope.name != nil and resource.attributes["foo"] != nil`,
expected: "spanevent",
},
{
name: "spanevent,span,scope,resource",
condition: `span.name != nil and spanevent.name != nil and scope.name != nil and resource.attributes["foo"] != nil`,
expected: "spanevent",
},
{
name: "resource",
condition: `resource.attributes["bar"] != nil and dummy.attributes["foo"] != nil`,
expected: "resource",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
inferred, err := inferrer.inferFromConditions([]string{tt.condition})
require.NoError(t, err)
assert.Equal(t, tt.expected, inferred)
})
}
}