Skip to content

Commit d9c4f2d

Browse files
authored
[pkg/ottl] Add new path access for body as a string (#22786)
* Add new path access for body as a string * changelog
1 parent de252f7 commit d9c4f2d

File tree

4 files changed

+65
-6
lines changed

4 files changed

+65
-6
lines changed

.chloggen/ottl-body-string.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use this changelog template to create an entry for release notes.
2+
# If your change doesn't affect end users, such as a test fix or a tooling change,
3+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
4+
5+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
6+
change_type: enhancement
7+
8+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
9+
component: pkg/ottl
10+
11+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
12+
note: Add `body.string` accessor to ottllog
13+
14+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
15+
issues: [22786]
16+
17+
# (Optional) One or more lines of additional information to render under the primary note.
18+
# These lines will be padded with 2 spaces and then inserted directly into the document.
19+
# Use pipe (|) for multiline entries.
20+
subtext:

pkg/ottl/contexts/ottllog/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ The following paths are supported.
3434
| severity_number | the severity numbner of the log being processed | int64 |
3535
| severity_text | the severity text of the log being processed | string |
3636
| body | the body of the log being processed | any |
37-
| 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 |
38-
| 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 |
37+
| 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 |
38+
| 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 |
39+
| body.string | the body of the log being processed represented as a string. When setting must pass a string. | string |
3940
| dropped_attributes_count | the number of dropped attributes of the log being processed | int64 |
4041
| flags | the flags of the log being processed | int64 |
4142

pkg/ottl/contexts/ottllog/log.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,16 @@ func newPathGetSetter(path []ottl.Field) (ottl.GetSetter[TransformContext], erro
153153
case "severity_text":
154154
return accessSeverityText(), nil
155155
case "body":
156-
keys := path[0].Keys
157-
if keys == nil {
158-
return accessBody(), nil
156+
if len(path) == 1 {
157+
keys := path[0].Keys
158+
if keys == nil {
159+
return accessBody(), nil
160+
}
161+
return accessBodyKey(keys), nil
162+
}
163+
if path[1].Name == "string" {
164+
return accessStringBody(), nil
159165
}
160-
return accessBodyKey(keys), nil
161166
case "attributes":
162167
mapKey := path[0].Keys
163168
if mapKey == nil {
@@ -306,6 +311,20 @@ func accessBodyKey(keys []ottl.Key) ottl.StandardGetSetter[TransformContext] {
306311
}
307312
}
308313

314+
func accessStringBody() ottl.StandardGetSetter[TransformContext] {
315+
return ottl.StandardGetSetter[TransformContext]{
316+
Getter: func(ctx context.Context, tCtx TransformContext) (interface{}, error) {
317+
return tCtx.GetLogRecord().Body().AsString(), nil
318+
},
319+
Setter: func(ctx context.Context, tCtx TransformContext, val interface{}) error {
320+
if str, ok := val.(string); ok {
321+
tCtx.GetLogRecord().Body().SetStr(str)
322+
}
323+
return nil
324+
},
325+
}
326+
}
327+
309328
func accessAttributes() ottl.StandardGetSetter[TransformContext] {
310329
return ottl.StandardGetSetter[TransformContext]{
311330
Getter: func(ctx context.Context, tCtx TransformContext) (interface{}, error) {

pkg/ottl/contexts/ottllog/log_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,23 @@ func Test_newPathGetSetter(t *testing.T) {
197197
},
198198
bodyType: "slice",
199199
},
200+
{
201+
name: "body string",
202+
path: []ottl.Field{
203+
{
204+
Name: "body",
205+
},
206+
{
207+
Name: "string",
208+
},
209+
},
210+
orig: "1",
211+
newVal: "2",
212+
modified: func(log plog.LogRecord, il pcommon.InstrumentationScope, resource pcommon.Resource, cache pcommon.Map) {
213+
log.Body().SetStr("2")
214+
},
215+
bodyType: "int",
216+
},
200217
{
201218
name: "flags",
202219
path: []ottl.Field{
@@ -724,6 +741,8 @@ func createTelemetry(bodyType string) (plog.LogRecord, pcommon.InstrumentationSc
724741
log.Body().SetEmptyMap().PutStr("key", "val")
725742
case "slice":
726743
log.Body().SetEmptySlice().AppendEmpty().SetStr("body")
744+
case "int":
745+
log.Body().SetInt(1)
727746
case "string":
728747
fallthrough
729748
default:

0 commit comments

Comments
 (0)