-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathnotice.go
159 lines (133 loc) · 3.34 KB
/
notice.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
package gobrake
import (
"fmt"
"go/build"
"net/http"
"os"
"runtime"
"strings"
"sync"
"github.com/pkg/errors"
)
var defaultContextOnce sync.Once
var defaultContext map[string]interface{}
func getDefaultContext() map[string]interface{} {
defaultContextOnce.Do(func() {
defaultContext = map[string]interface{}{
"notifier": map[string]interface{}{
"name": notifierName,
"version": notifierVersion,
"url": "https://github.com/airbrake/gobrake",
},
"language": runtime.Version(),
"os": runtime.GOOS,
"architecture": runtime.GOARCH,
}
if s, err := os.Hostname(); err == nil {
defaultContext["hostname"] = s
}
if wd, err := os.Getwd(); err == nil {
defaultContext["rootDirectory"] = wd
}
if s := gopath(); s != "" {
defaultContext["gopath"] = s
}
})
return defaultContext
}
// Returns the GOPATH of the application
func gopath() string {
if path, ok := os.LookupEnv("GOPATH"); ok {
return path
}
return build.Default.GOPATH
}
type StackFrame struct {
File string `json:"file"`
Line int `json:"line"`
Func string `json:"function"`
Code map[int]string `json:"code,omitempty"`
}
type Error struct {
Type string `json:"type"`
Message string `json:"message"`
Backtrace []StackFrame `json:"backtrace"`
}
type Notice struct {
Id string `json:"-"` // id returned by SendNotice
Error error `json:"-"` // error returned by SendNotice
Errors []Error `json:"errors"`
Context map[string]interface{} `json:"context"`
Env map[string]interface{} `json:"environment"`
Session map[string]interface{} `json:"session"`
Params map[string]interface{} `json:"params"`
}
func (n *Notice) String() string {
if len(n.Errors) == 0 {
return "Notice<no errors>"
}
e := n.Errors[0]
return fmt.Sprintf("Notice<%s: %s>", e.Type, e.Message)
}
func (n *Notice) SetRequest(req *http.Request) {
n.Context["url"] = req.URL.String()
n.Context["httpMethod"] = req.Method
if ua := req.Header.Get("User-Agent"); ua != "" {
n.Context["userAgent"] = ua
}
n.Context["userAddr"] = remoteAddr(req)
for k, v := range req.Header {
if len(v) == 1 {
n.Env[k] = v[0]
} else {
n.Env[k] = v
}
}
}
func remoteAddr(req *http.Request) string {
if s := req.Header.Get("X-Forwarded-For"); s != "" {
parts := strings.Split(s, ",")
return parts[0]
}
if s := req.Header.Get("X-Real-Ip"); s != "" {
return s
}
parts := strings.Split(req.RemoteAddr, ":")
return parts[0]
}
func NewNotice(e interface{}, req *http.Request, depth int) *Notice {
notice, ok := e.(*Notice)
if ok {
return notice
}
typeName := getTypeName(e)
notice = &Notice{
Errors: []Error{{
Type: typeName,
Message: fmt.Sprint(e),
}},
Context: make(map[string]interface{}),
Env: make(map[string]interface{}),
Session: make(map[string]interface{}),
Params: make(map[string]interface{}),
}
for k, v := range getDefaultContext() {
notice.Context[k] = v
}
if depth != -1 {
packageName, backtrace := getBacktrace(e, depth+2)
notice.Errors[0].Backtrace = backtrace
notice.Context["component"] = packageName
}
if req != nil {
notice.SetRequest(req)
}
return notice
}
// getTypeName returns the type name of e.
func getTypeName(e interface{}) string {
if err, ok := e.(error); ok {
e = errors.Cause(err)
}
return fmt.Sprintf("%T", e)
}