forked from kaplanelad/veneur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentry.go
119 lines (101 loc) · 3.01 KB
/
sentry.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package veneur
import (
"fmt"
"github.com/getsentry/raven-go"
"github.com/sirupsen/logrus"
"github.com/stripe/veneur/ssf"
"github.com/stripe/veneur/trace"
"github.com/stripe/veneur/trace/metrics"
)
// ConsumePanic is intended to be called inside a deferred function when recovering
// from a panic. It accepts the value of recover() as its only argument,
// and reports the panic to Sentry, prints the stack, and then repanics (to ensure your program terminates)
func ConsumePanic(sentry *raven.Client, cl *trace.Client, hostname string, err interface{}) {
if err == nil {
return
}
if sentry != nil {
p := raven.Packet{
Level: raven.FATAL,
ServerName: hostname,
Interfaces: []raven.Interface{
// ignore 2 stack frames:
// - the frame for ConsumePanic itself
// - the frame for the deferred function that invoked ConsumePanic
raven.NewStacktrace(2, 3, []string{"main", "github.com/stripe/veneur"}),
},
}
// remember to block, since we're about to re-panic, which will probably terminate
switch e := err.(type) {
case error:
p.Message = e.Error()
case fmt.Stringer:
p.Message = e.String()
default:
p.Message = fmt.Sprintf("%#v", e)
}
_, ch := sentry.Capture(&p, nil)
metrics.ReportOne(cl, ssf.Count("sentry.errors_total", 1, nil))
// we don't want the program to terminate before reporting to sentry
<-ch
}
panic(err)
}
// logrus hook to send error/fatal/panic messages to sentry
type sentryHook struct {
c *raven.Client
hostname string
lv []logrus.Level
}
var _ logrus.Hook = sentryHook{}
func (s sentryHook) Levels() []logrus.Level {
return s.lv
}
func (s sentryHook) Fire(e *logrus.Entry) error {
if s.c == nil {
// raven.Client works when it is nil, but skip the useless work and don't hang on Fatal
return nil
}
p := raven.Packet{
ServerName: s.hostname,
Interfaces: []raven.Interface{
// ignore the stack frames for the Fire function itself
// the logrus machinery that invoked Fire will also be hidden
// because it is not an "in-app" library
raven.NewStacktrace(2, 3, []string{"main", "github.com/stripe/veneur"}),
},
}
packetExtraLength := len(e.Data)
if err, ok := e.Data[logrus.ErrorKey].(error); ok {
p.Message = err.Error()
// don't send the error as an extra field
packetExtraLength--
} else {
p.Message = e.Message
}
p.Extra = make(map[string]interface{}, packetExtraLength)
for k, v := range e.Data {
if k == logrus.ErrorKey {
continue // already handled this key, don't put it into the Extra hash
}
p.Extra[k] = v
}
switch e.Level {
case logrus.FatalLevel, logrus.PanicLevel:
p.Level = raven.FATAL
case logrus.ErrorLevel:
p.Level = raven.ERROR
case logrus.WarnLevel:
p.Level = raven.WARNING
case logrus.InfoLevel:
p.Level = raven.INFO
case logrus.DebugLevel:
p.Level = raven.DEBUG
}
_, ch := s.c.Capture(&p, nil)
if e.Level == logrus.PanicLevel || e.Level == logrus.FatalLevel {
// we don't want the program to terminate before reporting to sentry
return <-ch
}
return nil
}