-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pkg/ottl] Add log body indexing (#22102)
* Add log body indexing * Validate that an index was actually passed when trying to index an empty pdata value * Fix lint * Address PR feedback --------- Co-authored-by: Evan Bradley <[email protected]>
- Loading branch information
1 parent
35739c4
commit 85a618f
Showing
9 changed files
with
475 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
# If your change doesn't affect end users, such as a test fix or a tooling change, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# 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: Allow indexing map and slice log bodies | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [17396, 22068] | ||
|
||
# (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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package internal // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal" | ||
|
||
import ( | ||
"fmt" | ||
|
||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
func GetSliceValue(s pcommon.Slice, keys []ottl.Key) (interface{}, error) { | ||
if len(keys) == 0 { | ||
return nil, fmt.Errorf("cannot get slice value without key") | ||
} | ||
if keys[0].Int == nil { | ||
return nil, fmt.Errorf("non-integer indexing is not supported") | ||
} | ||
idx := int(*keys[0].Int) | ||
|
||
if idx < 0 || idx >= s.Len() { | ||
return nil, fmt.Errorf("index %d out of bounds", idx) | ||
} | ||
|
||
return getIndexableValue(s.At(int(*keys[0].Int)), keys[1:]) | ||
} | ||
|
||
func SetSliceValue(s pcommon.Slice, keys []ottl.Key, val interface{}) error { | ||
if len(keys) == 0 { | ||
return fmt.Errorf("cannot set slice value without key") | ||
} | ||
if keys[0].Int == nil { | ||
return fmt.Errorf("non-integer indexing is not supported") | ||
} | ||
idx := int(*keys[0].Int) | ||
|
||
if idx < 0 || idx >= s.Len() { | ||
return fmt.Errorf("index %d out of bounds", idx) | ||
} | ||
|
||
return setIndexableValue(s.At(int(*keys[0].Int)), val, keys[1:]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package internal | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottltest" | ||
) | ||
|
||
func Test_GetSliceValue_Invalid(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
keys []ottl.Key | ||
err error | ||
}{ | ||
{ | ||
name: "no keys", | ||
keys: []ottl.Key{}, | ||
err: fmt.Errorf("cannot get slice value without key"), | ||
}, | ||
{ | ||
name: "first key not an integer", | ||
keys: []ottl.Key{ | ||
{ | ||
String: ottltest.Strp("key"), | ||
}, | ||
}, | ||
err: fmt.Errorf("non-integer indexing is not supported"), | ||
}, | ||
{ | ||
name: "index too large", | ||
keys: []ottl.Key{ | ||
{ | ||
Int: ottltest.Intp(1), | ||
}, | ||
}, | ||
err: fmt.Errorf("index 1 out of bounds"), | ||
}, | ||
{ | ||
name: "index too small", | ||
keys: []ottl.Key{ | ||
{ | ||
Int: ottltest.Intp(-1), | ||
}, | ||
}, | ||
err: fmt.Errorf("index -1 out of bounds"), | ||
}, | ||
{ | ||
name: "invalid type", | ||
keys: []ottl.Key{ | ||
{ | ||
Int: ottltest.Intp(0), | ||
}, | ||
{ | ||
String: ottltest.Strp("string"), | ||
}, | ||
}, | ||
err: fmt.Errorf("type Str does not support string indexing"), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
s := pcommon.NewSlice() | ||
s.AppendEmpty().SetStr("val") | ||
|
||
_, err := GetSliceValue(s, tt.keys) | ||
assert.Equal(t, tt.err, err) | ||
}) | ||
} | ||
} | ||
|
||
func Test_SetSliceValue_Invalid(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
keys []ottl.Key | ||
err error | ||
}{ | ||
{ | ||
name: "no keys", | ||
keys: []ottl.Key{}, | ||
err: fmt.Errorf("cannot set slice value without key"), | ||
}, | ||
{ | ||
name: "first key not an integer", | ||
keys: []ottl.Key{ | ||
{ | ||
String: ottltest.Strp("key"), | ||
}, | ||
}, | ||
err: fmt.Errorf("non-integer indexing is not supported"), | ||
}, | ||
{ | ||
name: "index too large", | ||
keys: []ottl.Key{ | ||
{ | ||
Int: ottltest.Intp(1), | ||
}, | ||
}, | ||
err: fmt.Errorf("index 1 out of bounds"), | ||
}, | ||
{ | ||
name: "index too small", | ||
keys: []ottl.Key{ | ||
{ | ||
Int: ottltest.Intp(-1), | ||
}, | ||
}, | ||
err: fmt.Errorf("index -1 out of bounds"), | ||
}, | ||
{ | ||
name: "invalid type", | ||
keys: []ottl.Key{ | ||
{ | ||
Int: ottltest.Intp(0), | ||
}, | ||
{ | ||
String: ottltest.Strp("string"), | ||
}, | ||
}, | ||
err: fmt.Errorf("type Str does not support string indexing"), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
s := pcommon.NewSlice() | ||
s.AppendEmpty().SetStr("val") | ||
|
||
err := SetSliceValue(s, tt.keys, "value") | ||
assert.Equal(t, tt.err, err) | ||
}) | ||
} | ||
} |
Oops, something went wrong.