@@ -106,14 +106,14 @@ type pagerDutyImage struct {
106
106
}
107
107
108
108
type pagerDutyPayload struct {
109
- Summary string `json:"summary"`
110
- Source string `json:"source"`
111
- Severity string `json:"severity"`
112
- Timestamp string `json:"timestamp,omitempty"`
113
- Class string `json:"class,omitempty"`
114
- Component string `json:"component,omitempty"`
115
- Group string `json:"group,omitempty"`
116
- CustomDetails map [string ]string `json:"custom_details,omitempty"`
109
+ Summary string `json:"summary"`
110
+ Source string `json:"source"`
111
+ Severity string `json:"severity"`
112
+ Timestamp string `json:"timestamp,omitempty"`
113
+ Class string `json:"class,omitempty"`
114
+ Component string `json:"component,omitempty"`
115
+ Group string `json:"group,omitempty"`
116
+ CustomDetails map [string ]interface {} `json:"custom_details,omitempty"`
117
117
}
118
118
119
119
func (n * Notifier ) encodeMessage (msg * pagerDutyMessage ) (bytes.Buffer , error ) {
@@ -128,7 +128,7 @@ func (n *Notifier) encodeMessage(msg *pagerDutyMessage) (bytes.Buffer, error) {
128
128
if n .apiV1 != "" {
129
129
msg .Details = map [string ]string {"error" : truncatedMsg }
130
130
} else {
131
- msg .Payload .CustomDetails = map [string ]string {"error" : truncatedMsg }
131
+ msg .Payload .CustomDetails = map [string ]interface {} {"error" : truncatedMsg }
132
132
}
133
133
134
134
warningMsg := fmt .Sprintf ("Truncated Details because message of size %s exceeds limit %s" , units .MetricBytes (buf .Len ()).String (), units .MetricBytes (maxEventSize ).String ())
@@ -149,7 +149,6 @@ func (n *Notifier) notifyV1(
149
149
key notify.Key ,
150
150
data * template.Data ,
151
151
details map [string ]string ,
152
- as ... * types.Alert ,
153
152
) (bool , error ) {
154
153
var tmplErr error
155
154
tmpl := notify .TmplText (n .tmpl , data , & tmplErr )
@@ -209,8 +208,7 @@ func (n *Notifier) notifyV2(
209
208
eventType string ,
210
209
key notify.Key ,
211
210
data * template.Data ,
212
- details map [string ]string ,
213
- as ... * types.Alert ,
211
+ details map [string ]interface {},
214
212
) (bool , error ) {
215
213
var tmplErr error
216
214
tmpl := notify .TmplText (n .tmpl , data , & tmplErr )
@@ -320,19 +318,23 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
320
318
321
319
n .logger .Debug ("extracted group key" , "key" , key , "eventType" , eventType )
322
320
323
- details := make (map [string ]string , len (n .conf .Details ))
324
- for k , v := range n .conf .Details {
325
- detail , err := n .tmpl .ExecuteTextString (v , data )
326
- if err != nil {
327
- return false , fmt .Errorf ("%q: failed to template %q: %w" , k , v , err )
321
+ if n .apiV1 != "" {
322
+ details := make (map [string ]string , len (n .conf .Details ))
323
+ for k , v := range n .conf .Details {
324
+ detail , err := n .tmpl .ExecuteTextString (v .(string ), data )
325
+ if err != nil {
326
+ return false , fmt .Errorf ("%q: failed to template %q: %w" , k , v , err )
327
+ }
328
+ details [k ] = detail
328
329
}
329
- details [ k ] = detail
330
+ return n . notifyV1 ( ctx , eventType , key , data , details )
330
331
}
331
332
332
- if n .apiV1 != "" {
333
- return n .notifyV1 (ctx , eventType , key , data , details , as ... )
333
+ details , err := renderDetails (n .conf .Details , data , n .tmpl .ExecuteTextString )
334
+ if err != nil {
335
+ return false , err
334
336
}
335
- return n .notifyV2 (ctx , eventType , key , data , details , as ... )
337
+ return n .notifyV2 (ctx , eventType , key , data , details )
336
338
}
337
339
338
340
func errDetails (status int , body io.Reader ) string {
@@ -351,3 +353,30 @@ func errDetails(status int, body io.Reader) string {
351
353
}
352
354
return fmt .Sprintf ("%s: %s" , pgr .Message , strings .Join (pgr .Errors , "," ))
353
355
}
356
+
357
+ func renderDetails (
358
+ details map [string ]interface {},
359
+ data * template.Data ,
360
+ exec func (text string , data interface {}) (string , error ),
361
+ ) (map [string ]interface {}, error ) {
362
+ result := make (map [string ]interface {}, len (details ))
363
+ for k , v := range details {
364
+ switch t := v .(type ) {
365
+ case string :
366
+ detail , err := exec (v .(string ), data )
367
+ if err != nil {
368
+ return nil , fmt .Errorf ("%q: failed to template %q: %w" , k , v , err )
369
+ }
370
+ result [k ] = detail
371
+ case map [string ]interface {}:
372
+ detail , err := renderDetails (v .(map [string ]interface {}), data , exec )
373
+ if err != nil {
374
+ return nil , fmt .Errorf ("%q: failed to template %q: %w" , k , v , err )
375
+ }
376
+ result [k ] = detail
377
+ default :
378
+ return nil , fmt .Errorf ("%q: unsupported detail field type %v: %v" , k , t , v )
379
+ }
380
+ }
381
+ return result , nil
382
+ }
0 commit comments