Template functions
This document contains the various template functions that can be used in the ingester configuration (manifest.yaml).
toFloat
toFloatThis function is used to convert the passed in int64 value to a float64 value.
Example Usage
${toFloat 56}renders as 56.0.
minus
minusThis function is used to subtract a int64 value from another.
Example Usage
${minus 34 20}renders as 14.
minusf
minusfSimilar to minus this used to subtract 2 float64 values.
Example Usage
${minusf 34 20}renders as 14.0.
add
addThis function is used to add two int64 values.
Example Usage
renders as 42
httpGet
httpGetThis function is used to call a HTTP URL.
Example Usage
renders as true
Why
This function was written because some systems that we integrate with requires a webhook validation step before that system can send alerts through the webhook. For example, AWS Cloudwatch sends a JSON payload when the user configures it with squadcast. This initial payload is not an alert event, rather this payload contains an SubscriptionConfirmation URL which had to be called. Once this is called, AWS will start sending any alerts if any.
Checkout AWS Configuration for more info.
concat
concatThis function used to add/concatenate two strings.
Example Usage
renders as Hello World!
trim
trimThis function is used to trim the spaces from the start and end of the passed string.
Example Usage
renders as Lots of me
unsnake
unsnakeThis function is used to replace all the underscores (_) wirh a single space each from the passed string.
Example Usage
renders as hiss hiss hiss
capitalize
capitalizeThis function capitalizes the first letter of the passed string.
Example Usage
renders as Hello World!!
toLower
toLowerThis function is used to convert the passed string to lower case.
Example Usage
renders as don't shout!!
jsonParse
jsonParseThis function is used to parse the passed JSON string and return the Go's notation of the JSON object.
If the passed string contains a JSON Object at the root level, then a map[string]interface{} is returned. If array is at the root level, []interface{} is returned. This inturn can be used inside the template as well.
Example Usage
Generally this is used as value with with the subsequent templates can be formed. This should not be rendered directly else will yield unfavourable/unreadable results.
Why
AWS Cloudwatch sends JSON object as a serialized string in it's alerts instead of a nested JSON Object. So we have to parse that string to get the field values inside the JSON string.
Checkout AWS Cloudwatch configuration and Sample AWS Cloudwatch payload for more.
toJSON
toJSONThis function is used to convert the passed template variable into the JSON representation.
Example Usage
This function is only used internally to get the array selector from the incoming payload as a Go variable. This is not used in any of the alert source configurations so far.
Assuming $ages is a variable which contains a map of names to ages, this renders as:
toYAML
toYAMLThis function is used to convert the passed template variable into the YAML representation.
Example Usage
This function is used to display nested JSON data since YAML makes it more readable
Assuming $stats is the following JSON
the output of toYAML:
toInt
toIntThis function is used convert the passed string to int64 (Base 10). If the passed string cannot be parsed into integer, then 0 is returned.
Example Usage
renders as 121231. It is same for rendering the string and the int version. This is mostly used to perform calculations like minus, add etc.,
typeOf
typeOfThis is a debug function which is used to print the type of the passed variable. It returns 0 no matter what.
Example Usage
renders as 0 but Type of 45 is int will be printed in the stdout of ingester. This is merely a debug helper function. Should not be used for production alert source configurations.
str
strThis function is used to sprint any passed arguments.
Example Usage
Assuming similar example as toJSON, this will render as map[John:28 Eve:30 Mary:27]. This should be used only for debugging purposes.
strf
strfThis function is similar to sprintf where the arguments are formatted into a string based on a passed format.
Example Usage
renders as 1E.
ftoi
ftoiThis function is used to convert the passed float64 to int64.
Example Usage
renders as 45. When a JSON data is unmarshalled using a generic data type interface{}, the underlying type becomes map[string]interface{} / []interface{}. And the string values are stored as string, true/false as booleans, numbers as float64 even though it might have been an int64. Go prints the float64 representation as exponential format like 2.124987e6 which when converted to int64 gets formatted right.
showIf
showIfThis function is used to render a line with a variable, only if the passed variable is not nil.
Example Usage
(assuming $root.agent_name is falcon)
This renders the string
if the passed payload has a field named agent_name. If the passed payload doesn't have a agent_name, then this line not rendered. Be sure add a - in the end of the template closer, to remove any empty lines in the rendered content.
timestampToString
timestampToStringThis function is used to convert unix timestamp to RFC1123 format.
Example Usage
renders as Mon, 01 Jun 2020 16:42:03 IST.
convertTimestamp
convertTimestampThis function is used to convert timestamp from one format to another.
Example Usage
renders as Fri, 30 Jul 2021 12:56:23 +0530.
html2markdown
html2markdownThis function is used to convert html to markdown.
Example Usage
renders as #Html to markdown.
reNamedMatches
reNamedMatchesThis function is used to extract out the named matches from a string using a regex.
Example Usage
yields :
reverse
reverseThis function is used to reverse the passed arbitrary array ([]interface{}).
Example Usage
Say the payload is
the following template
should yield
divide
divideThis function is used to 2 floats and yield a float. (all float64s)
Example Usage
toJSONIndent
toJSONIndentThis function is similar to toJSON, but adds an indentation spaces (4 spaces) at every level to the JSON output.
Example Usage
Say the $pl variable in the template is a Go map.
should yield
dehtml
dehtmlThis template function is used to convert the HTML escape sequence in a string and return the resulting string. This is used in Kapacitor alert source definition where the alert details are sent in a HTML escaped JSON string (for some reason).
Example Usage
Say $h is a variable that contains a HTML escaped string, with the following content:
should yield
join
joinThis template function is used to join string values with a passed string as the seperator.
Example Usage
Say $hosts has the following value. (in JSON)
should yield
replace
replaceThis template function is used to replace a specific string from the input string with another string.
Example Usage
should yield Bass
urlQueryEscape
urlQueryEscapeThis template function is used to escape a passed in string so that it can be used in the query part of the string.
Example Usage
should yield This+is+a+testing+query+value
emptyStringIfNil
emptyStringIfNilThis template function returns an empty string if a nil value is passed. This is used as a null alternative when the payloads can contain null values, as handling empty string is safer than null.
Example Usage
Consider this payload
will yield "" (nothing will be rendered).
hasPrefix
hasPrefixThis template function is used to check if the passed string contains the prefix passed.
Example Usage
Consider this payload
will yield true.
will yield false.
reReplace
reReplaceThis template function is used to replace the substrings of the parent string which matches the passed regular expression with the replacement string.
Example Usage
will yield Ball.
repeat
repeatThis template function is used to repeat a specific string n times.
Example Usage
will yield NaNaNaNaNa.
split
splitThis template function is used to split a string into multiple substrings by the passed separator string.
Example Usage
will yield an Array [Ba,a,a].
trimPrefix
trimPrefixThis template function is used to trim any prefix from the original string.
Example Usage
will yield nana.
Last updated