Case Conversion Functions
2 minute read
OTTL provides two approaches for converting string case formats:
Dynamic Case Conversion with ConvertCase
The ConvertCase function accepts a case type parameter, making it useful when the case format needs to be determined dynamically.
Syntax: ConvertCase(value, caseType)
- value: the bracket notation location of the string field
- caseType: the desired case format (“lower”, “upper”, “snake”, “camel”)
Example: Converting to Uppercase
Input
{
"_type": "log",
"attributes": {
"method": "post"
},
"body": "172.28.34.214 - - [06/Dec/2024:03:39:40 +0000] \"POST /admin/users HTTP/1.1\" 204 0 \"-\" \"lambda.amazonaws.com\"",
"resource": {...},
"timestamp": 1733456539663
}
Statement
set(attributes["method"], ConvertCase(attributes["method"], "upper"))
Output
{
"_type": "log",
"attributes": {
"method": "POST"
},
"body": "172.28.34.214 - - [06/Dec/2024:03:39:40 +0000] \"POST /admin/users HTTP/1.1\" 204 0 \"-\" \"lambda.amazonaws.com\"",
"resource": {...},
"timestamp": 1733456574823
}
The method attribute was converted to uppercase.
Case-Specific Conversion Functions
For fixed case conversions, OTTL provides dedicated functions for each case format:
ToLowerCase(value)- converts to lowercase (e.g., “HELLO_WORLD” → “hello_world”)ToUpperCase(value)- converts to uppercase (e.g., “hello_world” → “HELLO_WORLD”)ToCamelCase(value)- converts to PascalCase with first letter capitalized (e.g., “hello_world” → “HelloWorld”)ToSnakeCase(value)- converts to snake_case (e.g., “HelloWorld” → “hello_world”)
These functions are simpler when the case format is known at configuration time.
Example: Multiple Case Conversions
Input
{
"_type": "log",
"body": {
"test_string": "hello_world",
"run": 23
},
"resource": {...},
"timestamp": 1727750400000
}
Statement
set(body["camel_case"], ToCamelCase(body["test_string"]))
set(body["lower_case"], ToLowerCase(body["test_string"]))
set(body["snake_case"], ToSnakeCase(body["test_string"]))
set(body["upper_case"], ToUpperCase(body["test_string"]))
Output
{
"_type": "log",
"body": {
"test_string": "hello_world",
"camel_case": "HelloWorld",
"lower_case": "hello_world",
"snake_case": "hello_world",
"upper_case": "HELLO_WORLD",
"run": 23
},
"resource": {...},
"timestamp": 1727750400000
}
Each function converted the string to its respective case format.