-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook.go
158 lines (128 loc) · 3.45 KB
/
hook.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package logrussentryhook
import (
"errors"
"reflect"
"runtime"
"strings"
"github.com/getsentry/sentry-go"
"github.com/sirupsen/logrus"
)
var (
levelsMap = map[logrus.Level]sentry.Level{
logrus.PanicLevel: sentry.LevelFatal,
logrus.FatalLevel: sentry.LevelFatal,
logrus.ErrorLevel: sentry.LevelError,
logrus.WarnLevel: sentry.LevelWarning,
logrus.InfoLevel: sentry.LevelInfo,
logrus.DebugLevel: sentry.LevelDebug,
logrus.TraceLevel: sentry.LevelDebug,
}
levels = []logrus.Level{logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel}
)
type SentryHook struct {
IgnoredModules []string
dsn, environment string
}
func (hook *SentryHook) Levels() []logrus.Level {
return levels
}
func (hook *SentryHook) Fire(entry *logrus.Entry) error {
// вытаскиваем fields
for key, value := range entry.Data {
if key != logrus.ErrorKey {
sentry.ConfigureScope(func(scope *sentry.Scope) {
scope.SetExtra(key, value)
})
}
}
// если есть error - и ее вытаскиваем
var err error
if val, ok := entry.Data[logrus.ErrorKey]; ok {
if errValue, ok := val.(error); ok {
err = errValue
}
}
// если ошибку не передали - сделаем пустую
if err == nil {
err = errors.New("nil")
}
//
event := sentry.NewEvent()
stacktrace := hook.newStacktrace()
event.Level = levelsMap[entry.Level]
event.Exception = []sentry.Exception{{
Value: err.Error(),
Type: reflect.TypeOf(err).String(),
Stacktrace: stacktrace,
}}
sentry.CaptureEvent(event)
return nil
}
func New(dsn, environment string, ignoredModules ...string) *SentryHook {
// игнорю сам себя и логрус
ignoredModules = append(ignoredModules, reflect.TypeOf(SentryHook{}).PkgPath(), "github.com/sirupsen/logrus")
return &SentryHook{IgnoredModules: ignoredModules, dsn:dsn, environment:environment}
}
func (hook *SentryHook) SetLevels(newLevels []logrus.Level) {
levels = newLevels
}
func (hook *SentryHook) Init() error {
err := sentry.Init(sentry.ClientOptions{
Dsn: hook.dsn,
Environment: hook.environment,
})
return err
}
func (hook *SentryHook) extractFrames(pcs []uintptr) []sentry.Frame {
var frames []sentry.Frame
callersFrames := runtime.CallersFrames(pcs)
for {
callerFrame, more := callersFrames.Next()
frames = append([]sentry.Frame{
sentry.NewFrame(callerFrame),
}, frames...)
if !more {
break
}
}
return frames
}
func (hook *SentryHook) filterFrames(frames []sentry.Frame) []sentry.Frame {
if len(frames) == 0 {
return nil
}
filteredFrames := make([]sentry.Frame, 0, len(frames))
OUTER:
for _, frame := range frames {
// Skip Go internal frames.
if frame.Module == "runtime" || frame.Module == "testing" {
continue
}
// Skip Sentry internal frames, except for frames in _test packages (for
// testing).
if strings.HasPrefix(frame.Module, "github.com/getsentry/sentry-go") &&
!strings.HasSuffix(frame.Module, "_test") {
continue
}
for _, module := range hook.IgnoredModules {
if strings.HasPrefix(frame.Module, module) {
continue OUTER
}
}
filteredFrames = append(filteredFrames, frame)
}
return filteredFrames
}
func (hook *SentryHook) newStacktrace() *sentry.Stacktrace {
pcs := make([]uintptr, 100)
n := runtime.Callers(1, pcs)
if n == 0 {
return nil
}
frames := hook.extractFrames(pcs[:n])
frames = hook.filterFrames(frames)
stacktrace := sentry.Stacktrace{
Frames: frames,
}
return &stacktrace
}