forked from heroku/rollrus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook.go
211 lines (175 loc) · 5.11 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package rollrus
import (
"context"
"fmt"
"runtime"
"strings"
"time"
"github.com/rollbar/rollbar-go"
"github.com/sirupsen/logrus"
"github.com/heroku/rollrus/internal/transport"
)
var _ logrus.Hook = &Hook{} //assert that *Hook is a logrus.Hook
// Hook is a wrapper for the Rollbar Client and is usable as a logrus.Hook.
type Hook struct {
*rollbar.Client
triggers []logrus.Level
ignoredErrors []error
setPerson bool
ignoreErrorFunc func(error) bool
ignoreFunc func(error, map[string]interface{}) bool
// only used for tests to verify whether or not a report happened.
reported bool
}
// NewHookForLevels provided by the caller. Otherwise works like NewHook.
func NewHookForLevels(token string, env string, levels []logrus.Level) *Hook {
client := rollbar.NewSync(token, env, "", "", "")
client.Transport = transport.NewBuffered(client.Transport, rollbar.DefaultBuffer)
return &Hook{
Client: client,
triggers: levels,
ignoredErrors: make([]error, 0),
ignoreErrorFunc: func(error) bool { return false },
ignoreFunc: func(error, map[string]interface{}) bool { return false },
}
}
// Levels returns the logrus log.Levels that this hook handles
func (r *Hook) Levels() []logrus.Level {
if r.triggers == nil {
return defaultTriggerLevels
}
return r.triggers
}
// Fire the hook. This is called by Logrus for entries that match the levels
// returned by Levels().
func (r *Hook) Fire(entry *logrus.Entry) error {
err := extractError(entry)
cause := errorCause(err)
for _, ie := range r.ignoredErrors {
if ie == cause {
return nil
}
}
if r.ignoreErrorFunc(cause) {
return nil
}
m := convertFields(entry.Data)
if _, exists := m["time"]; !exists {
m["time"] = entry.Time.Format(time.RFC3339)
}
if _, exists := m["msg"]; !exists && entry.Message != "" {
m["msg"] = entry.Message
}
if r.ignoreFunc(cause, m) {
return nil
}
r.report(entry, err, m)
return nil
}
func (r *Hook) report(entry *logrus.Entry, cause error, m map[string]interface{}) {
level := entry.Level
r.reported = true
ctx := context.Background()
if r.setPerson {
userID, userName, userEmail := "", "", ""
if userIDInterface, exists := m["user_id"]; exists {
userID = fmt.Sprint(userIDInterface)
}
if userIDInterface, exists := m["user_name"]; exists {
userName = fmt.Sprint(userIDInterface)
}
if userIDInterface, exists := m["user_email"]; exists {
userEmail = fmt.Sprint(userIDInterface)
}
if userID != "" {
ctx = rollbar.NewPersonContext(ctx, &rollbar.Person{Id: userID, Username: userName, Email: userEmail})
}
}
switch {
case level == logrus.FatalLevel || level == logrus.PanicLevel:
skip := framesToSkip(2)
r.Client.ErrorWithStackSkipWithExtrasAndContext(ctx, rollbar.CRIT, cause, skip, m)
r.Client.Wait()
case level == logrus.ErrorLevel:
skip := framesToSkip(2)
r.Client.ErrorWithStackSkipWithExtrasAndContext(ctx, rollbar.ERR, cause, skip, m)
case level == logrus.WarnLevel:
skip := framesToSkip(2)
r.Client.ErrorWithStackSkipWithExtrasAndContext(ctx, rollbar.WARN, cause, skip, m)
case level == logrus.InfoLevel:
r.Client.MessageWithExtrasAndContext(ctx, rollbar.INFO, entry.Message, m)
case level == logrus.DebugLevel:
r.Client.MessageWithExtrasAndContext(ctx, rollbar.DEBUG, entry.Message, m)
case level == logrus.TraceLevel:
r.Client.MessageWithExtrasAndContext(ctx, rollbar.DEBUG, entry.Message, m)
}
}
// convertFields converts from log.Fields to map[string]interface{} so that we can
// report extra fields to Rollbar
func convertFields(fields logrus.Fields) map[string]interface{} {
m := make(map[string]interface{})
for k, v := range fields {
switch t := v.(type) {
case time.Time:
m[k] = t.Format(time.RFC3339)
case error:
m[k] = t.Error()
default:
if s, ok := v.(fmt.Stringer); ok {
m[k] = s.String()
} else {
m[k] = fmt.Sprintf("%+v", t)
}
}
}
return m
}
// extractError attempts to extract an error from a well known field, err or error
func extractError(entry *logrus.Entry) error {
for _, f := range wellKnownErrorFields {
e, ok := entry.Data[f]
if !ok {
continue
}
err, ok := e.(error)
if !ok {
continue
}
return err
}
// when no error found, default to the logged message.
return fmt.Errorf(entry.Message)
}
// framesToSkip returns the number of caller frames to skip
// to get a stack trace that excludes rollrus and logrus.
func framesToSkip(rollrusSkip int) int {
// skip 1 to get out of this function
skip := rollrusSkip + 1
// to get out of logrus, the amount can vary
// depending on how the user calls the log functions
// figure it out dynamically by skipping until
// we're out of the logrus package
for i := skip; ; i++ {
_, file, _, ok := runtime.Caller(i)
if !ok || !strings.Contains(file, "github.com/sirupsen/logrus") {
skip = i
break
}
}
// rollbar-go is skipping too few frames (2)
// subtract 1 since we're currently working from a function
return skip + 2 - 1
}
func errorCause(err error) error {
type causer interface {
Cause() error
}
for err != nil {
cause, ok := err.(causer)
if !ok {
break
}
err = cause.Cause()
}
return err
}