forked from yeller/yeller-golang
/
yeller.go
220 lines (186 loc) · 5.53 KB
/
yeller.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
package yeller
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"os"
"runtime"
"strconv"
"strings"
)
type ErrorNotification struct {
Type string `json:"type"`
Message string `json:"message"`
StackTrace []StackFrame `json:"stacktrace"`
Url string `json:"url"`
Host string `json:"host"`
Environment string `json:"application-environment"`
CustomData map[string]interface{} `json:"custom-data"`
Location string `json:"location"`
ClientVersion string `json:"client-version"`
}
type StackFrame struct {
Filename string
LineNumber string
FunctionName string
Options map[string]interface{}
}
const (
MAX_STACK_DEPTH = 256
)
var client *Client
func Start(apiKey string) {
client = NewClient(apiKey, "production", "", NewStdErrErrorHandler())
}
func StartApplicationRoot(apiKey string, applicationRoot string) {
client = NewClient(apiKey, "production", applicationRoot, NewStdErrErrorHandler())
}
func StartEnv(apiKey string, env string) {
client = NewClient(apiKey, env, "", NewStdErrErrorHandler())
}
func StartEnvApplicationRoot(apiKey string, env string, applicationRoot string) {
client = NewClient(apiKey, env, applicationRoot, NewStdErrErrorHandler())
}
func StartWithErrorHandler(apiKey string, env string, errorHandler YellerErrorHandler) {
client = NewClient(apiKey, env, "", errorHandler)
}
func StartWithErrorHandlerApplicationRoot(apiKey string, env string, applicationRoot string, errorHandler YellerErrorHandler) {
client = NewClient(apiKey, env, "", errorHandler)
}
func StartWithErrorHandlerEnv(apiKey string, env string, errorHandler YellerErrorHandler) {
client = NewClient(apiKey, env, "", errorHandler)
}
func StartWithErrorHandlerEnvApplicationRoot(apiKey string, env string, applicationRoot string, errorHandler YellerErrorHandler) {
client = NewClient(apiKey, env, applicationRoot, errorHandler)
}
func StartWithClient(newClient *Client) {
client = newClient
}
func Notify(appErr error) {
NotifyInfo(appErr, nil)
}
func NotifyInfo(appErr error, info map[string]interface{}) {
notification := newErrorNotification(client, appErr, info)
err := client.Notify(notification)
if err != nil {
log.Println(err)
}
}
func NotifyPanic(panicErr interface{}) {
switch v := panicErr.(type) {
case error:
Notify(v)
case string:
Notify(errors.New(v))
default:
Notify(errors.New(fmt.Sprint(panicErr)))
}
}
func NotifyPanicInfo(panicErr interface{}, info map[string]interface{}) {
switch v := panicErr.(type) {
case error:
NotifyInfo(v, info)
case string:
NotifyInfo(errors.New(v), info)
default:
NotifyInfo(errors.New(fmt.Sprint(panicErr)), info)
}
}
func NotifyHTTP(appErr error, request http.Request) {
info := make(map[string]interface{})
NotifyHTTPInfo(appErr, request, info)
}
func NotifyHTTPInfo(appErr error, request http.Request, info map[string]interface{}) {
// we have to copy the values out of the
// map because we're about to mutate the map
// and we don't want to mutate user provided data
newInfo := make(map[string]interface{})
formErr := request.ParseForm()
if formErr != nil {
newInfo["Params"] = request.Form
}
newInfo["Cookies"] = getCookies(request)
newInfo["url"] = request.URL
requestInfo := make(map[string]interface{})
requestInfo["request-method"] = request.Method
if len(request.Header["User-Agent"]) != 0 {
requestInfo["user-agent"] = request.Header["User-Agent"][0]
}
if len(request.Header["Referer"]) != 0 {
requestInfo["referrer"] = request.Header["Referer"][0]
}
newInfo["http-request"] = requestInfo
for k, v := range info {
newInfo[k] = v
}
NotifyInfo(appErr, newInfo)
}
func getCookies(request http.Request) map[string]interface{} {
cookies := make(map[string]interface{})
for _, cookie := range request.Cookies() {
cookies[cookie.Name] = cookie.Value
}
return cookies
}
func (f StackFrame) MarshalJSON() ([]byte, error) {
fields := []interface{}{f.Filename, f.LineNumber, f.FunctionName, f.Options}
return json.Marshal(fields)
}
func newErrorNotification(client *Client, appErr error, info map[string]interface{}) *ErrorNotification {
if info == nil {
info = make(map[string]interface{})
}
newErr := &ErrorNotification{
Type: "error",
Message: appErr.Error(),
StackTrace: applicationStackTrace(client),
Host: applicationHostname(),
Environment: client.Environment,
CustomData: info,
ClientVersion: client.Version,
}
url, ok := info["url"].(string)
if ok {
newErr.Url = url
}
return newErr
}
func applicationHostname() string {
hostname, _ := os.Hostname()
return hostname
}
func applicationStackTrace(client *Client) (stackTrace []StackFrame) {
goroot := runtime.GOROOT()
for i := 1; i <= MAX_STACK_DEPTH+1; i++ {
pc, file, line, ok := runtime.Caller(i)
if !ok {
break
}
// Ignore all stack frames coming from this package
if strings.Contains(file, "github.com/yeller/yeller-golang") {
continue
}
opts := make(map[string]interface{})
if client.ProjectRoot != "" && strings.HasPrefix(file, client.ProjectRoot) {
opts["in-app"] = true
}
file = strings.Replace(file, goroot, "", -1)
frame := StackFrame{
Filename: file,
LineNumber: strconv.Itoa(line),
FunctionName: functionName(pc),
Options: opts,
}
stackTrace = append(stackTrace, frame)
}
return stackTrace
}
func functionName(pc uintptr) string {
fn := runtime.FuncForPC(pc)
if fn == nil {
return "???"
}
return fn.Name()
}