-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathsentry.go
424 lines (381 loc) · 12.1 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package logrus_sentry
import (
"encoding/json"
"fmt"
"runtime"
"sync"
"time"
raven "github.com/getsentry/raven-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
var (
severityMap = map[logrus.Level]raven.Severity{
logrus.TraceLevel: raven.DEBUG,
logrus.DebugLevel: raven.DEBUG,
logrus.InfoLevel: raven.INFO,
logrus.WarnLevel: raven.WARNING,
logrus.ErrorLevel: raven.ERROR,
logrus.FatalLevel: raven.FATAL,
logrus.PanicLevel: raven.FATAL,
}
)
// SentryHook delivers logs to a sentry server.
type SentryHook struct {
// Timeout sets the time to wait for a delivery error from the sentry server.
// If this is set to zero the server will not wait for any response and will
// consider the message correctly sent.
//
// This is ignored for asynchronous hooks. If you want to set a timeout when
// using an async hook (to bound the length of time that hook.Flush can take),
// you probably want to create your own raven.Client and set
// ravenClient.Transport.(*raven.HTTPTransport).Client.Timeout to set a
// timeout on the underlying HTTP request instead.
Timeout time.Duration
StacktraceConfiguration StackTraceConfiguration
client *raven.Client
levels []logrus.Level
serverName string
ignoreFields map[string]struct{}
extraFilters map[string]func(interface{}) interface{}
errorHandlers []func(entry *logrus.Entry, err error)
asynchronous bool
mu sync.RWMutex
wg sync.WaitGroup
}
// The Stacktracer interface allows an error type to return a raven.Stacktrace.
type Stacktracer interface {
GetStacktrace() *raven.Stacktrace
}
type causer interface {
Cause() error
}
type pkgErrorStackTracer interface {
StackTrace() errors.StackTrace
}
// StackTraceConfiguration allows for configuring stacktraces
type StackTraceConfiguration struct {
// whether stacktraces should be enabled
Enable bool
// the level at which to start capturing stacktraces
Level logrus.Level
// how many stack frames to skip before stacktrace starts recording
Skip int
// the number of lines to include around a stack frame for context
Context int
// the prefixes that will be matched against the stack frame.
// if the stack frame's package matches one of these prefixes
// sentry will identify the stack frame as "in_app"
InAppPrefixes []string
// whether sending exception type should be enabled.
SendExceptionType bool
// whether the exception type and message should be switched.
SwitchExceptionTypeAndMessage bool
// whether to include a breadcrumb with the full error stack
IncludeErrorBreadcrumb bool
}
// NewSentryHook creates a hook to be added to an instance of logger
// and initializes the raven client.
// This method sets the timeout to 100 milliseconds.
func NewSentryHook(DSN string, levels []logrus.Level) (*SentryHook, error) {
client, err := raven.New(DSN)
if err != nil {
return nil, err
}
return NewWithClientSentryHook(client, levels)
}
// NewWithTagsSentryHook creates a hook with tags to be added to an instance
// of logger and initializes the raven client. This method sets the timeout to
// 100 milliseconds.
func NewWithTagsSentryHook(DSN string, tags map[string]string, levels []logrus.Level) (*SentryHook, error) {
client, err := raven.NewWithTags(DSN, tags)
if err != nil {
return nil, err
}
return NewWithClientSentryHook(client, levels)
}
// NewWithClientSentryHook creates a hook using an initialized raven client.
// This method sets the timeout to 100 milliseconds.
func NewWithClientSentryHook(client *raven.Client, levels []logrus.Level) (*SentryHook, error) {
return &SentryHook{
Timeout: 100 * time.Millisecond,
StacktraceConfiguration: StackTraceConfiguration{
Enable: false,
Level: logrus.ErrorLevel,
Skip: 6,
Context: 0,
InAppPrefixes: nil,
SendExceptionType: true,
},
client: client,
levels: levels,
ignoreFields: make(map[string]struct{}),
extraFilters: make(map[string]func(interface{}) interface{}),
}, nil
}
// NewAsyncSentryHook creates a hook same as NewSentryHook, but in asynchronous
// mode.
func NewAsyncSentryHook(DSN string, levels []logrus.Level) (*SentryHook, error) {
hook, err := NewSentryHook(DSN, levels)
return setAsync(hook), err
}
// NewAsyncWithTagsSentryHook creates a hook same as NewWithTagsSentryHook, but
// in asynchronous mode.
func NewAsyncWithTagsSentryHook(DSN string, tags map[string]string, levels []logrus.Level) (*SentryHook, error) {
hook, err := NewWithTagsSentryHook(DSN, tags, levels)
return setAsync(hook), err
}
// NewAsyncWithClientSentryHook creates a hook same as NewWithClientSentryHook,
// but in asynchronous mode.
func NewAsyncWithClientSentryHook(client *raven.Client, levels []logrus.Level) (*SentryHook, error) {
hook, err := NewWithClientSentryHook(client, levels)
return setAsync(hook), err
}
func setAsync(hook *SentryHook) *SentryHook {
if hook == nil {
return nil
}
hook.asynchronous = true
return hook
}
// Fire is called when an event should be sent to sentry
// Special fields that sentry uses to give more information to the server
// are extracted from entry.Data (if they are found)
// These fields are: error, logger, server_name, http_request, tags
func (hook *SentryHook) Fire(entry *logrus.Entry) error {
hook.mu.RLock() // Allow multiple go routines to log simultaneously
defer hook.mu.RUnlock()
df := newDataField(entry.Data)
err, hasError := df.getError()
var crumbs *Breadcrumbs
if hasError && hook.StacktraceConfiguration.IncludeErrorBreadcrumb {
crumbs = &Breadcrumbs{
Values: []Value{{
Timestamp: int64(time.Now().Unix()),
Type: "error",
Message: fmt.Sprintf("%+v", err),
}},
}
}
packet := raven.NewPacketWithExtra(entry.Message, nil, crumbs)
packet.Timestamp = raven.Timestamp(entry.Time)
packet.Level = severityMap[entry.Level]
packet.Platform = "go"
// set special fields
if hook.serverName != "" {
packet.ServerName = hook.serverName
}
if logger, ok := df.getLogger(); ok {
packet.Logger = logger
}
if serverName, ok := df.getServerName(); ok {
packet.ServerName = serverName
}
if eventID, ok := df.getEventID(); ok {
packet.EventID = eventID
}
if tags, ok := df.getTags(); ok {
packet.Tags = tags
}
if fingerprint, ok := df.getFingerprint(); ok {
packet.Fingerprint = fingerprint
}
if req, ok := df.getHTTPRequest(); ok {
packet.Interfaces = append(packet.Interfaces, req)
}
if user, ok := df.getUser(); ok {
packet.Interfaces = append(packet.Interfaces, user)
}
// set stacktrace data
stConfig := &hook.StacktraceConfiguration
if stConfig.Enable && entry.Level <= stConfig.Level {
if err, ok := df.getError(); ok {
var currentStacktrace *raven.Stacktrace
currentStacktrace = hook.findStacktrace(err)
if currentStacktrace == nil {
currentStacktrace = raven.NewStacktrace(stConfig.Skip, stConfig.Context, stConfig.InAppPrefixes)
}
cause := errors.Cause(err)
if cause == nil {
cause = err
}
exc := raven.NewException(cause, currentStacktrace)
if !stConfig.SendExceptionType {
exc.Type = ""
}
if stConfig.SwitchExceptionTypeAndMessage {
packet.Interfaces = append(packet.Interfaces, currentStacktrace)
packet.Culprit = exc.Type + ": " + currentStacktrace.Culprit()
} else {
packet.Interfaces = append(packet.Interfaces, exc)
packet.Culprit = err.Error()
}
} else {
currentStacktrace := raven.NewStacktrace(stConfig.Skip, stConfig.Context, stConfig.InAppPrefixes)
if currentStacktrace != nil {
packet.Interfaces = append(packet.Interfaces, currentStacktrace)
}
}
} else {
// set the culprit even when the stack trace is disabled, as long as we have an error
if err, ok := df.getError(); ok {
packet.Culprit = err.Error()
}
}
// set other fields
dataExtra := hook.formatExtraData(df)
if packet.Extra == nil {
packet.Extra = dataExtra
} else {
for k, v := range dataExtra {
packet.Extra[k] = v
}
}
_, errCh := hook.client.Capture(packet, nil)
switch {
case hook.asynchronous:
// Our use of hook.mu guarantees that we are following the WaitGroup rule of
// not calling Add in parallel with Wait.
hook.wg.Add(1)
go func() {
if err := <-errCh; err != nil {
for _, handlerFn := range hook.errorHandlers {
handlerFn(entry, err)
}
}
hook.wg.Done()
}()
return nil
case hook.Timeout == 0:
return nil
default:
timeout := hook.Timeout
timeoutCh := time.After(timeout)
select {
case err := <-errCh:
for _, handlerFn := range hook.errorHandlers {
handlerFn(entry, err)
}
return err
case <-timeoutCh:
return fmt.Errorf("no response from sentry server in %s", timeout)
}
}
}
// Flush waits for the log queue to empty. This function only does anything in
// asynchronous mode.
func (hook *SentryHook) Flush() {
if !hook.asynchronous {
return
}
hook.mu.Lock() // Claim exclusive access; any logging goroutines will block until the flush completes
defer hook.mu.Unlock()
hook.wg.Wait()
}
func (hook *SentryHook) findStacktrace(err error) *raven.Stacktrace {
var stacktrace *raven.Stacktrace
var stackErr errors.StackTrace
for err != nil {
// Find the earliest *raven.Stacktrace, or error.StackTrace
if tracer, ok := err.(Stacktracer); ok {
stacktrace = tracer.GetStacktrace()
stackErr = nil
} else if tracer, ok := err.(pkgErrorStackTracer); ok {
stacktrace = nil
stackErr = tracer.StackTrace()
}
if cause, ok := err.(causer); ok {
err = cause.Cause()
} else {
break
}
}
if stackErr != nil {
stacktrace = hook.convertStackTrace(stackErr)
}
return stacktrace
}
// convertStackTrace converts an errors.StackTrace into a natively consumable
// *raven.Stacktrace
func (hook *SentryHook) convertStackTrace(st errors.StackTrace) *raven.Stacktrace {
stConfig := &hook.StacktraceConfiguration
stFrames := []errors.Frame(st)
frames := make([]*raven.StacktraceFrame, 0, len(stFrames))
for i := range stFrames {
pc := uintptr(stFrames[i])
fn := runtime.FuncForPC(pc)
file, line := fn.FileLine(pc)
frame := raven.NewStacktraceFrame(pc, fn.Name(), file, line, stConfig.Context, stConfig.InAppPrefixes)
if frame != nil {
frames = append(frames, frame)
}
}
// Sentry wants the frames with the oldest first, so reverse them
for i, j := 0, len(frames)-1; i < j; i, j = i+1, j-1 {
frames[i], frames[j] = frames[j], frames[i]
}
return &raven.Stacktrace{Frames: frames}
}
// Levels returns the available logging levels.
func (hook *SentryHook) Levels() []logrus.Level {
return hook.levels
}
// AddIgnore adds field name to ignore.
func (hook *SentryHook) AddIgnore(name string) {
hook.ignoreFields[name] = struct{}{}
}
// AddExtraFilter adds a custom filter function.
func (hook *SentryHook) AddExtraFilter(name string, fn func(interface{}) interface{}) {
hook.extraFilters[name] = fn
}
// AddErrorHandler adds a error handler function used when Sentry returns error.
func (hook *SentryHook) AddErrorHandler(fn func(entry *logrus.Entry, err error)) {
hook.errorHandlers = append(hook.errorHandlers, fn)
}
func (hook *SentryHook) formatExtraData(df *dataField) (result map[string]interface{}) {
// create a map for passing to Sentry's extra data
result = make(map[string]interface{}, df.len())
for k, v := range df.data {
if df.isOmit(k) {
continue // skip already used special fields
}
if _, ok := hook.ignoreFields[k]; ok {
continue
}
if fn, ok := hook.extraFilters[k]; ok {
v = fn(v) // apply custom filter
} else {
v = formatData(v) // use default formatter
}
result[k] = v
}
return result
}
// formatData returns value as a suitable format.
func formatData(value interface{}) (formatted interface{}) {
switch value := value.(type) {
case json.Marshaler:
return value
case error:
return value.Error()
case fmt.Stringer:
return value.String()
default:
return value
}
}
// utility classes for breadcrumb support
type Breadcrumbs struct {
Values []Value `json:"values"`
}
type Value struct {
Timestamp int64 `json:"timestamp"`
Type string `json:"type"`
Message string `json:"message"`
Category string `json:"category"`
Level string `json:"string"`
Data interface{} `json:"data"`
}
func (b *Breadcrumbs) Class() string {
return "breadcrumbs"
}