Skip to content

Commit

Permalink
Alerting: Add image_urls to OpsGenie notification details. (#49379)
Browse files Browse the repository at this point in the history
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"]
  }
}
```
  • Loading branch information
joeblubaugh committed May 23, 2022
1 parent 5fc5899 commit 307e336
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
31 changes: 29 additions & 2 deletions pkg/services/ngalert/notifier/channels/opsgenie.go
Expand Up @@ -40,6 +40,7 @@ type OpsgenieNotifier struct {
tmpl *template.Template
log log.Logger
ns notifications.WebhookSender
images ImageStore
}

type OpsgenieConfig struct {
Expand All @@ -59,7 +60,7 @@ func OpsgenieFactory(fc FactoryConfig) (NotificationChannel, error) {
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) {
Expand All @@ -84,7 +85,7 @@ func NewOpsgenieConfig(config *NotificationChannelConfig, decryptFunc GetDecrypt
}

// 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{
Base: NewBase(&models.AlertNotification{
Uid: config.UID,
Expand All @@ -101,6 +102,7 @@ func NewOpsgenieNotifier(config *OpsgenieConfig, ns notifications.WebhookSender,
tmpl: t,
log: log.New("alerting.notifier." + config.Name),
ns: ns,
images: images,
}
}

Expand Down Expand Up @@ -208,6 +210,31 @@ func (on *OpsgenieNotifier) buildOpsgenieMessage(ctx context.Context, alerts mod
for k, v := range lbls {
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))
Expand Down
2 changes: 1 addition & 1 deletion pkg/services/ngalert/notifier/channels/opsgenie_test.go
Expand Up @@ -184,7 +184,7 @@ func TestOpsgenieNotifier(t *testing.T) {

ctx := notify.WithGroupKey(context.Background(), "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...)
if c.expMsgError != nil {
require.False(t, ok)
Expand Down

0 comments on commit 307e336

Please sign in to comment.