-
Notifications
You must be signed in to change notification settings - Fork 16
/
notice.go
209 lines (185 loc) · 4.44 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
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
package honeybadger
import (
"encoding/json"
"net/http"
"net/url"
"os"
"regexp"
"strings"
"time"
"github.com/pborman/uuid"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
)
// ErrorClass represents the class name of the error which is sent to
// Honeybadger.
type ErrorClass struct {
Name string
}
// Fingerprint represents the fingerprint of the error, which controls grouping
// in Honeybadger.
type Fingerprint struct {
Content string
}
func (f *Fingerprint) String() string {
return f.Content
}
// Notice is a representation of the error which is sent to Honeybadger, and
// implements the Payload interface.
type Notice struct {
APIKey string
Error Error
Token string
ErrorMessage string
ErrorClass string
Tags []string
Hostname string
Env string
Backtrace []*Frame
ProjectRoot string
Context Context
Params Params
CGIData CGIData
URL string
Fingerprint string
}
func (n *Notice) asJSON() *hash {
return &hash{
"api_key": n.APIKey,
"notifier": &hash{
"name": "honeybadger",
"url": "https://github.com/honeybadger-io/honeybadger-go",
"version": VERSION,
},
"error": &hash{
"token": n.Token,
"message": n.ErrorMessage,
"class": n.ErrorClass,
"tags": n.Tags,
"backtrace": n.Backtrace,
"fingerprint": n.Fingerprint,
},
"request": &hash{
"context": n.Context,
"params": n.Params,
"cgi_data": n.CGIData,
"url": n.URL,
},
"server": &hash{
"project_root": n.ProjectRoot,
"environment_name": n.Env,
"hostname": n.Hostname,
"time": time.Now().UTC(),
"pid": os.Getpid(),
"stats": getStats(),
},
}
}
func bytesToKB(bytes uint64) float64 {
return float64(bytes) / 1024.0
}
func getStats() *hash {
var m, l *hash
if stat, err := mem.VirtualMemory(); err == nil {
m = &hash{
"total": bytesToKB(stat.Total),
"free": bytesToKB(stat.Free),
"buffers": bytesToKB(stat.Buffers),
"cached": bytesToKB(stat.Cached),
"free_total": bytesToKB(stat.Free + stat.Buffers + stat.Cached),
}
}
if stat, err := load.Avg(); err == nil {
l = &hash{
"one": stat.Load1,
"five": stat.Load5,
"fifteen": stat.Load15,
}
}
return &hash{"mem": m, "load": l}
}
func (n *Notice) toJSON() []byte {
out, err := json.Marshal(n.asJSON())
if err == nil {
return out
}
panic(err)
}
func (n *Notice) setContext(context Context) {
n.Context.Update(context)
}
func composeStack(stack []*Frame, root string) (frames []*Frame) {
if root == "" {
return stack
}
re, err := regexp.Compile("^" + regexp.QuoteMeta(root))
if err != nil {
return stack
}
for _, frame := range stack {
file := re.ReplaceAllString(frame.File, "[PROJECT_ROOT]")
frames = append(frames, &Frame{
File: file,
Number: frame.Number,
Method: frame.Method,
})
}
return
}
func newNotice(config *Configuration, err Error, extra ...interface{}) *Notice {
notice := Notice{
APIKey: config.APIKey,
Error: err,
Token: uuid.NewRandom().String(),
ErrorMessage: err.Message,
ErrorClass: err.Class,
Env: config.Env,
Hostname: config.Hostname,
Backtrace: composeStack(err.Stack, config.Root),
ProjectRoot: config.Root,
Context: Context{},
}
for _, thing := range extra {
switch t := thing.(type) {
case Context:
notice.setContext(t)
case ErrorClass:
notice.ErrorClass = t.Name
case Tags:
for _, tag := range t {
notice.Tags = append(notice.Tags, tag)
}
case Fingerprint:
notice.Fingerprint = t.String()
case Params:
notice.Params = t
case CGIData:
notice.CGIData = t
case url.URL:
notice.URL = t.String()
case *http.Request:
setHttpRequest(¬ice, t)
}
}
return ¬ice
}
func setHttpRequest(notice *Notice, r *http.Request) {
if r == nil {
return
}
notice.URL = r.URL.String()
notice.CGIData = CGIData{}
replacer := strings.NewReplacer("-", "_")
for k, v := range r.Header {
key := "HTTP_" + replacer.Replace(strings.ToUpper(k))
notice.CGIData[key] = v[0]
}
// Form is only populated if ParseForm() is called on the request and it
// will include URL query parameters. So if it's empty, then it's possible
// that ParseForm wasn't called, and we will miss reporting URL params.
if form := r.Form; len(form) == 0 {
notice.Params = Params(r.URL.Query())
} else {
notice.Params = Params(form)
}
}