forked from bugsnag/bugsnag-go
/
payload.go
134 lines (117 loc) · 3.03 KB
/
payload.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
package bugsnag
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"runtime"
"sync"
"time"
"github.com/bugsnag/bugsnag-go/device"
"github.com/bugsnag/bugsnag-go/headers"
"github.com/bugsnag/bugsnag-go/sessions"
)
const notifyPayloadVersion = "4"
var sessionMutex sync.Mutex
type payload struct {
*Event
*Configuration
}
type hash map[string]interface{}
func (p *payload) deliver() error {
if len(p.APIKey) != 32 {
return fmt.Errorf("bugsnag/payload.deliver: invalid api key: '%s'", p.APIKey)
}
buf, err := p.MarshalJSON()
if err != nil {
return fmt.Errorf("bugsnag/payload.deliver: %v", err)
}
client := http.Client{
Transport: p.Transport,
}
req, err := http.NewRequest("POST", p.Endpoints.Notify, bytes.NewBuffer(buf))
if err != nil {
return fmt.Errorf("bugsnag/payload.deliver unable to create request: %v", err)
}
for k, v := range headers.PrefixedHeaders(p.APIKey, notifyPayloadVersion) {
req.Header.Add(k, v)
}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("bugsnag/payload.deliver: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("bugsnag/payload.deliver: Got HTTP %s", resp.Status)
}
return nil
}
func (p *payload) MarshalJSON() ([]byte, error) {
return json.Marshal(reportJSON{
APIKey: p.APIKey,
Events: []eventJSON{
eventJSON{
App: &appJSON{
ReleaseStage: p.ReleaseStage,
Type: p.AppType,
Version: p.AppVersion,
},
Context: p.Context,
Device: &deviceJSON{
Hostname: p.Hostname,
OsName: runtime.GOOS,
RuntimeVersions: device.GetRuntimeVersions(),
},
Request: p.Request,
Exceptions: []exceptionJSON{
exceptionJSON{
ErrorClass: p.ErrorClass,
Message: p.Message,
Stacktrace: p.Stacktrace,
},
},
GroupingHash: p.GroupingHash,
Metadata: p.MetaData.sanitize(p.ParamsFilters),
PayloadVersion: notifyPayloadVersion,
Session: p.makeSession(),
Severity: p.Severity.String,
SeverityReason: p.severityReasonPayload(),
Unhandled: p.handledState.Unhandled,
User: p.User,
},
},
Notifier: notifierJSON{
Name: "Bugsnag Go",
URL: "https://github.com/bugsnag/bugsnag-go",
Version: VERSION,
},
})
}
func (p *payload) makeSession() *sessionJSON {
// If a context has not been applied to the payload then assume that no
// session has started either
if p.Ctx == nil {
return nil
}
sessionMutex.Lock()
defer sessionMutex.Unlock()
session := sessions.IncrementEventCountAndGetSession(p.Ctx, p.handledState.Unhandled)
if session != nil {
s := *session
return &sessionJSON{
ID: s.ID,
StartedAt: s.StartedAt.UTC().Format(time.RFC3339),
Events: sessions.EventCounts{
Handled: s.EventCounts.Handled,
Unhandled: s.EventCounts.Unhandled,
},
}
}
return nil
}
func (p *payload) severityReasonPayload() *severityReasonJSON {
if reason := p.handledState.SeverityReason; reason != "" {
return &severityReasonJSON{Type: reason}
}
return nil
}