forked from TheZeroSlave/zapsentry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.go
150 lines (126 loc) · 3.28 KB
/
core.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
package zapsentry
import (
"strings"
"time"
"github.com/getsentry/sentry-go"
"go.uber.org/zap/zapcore"
)
// NewCore creates new zapcore object
func NewCore(cfg Configuration, factory SentryClientFactory) (zapcore.Core, error) {
client, err := factory()
if err != nil {
return zapcore.NewNopCore(), err
}
core := core{
client: client,
cfg: &cfg,
LevelEnabler: cfg.Level,
flushTimeout: 5 * time.Second,
fields: make(map[string]interface{}),
}
if cfg.FlushTimeout > 0 {
core.flushTimeout = cfg.FlushTimeout
}
return &core, nil
}
func (c *core) With(fs []zapcore.Field) zapcore.Core {
return c.with(fs)
}
func (c *core) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
if c.cfg.Level.Enabled(ent.Level) {
return ce.AddCore(ent, c)
}
return ce
}
func (c *core) 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 = sentrySeverity(ent.Level)
event.Platform = "Golang"
event.Extra = clone.fields
event.Tags = c.cfg.Tags
if !c.cfg.DisableStacktrace {
trace := sentry.NewStacktrace()
if trace != nil {
trace.Frames = filterFrames(trace.Frames)
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
}
func (c *core) Sync() error {
c.client.Flush(c.flushTimeout)
return nil
}
func (c *core) with(fs []zapcore.Field) *core {
// 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 &core{
client: c.client,
cfg: c.cfg,
flushTimeout: c.flushTimeout,
fields: m,
LevelEnabler: c.LevelEnabler,
}
}
// ClientGetter interface getter for sentry client
type ClientGetter interface {
GetClient() *sentry.Client
}
func (c *core) GetClient() *sentry.Client {
return c.client
}
type core struct {
client *sentry.Client
cfg *Configuration
zapcore.LevelEnabler
flushTimeout time.Duration
fields map[string]interface{}
}
// follow same logic with sentry-go to filter unnecessary frames
// ref:
// https://github.com/getsentry/sentry-go/blob/362a80dcc41f9ad11c8df556104db3efa27a419e/stacktrace.go#L256-L280
func filterFrames(frames []sentry.Frame) []sentry.Frame {
if len(frames) == 0 {
return nil
}
filteredFrames := make([]sentry.Frame, 0, len(frames))
for i := range frames {
// Skip zapsentry and zap internal frames, except for frames in _test packages (for
// testing).
if (strings.HasPrefix(frames[i].Module, "github.com/TheZeroSlave/zapsentry") ||
strings.HasPrefix(frames[i].Function, "go.uber.org/zap")) &&
!strings.HasSuffix(frames[i].Module, "_test") {
continue
}
filteredFrames = append(filteredFrames, frames[i])
}
return filteredFrames
}