@@ -21,6 +21,7 @@ import (
21
21
22
22
"github.com/google/go-github/v32/github"
23
23
"github.com/pkg/errors"
24
+ "github.com/rcrowley/go-metrics"
24
25
"github.com/rs/zerolog"
25
26
)
26
27
@@ -201,24 +202,36 @@ func (d *eventDispatcher) ServeHTTP(w http.ResponseWriter, r *http.Request) {
201
202
d .onResponse (w , r , eventType , ok )
202
203
}
203
204
204
- // DefaultErrorCallback logs errors and responds with a 500 status code.
205
+ // DefaultErrorCallback logs errors and responds with an appropriate status code.
205
206
func DefaultErrorCallback (w http.ResponseWriter , r * http.Request , err error ) {
206
- logger := zerolog .Ctx (r .Context ())
207
+ defaultErrorCallback (w , r , err )
208
+ }
207
209
208
- var ve ValidationError
209
- if errors .As (err , & ve ) {
210
- logger .Warn ().Err (ve .Cause ).Msgf ("Received invalid webhook headers or payload" )
211
- http .Error (w , "Invalid webhook headers or payload" , http .StatusBadRequest )
212
- return
213
- }
214
- if errors .Is (err , ErrCapacityExceeded ) {
215
- logger .Warn ().Msg ("Dropping webhook event due to over-capacity scheduler" )
216
- http .Error (w , "No capacity available to processes this event" , http .StatusServiceUnavailable )
217
- return
218
- }
210
+ var defaultErrorCallback = MetricsErrorCallback (nil )
211
+
212
+ // MetricsErrorCallback logs errors, increments an error counter, and responds
213
+ // with an appropriate status code.
214
+ func MetricsErrorCallback (reg metrics.Registry ) ErrorCallback {
215
+ return func (w http.ResponseWriter , r * http.Request , err error ) {
216
+ logger := zerolog .Ctx (r .Context ())
219
217
220
- logger .Error ().Err (err ).Msg ("Unexpected error handling webhook" )
221
- http .Error (w , http .StatusText (http .StatusInternalServerError ), http .StatusInternalServerError )
218
+ var ve ValidationError
219
+ if errors .As (err , & ve ) {
220
+ logger .Warn ().Err (ve .Cause ).Msgf ("Received invalid webhook headers or payload" )
221
+ http .Error (w , "Invalid webhook headers or payload" , http .StatusBadRequest )
222
+ return
223
+ }
224
+ if errors .Is (err , ErrCapacityExceeded ) {
225
+ logger .Warn ().Msg ("Dropping webhook event due to over-capacity scheduler" )
226
+ http .Error (w , "No capacity available to processes this event" , http .StatusServiceUnavailable )
227
+ return
228
+ }
229
+
230
+ logger .Error ().Err (err ).Msg ("Unexpected error handling webhook" )
231
+ errorCounter (reg , r .Header .Get ("X-Github-Event" )).Inc (1 )
232
+
233
+ http .Error (w , http .StatusText (http .StatusInternalServerError ), http .StatusInternalServerError )
234
+ }
222
235
}
223
236
224
237
// DefaultResponseCallback responds with a 200 OK for handled events and a 202
0 commit comments