Skip to content

Commit

Permalink
[processor/transformprocessor] Implement ConvertCase OTTL function in…
Browse files Browse the repository at this point in the history
…to transform processor (open-telemetry#16217)

This commit adds ConvertCase OTTL function - which allows string conversion to lowercase, uppercase, snakecase and camelcase - to the Transform Processor.
  • Loading branch information
clarkjohnd committed Nov 9, 2022
1 parent 2475cc7 commit aede915
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .chloggen/transform-processor-convert-case.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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: transformprocessor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Added OTTL function ConvertCase into the Transform Processor

# One or more tracking issues related to the change
issues: [16083]

# (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:
1 change: 1 addition & 0 deletions processor/transformprocessor/internal/common/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func Functions[K any]() map[string]interface{} {
"Concat": ottlfuncs.Concat[K],
"Split": ottlfuncs.Split[K],
"Int": ottlfuncs.Int[K],
"ConvertCase": ottlfuncs.ConvertCase[K],
"keep_keys": ottlfuncs.KeepKeys[K],
"set": ottlfuncs.Set[K],
"truncate_all": ottlfuncs.TruncateAll[K],
Expand Down
24 changes: 24 additions & 0 deletions processor/transformprocessor/internal/logs/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,30 @@ func TestProcess(t *testing.T) {
statement: `set(attributes["test"], Split(attributes["not_exist"], "|"))`,
want: func(td plog.Logs) {},
},
{
statement: `set(attributes["test"], ConvertCase(body, "lower")) where body == "operationA"`,
want: func(td plog.Logs) {
td.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0).Attributes().PutStr("test", "operationa")
},
},
{
statement: `set(attributes["test"], ConvertCase(body, "upper")) where body == "operationA"`,
want: func(td plog.Logs) {
td.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0).Attributes().PutStr("test", "OPERATIONA")
},
},
{
statement: `set(attributes["test"], ConvertCase(body, "snake")) where body == "operationA"`,
want: func(td plog.Logs) {
td.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0).Attributes().PutStr("test", "operation_a")
},
},
{
statement: `set(attributes["test"], ConvertCase(body, "camel")) where body == "operationA"`,
want: func(td plog.Logs) {
td.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0).Attributes().PutStr("test", "OperationA")
},
},
}

for _, tt := range tests {
Expand Down
18 changes: 18 additions & 0 deletions processor/transformprocessor/internal/metrics/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,24 @@ func TestProcess(t *testing.T) {
statements: []string{`set(attributes["test"], Split(attributes["not_exist"], "|"))`},
want: func(td pmetric.Metrics) {},
},
{
statements: []string{
`set(attributes["test_lower"], ConvertCase(metric.name, "lower")) where metric.name == "operationA"`,
`set(attributes["test_upper"], ConvertCase(metric.name, "upper")) where metric.name == "operationA"`,
`set(attributes["test_snake"], ConvertCase(metric.name, "snake")) where metric.name == "operationA"`,
`set(attributes["test_camel"], ConvertCase(metric.name, "camel")) where metric.name == "operationA"`,
},
want: func(td pmetric.Metrics) {
td.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0).Sum().DataPoints().At(0).Attributes().PutStr("test_lower", "operationa")
td.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0).Sum().DataPoints().At(1).Attributes().PutStr("test_lower", "operationa")
td.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0).Sum().DataPoints().At(0).Attributes().PutStr("test_upper", "OPERATIONA")
td.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0).Sum().DataPoints().At(1).Attributes().PutStr("test_upper", "OPERATIONA")
td.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0).Sum().DataPoints().At(0).Attributes().PutStr("test_snake", "operation_a")
td.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0).Sum().DataPoints().At(1).Attributes().PutStr("test_snake", "operation_a")
td.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0).Sum().DataPoints().At(0).Attributes().PutStr("test_camel", "OperationA")
td.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0).Sum().DataPoints().At(1).Attributes().PutStr("test_camel", "OperationA")
},
},
}

for _, tt := range tests {
Expand Down
21 changes: 21 additions & 0 deletions processor/transformprocessor/internal/traces/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,27 @@ func TestProcess(t *testing.T) {
td.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(1).Attributes().PutStr("entrypoint", "operationB")
},
},
{
statement: `set(attributes["test"], ConvertCase(name, "lower")) where name == "operationA"`,
want: func(td ptrace.Traces) {
td.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0).Attributes().PutStr("test", "operationa")
},
}, {
statement: `set(attributes["test"], ConvertCase(name, "upper")) where name == "operationA"`,
want: func(td ptrace.Traces) {
td.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0).Attributes().PutStr("test", "OPERATIONA")
},
}, {
statement: `set(attributes["test"], ConvertCase(name, "snake")) where name == "operationA"`,
want: func(td ptrace.Traces) {
td.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0).Attributes().PutStr("test", "operation_a")
},
}, {
statement: `set(attributes["test"], ConvertCase(name, "camel")) where name == "operationA"`,
want: func(td ptrace.Traces) {
td.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0).Attributes().PutStr("test", "OperationA")
},
},
}

for _, tt := range tests {
Expand Down

0 comments on commit aede915

Please sign in to comment.