forked from airbrake/gobrake
/
notifier.go
335 lines (278 loc) · 7.45 KB
/
notifier.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package gobrake
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"os"
"regexp"
"runtime"
"strconv"
"sync"
"sync/atomic"
"time"
)
const waitTimeout = 5 * time.Second
const httpEnhanceYourCalm = 420
const httpStatusTooManyRequests = 429
const maxNoticeLen = 64 * 1024
var (
errClosed = errors.New("gobrake: notifier is closed")
errQueueFull = errors.New("gobrake: queue is full (error is dropped)")
errUnauthorized = errors.New("gobrake: unauthorized: invalid project id or key")
errAccountRateLimited = errors.New("gobrake: account is rate limited")
errIPRateLimited = errors.New("gobrake: IP is rate limited")
errNoticeTooBig = errors.New("gobrake: notice exceeds 64KB max size limit")
)
var (
httpClientOnce sync.Once
httpClient *http.Client
)
func defaultHTTPClient() *http.Client {
httpClientOnce.Do(func() {
httpClient = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 15 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
TLSClientConfig: &tls.Config{
ClientSessionCache: tls.NewLRUClientSessionCache(1024),
},
MaxIdleConnsPerHost: 10,
ResponseHeaderTimeout: 10 * time.Second,
},
Timeout: 10 * time.Second,
}
})
return httpClient
}
var buffers = sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
type filter func(*Notice) *Notice
type NotifierOptions struct {
// Airbrake project id.
ProjectId int64
// Airbrake project key.
ProjectKey string
// Airbrake host name. Default is https://airbrake.io.
Host string
// Environment such as production or development.
Environment string
// Git revision. Default is SOURCE_VERSION on Heroku.
Revision string
// List of keys containing sensitive information that must be filtered out.
// Default is password, secret.
KeysBlacklist []interface{}
// http.Client that is used to interact with Airbrake API.
HTTPClient *http.Client
}
func (opt *NotifierOptions) init() {
if opt.Host == "" {
opt.Host = "https://api.airbrake.io"
}
if opt.Revision == "" {
// https://devcenter.heroku.com/changelog-items/630
opt.Revision = os.Getenv("SOURCE_VERSION")
}
if opt.KeysBlacklist == nil {
opt.KeysBlacklist = []interface{}{
regexp.MustCompile("password"),
regexp.MustCompile("secret"),
}
}
if opt.HTTPClient == nil {
opt.HTTPClient = defaultHTTPClient()
}
}
type Notifier struct {
opt *NotifierOptions
createNoticeURL string
filters []filter
inFlight int32 // atomic
limit chan struct{}
wg sync.WaitGroup
routes *routeStats
rateLimitReset uint32 // atomic
_closed uint32 // atomic
}
func NewNotifierWithOptions(opt *NotifierOptions) *Notifier {
opt.init()
n := &Notifier{
opt: opt,
createNoticeURL: fmt.Sprintf("%s/api/v3/projects/%d/notices",
opt.Host, opt.ProjectId),
limit: make(chan struct{}, 2*runtime.NumCPU()),
routes: newRouteStats(opt),
}
n.AddFilter(newNotifierFilter(n))
n.AddFilter(gopathFilter)
n.AddFilter(gitFilter)
if len(opt.KeysBlacklist) > 0 {
n.AddFilter(NewBlacklistKeysFilter(opt.KeysBlacklist...))
}
return n
}
func NewNotifier(projectId int64, projectKey string) *Notifier {
return NewNotifierWithOptions(&NotifierOptions{
ProjectId: projectId,
ProjectKey: projectKey,
})
}
// AddFilter adds filter that can change notice or ignore it by returning nil.
func (n *Notifier) AddFilter(fn func(*Notice) *Notice) {
n.filters = append(n.filters, fn)
}
// Notify notifies Airbrake about the error.
func (n *Notifier) Notify(e interface{}, req *http.Request) {
notice := n.Notice(e, req, 1)
n.SendNoticeAsync(notice)
}
// Notice returns Aibrake notice created from error and request. depth
// determines which call frame to use when constructing backtrace.
func (n *Notifier) Notice(err interface{}, req *http.Request, depth int) *Notice {
return NewNotice(err, req, depth+3)
}
type sendResponse struct {
Id string `json:"id"`
}
// SendNotice sends notice to Airbrake.
func (n *Notifier) SendNotice(notice *Notice) (string, error) {
if n.closed() {
return "", errClosed
}
for _, fn := range n.filters {
notice = fn(notice)
if notice == nil {
// Notice is ignored.
return "", nil
}
}
if time.Now().Unix() < int64(atomic.LoadUint32(&n.rateLimitReset)) {
return "", errIPRateLimited
}
buf := buffers.Get().(*bytes.Buffer)
defer buffers.Put(buf)
buf.Reset()
err := json.NewEncoder(buf).Encode(notice)
if err != nil {
return "", err
}
if buf.Len() > maxNoticeLen {
return "", errNoticeTooBig
}
req, err := http.NewRequest("POST", n.createNoticeURL, buf)
if err != nil {
return "", err
}
req.Header.Set("Authorization", "Bearer "+n.opt.ProjectKey)
req.Header.Set("Content-Type", "application/json")
resp, err := n.opt.HTTPClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
buf.Reset()
_, err = buf.ReadFrom(resp.Body)
if err != nil {
return "", err
}
switch resp.StatusCode {
case http.StatusCreated:
var sendResp sendResponse
err = json.NewDecoder(buf).Decode(&sendResp)
if err != nil {
return "", err
}
return sendResp.Id, nil
case http.StatusUnauthorized:
return "", errUnauthorized
case httpStatusTooManyRequests:
delayStr := resp.Header.Get("X-RateLimit-Delay")
delay, err := strconv.ParseInt(delayStr, 10, 64)
if err == nil {
atomic.StoreUint32(&n.rateLimitReset, uint32(time.Now().Unix()+delay))
}
return "", errIPRateLimited
case httpEnhanceYourCalm:
return "", errAccountRateLimited
}
err = fmt.Errorf("got response status=%q, wanted 201 CREATED", resp.Status)
logger.Printf("SendNotice failed reporting notice=%q: %s", notice, err)
return "", err
}
// SendNoticeAsync is like SendNotice, but sends notice asynchronously.
// Pending notices can be flushed with Flush.
func (n *Notifier) SendNoticeAsync(notice *Notice) {
if n.closed() {
notice.Error = errClosed
return
}
inFlight := atomic.AddInt32(&n.inFlight, 1)
if inFlight > 1000 {
atomic.AddInt32(&n.inFlight, -1)
notice.Error = errQueueFull
return
}
n.wg.Add(1)
go func() {
n.limit <- struct{}{}
notice.Id, notice.Error = n.SendNotice(notice)
atomic.AddInt32(&n.inFlight, -1)
n.wg.Done()
<-n.limit
}()
}
// NotifyOnPanic notifies Airbrake about the panic and should be used
// with defer statement.
func (n *Notifier) NotifyOnPanic() {
if v := recover(); v != nil {
notice := n.Notice(v, nil, 3)
notice.Context["severity"] = "critical"
n.SendNotice(notice)
panic(v)
}
}
// Flush waits for pending requests to finish.
func (n *Notifier) Flush() {
n.waitTimeout(waitTimeout)
}
func (n *Notifier) Close() error {
return n.CloseTimeout(waitTimeout)
}
// CloseTimeout waits for pending requests to finish and then closes the notifier.
func (n *Notifier) CloseTimeout(timeout time.Duration) error {
if !atomic.CompareAndSwapUint32(&n._closed, 0, 1) {
return nil
}
return n.waitTimeout(timeout)
}
func (n *Notifier) closed() bool {
return atomic.LoadUint32(&n._closed) == 1
}
func (n *Notifier) waitTimeout(timeout time.Duration) error {
done := make(chan struct{})
go func() {
n.wg.Wait()
close(done)
}()
select {
case <-done:
return nil
case <-time.After(timeout):
return fmt.Errorf("Wait timed out after %s", timeout)
}
}
func (n *Notifier) IncRequest(
method, route string, statusCode int, dur time.Duration, tm time.Time,
) error {
return n.routes.IncRequest(method, route, statusCode, dur, tm)
}