@ -7,6 +7,7 @@ import (
"fmt"
"fmt"
"net/http"
"net/http"
"sort"
"sort"
"strings"
"github.com/prometheus/alertmanager/notify"
"github.com/prometheus/alertmanager/notify"
"github.com/prometheus/alertmanager/template"
"github.com/prometheus/alertmanager/template"
@ -36,6 +37,8 @@ type OpsgenieNotifier struct {
* Base
* Base
APIKey string
APIKey string
APIUrl string
APIUrl string
Message string
Description string
AutoClose bool
AutoClose bool
OverridePriority bool
OverridePriority bool
SendTagsAs string
SendTagsAs string
@ -49,6 +52,8 @@ type OpsgenieConfig struct {
* NotificationChannelConfig
* NotificationChannelConfig
APIKey string
APIKey string
APIUrl string
APIUrl string
Message string
Description string
AutoClose bool
AutoClose bool
OverridePriority bool
OverridePriority bool
SendTagsAs string
SendTagsAs string
@ -82,6 +87,8 @@ func NewOpsgenieConfig(config *NotificationChannelConfig, decryptFunc GetDecrypt
APIUrl : config . Settings . Get ( "apiUrl" ) . MustString ( OpsgenieAlertURL ) ,
APIUrl : config . Settings . Get ( "apiUrl" ) . MustString ( OpsgenieAlertURL ) ,
AutoClose : config . Settings . Get ( "autoClose" ) . MustBool ( true ) ,
AutoClose : config . Settings . Get ( "autoClose" ) . MustBool ( true ) ,
OverridePriority : config . Settings . Get ( "overridePriority" ) . MustBool ( true ) ,
OverridePriority : config . Settings . Get ( "overridePriority" ) . MustBool ( true ) ,
Message : config . Settings . Get ( "message" ) . MustString ( ` {{ template "default.title" . }} ` ) ,
Description : config . Settings . Get ( "description" ) . MustString ( "" ) ,
SendTagsAs : sendTagsAs ,
SendTagsAs : sendTagsAs ,
} , nil
} , nil
}
}
@ -98,6 +105,8 @@ func NewOpsgenieNotifier(config *OpsgenieConfig, ns notifications.WebhookSender,
} ) ,
} ) ,
APIKey : config . APIKey ,
APIKey : config . APIKey ,
APIUrl : config . APIUrl ,
APIUrl : config . APIUrl ,
Description : config . Description ,
Message : config . Message ,
AutoClose : config . AutoClose ,
AutoClose : config . AutoClose ,
OverridePriority : config . OverridePriority ,
OverridePriority : config . OverridePriority ,
SendTagsAs : config . SendTagsAs ,
SendTagsAs : config . SendTagsAs ,
@ -180,13 +189,25 @@ func (on *OpsgenieNotifier) buildOpsgenieMessage(ctx context.Context, alerts mod
var tmplErr error
var tmplErr error
tmpl , data := TmplText ( ctx , on . tmpl , as , on . log , & tmplErr )
tmpl , data := TmplText ( ctx , on . tmpl , as , on . log , & tmplErr )
title := tmpl ( DefaultMessageTitleEmbed )
titleTmpl := on . Message
description := fmt . Sprintf (
if strings . TrimSpace ( titleTmpl ) == "" {
"%s\n%s\n\n%s" ,
titleTmpl = ` {{ template "default.title" . }} `
tmpl ( DefaultMessageTitleEmbed ) ,
}
ruleURL ,
tmpl ( ` {{ template "default.message" . }} ` ) ,
title := tmpl ( titleTmpl )
)
if len ( title ) > 130 {
title = title [ : 127 ] + "..."
}
description := tmpl ( on . Description )
if strings . TrimSpace ( description ) == "" {
description = fmt . Sprintf (
"%s\n%s\n\n%s" ,
tmpl ( DefaultMessageTitleEmbed ) ,
ruleURL ,
tmpl ( ` {{ template "default.message" . }} ` ) ,
)
}
var priority string
var priority string
@ -202,6 +223,12 @@ func (on *OpsgenieNotifier) buildOpsgenieMessage(ctx context.Context, alerts mod
}
}
}
}
// Check for templating errors
if tmplErr != nil {
on . log . Warn ( "failed to template Opsgenie message" , "err" , tmplErr . Error ( ) )
tmplErr = nil
}
bodyJSON . Set ( "message" , title )
bodyJSON . Set ( "message" , title )
bodyJSON . Set ( "source" , "Grafana" )
bodyJSON . Set ( "source" , "Grafana" )
bodyJSON . Set ( "alias" , alias )
bodyJSON . Set ( "alias" , alias )
@ -244,9 +271,9 @@ func (on *OpsgenieNotifier) buildOpsgenieMessage(ctx context.Context, alerts mod
bodyJSON . Set ( "tags" , tags )
bodyJSON . Set ( "tags" , tags )
bodyJSON . Set ( "details" , details )
bodyJSON . Set ( "details" , details )
apiURL = tmpl ( on . APIUrl )
apiURL = tmpl ( on . APIUrl )
if tmplErr != nil {
if tmplErr != nil {
on . log . Warn ( "failed to template Opsgenie message" , "err" , tmplErr . Error ( ) )
on . log . Warn ( "failed to template Opsgenie URL" , "err" , tmplErr . Error ( ) , "fallback" , on . APIUrl )
apiURL = on . APIUrl
}
}
return bodyJSON , apiURL , nil
return bodyJSON , apiURL , nil