-
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
[ResponseOps] adds mustache lambdas and array.asJSON #150572
Conversation
ff7a84f
to
9d5f46e
Compare
8b8ead2
to
64efc10
Compare
Played a bit more with this, this weekend. I'm not happy about the date formatting:
So it seems like we should come up with a Edit: pmuellr 2023-04-11 - I did just that; FormatDate now takes all the params, separated by semi-colons. |
b62e794
to
2857bb5
Compare
2857bb5
to
a4f6bcc
Compare
Playing with this a bit. Built an es query rule against the event log, to alert on active alerts (meta-alert, as it were). Then built another rule that I could easily make active. Here's the mustache template I used for the email:
and here's the resulting formatted email: Some notes. Showing the "classic" way dates are displayed, with a formatted version below that. The table at the bottom shows the alert duration. This value is in nanosecond units, so we divide by a billion to get the value in seconds units, rounding to one decimal place. I had to triple escape both
No braces at all! Which will work fine, for values which are numbers. But in this case, the value is a string. |
We can use this Wiki as a source of timezone identifiers for docs: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones. |
a4f6bcc
to
45e3bf0
Compare
|
||
export type Escape = 'markdown' | 'slack' | 'json' | 'none'; | ||
type Variables = Record<string, unknown>; | ||
export type Variables = Record<string, unknown>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I know circular dependencies ( import mustache_lambdas
in mustache_renderer
and mustache_renderer
in mustache_lambdas
. ) can potentially lead to unintended consequences.
So maybe it would be safer to just copy this type to mustache_lambdas
function getLambdas() { | ||
return { | ||
EvalMath: () => | ||
function (this: Variables, text: string, render: RenderFn) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about use vars instead of this? Or do you like this more on some reason?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not an option. The way these lambdas are set up by mustache, is that they call the function with this
set to the "variables" available to the lambda. We HAVE to define it this way.
Worth a comment though, I think, because it's obviously a little confusing, especially using this
as a "function parameter" like this (it's a special typescript thing). see: https://www.typescriptlang.org/docs/handbook/2/functions.html#declaring-this-in-a-function
45e3bf0
to
0b6013c
Compare
d1fae57
to
aaf0080
Compare
Pinging @elastic/response-ops (Team:ResponseOps) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
super cool @pmuellr!
I used your example action message in a server log logging at info level and left it running for a bit and saw some errors show up but as part of the server log message:
So you can see some of the messages render correctly but some of them print an error message |
I'm not sure there was an easy way to force errors within Mustache processing before we had the lambdas. It was easy to get Mustache to choke, by giving it something like With the lambdas, whole new opportunities for runtime errors! I'm kinda wondering if we can do better. Maybe we should render just that lambda as an error message, so you'd see everything else rendered, and then the error message "in place" where the valid value would go. Something to think about ... I'm curious what you were filtering on. It appears from the message like the duration was "empty"
The duration field should have been between |
Yes, that's probably it. I did a match all docs in the event log so I would be getting docs without a duration. I wouldn't expect it to render in the action message with the error though...which is what it looks like it's doing? |
@elasticmachine merge upstream |
seems like the build got stuck, merge master again ... |
@elasticmachine merge upstream |
💚 Build Succeeded
Metrics [docs]Unknown metric groupsESLint disabled line counts
Total ESLint disabled count
History
To update your PR or re-run it, just comment with: |
…array (elastic#155417) resolves: elastic#155408 ## Summary adds doc for function added in [adds mustache lambdas and array.asJSON](elastic#150572) (cherry picked from commit 120fa44)
…N for array (#155417) (#157668) # Backport This will backport the following commits from `main` to `8.8`: - [[ResponseOps][docs] add docs for new mustache lambdas and asJSON for array (#155417)](#155417) <!--- Backport version: 8.9.7 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Patrick Mueller","email":"patrick.mueller@elastic.co"},"sourceCommit":{"committedDate":"2023-05-14T18:39:25Z","message":"[ResponseOps][docs] add docs for new mustache lambdas and asJSON for array (#155417)\n\nresolves: https://github.com/elastic/kibana/issues/155408\r\n\r\n## Summary\r\n\r\nadds doc for function added in [adds mustache lambdas and\r\narray.asJSON](https://github.com/elastic/kibana/pull/150572)","sha":"120fa44afd6b87764d114475caa97d862c55f343","branchLabelMapping":{"^v8.9.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Feature:Actions","Team:ResponseOps","docs","backport:prev-minor","v8.9.0"],"number":155417,"url":"https://github.com/elastic/kibana/pull/155417","mergeCommit":{"message":"[ResponseOps][docs] add docs for new mustache lambdas and asJSON for array (#155417)\n\nresolves: https://github.com/elastic/kibana/issues/155408\r\n\r\n## Summary\r\n\r\nadds doc for function added in [adds mustache lambdas and\r\narray.asJSON](https://github.com/elastic/kibana/pull/150572)","sha":"120fa44afd6b87764d114475caa97d862c55f343"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.9.0","labelRegex":"^v8.9.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/155417","number":155417,"mergeCommit":{"message":"[ResponseOps][docs] add docs for new mustache lambdas and asJSON for array (#155417)\n\nresolves: https://github.com/elastic/kibana/issues/155408\r\n\r\n## Summary\r\n\r\nadds doc for function added in [adds mustache lambdas and\r\narray.asJSON](https://github.com/elastic/kibana/pull/150572)","sha":"120fa44afd6b87764d114475caa97d862c55f343"}}]}] BACKPORT--> Co-authored-by: Patrick Mueller <patrick.mueller@elastic.co>
Cloud CI deployment: https://kibana-pr-150572.kb.us-west2.gcp.elastic-cloud.com:9243/
Summary
Extends mustache support in actions. Based on some work in an on-week project; see #146487 for more details.
Also see #84217 as a issue collecting requirements for mustache.
Release note
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 newasJSON
property added to arrays.lambda
FormatDate
to format date stringsThis lambda uses the moment npm package to parse and generate date strings.
The lambda expects a parseable date string as it's body, and optional time zone and date format, separated by semi-colons:
Given the variable
timeStamp
with a value of2022-11-29T15:52:44Z
, this would render aslambda
ParseHjson
to parse HjsonHjson is a version of JSON that is not so strict as JSON and has many amenities. This lambda treats the enclosed text as Hjson and parses it, then generates the serialized JSON of that. Basically, it makes it easier to write JSON in a template. One of the little goodies is that it accepts trailing comma's in arrays, which is one of the existing problems we've dealt with. It's not possible to use mustache section looping to generate array elements, because you have to generate a comma for all but the last, but there's no way to do that. Now you can generate one, and the proper JSON will be rendered.
Given the variables
and template (this looks quite a bit like yaml, but is actually Hjson)
The following object will be rendered:
lambda
EvalMath
to evaluate TinyMath expressionsThis lambda expects a TinyMath expression, in a string. Example:
Given the variable
context
with value{ a: { b: 1 }}
, this would render as:2
.arrays have an
asJSON
propertyJust as we extended "objects" in JSON to render as their JSON representation, we've extended arrays with an
asJSON
property which does the same, renders their JSON represention.Given the variable
context
with value{ a: [ 1, 2, 3 ] }
, the template{context.a}
would render as1,2,3
, and the template{context.a.asJSON}
would render as[1,2,3]
.For historical reference, we specifically excluded arrays when we added the auto-JSON-ification of objects, as mustache already rendered arrays as above, and we were afraid customers might have been making use of it. It can be a nice format, if it works for you. But often doesn't!
Checklist
Delete any items that are not applicable to this PR.
asJSON
for arrays #155408)