-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[alerts] provide mustache functions for ease-of-use in transforming mustache variables #84217
Comments
Pinging @elastic/kibana-alerting-services (Team:Alerting Services) |
It'd also be helpful if we supported One example use case would be sending an alert's |
@n0othing - there is already some implicit support for array joins and json-ification. When you use an array object as a bare mustache variable, it will be rendered as if you had done |
Realizing that I haven't provided a reference to the existing mustache "functions" supported by elasticsearch - they seem to be documented here: https://www.elastic.co/guide/en/elasticsearch/reference/7.13/search-template.html Quick take on them:
|
The array field will join the entires with commas, however the problem with this is, it does not become valid JSON. For example, if given the following json in a Kibana Alert action, {
"priority": "P3",
"message": "{{alertName}}",
"description": """
Some Description
""",
"tags": ["{{tags}}"]
} Where "tags" is an array containing three tags, then what is output is the following: {
"priority": "P3",
"message": "AlertNameHere",
"description": """
Some Description
""",
"tags": ["TAG1, TAG2, TAG3"]
} Which is not a valid JSON Array. There needs to be quotes around each of them. What we need in the tags field is |
Yes, I think it's not possible, even with our I think you'd want to use the elasticsearch form of Thanks for the use case! |
It would be a big timesaver for me to have the ability to dynamically generate links to specific monitors in Uptime, rather than having to put static links in each alert configuration. In Kibana 7.14.2, the
While having a dedicated variable (like Thinking about similar stuff I've seen in the wild, Helm template functions are kind of nice for transformations. For example:
|
I think it will be very helpful to have: Sha256This function is useful when you want to create grouping keys to be used in external services like PagerDuty (dedup key) or ServiceNow (correlation ID) where there is a limit on the number of characters you can post.
Nice to have:SetIf you want to take the unique values of an array of objects. For example all unique hosts.
Sort
Filter
|
One thing we should do before adding these, is do a survey of other mustache usage within the stack - watcher and something else in Kibana (canvas?) are using "mustache", though I think handlebars is one of those variants in use. It would be good to align with other mustache "lambda" things that they have added. |
We have also seens situations where it would be useful to be able to do simple conversions of values using a function in the template. Example: we have a context variable that represents latency in microseconds, but we'd like to display it in milliseconds when rendering the template. |
For the use case above, it would be nice to provide math functions. We have an internal package available called
Where we would expect the context variables in the middle to be expanded out, and then a special Mustache variable This may not work well in practice. If a connector parm needs to be a number, we may not accept a mustache template as it's value currently, since that would be a string. I'm thinking both runtime validation within the connector type, as well as the existing UX. |
Just had a thought on the problem here that seems to come up the most. Folks wanting to "pass through" a JSON array value as JSON. This doesn't work since we preserve array's Since we only support typical array access, there won't be any other properties on arrays that you could reference. For instance, while this is valid Javascript const a = [1,2,3]
a.foo = 'bar'
console.log(a.foo) // prints "bar" we end up cloning the context and would end up removing kibana/x-pack/plugins/actions/server/lib/mustache_renderer.ts Lines 64 to 78 in c4b43f9
So if |
Just had a user report an issue with url encoding of spaces within markdown links. To repro, create an rule that you can trigger to become active, and add an email action. The name of the rule should have a space in it, for instance "my rule". In the action, use the following template:
You would hope this would get rendered as one of:
The bottom two use an encoding for the space char; the hope would be that markdown adds this. The first just uses the space in the url as-is, in which case the hope is the browser will handle it - Chrome does today by changing the space to However, what we get instead is this:
One way to fix this would be to provide some kind of mechanism - a lambda perhaps - to URL encode a value. So you might do something like this:
|
|
partially resolves some issues in #84217 Adds Mustache lambdas for alerting actions to format dates with `{{#FormatDate}}`, evaluate math expressions with `{{#EvalMath}}`, and provide easier JSON formatting with `{{#ParseHjson}}` and a new `asJSON` property added to arrays.
Thought I'd note for folks watching: we've merged some mustache extensions slated (but not guaranteed!) to be shipped in 8.8.0; should help with date formatting, constructing JSON documents, and some math expressions. |
Thought I'd note for folks watching: we've merged a mustache extension slated (but not guaranteed!) to be shipped in 8.9.0 for formatting numbers with |
Hello all, I'm currently using the {{#ParseHjson}}{{/ParseHjson}} Mustache lambda and I'm experiencing an issue with value containing the "&" character. Here is an example:
With the following value in the "observer.product" fields I'm getting the following error : I've tried to add ".asJSON" at the end but it's not working. Is there another lambdas like "asString" that I can use to escape special characters ? Thanks, |
Any chance to implement some sort of URL-encoding function? We get alerts from Detection Rules that often include usernames with spaces in them; |
I suspect there may be an escaping issue here. Can you try using the following - triple vs double quotes. This should bypass the escaping that may be occurring.
|
I'm going to close this long-lived issue as we've added some support in this area in the last few releases:
Beyond that support, it seems like there are three other areas of interest, which we either had an issue open for, or I just opened one:
|
One feature of mustache that we are not currently taking advantage of is "functions", also sometimes referred to as lambdas.
It's not clear how much value we can add with these, but seems like worthy of investigation.
One of the use cases I have in mind is date formatting. We now provide a
{{date}}
variable in the mustache variables applied to action parameters, in ISO format. How might you provide a function which can format that date with a traditional date formatter? The challenge is, the arguments have to be in the "body" of the function invocation.Here's an example of how this might work:
The
date-format
function would be provided by us, and it's "arguments" would be<iso-date> MM/DD/YY
. So the function would parse the arguments expecting a date as the first part, and the format as the second.So many issues tho.
Presumably dates coming out of ES that are not under our control could be in any date format, so it's not clear how we'd parse; maybe we need a better separator like
<iso-date> --- MM/DD/YY
.Ideally we could support various sorts of "string functions", and so dealing with some kind of separator story that allows strings to be transformed and additional arguments seems really hard/yucky. I think maybe we'd need to go with something
#<iso-date>#MM-DD-YY#
where the arg delimiter is passed as the first char, kinda thing.We'd likely need a specialized "test" environment for this - the current action tester (beside the action editor) doesn't do any mustache templating, and shouldn't, because it doesn't have access to alert data.
The text was updated successfully, but these errors were encountered: