-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbugsnag.go
168 lines (141 loc) · 3.51 KB
/
bugsnag.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
package bugsnack
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"reflect"
"strconv"
"github.com/pkg/errors"
)
const clientVersion = "0.0.3"
// BugsnagReporter is an implementation of ErrorReporter that fires to
// BugSnag
type BugsnagReporter struct {
Doer Doer
APIKey string
ReleaseStage string
Backup ErrorReporter
}
type BugsnagMetadata struct {
ErrorClass string
Context string
GroupingHash string
Severity string
EventMetadata *map[string]interface{}
}
func (metadata *BugsnagMetadata) populateMetadata(err error) {
if metadata.ErrorClass == "" {
metadata.ErrorClass = reflect.TypeOf(err).String()
}
if metadata.Severity == "" {
metadata.Severity = "error"
}
}
func (er *BugsnagReporter) Report(ctx context.Context, newErr error, meta ...interface{}) {
newErr = errors.WithStack(newErr)
metadata := &BugsnagMetadata{}
if len(meta) > 0 {
metadata = meta[0].(*BugsnagMetadata)
}
payload := er.newPayload(newErr, metadata)
var b bytes.Buffer
err := json.NewEncoder(&b).Encode(payload)
if err != nil {
er.Backup.Report(ctx, err)
return
}
req, err := http.NewRequest(http.MethodPost, "https://notify.bugsnag.com", &b)
if err != nil {
er.Backup.Report(ctx, err)
return
}
req = req.WithContext(ctx)
req.Header.Set("Content-Type", "application/json")
resp, err := er.Doer.Do(req)
if err != nil {
er.Backup.Report(ctx, err)
return
}
defer func() {
_, err = io.Copy(ioutil.Discard, io.LimitReader(resp.Body, 1024))
if err != nil {
er.Backup.Report(ctx, err)
}
err = resp.Body.Close()
if err != nil {
er.Backup.Report(ctx, err)
}
}()
if resp.StatusCode != http.StatusOK {
er.Backup.Report(ctx, errors.New("could not report to bugsnag"))
return
}
}
func (er *BugsnagReporter) newPayload(err error, metadata *BugsnagMetadata) *map[string]interface{} {
metadata.populateMetadata(err)
return &map[string]interface{}{
"apiKey": er.APIKey,
"notifier": &map[string]interface{}{
"name": "Bugsnack/Bugsnag",
"url": "https://github.com/fromatob/bugsnack",
"version": clientVersion,
},
"events": []*map[string]interface{}{
er.newEvent(err, metadata),
},
}
}
func (er *BugsnagReporter) newEvent(err error, metadata *BugsnagMetadata) *map[string]interface{} {
type stackTracer interface {
StackTrace() errors.StackTrace
}
host, _ := os.Hostname()
stacktrace := err.(stackTracer).StackTrace()[1:]
event := map[string]interface{}{
"PayloadVersion": "2",
"exceptions": []*map[string]interface{}{
{
"errorClass": metadata.ErrorClass,
"message": err.Error(),
"stacktrace": formatStack(stacktrace),
},
},
"severity": metadata.Severity,
"app": &map[string]interface{}{
"releaseStage": er.ReleaseStage,
},
"device": &map[string]interface{}{
"hostname": host,
},
}
if "" != metadata.GroupingHash {
event["groupingHash"] = metadata.GroupingHash
}
if "" != metadata.Context {
event["context"] = metadata.Context
}
if !IsZeroInterface(metadata.EventMetadata) {
event["metaData"] = metadata.EventMetadata
}
return &event
}
func IsZeroInterface(i interface{}) bool {
return i == reflect.Zero(reflect.TypeOf(i)).Interface()
}
func formatStack(s errors.StackTrace) []map[string]interface{} {
var o []map[string]interface{}
for _, f := range s {
line, _ := strconv.Atoi(fmt.Sprintf("%d", f))
o = append(o, map[string]interface{}{
"method": fmt.Sprintf("%n", f),
"file": fmt.Sprintf("%s", f),
"lineNumber": line,
})
}
return o
}