Skip to content

Commit

Permalink
added e2e, updated test & README
Browse files Browse the repository at this point in the history
  • Loading branch information
led0nk committed Mar 20, 2024
1 parent 55b0ff7 commit 008a0cb
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
30 changes: 30 additions & 0 deletions pkg/ottl/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
21 changes: 11 additions & 10 deletions pkg/ottl/ottlfuncs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 13 additions & 4 deletions pkg/ottl/ottlfuncs/func_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 008a0cb

Please sign in to comment.