forked from imgproxy/imgproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_reporting.go
75 lines (62 loc) · 1.5 KB
/
errors_reporting.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
package main
import (
"net/http"
"strings"
"time"
"github.com/bugsnag/bugsnag-go"
"github.com/getsentry/sentry-go"
"github.com/honeybadger-io/honeybadger-go"
)
var (
bugsnagEnabled bool
honeybadgerEnabled bool
sentryEnabled bool
headersReplacer = strings.NewReplacer("-", "_")
sentryTimeout = 5 * time.Second
)
func initErrorsReporting() {
if len(conf.BugsnagKey) > 0 {
bugsnag.Configure(bugsnag.Configuration{
APIKey: conf.BugsnagKey,
ReleaseStage: conf.BugsnagStage,
})
bugsnagEnabled = true
}
if len(conf.HoneybadgerKey) > 0 {
honeybadger.Configure(honeybadger.Configuration{
APIKey: conf.HoneybadgerKey,
Env: conf.HoneybadgerEnv,
})
honeybadgerEnabled = true
}
if len(conf.SentryDSN) > 0 {
sentry.Init(sentry.ClientOptions{
Dsn: conf.SentryDSN,
Release: conf.SentryRelease,
Environment: conf.SentryEnvironment,
})
sentryEnabled = true
}
}
func reportError(err error, req *http.Request) {
if bugsnagEnabled {
bugsnag.Notify(err, req)
}
if honeybadgerEnabled {
headers := make(honeybadger.CGIData)
for k, v := range req.Header {
key := "HTTP_" + headersReplacer.Replace(strings.ToUpper(k))
headers[key] = v[0]
}
honeybadger.Notify(err, req.URL, headers)
}
if sentryEnabled {
hub := sentry.CurrentHub().Clone()
hub.Scope().SetRequest(sentry.Request{}.FromHTTPRequest(req))
hub.Scope().SetLevel(sentry.LevelError)
eventID := hub.CaptureException(err)
if eventID != nil {
hub.Flush(sentryTimeout)
}
}
}