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

Add option for resolved template #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ use_privmsg: yes
#
# The formatting is based on golang's text/template .
msg_template: "Alert {{ .Labels.alertname }} on {{ .Labels.instance }} is {{ .Status }}"
# By default, firing and resolved messages use the template defined as msg_template.
# Define msg_template_resolved to define a different template for resolved messages.
# msg_template_resolved: "Resolved: {{ .Labels.alertname }} on {{ .Labels.instance }}"
# Note: When sending only one message per alert group the default
# msg_template is set to
# "Alert {{ .GroupLabels.alertname }} for {{ .GroupLabels.job }} is {{ .Status }}"
Expand Down
4 changes: 4 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Config struct {
IRCVerifySSL bool `yaml:"irc_verify_ssl"`
IRCChannels []IRCChannel `yaml:"irc_channels"`
MsgTemplate string `yaml:"msg_template"`
MsgTemplateResolved string `yaml:"msg_template_resolved"`
MsgOnce bool `yaml:"msg_once_per_alert_group"`
UsePrivmsg bool `yaml:"use_privmsg"`
AlertBufferSize int `yaml:"alert_buffer_size"`
Expand Down Expand Up @@ -99,6 +100,9 @@ func LoadConfig(configFile string) (*Config, error) {
config.MsgTemplate = defaultMsgTemplate
}
}
if config.MsgTemplateResolved == "" {
config.MsgTemplateResolved = config.MsgTemplate
}

loadedConfig, _ := yaml.Marshal(config)
logging.Debug("Loaded config:\n%s", loadedConfig)
Expand Down
20 changes: 16 additions & 4 deletions format.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

type Formatter struct {
MsgTemplate *template.Template
MsgTemplateResolved *template.Template
MsgOnce bool
}

Expand All @@ -44,16 +45,27 @@ func NewFormatter(config *Config) (*Formatter, error) {
if err != nil {
return nil, err
}
tmplresolved, err := template.New("msg").Funcs(funcMap).Parse(config.MsgTemplateResolved)
if err != nil {
return nil, err
}
return &Formatter{
MsgTemplate: tmpl,
MsgTemplateResolved: tmplresolved,
MsgOnce: config.MsgOnce,
}, nil
}

func (f *Formatter) FormatMsg(ircChannel string, data interface{}) []string {
func (f *Formatter) FormatMsg(ircChannel string, data interface{}, status string) []string {
output := bytes.Buffer{}
var msg string
if err := f.MsgTemplate.Execute(&output, data); err != nil {
var msgtemplate *template.Template
if status == "resolved" {
msgtemplate = f.MsgTemplateResolved
} else {
msgtemplate = f.MsgTemplate
}
if err := msgtemplate.Execute(&output, data); err != nil {
msg_bytes, _ := json.Marshal(data)
msg = string(msg_bytes)
logging.Error("Could not apply msg template on alert (%s): %s",
Expand All @@ -75,13 +87,13 @@ func (f *Formatter) GetMsgsFromAlertMessage(ircChannel string,
data *promtmpl.Data) []AlertMsg {
msgs := []AlertMsg{}
if f.MsgOnce {
for _, msg := range f.FormatMsg(ircChannel, data) {
for _, msg := range f.FormatMsg(ircChannel, data, data.Status) {
msgs = append(msgs,
AlertMsg{Channel: ircChannel, Alert: msg})
}
} else {
for _, alert := range data.Alerts {
for _, msg := range f.FormatMsg(ircChannel, alert) {
for _, msg := range f.FormatMsg(ircChannel, alert, data.Status) {
msgs = append(msgs,
AlertMsg{Channel: ircChannel, Alert: msg})
}
Expand Down
17 changes: 17 additions & 0 deletions format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,20 @@ func TestMultilineTemplates(t *testing.T) {

CreateFormatterAndCheckOutput(t, &testingConfig, expectedAlertMsgs)
}

func TestResolvedTemplate(t *testing.T) {
testingConfig := Config{
MsgTemplateResolved: "Resolved: {{ .Labels.alertname }}",
}
expectedAlertMsgs := []AlertMsg{
AlertMsg{
Channel: "#somechannel",
Alert: "Resolved: airDown",
},
AlertMsg{
Channel: "#somechannel",
Alert: "Resolved: airDown",
},
}
CreateFormatterAndCheckOutput(t, &testingConfig, expectedAlertMsgs)
}