Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alerting: Add image_urls to OpsGenie notification details. #49379

Merged
merged 2 commits into from May 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 27 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,29 @@ 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)
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 {
images = append(images, imgURL)
}
cancel()
}
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