mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Add support for images in VictorOps alerts (#50759)
This commit is contained in:
parent
2698e37291
commit
99516360c9
@ -14,6 +14,7 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
|
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
|
||||||
"github.com/grafana/grafana/pkg/services/notifications"
|
"github.com/grafana/grafana/pkg/services/notifications"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
)
|
)
|
||||||
@ -40,7 +41,7 @@ func VictorOpsFactory(fc FactoryConfig) (NotificationChannel, error) {
|
|||||||
Cfg: *fc.Config,
|
Cfg: *fc.Config,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return NewVictoropsNotifier(cfg, fc.NotificationService, fc.Template), nil
|
return NewVictoropsNotifier(cfg, fc.ImageStore, fc.NotificationService, fc.Template), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewVictorOpsConfig(config *NotificationChannelConfig) (*VictorOpsConfig, error) {
|
func NewVictorOpsConfig(config *NotificationChannelConfig) (*VictorOpsConfig, error) {
|
||||||
@ -57,7 +58,7 @@ func NewVictorOpsConfig(config *NotificationChannelConfig) (*VictorOpsConfig, er
|
|||||||
|
|
||||||
// NewVictoropsNotifier creates an instance of VictoropsNotifier that
|
// NewVictoropsNotifier creates an instance of VictoropsNotifier that
|
||||||
// handles posting notifications to Victorops REST API
|
// handles posting notifications to Victorops REST API
|
||||||
func NewVictoropsNotifier(config *VictorOpsConfig, ns notifications.WebhookSender, t *template.Template) *VictoropsNotifier {
|
func NewVictoropsNotifier(config *VictorOpsConfig, images ImageStore, ns notifications.WebhookSender, t *template.Template) *VictoropsNotifier {
|
||||||
return &VictoropsNotifier{
|
return &VictoropsNotifier{
|
||||||
Base: NewBase(&models.AlertNotification{
|
Base: NewBase(&models.AlertNotification{
|
||||||
Uid: config.UID,
|
Uid: config.UID,
|
||||||
@ -69,6 +70,7 @@ func NewVictoropsNotifier(config *VictorOpsConfig, ns notifications.WebhookSende
|
|||||||
URL: config.URL,
|
URL: config.URL,
|
||||||
MessageType: config.MessageType,
|
MessageType: config.MessageType,
|
||||||
log: log.New("alerting.notifier.victorops"),
|
log: log.New("alerting.notifier.victorops"),
|
||||||
|
images: images,
|
||||||
ns: ns,
|
ns: ns,
|
||||||
tmpl: t,
|
tmpl: t,
|
||||||
}
|
}
|
||||||
@ -82,6 +84,7 @@ type VictoropsNotifier struct {
|
|||||||
URL string
|
URL string
|
||||||
MessageType string
|
MessageType string
|
||||||
log log.Logger
|
log log.Logger
|
||||||
|
images ImageStore
|
||||||
ns notifications.WebhookSender
|
ns notifications.WebhookSender
|
||||||
tmpl *template.Template
|
tmpl *template.Template
|
||||||
}
|
}
|
||||||
@ -115,6 +118,15 @@ func (vn *VictoropsNotifier) Notify(ctx context.Context, as ...*types.Alert) (bo
|
|||||||
bodyJSON.Set("state_message", tmpl(`{{ template "default.message" . }}`))
|
bodyJSON.Set("state_message", tmpl(`{{ template "default.message" . }}`))
|
||||||
bodyJSON.Set("monitoring_tool", "Grafana v"+setting.BuildVersion)
|
bodyJSON.Set("monitoring_tool", "Grafana v"+setting.BuildVersion)
|
||||||
|
|
||||||
|
_ = withStoredImages(ctx, vn.log, vn.images,
|
||||||
|
func(index int, image *ngmodels.Image) error {
|
||||||
|
if image != nil && image.URL != "" {
|
||||||
|
bodyJSON.Set("image_url", image.URL)
|
||||||
|
return ErrImagesDone
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}, as...)
|
||||||
|
|
||||||
ruleURL := joinUrlPath(vn.tmpl.ExternalURL.String(), "/alerting/list", vn.log)
|
ruleURL := joinUrlPath(vn.tmpl.ExternalURL.String(), "/alerting/list", vn.log)
|
||||||
bodyJSON.Set("alert_url", ruleURL)
|
bodyJSON.Set("alert_url", ruleURL)
|
||||||
|
|
||||||
|
@ -18,6 +18,8 @@ import (
|
|||||||
func TestVictoropsNotifier(t *testing.T) {
|
func TestVictoropsNotifier(t *testing.T) {
|
||||||
tmpl := templateForTests(t)
|
tmpl := templateForTests(t)
|
||||||
|
|
||||||
|
images := newFakeImageStore(2)
|
||||||
|
|
||||||
externalURL, err := url.Parse("http://localhost")
|
externalURL, err := url.Parse("http://localhost")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
tmpl.ExternalURL = externalURL
|
tmpl.ExternalURL = externalURL
|
||||||
@ -31,13 +33,13 @@ func TestVictoropsNotifier(t *testing.T) {
|
|||||||
expMsgError error
|
expMsgError error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "One alert",
|
name: "A single alert with image",
|
||||||
settings: `{"url": "http://localhost"}`,
|
settings: `{"url": "http://localhost"}`,
|
||||||
alerts: []*types.Alert{
|
alerts: []*types.Alert{
|
||||||
{
|
{
|
||||||
Alert: model.Alert{
|
Alert: model.Alert{
|
||||||
Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"},
|
Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"},
|
||||||
Annotations: model.LabelSet{"ann1": "annv1", "__dashboardUid__": "abcd", "__panelId__": "efgh"},
|
Annotations: model.LabelSet{"ann1": "annv1", "__dashboardUid__": "abcd", "__panelId__": "efgh", "__alertScreenshotToken__": "test-image-1"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -45,24 +47,25 @@ func TestVictoropsNotifier(t *testing.T) {
|
|||||||
"alert_url": "http://localhost/alerting/list",
|
"alert_url": "http://localhost/alerting/list",
|
||||||
"entity_display_name": "[FIRING:1] (val1)",
|
"entity_display_name": "[FIRING:1] (val1)",
|
||||||
"entity_id": "6e3538104c14b583da237e9693b76debbc17f0f8058ef20492e5853096cf8733",
|
"entity_id": "6e3538104c14b583da237e9693b76debbc17f0f8058ef20492e5853096cf8733",
|
||||||
|
"image_url": "https://www.example.com/test-image-1.jpg",
|
||||||
"message_type": "CRITICAL",
|
"message_type": "CRITICAL",
|
||||||
"monitoring_tool": "Grafana v" + setting.BuildVersion,
|
"monitoring_tool": "Grafana v" + setting.BuildVersion,
|
||||||
"state_message": "**Firing**\n\nValue: [no value]\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval1\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n",
|
"state_message": "**Firing**\n\nValue: [no value]\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval1\nDashboard: http://localhost/d/abcd\nPanel: http://localhost/d/abcd?viewPanel=efgh\n",
|
||||||
},
|
},
|
||||||
expMsgError: nil,
|
expMsgError: nil,
|
||||||
}, {
|
}, {
|
||||||
name: "Multiple alerts",
|
name: "Multiple alerts with images",
|
||||||
settings: `{"url": "http://localhost"}`,
|
settings: `{"url": "http://localhost"}`,
|
||||||
alerts: []*types.Alert{
|
alerts: []*types.Alert{
|
||||||
{
|
{
|
||||||
Alert: model.Alert{
|
Alert: model.Alert{
|
||||||
Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"},
|
Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"},
|
||||||
Annotations: model.LabelSet{"ann1": "annv1"},
|
Annotations: model.LabelSet{"ann1": "annv1", "__alertScreenshotToken__": "test-image-1"},
|
||||||
},
|
},
|
||||||
}, {
|
}, {
|
||||||
Alert: model.Alert{
|
Alert: model.Alert{
|
||||||
Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val2"},
|
Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val2"},
|
||||||
Annotations: model.LabelSet{"ann1": "annv2"},
|
Annotations: model.LabelSet{"ann1": "annv2", "__alertScreenshotToken__": "test-image-2"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -70,6 +73,7 @@ func TestVictoropsNotifier(t *testing.T) {
|
|||||||
"alert_url": "http://localhost/alerting/list",
|
"alert_url": "http://localhost/alerting/list",
|
||||||
"entity_display_name": "[FIRING:2] ",
|
"entity_display_name": "[FIRING:2] ",
|
||||||
"entity_id": "6e3538104c14b583da237e9693b76debbc17f0f8058ef20492e5853096cf8733",
|
"entity_id": "6e3538104c14b583da237e9693b76debbc17f0f8058ef20492e5853096cf8733",
|
||||||
|
"image_url": "https://www.example.com/test-image-1.jpg",
|
||||||
"message_type": "CRITICAL",
|
"message_type": "CRITICAL",
|
||||||
"monitoring_tool": "Grafana v" + setting.BuildVersion,
|
"monitoring_tool": "Grafana v" + setting.BuildVersion,
|
||||||
"state_message": "**Firing**\n\nValue: [no value]\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval1\n\nValue: [no value]\nLabels:\n - alertname = alert1\n - lbl1 = val2\nAnnotations:\n - ann1 = annv2\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval2\n",
|
"state_message": "**Firing**\n\nValue: [no value]\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval1\n\nValue: [no value]\nLabels:\n - alertname = alert1\n - lbl1 = val2\nAnnotations:\n - ann1 = annv2\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval2\n",
|
||||||
@ -179,7 +183,7 @@ func TestVictoropsNotifier(t *testing.T) {
|
|||||||
|
|
||||||
ctx := notify.WithGroupKey(context.Background(), "alertname")
|
ctx := notify.WithGroupKey(context.Background(), "alertname")
|
||||||
ctx = notify.WithGroupLabels(ctx, model.LabelSet{"alertname": ""})
|
ctx = notify.WithGroupLabels(ctx, model.LabelSet{"alertname": ""})
|
||||||
pn := NewVictoropsNotifier(cfg, webhookSender, tmpl)
|
pn := NewVictoropsNotifier(cfg, images, webhookSender, tmpl)
|
||||||
ok, err := pn.Notify(ctx, c.alerts...)
|
ok, err := pn.Notify(ctx, c.alerts...)
|
||||||
if c.expMsgError != nil {
|
if c.expMsgError != nil {
|
||||||
require.False(t, ok)
|
require.False(t, ok)
|
||||||
|
Loading…
Reference in New Issue
Block a user