Index

Learn about the Index OTTL converter function.

The Index converter returns the index (position) of the first occurrence of a value within a string or slice. Returns -1 if the value is not found.

Syntax: Index(target, value)

  • target: a string or slice to search in
  • value: the content to search for

The returned type is int64.

Values are compared using OTTL comparison rules, which means type-aware matching is performed.

Use Cases:

  • Find the position of a substring in a string
  • Locate an element in an array/slice
  • Check if a value exists (index >= 0 means found, -1 means not found)

Input

{
	"_type": "log",
	"attributes": {
		"tags": ["debug", "error", "warning"],
		"message": "User login failed"
	},
	"body": "Failed authentication attempt",
	"resource": {...},
	"timestamp": 1733900000000
}

Statement

set(attributes["error_position"], Index(attributes["tags"], "error"))
set(attributes["login_word_position"], Index(attributes["message"], "login"))

Output

{
	"_type": "log",
	"attributes": {
		"tags": ["debug", "error", "warning"],
		"message": "User login failed",
		"error_position": 1,
		"login_word_position": 5
	},
	"body": "Failed authentication attempt",
	"resource": {...},
	"timestamp": 1733900000000
}

The function found “error” at position 1 in the tags array (zero-indexed), and found “login” starting at character position 5 in the message string.