forked from airbrake/gobrake
/
notice.go
151 lines (127 loc) · 3.21 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
package gobrake
import (
"fmt"
"net/http"
"os"
"runtime"
"strings"
"sync"
)
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": "gobrake",
"version": "3.4.0",
"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 := os.Getenv("GOPATH"); s != "" {
defaultContext["gopath"] = s
}
})
return defaultContext
}
type Error struct {
Type string `json:"type"`
Message string `json:"message"`
Backtrace []StackFrame `json:"backtrace"`
}
type StackFrame struct {
File string `json:"file"`
Line int `json:"line"`
Func string `json:"function"`
Code map[int]string `json:"code,omitempty"`
}
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)
packageName, backtrace := getBacktrace(e, depth)
for i := range backtrace {
frame := &backtrace[i]
code, err := getCode(frame.File, frame.Line)
if err != nil {
if !os.IsNotExist(err) {
logger.Printf("getCode file=%q line=%d failed: %s",
frame.File, frame.Line, err)
}
continue
}
frame.Code = code
}
notice = &Notice{
Errors: []Error{{
Type: typeName,
Message: fmt.Sprint(e),
Backtrace: backtrace,
}},
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
}
notice.Context["component"] = packageName
if req != nil {
notice.SetRequest(req)
}
return notice
}