Skip to content
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

Merged
merged 6 commits into from
Aug 4, 2021

Conversation

GiedriusS
Copy link
Contributor

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

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>
f = tmpl.ExecuteHTMLString
}

data := template.Data{
Copy link
Contributor Author

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?

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/.

Copy link
Contributor Author

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? (:

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>
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)

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?

Copy link
Contributor Author

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 🙂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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>
@GiedriusS
Copy link
Contributor Author

@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.

Copy link
Member

@roidelapluie roidelapluie left a 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?

Copy link
Member

@bwplotka bwplotka left a 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


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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

configFlag = routingCmd.Flag("config.file", "Config file to be tested.")
(as @roidelapluie mentioned)

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)

)

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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>
@GiedriusS
Copy link
Contributor Author

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.

@GiedriusS
Copy link
Contributor Author

@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:

Copy link
Member

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?

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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>
@roidelapluie roidelapluie merged commit 3962da4 into prometheus:main Aug 4, 2021
@roidelapluie
Copy link
Member

Thanks!

@GiedriusS GiedriusS deleted the amtool_render branch August 4, 2021 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants