Skip to content

Commit

Permalink
cli: permit passing alert data via a file
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
GiedriusS committed Apr 13, 2021
1 parent 392b8de commit 3ae0098
Showing 1 changed file with 75 additions and 57 deletions.
132 changes: 75 additions & 57 deletions cli/template_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,80 @@ package cli

import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"time"

"github.com/prometheus/alertmanager/template"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)

var defaultData = template.Data{
Receiver: "receiver",
Status: "alertstatus",
Alerts: template.Alerts{
template.Alert{
Status: "alertstatus",
Labels: template.KV{
"label1": "value1",
"label2": "value2",
"instance": "foo.bar:1234",
"commonlabelkey1": "commonlabelvalue1",
"commonlabelkey2": "commonlabelvalue2",
},
Annotations: template.KV{
"annotation1": "value1",
"annotation2": "value2",
"commonannotationkey1": "commonannotationvalue1",
"commonannotationkey2": "commonannotationvalue2",
},
StartsAt: time.Now().Add(-5 * time.Minute),
EndsAt: time.Now(),
GeneratorURL: "https://generatorurl.com",
Fingerprint: "fingerprint1",
},
template.Alert{
Status: "alertstatus",
Labels: template.KV{
"foo": "bar",
"baz": "qux",
"commonlabelkey1": "commonlabelvalue1",
"commonlabelkey2": "commonlabelvalue2",
},
Annotations: template.KV{
"aaa": "bbb",
"ccc": "ddd",
"commonannotationkey1": "commonannotationvalue1",
"commonannotationkey2": "commonannotationvalue2",
},
StartsAt: time.Now().Add(-10 * time.Minute),
EndsAt: time.Now(),
GeneratorURL: "https://generatorurl.com",
Fingerprint: "fingerprint2",
},
},
GroupLabels: template.KV{
"grouplabelkey1": "grouplabelvalue1",
"grouplabelkey2": "grouplabelvalue2",
},
CommonLabels: template.KV{
"commonlabelkey1": "commonlabelvalue1",
"commonlabelkey2": "commonlabelvalue2",
},
CommonAnnotations: template.KV{
"commonannotationkey1": "commonannotationvalue1",
"commonannotationkey2": "commonannotationvalue2",
},
ExternalURL: "https://example.com",
}

type templateRenderCmd struct {
templateFilesGlobs []string
templateType string
templateText string
templateFile *os.File
}

func configureTemplateRenderCmd(cc *kingpin.CmdClause) {
Expand All @@ -37,6 +100,7 @@ func configureTemplateRenderCmd(cc *kingpin.CmdClause) {
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)

renderCmd.Action(execWithTimeout(c.render))
}
Expand All @@ -52,63 +116,17 @@ func (c *templateRenderCmd) render(ctx context.Context, _ *kingpin.ParseContext)
f = tmpl.ExecuteHTMLString
}

data := template.Data{
Receiver: "receiver",
Status: "alertstatus",
Alerts: template.Alerts{
template.Alert{
Status: "alertstatus",
Labels: template.KV{
"label1": "value1",
"label2": "value2",
"instance": "foo.bar:1234",
"commonlabelkey1": "commonlabelvalue1",
"commonlabelkey2": "commonlabelvalue2",
},
Annotations: template.KV{
"annotation1": "value1",
"annotation2": "value2",
"commonannotationkey1": "commonannotationvalue1",
"commonannotationkey2": "commonannotationvalue2",
},
StartsAt: time.Now().Add(-5 * time.Minute),
EndsAt: time.Now(),
GeneratorURL: "https://generatorurl.com",
Fingerprint: "fingerprint1",
},
template.Alert{
Status: "alertstatus",
Labels: template.KV{
"foo": "bar",
"baz": "qux",
"commonlabelkey1": "commonlabelvalue1",
"commonlabelkey2": "commonlabelvalue2",
},
Annotations: template.KV{
"aaa": "bbb",
"ccc": "ddd",
"commonannotationkey1": "commonannotationvalue1",
"commonannotationkey2": "commonannotationvalue2",
},
StartsAt: time.Now().Add(-10 * time.Minute),
EndsAt: time.Now(),
GeneratorURL: "https://generatorurl.com",
Fingerprint: "fingerprint2",
},
},
GroupLabels: template.KV{
"grouplabelkey1": "grouplabelvalue1",
"grouplabelkey2": "grouplabelvalue2",
},
CommonLabels: template.KV{
"commonlabelkey1": "commonlabelvalue1",
"commonlabelkey2": "commonlabelvalue2",
},
CommonAnnotations: template.KV{
"commonannotationkey1": "commonannotationvalue1",
"commonannotationkey2": "commonannotationvalue2",
},
ExternalURL: "https://example.com",
var data template.Data
if c.templateFile == nil {
data = defaultData
} else {
content, err := io.ReadAll(c.templateFile)
if err != nil {
return err
}
if err := json.Unmarshal(content, &data); err != nil {
return err
}
}

rendered, err := f(c.templateText, data)
Expand Down

0 comments on commit 3ae0098

Please sign in to comment.