-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistrator.go
78 lines (61 loc) · 1.56 KB
/
registrator.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
package reporting
import (
sdk "github.com/getsentry/sentry-go"
"github.com/imdario/mergo"
"github.com/Vinelab/go-reporting/sentry"
log "github.com/sirupsen/logrus"
)
type services struct {
Sentry string
}
//services supported by module
var Services = services{
Sentry: "sentry",
}
//panic loggers list for each supported service
var panicLoggers = map[string]func(err interface{}){
Services.Sentry: sentry.LogPanic,
}
//list of registered services for logging
var registeredServices = make(map[string]bool)
//register sentry hook for logrus
func RegisterSentry(level log.Level, config sentry.Options, injectors ...sentry.Injector) error {
sentry.InitDefaultConfig()
//merge config with defaults
err := mergo.Merge(&config, &sentry.DefaultConfig)
if err != nil {
return err
}
setupSentryInjectors(&config, injectors)
//init sentry client
err = sdk.Init(sdk.ClientOptions(config))
if err != nil {
return err
}
//register hook for logrus
hook := sentry.NewHook(level)
log.AddHook(hook)
//add to registered services list
registeredServices[Services.Sentry] = true
return nil
}
//log panic error for all registered services and repanic
func LogPanic() {
err := recover()
if err != nil {
//log panic
for service := range registeredServices {
panicLoggers[service](err)
}
//repanic
panic(err)
}
}
func setupSentryInjectors(config *sentry.Options, injectors []sentry.Injector) {
config.BeforeSend = func(event *sdk.Event, hint *sdk.EventHint) *sdk.Event {
for _, injector := range injectors {
injector.Inject(event)
}
return event
}
}