-
Notifications
You must be signed in to change notification settings - Fork 0
/
airbrake_service.go
84 lines (72 loc) · 2.08 KB
/
airbrake_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package bedrock
import (
"errors"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/gin-gonic/gin"
"gopkg.in/airbrake/gobrake.v2"
"net/http"
)
// Airbrake Config
type AirbrakeConfig struct {
Enabled bool
Host string
ProjectID int64
ProjectKey string
}
// Airbrake Service. Add this to your AppServicer and call Configure and Build
// to add Airbrake support to your AppServicer.
type AirbrakeService struct {
Config AirbrakeConfig
Notifier *gobrake.Notifier
}
// Prepares the application to use the AirbrakeService. The function will:
//
// 1. Add a recovery handler to gin
// 2. Replace app.OnException with a version that writes the airbrak in addition
// to logging
// 3. Sets the Notifier object that will be used to push notices to Airbrake
func (s *AirbrakeService) Build(r *gin.Engine) error {
if !s.Config.Enabled {
log.Info("AirbrakeService is disabled")
return nil
}
s.Notifier = gobrake.NewNotifier(s.Config.ProjectID, s.Config.ProjectKey)
s.Notifier.SetHost(s.Config.Host)
s.Notifier.AddFilter(func(notice *gobrake.Notice) *gobrake.Notice {
notice.Context["environment"] = gin.Mode()
return notice
})
r.Use(s.RecoveryMiddleware())
return nil
}
// Generates a gin route handler for triggering a panic. This is used for testing
// that the recovery works.
func (s *AirbrakeService) PanicHandler() func(*gin.Context) {
return func(c *gin.Context) {
panic("Panicking")
}
}
// Generates a gin middleware for recovering from panics.
func (s *AirbrakeService) RecoveryMiddleware() func(*gin.Context) {
return func(c *gin.Context) {
defer func() {
if rval := recover(); rval != nil {
rvalStr := fmt.Sprint(rval)
log.Errorf("recovering from:%s at:%s", rvalStr, c.Request.URL)
err := errors.New(rvalStr)
s.OnException(c, err)
c.JSON(http.StatusInternalServerError, Errorf("%v", err))
}
}()
c.Next()
}
}
// The OnExeption replacement for app
func (s *AirbrakeService) OnException(c *gin.Context, err error) {
log.WithFields(log.Fields{
"trace": StackTrace(),
}).Error(err)
s.Notifier.Notify(err, c.Request)
defer s.Notifier.Flush()
}