/
notice.go
175 lines (155 loc) · 3.6 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
package honeybadger
import (
"encoding/json"
"net/url"
"os"
"regexp"
"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
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,
"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 Fingerprint:
notice.Fingerprint = t.String()
case Params:
notice.Params = t
case CGIData:
notice.CGIData = t
case url.URL:
notice.URL = t.String()
}
}
return ¬ice
}