@@ -105,12 +105,15 @@ func newNotifyHooksMetrics(reg prometheus.Registerer) *notifyHooksMetrics {
105
105
}
106
106
107
107
func (n * notifyHooksNotifier ) Notify (ctx context.Context , alerts ... * types.Alert ) (bool , error ) {
108
- newAlerts := n .apply (ctx , alerts )
108
+ result := n .apply (ctx , alerts )
109
109
110
- return n .upstream .Notify (ctx , newAlerts ... )
110
+ // Add additional data from pre-notify hook to the context
111
+ ctxWithData := withExtraData (ctx , result .ExtraData )
112
+
113
+ return n .upstream .Notify (ctxWithData , result .Alerts ... )
111
114
}
112
115
113
- func (n * notifyHooksNotifier ) apply (ctx context.Context , alerts []* types.Alert ) [] * types. Alert {
116
+ func (n * notifyHooksNotifier ) apply (ctx context.Context , alerts []* types.Alert ) * hookData {
114
117
l := n .logger
115
118
116
119
receiver , _ := notify .ReceiverName (ctx )
@@ -122,19 +125,23 @@ func (n *notifyHooksNotifier) apply(ctx context.Context, alerts []*types.Alert)
122
125
url := n .limits .AlertmanagerNotifyHookURL (n .user )
123
126
if url == "" {
124
127
level .Debug (l ).Log ("msg" , "Notify hooks not applied, no URL configured" )
125
- return alerts
128
+ return & hookData {
129
+ Alerts : alerts ,
130
+ }
126
131
}
127
132
128
133
receivers := n .limits .AlertmanagerNotifyHookReceivers (n .user )
129
134
if len (receivers ) > 0 && ! slices .Contains (receivers , receiver ) {
130
135
level .Debug (l ).Log ("msg" , "Notify hooks not applied, not enabled for receiver" )
131
- return alerts
136
+ return & hookData {
137
+ Alerts : alerts ,
138
+ }
132
139
}
133
140
134
141
timeout := n .limits .AlertmanagerNotifyHookTimeout (n .user )
135
142
136
143
start := time .Now ()
137
- newAlerts , code , err := n .invoke (ctx , l , url , timeout , alerts )
144
+ result , code , err := n .invoke (ctx , l , url , timeout , alerts )
138
145
139
146
duration := time .Since (start )
140
147
n .metrics .hookDuration .Observe (float64 (duration ))
@@ -156,12 +163,14 @@ func (n *notifyHooksNotifier) apply(ctx context.Context, alerts []*types.Alert)
156
163
n .metrics .hookFailed .WithLabelValues (status ).Inc ()
157
164
level .Error (l ).Log ("msg" , "Notify hooks failed" , "err" , err )
158
165
}
159
- return alerts
166
+ return & hookData {
167
+ Alerts : alerts ,
168
+ }
160
169
}
161
170
162
171
level .Debug (l ).Log ("msg" , "Notify hooks applied successfully" )
163
172
164
- return newAlerts
173
+ return result
165
174
}
166
175
167
176
// hookData is the payload we send and receive from the notification hook.
@@ -170,6 +179,18 @@ type hookData struct {
170
179
Status string `json:"status"`
171
180
Alerts []* types.Alert `json:"alerts"`
172
181
GroupLabels model.LabelSet `json:"groupLabels"`
182
+
183
+ ExtraData json.RawMessage `json:"extraData,omitempty"`
184
+ }
185
+
186
+ type extraDataKey int
187
+
188
+ const (
189
+ ExtraDataKey extraDataKey = iota
190
+ )
191
+
192
+ func withExtraData (ctx context.Context , extraData json.RawMessage ) context.Context {
193
+ return context .WithValue (ctx , ExtraDataKey , extraData )
173
194
}
174
195
175
196
func (n * notifyHooksNotifier ) getData (ctx context.Context , l log.Logger , alerts []* types.Alert ) * hookData {
@@ -190,7 +211,7 @@ func (n *notifyHooksNotifier) getData(ctx context.Context, l log.Logger, alerts
190
211
}
191
212
}
192
213
193
- func (n * notifyHooksNotifier ) invoke (ctx context.Context , l log.Logger , url string , timeout time.Duration , alerts []* types.Alert ) ([] * types. Alert , int , error ) {
214
+ func (n * notifyHooksNotifier ) invoke (ctx context.Context , l log.Logger , url string , timeout time.Duration , alerts []* types.Alert ) (* hookData , int , error ) {
194
215
logger , ctx := spanlogger .New (ctx , l , tracer , "NotifyHooksNotifier.Invoke" )
195
216
defer logger .Finish ()
196
217
@@ -240,5 +261,5 @@ func (n *notifyHooksNotifier) invoke(ctx context.Context, l log.Logger, url stri
240
261
return nil , 0 , err
241
262
}
242
263
243
- return result . Alerts , 0 , nil
264
+ return & result , 0 , nil
244
265
}
0 commit comments