HasPrefix

Learn about the HasPrefix OTTL converter function.

Overview

The HasPrefix converter checks if a string starts with a specified prefix and returns a boolean value.

Syntax

HasPrefix(target, prefix)

  • target: the string to check
  • prefix: the prefix string to look for

Examples

Input

{
	"_type": "log",
	"body": {
		"prefix_test": "hello_world",
		"run": 23
	},
	"resource": {...},
	"timestamp": 1727750400000
}

Statement

set(body["has_prefix_hello"], HasPrefix(body["prefix_test"], "hello"))

Output

{
	"_type": "log",
	"body": {
		"prefix_test": "hello_world",
		"has_prefix_hello": true,
		"run": 23
	},
	"resource": {...},
	"timestamp": 1727750400000
}

The function correctly identified that “hello_world” starts with “hello”.

Dynamic arguments

The prefix argument can be read from a field instead of a literal string.

Input

{
	"_type": "log",
	"body": {
		"message": "ERROR: Connection to db-primary-west failed",
		"prefix_field": "ERROR"
	},
	"resource": {...},
	"attributes": {}
}

Statement

set(attributes["hasprefix_dynamic"], HasPrefix(body["message"], body["prefix_field"]))

Output

{
	"_type": "log",
	"body": {
		"message": "ERROR: Connection to db-primary-west failed",
		"prefix_field": "ERROR"
	},
	"resource": {...},
	"attributes": {
		"hasprefix_dynamic": true
	}
}

The prefix ERROR was read from body["prefix_field"] and matched against the start of the message.