-
Notifications
You must be signed in to change notification settings - Fork 3
/
airbrake.go
94 lines (79 loc) · 1.8 KB
/
airbrake.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
package glog
import (
"fmt"
"net/http"
"github.com/airbrake/gobrake/v5"
)
// Gobrake is an instance of Airbrake Go Notifier that is used to send
// logs to Airbrake.
var Gobrake *gobrake.Notifier
var gobrakeSeverity = errorLog
func SetGobrakeNotifier(notifier *gobrake.Notifier) {
Gobrake = notifier
}
// SetGobrakeSeverity sets minimum log severity that will be sent to Airbrake.
//
// Valid names are "INFO", "WARNING", "ERROR", and "FATAL". If the name is not
// recognized, "ERROR" severity is used.
func SetGobrakeSeverity(severity string) {
v, ok := severityByName(severity)
if ok {
gobrakeSeverity = v
}
}
type requester interface {
Request() *http.Request
}
// Implemented by context.Context
type valuer interface {
Value(key interface{}) interface{}
}
func notifyAirbrake(depth int, s severity, format string, args ...interface{}) {
if Gobrake == nil {
return
}
if s < gobrakeSeverity {
return
}
var msg string
if format != "" {
msg = fmt.Sprintf(format, args...)
} else {
msg = fmt.Sprint(args...)
}
var theErr error
var req *http.Request
var values valuer
for _, arg := range args {
if v, ok := arg.(error); ok {
theErr = v
}
if v, ok := arg.(*http.Request); ok {
req = v
}
if v, ok := arg.(requester); ok {
req = v.Request()
}
if v, ok := arg.(valuer); ok {
values = v
}
}
if values != nil && req == nil {
req, _ = values.Value("request").(*http.Request)
}
var notice *gobrake.Notice
if theErr != nil {
notice = Gobrake.Notice(theErr, req, depth)
notice.Errors[0].Message = msg
} else {
notice = Gobrake.Notice(msg, req, depth)
}
notice.Context["severity"] = severityName[s]
if values != nil {
route, _ := values.Value("route").(string)
if route != "" {
notice.Context["route"] = route
}
}
Gobrake.SendNoticeAsync(notice)
}