/
sentry.go
85 lines (72 loc) · 2.17 KB
/
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package sentry
import (
"context"
"time"
sentrygo "github.com/getsentry/sentry-go"
"github.com/saiya/dsps/server/config"
"golang.org/x/xerrors"
)
// Sentry integration interface.
type Sentry interface {
// WrapContext makes dedicated Sentry context object("hub") for the context.
// Should create separate "Hub" instances for each requests / background tasks.
// https://docs.sentry.io/platforms/go/concurrency/
WrapContext(ctx context.Context) context.Context
Shutdown(ctx context.Context)
}
type sentry struct {
flushTimeout time.Duration
}
type emptySentry struct{}
// NewSentry initialize Sentry integration.
func NewSentry(config *config.SentryConfig) (Sentry, error) {
if config == nil || config.DSN == "" {
return NewEmptySentry(), nil
}
ignoreErrors := make([]string, len(config.IgnoreErrors))
for i, regex := range config.IgnoreErrors {
ignoreErrors[i] = regex.String()
}
hideRequestData := config.HideRequestData
if err := sentrygo.Init(sentrygo.ClientOptions{
Dsn: config.DSN,
ServerName: config.ServerName,
Environment: config.Environment,
Dist: config.Distribution,
Release: config.Release,
SampleRate: *config.SampleRate,
AttachStacktrace: !config.DisableStacktrace,
BeforeSend: func(event *sentrygo.Event, hint *sentrygo.EventHint) *sentrygo.Event {
if hideRequestData && event.Request != nil {
event.Request.Data = ""
}
return event
},
IgnoreErrors: ignoreErrors,
Debug: false,
}); err != nil {
return nil, xerrors.Errorf("failed to initialize sentry: %w", err)
}
sentrygo.ConfigureScope(func(scope *sentrygo.Scope) {
scope.SetTags(config.Tags)
for key, value := range config.Contexts {
scope.SetContext(key, value)
}
})
return &sentry{
flushTimeout: config.FlushTimeout.Duration,
}, nil
}
// NewEmptySentry creates empty (no-op) Sentry
func NewEmptySentry() Sentry {
return &emptySentry{}
}
func (s *sentry) Shutdown(ctx context.Context) {
sentrygo.Flush(s.flushTimeout)
sentrygo.AddBreadcrumb(&sentrygo.Breadcrumb{
Level: sentrygo.LevelDebug,
})
}
func (s *emptySentry) Shutdown(ctx context.Context) {}
// Shutdown stub implementation
func (s *StubSentry) Shutdown(ctx context.Context) {}