mirror of
https://github.com/google/alertmanager-irc-relay.git
synced 2024-11-23 11:29:27 +01:00
Handle alert messages with newlines
Do not send to IRC messages with newlines, split in multiple messages instead (see issue #15 ). Signed-off-by: Luca Bigliardi <shammash@google.com>
This commit is contained in:
parent
33fe9e4ef8
commit
f7034fa2c2
23
format.go
23
format.go
@ -50,7 +50,7 @@ func NewFormatter(config *Config) (*Formatter, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (f *Formatter) FormatMsg(ircChannel string, data interface{}) string {
|
||||
func (f *Formatter) FormatMsg(ircChannel string, data interface{}) []string {
|
||||
output := bytes.Buffer{}
|
||||
var msg string
|
||||
if err := f.MsgTemplate.Execute(&output, data); err != nil {
|
||||
@ -63,22 +63,29 @@ func (f *Formatter) FormatMsg(ircChannel string, data interface{}) string {
|
||||
} else {
|
||||
msg = output.String()
|
||||
}
|
||||
return msg
|
||||
|
||||
// Do not send to IRC messages with newlines, split in multiple messages instead.
|
||||
newLinesSplit := func(r rune) bool {
|
||||
return r == '\n' || r == '\r'
|
||||
}
|
||||
return strings.FieldsFunc(msg, newLinesSplit)
|
||||
}
|
||||
|
||||
func (f *Formatter) GetMsgsFromAlertMessage(ircChannel string,
|
||||
data *promtmpl.Data) []AlertMsg {
|
||||
msgs := []AlertMsg{}
|
||||
if f.MsgOnce {
|
||||
msg := f.FormatMsg(ircChannel, data)
|
||||
msgs = append(msgs,
|
||||
AlertMsg{Channel: ircChannel, Alert: msg})
|
||||
} else {
|
||||
for _, alert := range data.Alerts {
|
||||
msg := f.FormatMsg(ircChannel, alert)
|
||||
for _, msg := range f.FormatMsg(ircChannel, data) {
|
||||
msgs = append(msgs,
|
||||
AlertMsg{Channel: ircChannel, Alert: msg})
|
||||
}
|
||||
} else {
|
||||
for _, alert := range data.Alerts {
|
||||
for _, msg := range f.FormatMsg(ircChannel, alert) {
|
||||
msgs = append(msgs,
|
||||
AlertMsg{Channel: ircChannel, Alert: msg})
|
||||
}
|
||||
}
|
||||
}
|
||||
return msgs
|
||||
}
|
||||
|
@ -109,3 +109,27 @@ func TestUrlFunctions(t *testing.T) {
|
||||
|
||||
CreateFormatterAndCheckOutput(t, &testingConfig, expectedAlertMsgs)
|
||||
}
|
||||
|
||||
func TestMultilineTemplates(t *testing.T) {
|
||||
testingConfig := Config{
|
||||
MsgTemplate: "Alert {{ .GroupLabels.alertname }}\nis\r{{ .Status }}",
|
||||
MsgOnce: true,
|
||||
}
|
||||
|
||||
expectedAlertMsgs := []AlertMsg{
|
||||
AlertMsg{
|
||||
Channel: "#somechannel",
|
||||
Alert: "Alert airDown",
|
||||
},
|
||||
AlertMsg{
|
||||
Channel: "#somechannel",
|
||||
Alert: "is",
|
||||
},
|
||||
AlertMsg{
|
||||
Channel: "#somechannel",
|
||||
Alert: "resolved",
|
||||
},
|
||||
}
|
||||
|
||||
CreateFormatterAndCheckOutput(t, &testingConfig, expectedAlertMsgs)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user