From 41686c789873d94762371433fa7435d27a5de65d Mon Sep 17 00:00:00 2001 From: Viktor Polishchuk Date: Tue, 19 Jun 2018 18:18:07 +0300 Subject: [PATCH] Added functionality to alter Webhook message --- config/notifiers.go | 3 +++ notify/impl.go | 21 +++++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/config/notifiers.go b/config/notifiers.go index f7f627c879..52206bf7db 100644 --- a/config/notifiers.go +++ b/config/notifiers.go @@ -344,6 +344,9 @@ type WebhookConfig struct { // URL to send POST request to. URL string `yaml:"url" json:"url"` + + // JSON message which will be sent instead of serialized data object + Message string `yaml:"message,omitempty" json:"message,omitempty"` } // UnmarshalYAML implements the yaml.Unmarshaler interface. diff --git a/notify/impl.go b/notify/impl.go index 8f3b555d2c..f069ac0358 100644 --- a/notify/impl.go +++ b/notify/impl.go @@ -147,7 +147,11 @@ type WebhookMessage struct { // Notify implements the Notifier interface. func (w *Webhook) Notify(ctx context.Context, alerts ...*types.Alert) (bool, error) { - data := w.tmpl.Data(receiverName(ctx, w.logger), groupLabels(ctx, w.logger), alerts...) + var err error + var ( + data = w.tmpl.Data(receiverName(ctx, w.logger), groupLabels(ctx, w.logger), alerts...) + tmplText = tmplText(w.tmpl, data, &err) + ) groupKey, ok := GroupKey(ctx) if !ok { @@ -161,9 +165,18 @@ func (w *Webhook) Notify(ctx context.Context, alerts ...*types.Alert) (bool, err } var buf bytes.Buffer - if err := json.NewEncoder(&buf).Encode(msg); err != nil { - return false, err - } + + if messageText := tmplText(w.conf.Message); messageText != "" { + _, err := buf.WriteString(messageText) + + if err != nil { + return false, err + } + } else { + if err := json.NewEncoder(&buf).Encode(msg); err != nil { + return false, err + } + } req, err := http.NewRequest("POST", w.conf.URL, &buf) if err != nil {