HasSuffix

Learn about the HasSuffix OTTL converter function.

Overview

The HasSuffix converter checks if a string ends with a specified suffix and returns a boolean value.

Syntax

HasSuffix(target, suffix)

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

Examples

Input

{
	"_type": "log",
	"body": {
		"suffix_test": "test_file.txt",
		"run": 23
	},
	"resource": {...},
	"timestamp": 1727750400000
}

Statement

set(body["has_suffix_txt"], HasSuffix(body["suffix_test"], ".txt"))

Output

{
	"_type": "log",
	"body": {
		"suffix_test": "test_file.txt",
		"has_suffix_txt": true,
		"run": 23
	},
	"resource": {...},
	"timestamp": 1727750400000
}

The function correctly identified that “test_file.txt” ends with “.txt”.

Dynamic arguments

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

Input

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

Statement

set(attributes["hassuffix_dynamic"], HasSuffix(body["message"], body["suffix_field"]))

Output

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

The suffix failed was read from body["suffix_field"] and matched against the end of the message.