-
Notifications
You must be signed in to change notification settings - Fork 3
/
sinks_sentry.go
68 lines (55 loc) · 1.94 KB
/
sinks_sentry.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
package log
import (
"github.com/getsentry/sentry-go"
"github.com/google/go-cmp/cmp"
"go.uber.org/zap/zapcore"
"github.com/sourcegraph/log/internal/sinkcores/sentrycore"
)
// SentrySink reports all warning-level and above log messages that contain an error field
// (via the `log.Error(err)` or `log.NamedError(name, err)` field constructors) to Sentry,
// complete with stacktrace data and any additional context logged in the corresponding
// log message (including anything accumulated on a sub-logger).
type SentrySink struct {
// ClientOptions expose various options to configure the Sentry client
sentry.ClientOptions
}
type sentrySink struct {
SentrySink
core *sentrycore.Core
}
// NewSentrySink instantiates a Sentry sink to provide to `log.Init` with the following default values:
// - SampleRate: 0.1
// To provide different values see `NewSentrySinkWith`
func NewSentrySink() Sink {
return &sentrySink{SentrySink: SentrySink{sentrycore.DefaultSentryClientOptions}}
}
// NewSentrySinkWith instantiates a Sentry sink to provide to `log.Init` with the values provided in SentrySink.
func NewSentrySinkWith(s SentrySink) Sink {
return &sentrySink{SentrySink: SentrySink{s.ClientOptions}}
}
func (s *sentrySink) Name() string { return "SentrySink" }
func (s *sentrySink) build() (zapcore.Core, error) {
client, err := sentry.NewClient(s.ClientOptions)
if err != nil {
return nil, err
}
s.core = sentrycore.NewCore(sentry.NewHub(client, sentry.NewScope()))
return s.core, nil
}
func (s *sentrySink) update(updated SinksConfig) error {
if updated.Sentry == nil {
// use zero-value, effectively disabling sentry next
updated.Sentry = &SentrySink{}
}
if cmp.Equal(s.ClientOptions, updated.Sentry.ClientOptions) {
return nil
}
s.ClientOptions = updated.Sentry.ClientOptions
client, err := sentry.NewClient(s.ClientOptions)
if err != nil {
return err
}
// Do sentry setup
s.core.SetHub(sentry.NewHub(client, sentry.NewScope()))
return nil
}