diff --git a/pkg/ottl/e2e/e2e_test.go b/pkg/ottl/e2e/e2e_test.go index 315a00e8c28b..0095a171f91d 100644 --- a/pkg/ottl/e2e/e2e_test.go +++ b/pkg/ottl/e2e/e2e_test.go @@ -534,6 +534,36 @@ func Test_e2e_converters(t *testing.T) { s.AppendEmpty().SetStr("C") }, }, + { + statement: `set(attributes["test"], String("test"))`, + want: func(tCtx ottllog.TransformContext) { + tCtx.GetLogRecord().Attributes().PutStr("test", "test") + }, + }, + { + statement: `set(attributes["test"], String(attributes["http.method"]))`, + want: func(tCtx ottllog.TransformContext) { + tCtx.GetLogRecord().Attributes().PutStr("test", "get") + }, + }, + { + statement: `set(attributes["test"], String(span_id))`, + want: func(tCtx ottllog.TransformContext) { + tCtx.GetLogRecord().Attributes().PutStr("test", "[1,2,3,4,5,6,7,8]") + }, + }, + { + statement: `set(attributes["test"], String([1,2,3]))`, + want: func(tCtx ottllog.TransformContext) { + tCtx.GetLogRecord().Attributes().PutStr("test", "[1,2,3]") + }, + }, + { + statement: `set(attributes["test"], String(true))`, + want: func(tCtx ottllog.TransformContext) { + tCtx.GetLogRecord().Attributes().PutStr("test", "true") + }, + }, { statement: `set(attributes["test"], Substring("pass", 0, 2))`, want: func(tCtx ottllog.TransformContext) { diff --git a/pkg/ottl/ottlfuncs/README.md b/pkg/ottl/ottlfuncs/README.md index 5f421bdbe852..fc7a104ce50d 100644 --- a/pkg/ottl/ottlfuncs/README.md +++ b/pkg/ottl/ottlfuncs/README.md @@ -1077,23 +1077,24 @@ The `String` Converter converts the `value` to string type. The returned type is `string`. -- float64. The function returns the `value` as a string type. -- string. The function returns the `value`, even if it's empty. -- bool. The function returns true/false as "true"/"false". -- int64. The function returns the `value` as a string type. -- struct. The function returns an empty struct as "{}" +- string. The function returns the `value` without changes. +- []byte. The function returns the `value` as a string encoded in hexadecimal. +- map. The function returns the `value` as a key-value-pair of type string. +- slice. The function returns the `value` as a list formatted string. +- pcommon.Value. The function returns the `value` as a string type. -If value is empty, another type or parsing failed, nil is always returned. +If `value` is of another type it gets marshalled to string type. +If `value` is empty, or parsing failed, nil is always returned. The `value` is either a path expression to a telemetry field to retrieve, or a literal. Examples: -- `String(2.7)` -- `String("")` +- `String("test")` +- `String(attributes["http.method"])` +- `String(span_id)` +- `String([1,2,3])` - `String(false)` -- `String(13)` -- `String(attributes["http.status_code"])` ### Substring diff --git a/pkg/ottl/ottlfuncs/func_string_test.go b/pkg/ottl/ottlfuncs/func_string_test.go index 35a330192654..a62598159400 100644 --- a/pkg/ottl/ottlfuncs/func_string_test.go +++ b/pkg/ottl/ottlfuncs/func_string_test.go @@ -65,10 +65,19 @@ func Test_String(t *testing.T) { expected: nil, }, { - name: "some struct", - value: struct{}{}, - expected: "{}", - err: false, + name: "byte", + value: []byte{123}, + expected: string("7b"), + }, + { + name: "map", + value: map[int]bool{1: true, 2: false}, + expected: string("{\"1\":true,\"2\":false}"), + }, + { + name: "slice", + value: []int{1, 2, 3}, + expected: string("[1,2,3]"), }, } for _, tt := range tests {