Skip to content

Commit 7aff95f

Browse files
authored
[pkg/ottl] Fix bug where function names were allowed in comparison (#33051)
**Description:** Fix a bug where a condition like `"foo" = Bar` resulted in the error `no value field set. This is a bug in the OpenTelemetry Transformation Language`. **Link to tracking Issue:** <Issue number if applicable> Closes #33048 **Testing:** <Describe what testing was performed and which tests were added.> Updated unit tests
1 parent 190db51 commit 7aff95f

File tree

6 files changed

+43
-17
lines changed

6 files changed

+43
-17
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: pkg/ottl
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Fixes a bug where function name could be used in a condition, resulting in a cryptic error message.
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [33051]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

pkg/ottl/e2e/e2e_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ func Test_e2e_editors(t *testing.T) {
188188
tCtx.GetLogRecord().Attributes().PutStr("http.path", "@health")
189189
},
190190
},
191+
{
192+
statement: `replace_pattern(attributes["http.path"], "/", "@", SHA256)`,
193+
want: func(tCtx ottllog.TransformContext) {
194+
tCtx.GetLogRecord().Attributes().PutStr("http.path", "c3641f8544d7c02f3580b07c0f9887f0c6a27ff5ab1d4a3e29caf197cfc299aehealth")
195+
},
196+
},
191197
{
192198
statement: `set(attributes["test"], "pass")`,
193199
want: func(tCtx ottllog.TransformContext) {

pkg/ottl/functions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ func (p *Parser[K]) buildArgs(ed editor, argsVal reflect.Value) error {
283283
switch {
284284
case arg.Value.Enum != nil:
285285
name = string(*arg.Value.Enum)
286-
case arg.Value.FunctionName != nil:
287-
name = *arg.Value.FunctionName
286+
case arg.FunctionName != nil:
287+
name = *arg.FunctionName
288288
default:
289289
return fmt.Errorf("invalid function name given")
290290
}

pkg/ottl/functions_test.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,7 @@ func Test_NewFunctionCall_invalid(t *testing.T) {
380380
Function: "testing_functiongetter",
381381
Arguments: []argument{
382382
{
383-
Value: value{
384-
FunctionName: (ottltest.Strp("SHA256")),
385-
},
383+
FunctionName: ottltest.Strp("SHA256"),
386384
},
387385
},
388386
},
@@ -1117,9 +1115,7 @@ func Test_NewFunctionCall(t *testing.T) {
11171115
Function: "testing_functiongetter",
11181116
Arguments: []argument{
11191117
{
1120-
Value: value{
1121-
FunctionName: (ottltest.Strp("SHA256")),
1122-
},
1118+
FunctionName: ottltest.Strp("SHA256"),
11231119
},
11241120
},
11251121
},
@@ -1131,9 +1127,7 @@ func Test_NewFunctionCall(t *testing.T) {
11311127
Function: "testing_functiongetter",
11321128
Arguments: []argument{
11331129
{
1134-
Value: value{
1135-
FunctionName: (ottltest.Strp("Sha256")),
1136-
},
1130+
FunctionName: ottltest.Strp("Sha256"),
11371131
},
11381132
},
11391133
},

pkg/ottl/grammar.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,9 @@ type converter struct {
218218
}
219219

220220
type argument struct {
221-
Name string `parser:"(@(Lowercase(Uppercase | Lowercase)*) Equal)?"`
222-
Value value `parser:"@@"`
221+
Name string `parser:"(@(Lowercase(Uppercase | Lowercase)*) Equal)?"`
222+
Value value `parser:"( @@"`
223+
FunctionName *string `parser:"| @(Uppercase(Uppercase | Lowercase)*) )"`
223224
}
224225

225226
func (a *argument) checkForCustomError() error {
@@ -236,7 +237,6 @@ type value struct {
236237
String *string `parser:"| @String"`
237238
Bool *boolean `parser:"| @Boolean"`
238239
Enum *enumSymbol `parser:"| @Uppercase (?! Lowercase)"`
239-
FunctionName *string `parser:"| @(Uppercase(Uppercase | Lowercase)*)"`
240240
List *list `parser:"| @@)"`
241241
}
242242

pkg/ottl/parser_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,7 @@ func Test_parse(t *testing.T) {
253253
},
254254
},
255255
{
256-
Value: value{
257-
FunctionName: (ottltest.Strp("Sha256")),
258-
},
256+
FunctionName: ottltest.Strp("Sha256"),
259257
},
260258
},
261259
},
@@ -1952,6 +1950,7 @@ func Test_parseCondition(t *testing.T) {
19521950
{`One() == 1`, false},
19531951
{`test(fail())`, true},
19541952
{`Test()`, false},
1953+
{`"test" == Foo`, true},
19551954
}
19561955
pat := regexp.MustCompile("[^a-zA-Z0-9]+")
19571956
for _, tt := range tests {

0 commit comments

Comments
 (0)