-
Notifications
You must be signed in to change notification settings - Fork 13
/
sentry.go
180 lines (155 loc) · 3.89 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// a core for sentry capture error
package logging
import (
"fmt"
"syscall"
"time"
"github.com/getsentry/sentry-go"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func sentryLevel(lvl zapcore.Level) sentry.Level {
switch lvl {
case zapcore.DebugLevel:
return sentry.LevelDebug
case zapcore.InfoLevel:
return sentry.LevelInfo
case zapcore.WarnLevel:
return sentry.LevelWarning
case zapcore.ErrorLevel:
return sentry.LevelError
case zapcore.DPanicLevel:
return sentry.LevelFatal
case zapcore.PanicLevel:
return sentry.LevelFatal
case zapcore.FatalLevel:
return sentry.LevelFatal
default:
return sentry.LevelFatal
}
}
// SentryCoreConfig is a minimal set of parameters for Sentry Core.
type SentryCoreConfig struct {
Tags map[string]string
DisableStacktrace bool
Level zapcore.Level
FlushTimeout time.Duration
Hub *sentry.Hub
}
// sentryCore the core for sentry
type sentryCore struct {
client *sentry.Client
cfg *SentryCoreConfig
zapcore.LevelEnabler
flushTimeout time.Duration
fields map[string]interface{}
}
// GetSentryClient return sentry client
func (c *sentryCore) GetSentryClient() *sentry.Client {
return c.client
}
func (c *sentryCore) with(fs []zapcore.Field) *sentryCore {
// Copy our map.
m := make(map[string]interface{}, len(c.fields))
for k, v := range c.fields {
m[k] = v
}
// Add fields to an in-memory encoder.
enc := zapcore.NewMapObjectEncoder()
for _, f := range fs {
f.AddTo(enc)
}
// Merge the two maps.
for k, v := range enc.Fields {
m[k] = v
}
return &sentryCore{
client: c.client,
cfg: c.cfg,
fields: m,
LevelEnabler: c.LevelEnabler,
}
}
// With zap core interface
func (c *sentryCore) With(fs []zapcore.Field) zapcore.Core {
return c.with(fs)
}
// Check zap core interface
func (c *sentryCore) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
if c.cfg.Level.Enabled(ent.Level) {
return ce.AddCore(ent, c)
}
return ce
}
// Write zap core interface
func (c *sentryCore) Write(ent zapcore.Entry, fs []zapcore.Field) error {
clone := c.with(fs)
event := sentry.NewEvent()
event.Message = ent.Message
event.Timestamp = ent.Time
event.Level = sentryLevel(ent.Level)
event.Platform = "go"
event.Extra = clone.fields
event.Tags = c.cfg.Tags
if !c.cfg.DisableStacktrace {
trace := sentry.NewStacktrace()
if trace != nil {
event.Exception = []sentry.Exception{{
Type: ent.Message,
Value: ent.Caller.TrimmedPath(),
Stacktrace: trace,
}}
}
}
hub := c.cfg.Hub
if hub == nil {
hub = sentry.CurrentHub()
}
_ = c.client.CaptureEvent(event, nil, hub.Scope())
// We may be crashing the program, so should flush any buffered events.
if ent.Level > zapcore.ErrorLevel {
c.client.Flush(c.flushTimeout)
}
return nil
}
// Sync zap core interface
func (c *sentryCore) Sync() error {
c.client.Flush(c.flushTimeout)
return nil
}
// NewSentryCore new a sentry core
func NewSentryCore(cfg SentryCoreConfig, sentryClient *sentry.Client) zapcore.Core {
core := sentryCore{
client: sentryClient,
cfg: &cfg,
LevelEnabler: cfg.Level,
flushTimeout: 3 * time.Second,
fields: make(map[string]interface{}),
}
if cfg.FlushTimeout > 0 {
core.flushTimeout = cfg.FlushTimeout
}
return &core
}
// NewSentryClient return sentry client by sentrydsn
func NewSentryClient(options sentry.ClientOptions) (*sentry.Client, error) {
client, err := sentry.NewClient(options)
if err != nil {
return nil, err
}
// init sentry hub
sentry.Init(options)
return client, nil
}
// SentryAttach attach sentrycore
func SentryAttach(l *zap.Logger, sentryClient *sentry.Client) *zap.Logger {
cfg := SentryCoreConfig{
Level: zap.ErrorLevel,
Tags: map[string]string{
"pid": fmt.Sprint(syscall.Getpid()),
"server_ip": ServerIP(),
},
}
core := NewSentryCore(cfg, sentryClient)
return AttachCore(l, core)
}