Alerting: Add image_urls to OpsGenie notification details. (#49379)
Adds an array of image_urls to the OpsGenie details field in a message, if image urls are available.
```json
{
"message": "Alert with Images!",
"details": {
"image_urls": ["http://www.example.com"]
}
}
```
This commit is contained in:
@@ -40,6 +40,7 @@ type OpsgenieNotifier struct {
|
|||||||
tmpl *template.Template
|
tmpl *template.Template
|
||||||
log log.Logger
|
log log.Logger
|
||||||
ns notifications.WebhookSender
|
ns notifications.WebhookSender
|
||||||
|
images ImageStore
|
||||||
}
|
}
|
||||||
|
|
||||||
type OpsgenieConfig struct {
|
type OpsgenieConfig struct {
|
||||||
@@ -59,7 +60,7 @@ func OpsgenieFactory(fc FactoryConfig) (NotificationChannel, error) {
|
|||||||
Cfg: *fc.Config,
|
Cfg: *fc.Config,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return NewOpsgenieNotifier(cfg, fc.NotificationService, fc.Template, fc.DecryptFunc), nil
|
return NewOpsgenieNotifier(cfg, fc.NotificationService, fc.ImageStore, fc.Template, fc.DecryptFunc), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewOpsgenieConfig(config *NotificationChannelConfig, decryptFunc GetDecryptedValueFn) (*OpsgenieConfig, error) {
|
func NewOpsgenieConfig(config *NotificationChannelConfig, decryptFunc GetDecryptedValueFn) (*OpsgenieConfig, error) {
|
||||||
@@ -84,7 +85,7 @@ func NewOpsgenieConfig(config *NotificationChannelConfig, decryptFunc GetDecrypt
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewOpsgenieNotifier is the constructor for the Opsgenie notifier
|
// NewOpsgenieNotifier is the constructor for the Opsgenie notifier
|
||||||
func NewOpsgenieNotifier(config *OpsgenieConfig, ns notifications.WebhookSender, t *template.Template, fn GetDecryptedValueFn) *OpsgenieNotifier {
|
func NewOpsgenieNotifier(config *OpsgenieConfig, ns notifications.WebhookSender, images ImageStore, t *template.Template, fn GetDecryptedValueFn) *OpsgenieNotifier {
|
||||||
return &OpsgenieNotifier{
|
return &OpsgenieNotifier{
|
||||||
Base: NewBase(&models.AlertNotification{
|
Base: NewBase(&models.AlertNotification{
|
||||||
Uid: config.UID,
|
Uid: config.UID,
|
||||||
@@ -101,6 +102,7 @@ func NewOpsgenieNotifier(config *OpsgenieConfig, ns notifications.WebhookSender,
|
|||||||
tmpl: t,
|
tmpl: t,
|
||||||
log: log.New("alerting.notifier." + config.Name),
|
log: log.New("alerting.notifier." + config.Name),
|
||||||
ns: ns,
|
ns: ns,
|
||||||
|
images: images,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,6 +210,31 @@ func (on *OpsgenieNotifier) buildOpsgenieMessage(ctx context.Context, alerts mod
|
|||||||
for k, v := range lbls {
|
for k, v := range lbls {
|
||||||
details.Set(k, v)
|
details.Set(k, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
images := []string{}
|
||||||
|
for i := range as {
|
||||||
|
imgToken := getTokenFromAnnotations(as[i].Annotations)
|
||||||
|
if len(imgToken) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
dbContext, cancel := context.WithTimeout(ctx, ImageStoreTimeout)
|
||||||
|
imgURL, err := on.images.GetURL(dbContext, imgToken)
|
||||||
|
cancel()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
if !errors.Is(err, ErrImagesUnavailable) {
|
||||||
|
// Ignore errors. Don't log "ImageUnavailable", which means the storage doesn't exist.
|
||||||
|
on.log.Warn("Error reading screenshot data from ImageStore: %v", err)
|
||||||
|
}
|
||||||
|
} else if len(imgURL) != 0 {
|
||||||
|
images = append(images, imgURL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(images) != 0 {
|
||||||
|
details.Set("image_urls", images)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tags := make([]string, 0, len(lbls))
|
tags := make([]string, 0, len(lbls))
|
||||||
|
|||||||
@@ -184,7 +184,7 @@ func TestOpsgenieNotifier(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 := NewOpsgenieNotifier(cfg, webhookSender, tmpl, decryptFn)
|
pn := NewOpsgenieNotifier(cfg, webhookSender, &UnavailableImageStore{}, tmpl, decryptFn)
|
||||||
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user