mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Add Screenshot URLs to Pagerduty Notifier (#49377)
PagerDuty takes an "images" array of link objects in it's request body.
This commit is contained in:
parent
cc0448927a
commit
11a908cc91
@ -39,6 +39,7 @@ type PagerdutyNotifier struct {
|
|||||||
tmpl *template.Template
|
tmpl *template.Template
|
||||||
log log.Logger
|
log log.Logger
|
||||||
ns notifications.WebhookSender
|
ns notifications.WebhookSender
|
||||||
|
images ImageStore
|
||||||
}
|
}
|
||||||
|
|
||||||
type PagerdutyConfig struct {
|
type PagerdutyConfig struct {
|
||||||
@ -59,7 +60,7 @@ func PagerdutyFactory(fc FactoryConfig) (NotificationChannel, error) {
|
|||||||
Cfg: *fc.Config,
|
Cfg: *fc.Config,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return NewPagerdutyNotifier(cfg, fc.NotificationService, fc.Template), nil
|
return NewPagerdutyNotifier(cfg, fc.NotificationService, fc.ImageStore, fc.Template), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPagerdutyConfig(config *NotificationChannelConfig, decryptFunc GetDecryptedValueFn) (*PagerdutyConfig, error) {
|
func NewPagerdutyConfig(config *NotificationChannelConfig, decryptFunc GetDecryptedValueFn) (*PagerdutyConfig, error) {
|
||||||
@ -79,7 +80,7 @@ func NewPagerdutyConfig(config *NotificationChannelConfig, decryptFunc GetDecryp
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewPagerdutyNotifier is the constructor for the PagerDuty notifier
|
// NewPagerdutyNotifier is the constructor for the PagerDuty notifier
|
||||||
func NewPagerdutyNotifier(config *PagerdutyConfig, ns notifications.WebhookSender, t *template.Template) *PagerdutyNotifier {
|
func NewPagerdutyNotifier(config *PagerdutyConfig, ns notifications.WebhookSender, images ImageStore, t *template.Template) *PagerdutyNotifier {
|
||||||
return &PagerdutyNotifier{
|
return &PagerdutyNotifier{
|
||||||
Base: NewBase(&models.AlertNotification{
|
Base: NewBase(&models.AlertNotification{
|
||||||
Uid: config.UID,
|
Uid: config.UID,
|
||||||
@ -103,6 +104,7 @@ func NewPagerdutyNotifier(config *PagerdutyConfig, ns notifications.WebhookSende
|
|||||||
tmpl: t,
|
tmpl: t,
|
||||||
log: log.New("alerting.notifier." + config.Name),
|
log: log.New("alerting.notifier." + config.Name),
|
||||||
ns: ns,
|
ns: ns,
|
||||||
|
images: images,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -184,6 +186,24 @@ func (pn *PagerdutyNotifier) buildPagerdutyMessage(ctx context.Context, alerts m
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for i := range as {
|
||||||
|
imgToken := getTokenFromAnnotations(as[i].Annotations)
|
||||||
|
if len(imgToken) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
timeoutCtx, cancel := context.WithTimeout(ctx, ImageStoreTimeout)
|
||||||
|
imgURL, err := pn.images.GetURL(timeoutCtx, imgToken)
|
||||||
|
cancel()
|
||||||
|
if err != nil {
|
||||||
|
if !errors.Is(err, ErrImagesUnavailable) {
|
||||||
|
// Ignore errors. Don't log "ImageUnavailable", which means the storage doesn't exist.
|
||||||
|
pn.log.Warn("failed to retrieve image url from store", "error", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
msg.Images = append(msg.Images, pagerDutyImage{Src: imgURL})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if len(msg.Payload.Summary) > 1024 {
|
if len(msg.Payload.Summary) > 1024 {
|
||||||
// This is the Pagerduty limit.
|
// This is the Pagerduty limit.
|
||||||
msg.Payload.Summary = msg.Payload.Summary[:1021] + "..."
|
msg.Payload.Summary = msg.Payload.Summary[:1021] + "..."
|
||||||
@ -215,6 +235,7 @@ type pagerDutyMessage struct {
|
|||||||
Client string `json:"client,omitempty"`
|
Client string `json:"client,omitempty"`
|
||||||
ClientURL string `json:"client_url,omitempty"`
|
ClientURL string `json:"client_url,omitempty"`
|
||||||
Links []pagerDutyLink `json:"links,omitempty"`
|
Links []pagerDutyLink `json:"links,omitempty"`
|
||||||
|
Images []pagerDutyImage `json:"images,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type pagerDutyLink struct {
|
type pagerDutyLink struct {
|
||||||
@ -222,6 +243,10 @@ type pagerDutyLink struct {
|
|||||||
Text string `json:"text"`
|
Text string `json:"text"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type pagerDutyImage struct {
|
||||||
|
Src string `json:"src"`
|
||||||
|
}
|
||||||
|
|
||||||
type pagerDutyPayload struct {
|
type pagerDutyPayload struct {
|
||||||
Summary string `json:"summary"`
|
Summary string `json:"summary"`
|
||||||
Source string `json:"source"`
|
Source string `json:"source"`
|
||||||
|
@ -149,7 +149,7 @@ func TestPagerdutyNotifier(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 := NewPagerdutyNotifier(cfg, webhookSender, tmpl)
|
pn := NewPagerdutyNotifier(cfg, webhookSender, &UnavailableImageStore{}, 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