-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
cli: add new template render command #2538
Conversation
Add a new template rendering command that allows users to test out their templates. This is especially needed because small bugs in templates do not surface until alertmanager actually tries to render them. Signed-off-by: Giedrius Statkevičius <giedrius.statkevicius@vinted.com>
cli/template_render.go
Outdated
f = tmpl.ExecuteHTMLString | ||
} | ||
|
||
data := template.Data{ |
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.
We might want to load this from a file or something so that users could try out how their logic works with certain labels/annotations. WDYT?
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.
I think the ability to load custom data from YAML or JSON would be ideal, like in https://juliusv.com/promslack/.
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.
template.Data
has more stuff such as receiver
or status
so it's going to be a bit different. Also, because that struct only has json
tags, I think we should opt for only json
support for now.
I have added this functionality via 3ae0098. Does it look good? (:
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.
Looks good to me ;)
Add a new parameter `--templatefile` for `amtool` so that it would be possible to pass custom alert data. Use an example `template.Data` if none has been passed to permit simple use-cases. Signed-off-by: Giedrius Statkevičius <giedrius.statkevicius@vinted.com>
Let's support older Go versions as well. Signed-off-by: Giedrius Statkevičius <giedrius.statkevicius@vinted.com>
cli/template_render.go
Outdated
renderCmd.Flag("templateglob", "Glob of paths that will be expanded and used for rendering").Required().StringsVar(&c.templateFilesGlobs) | ||
renderCmd.Flag("templatetext", "The template that will be rendered").Required().StringVar(&c.templateText) | ||
renderCmd.Flag("templatetype", "The type of the template. Can be either text (default) or html").EnumVar(&c.templateType, "html", "text") | ||
renderCmd.Flag("templatefile", `Full path to a file which contains the data of the alert(-s) with which the --templatetext will be rendered. Must be in JSON. File must be formatted according to the following layout: https://pkg.go.dev/github.com/prometheus/alertmanager/template#Data. If none has been specified then a predefined, simple alert will be used for rendering`).FileVar(&c.templateFile) |
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.
I think this name makes it sounds like it's maybe an alternative to templateglob
(i.e. use this specific file).
How about templatedata
or datasource
?
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.
I didn't think about this similarity. templatedata
sounds good, will rename it. BTW @mrliptontea have you ever seen a similar tool online? I haven't and I'm surprised that no one has made this yet
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.
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.
promslack was the closest to what I needed but sadly it's not quite it.
I saw that other Pull Request but I prefer this - it's simpler yet fulfils my needs.
I'm now writing some "tests" where I have a bunch of different alerts in JSON, I render the templates, and compare the output to make the alerts more user friendly so thanks @GiedriusS you made it possible 🙂
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.
renderCmd.Flag("templatefile", `Full path to a file which contains the data of the alert(-s) with which the --templatetext will be rendered. Must be in JSON. File must be formatted according to the following layout: https://pkg.go.dev/github.com/prometheus/alertmanager/template#Data. If none has been specified then a predefined, simple alert will be used for rendering`).FileVar(&c.templateFile) | |
renderCmd.Flag("template.file", `Full path to a file which contains the data of the alert(-s) with which the --template.text will be rendered. Must be in JSON. File must be formatted according to the following layout: https://pkg.go.dev/github.com/prometheus/alertmanager/template#Data. If none has been specified then a predefined, simple alert will be used for rendering`).FileVar(&c.templateFile) |
Signed-off-by: Giedrius Statkevičius <giedrius.statkevicius@vinted.com>
@roidelapluie kind ping 🤗 could you PTAL when you'll have a free minute? I'd love to have this in so that I could automate some template checks during CI. |
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.
Yes, template.data seems better.
Also, alertmanager uses dot-separated parameters. Can you update this?
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.
Looks good, just flag name nits as @roidelapluie mentioned 🤗
Otherwise LGTM from my side
cli/template_render.go
Outdated
|
||
renderCmd.Flag("templateglob", "Glob of paths that will be expanded and used for rendering").Required().StringsVar(&c.templateFilesGlobs) | ||
renderCmd.Flag("templatetext", "The template that will be rendered").Required().StringVar(&c.templateText) | ||
renderCmd.Flag("templatetype", "The type of the template. Can be either text (default) or html").EnumVar(&c.templateType, "html", "text") |
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.
renderCmd.Flag("templatetype", "The type of the template. Can be either text (default) or html").EnumVar(&c.templateType, "html", "text") | |
renderCmd.Flag("template.type", "The type of the template. Can be either text (default) or html").EnumVar(&c.templateType, "html", "text") |
Similar to
Line 56 in 4d7251f
configFlag = routingCmd.Flag("config.file", "Config file to be tested.") |
cli/template_render.go
Outdated
renderCmd.Flag("templateglob", "Glob of paths that will be expanded and used for rendering").Required().StringsVar(&c.templateFilesGlobs) | ||
renderCmd.Flag("templatetext", "The template that will be rendered").Required().StringVar(&c.templateText) | ||
renderCmd.Flag("templatetype", "The type of the template. Can be either text (default) or html").EnumVar(&c.templateType, "html", "text") | ||
renderCmd.Flag("templatefile", `Full path to a file which contains the data of the alert(-s) with which the --templatetext will be rendered. Must be in JSON. File must be formatted according to the following layout: https://pkg.go.dev/github.com/prometheus/alertmanager/template#Data. If none has been specified then a predefined, simple alert will be used for rendering`).FileVar(&c.templateFile) |
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.
renderCmd.Flag("templatefile", `Full path to a file which contains the data of the alert(-s) with which the --templatetext will be rendered. Must be in JSON. File must be formatted according to the following layout: https://pkg.go.dev/github.com/prometheus/alertmanager/template#Data. If none has been specified then a predefined, simple alert will be used for rendering`).FileVar(&c.templateFile) | |
renderCmd.Flag("template.file", `Full path to a file which contains the data of the alert(-s) with which the --template.text will be rendered. Must be in JSON. File must be formatted according to the following layout: https://pkg.go.dev/github.com/prometheus/alertmanager/template#Data. If none has been specified then a predefined, simple alert will be used for rendering`).FileVar(&c.templateFile) |
cli/template_render.go
Outdated
) | ||
|
||
renderCmd.Flag("templateglob", "Glob of paths that will be expanded and used for rendering").Required().StringsVar(&c.templateFilesGlobs) | ||
renderCmd.Flag("templatetext", "The template that will be rendered").Required().StringVar(&c.templateText) |
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.
renderCmd.Flag("templatetext", "The template that will be rendered").Required().StringVar(&c.templateText) | |
renderCmd.Flag("template.text", "The template that will be rendered").Required().StringVar(&c.templateText) |
cli/template_render.go
Outdated
renderCmd = cc.Command("render", "Render a given definition in a template file to standard output") | ||
) | ||
|
||
renderCmd.Flag("templateglob", "Glob of paths that will be expanded and used for rendering").Required().StringsVar(&c.templateFilesGlobs) |
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.
renderCmd.Flag("templateglob", "Glob of paths that will be expanded and used for rendering").Required().StringsVar(&c.templateFilesGlobs) | |
renderCmd.Flag("template.glob", "Glob of paths that will be expanded and used for rendering").Required().StringsVar(&c.templateFilesGlobs) |
Use dot separator for different words in command-line parameters. Signed-off-by: Giedrius Statkevičius <giedrius.statkevicius@vinted.com>
Added a dot separator for the parameters, PTAL @roidelapluie @bwplotka 🤗 Sorry for the force-push, I forgot that the DCO is necessary for this repo 😢 Nothing else has been changed. |
@bwplotka @roidelapluie 🥺 friendly ping 🤗 |
README.md
Outdated
@@ -294,6 +294,18 @@ Expire all silences: | |||
$ amtool silence expire $(amtool silence query -q) | |||
``` | |||
|
|||
Try out how a template works. Let's say you have this in your configuration file: | |||
|
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.
Can we remove this BL for consistency?
cli/template_render.go
Outdated
renderCmd.Flag("template.glob", "Glob of paths that will be expanded and used for rendering").Required().StringsVar(&c.templateFilesGlobs) | ||
renderCmd.Flag("template.text", "The template that will be rendered").Required().StringVar(&c.templateText) | ||
renderCmd.Flag("template.type", "The type of the template. Can be either text (default) or html").EnumVar(&c.templateType, "html", "text") | ||
renderCmd.Flag("template.data", `Full path to a file which contains the data of the alert(-s) with which the --template.text will be rendered. Must be in JSON. File must be formatted according to the following layout: https://pkg.go.dev/github.com/prometheus/alertmanager/template#Data. If none has been specified then a predefined, simple alert will be used for rendering`).FileVar(&c.templateData) |
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.
renderCmd.Flag("template.data", `Full path to a file which contains the data of the alert(-s) with which the --template.text will be rendered. Must be in JSON. File must be formatted according to the following layout: https://pkg.go.dev/github.com/prometheus/alertmanager/template#Data. If none has been specified then a predefined, simple alert will be used for rendering`).FileVar(&c.templateData) | |
renderCmd.Flag("template.data", "Full path to a file which contains the data of the alert(-s) with which the --template.text will be rendered. Must be in JSON. File must be formatted according to the following layout: https://pkg.go.dev/github.com/prometheus/alertmanager/template#Data. If none has been specified then a predefined, simple alert will be used for rendering.").FileVar(&c.templateData) |
cli/template.go
Outdated
"gopkg.in/alecthomas/kingpin.v2" | ||
) | ||
|
||
// configureTemplateCmd represents the template command |
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.
// configureTemplateCmd represents the template command | |
// configureTemplateCmd represents the template command. |
Use full stops and remove a blank line. Signed-off-by: Giedrius Statkevičius <giedrius.statkevicius@vinted.com>
Thanks! |
Add a new template rendering command that allows users to test out their
templates. This is especially needed because small bugs in templates do
not surface until alertmanager actually tries to render them.
Signed-off-by: Giedrius Statkevičius giedrius.statkevicius@vinted.com