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/ottl] Add new path access for body as a string #22786

Merged
merged 2 commits into from
May 26, 2023
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
20 changes: 20 additions & 0 deletions .chloggen/ottl-body-string.yaml
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: Add `body.string` accessor to ottllog

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

# (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:
5 changes: 3 additions & 2 deletions pkg/ottl/contexts/ottllog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ The following paths are supported.
| severity_number | the severity numbner of the log being processed | int64 |
| severity_text | the severity text of the log being processed | string |
| body | the body of the log being processed | any |
| body\[""\] | a value in a map body of the log being processed. Supports multiple indexes to access nested fields. | string, bool, int64, float64, pcommon.Map, pcommon.Slice, []byte or nil |
| body\[\] | a value in a slice body of the log being processed. Supports multiple indexes to access nested fields. | string, bool, int64, float64, pcommon.Map, pcommon.Slice, []byte or nil |
| body\[""\] | a value in a map body of the log being processed. Supports multiple indexes to access nested fields. | string, bool, int64, float64, pcommon.Map, pcommon.Slice, []byte or nil |
| body\[\] | a value in a slice body of the log being processed. Supports multiple indexes to access nested fields. | string, bool, int64, float64, pcommon.Map, pcommon.Slice, []byte or nil |
| body.string | the body of the log being processed represented as a string. When setting must pass a string. | string |
| dropped_attributes_count | the number of dropped attributes of the log being processed | int64 |
| flags | the flags of the log being processed | int64 |

Expand Down
27 changes: 23 additions & 4 deletions pkg/ottl/contexts/ottllog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,16 @@ func newPathGetSetter(path []ottl.Field) (ottl.GetSetter[TransformContext], erro
case "severity_text":
return accessSeverityText(), nil
case "body":
keys := path[0].Keys
if keys == nil {
return accessBody(), nil
if len(path) == 1 {
keys := path[0].Keys
if keys == nil {
return accessBody(), nil
}
return accessBodyKey(keys), nil
}
if path[1].Name == "string" {
return accessStringBody(), nil
}
return accessBodyKey(keys), nil
case "attributes":
mapKey := path[0].Keys
if mapKey == nil {
Expand Down Expand Up @@ -306,6 +311,20 @@ func accessBodyKey(keys []ottl.Key) ottl.StandardGetSetter[TransformContext] {
}
}

func accessStringBody() ottl.StandardGetSetter[TransformContext] {
return ottl.StandardGetSetter[TransformContext]{
Getter: func(ctx context.Context, tCtx TransformContext) (interface{}, error) {
return tCtx.GetLogRecord().Body().AsString(), nil
},
Setter: func(ctx context.Context, tCtx TransformContext, val interface{}) error {
if str, ok := val.(string); ok {
tCtx.GetLogRecord().Body().SetStr(str)
}
return nil
},
}
}

func accessAttributes() ottl.StandardGetSetter[TransformContext] {
return ottl.StandardGetSetter[TransformContext]{
Getter: func(ctx context.Context, tCtx TransformContext) (interface{}, error) {
Expand Down
19 changes: 19 additions & 0 deletions pkg/ottl/contexts/ottllog/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,23 @@ func Test_newPathGetSetter(t *testing.T) {
},
bodyType: "slice",
},
{
name: "body string",
path: []ottl.Field{
{
Name: "body",
},
{
Name: "string",
},
},
orig: "1",
newVal: "2",
modified: func(log plog.LogRecord, il pcommon.InstrumentationScope, resource pcommon.Resource, cache pcommon.Map) {
log.Body().SetStr("2")
},
bodyType: "int",
},
{
name: "flags",
path: []ottl.Field{
Expand Down Expand Up @@ -724,6 +741,8 @@ func createTelemetry(bodyType string) (plog.LogRecord, pcommon.InstrumentationSc
log.Body().SetEmptyMap().PutStr("key", "val")
case "slice":
log.Body().SetEmptySlice().AppendEmpty().SetStr("body")
case "int":
log.Body().SetInt(1)
case "string":
fallthrough
default:
Expand Down