1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-01-08 10:45:04 +02:00
imgproxy/errors_reporting.go

99 lines
2.0 KiB
Go
Raw Normal View History

2018-11-14 15:41:16 +02:00
package main
import (
"net/http"
"strings"
2019-08-19 14:05:57 +02:00
"time"
2018-11-14 15:41:16 +02:00
"github.com/airbrake/gobrake/v5"
2021-04-26 14:20:05 +02:00
"github.com/bugsnag/bugsnag-go/v2"
2019-08-19 14:05:57 +02:00
"github.com/getsentry/sentry-go"
2018-11-14 15:41:16 +02:00
"github.com/honeybadger-io/honeybadger-go"
)
var (
bugsnagEnabled bool
honeybadgerEnabled bool
sentryEnabled bool
airbrakeEnabled bool
airbrake *gobrake.Notifier
2018-11-14 15:41:16 +02:00
headersReplacer = strings.NewReplacer("-", "_")
2019-08-19 14:05:57 +02:00
sentryTimeout = 5 * time.Second
2018-11-14 15:41:16 +02:00
)
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 {
2019-08-19 14:05:57 +02:00
sentry.Init(sentry.ClientOptions{
Dsn: conf.SentryDSN,
Release: conf.SentryRelease,
Environment: conf.SentryEnvironment,
})
sentryEnabled = true
}
if len(conf.AirbrakeProjecKey) > 0 {
airbrake = gobrake.NewNotifierWithOptions(&gobrake.NotifierOptions{
2021-05-17 14:27:04 +02:00
ProjectId: int64(conf.AirbrakeProjecID),
ProjectKey: conf.AirbrakeProjecKey,
Environment: conf.AirbrakeEnv,
})
airbrakeEnabled = true
}
}
func closeErrorsReporting() {
2021-05-17 14:27:04 +02:00
if airbrake != nil {
airbrake.Close()
}
2018-11-14 15:41:16 +02:00
}
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 {
2019-08-19 14:05:57 +02:00
hub := sentry.CurrentHub().Clone()
2020-06-09 18:07:48 +02:00
hub.Scope().SetRequest(req)
2019-08-19 14:05:57 +02:00
hub.Scope().SetLevel(sentry.LevelError)
eventID := hub.CaptureException(err)
if eventID != nil {
hub.Flush(sentryTimeout)
}
}
if airbrakeEnabled {
airbrake.Notify(err, req)
}
2018-11-14 15:41:16 +02:00
}