-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsentry.go
152 lines (130 loc) · 3.08 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
package zapsentry
import (
raven "github.com/getsentry/raven-go"
"go.uber.org/zap/zapcore"
)
const (
_platform = "go"
_traceContextLines = 3
_traceSkipFrames = 2
)
func ravenSeverity(lvl zapcore.Level) raven.Severity {
switch lvl {
case zapcore.DebugLevel:
return raven.INFO
case zapcore.InfoLevel:
return raven.INFO
case zapcore.WarnLevel:
return raven.WARNING
case zapcore.ErrorLevel:
return raven.ERROR
case zapcore.DPanicLevel:
return raven.FATAL
case zapcore.PanicLevel:
return raven.FATAL
case zapcore.FatalLevel:
return raven.FATAL
default:
// Unrecognized levels are fatal.
return raven.FATAL
}
}
type client interface {
Capture(*raven.Packet, map[string]string) (string, chan error)
Wait()
}
// Configuration is a minimal set of parameters for Sentry integration.
type Configuration struct {
DSN string `yaml:"DSN"`
Tags map[string]string
Trace trace
Release string
}
type trace struct {
Disabled bool
}
// Build uses the provided configuration to construct a Sentry-backed logging
// core.
func (c Configuration) Build() (zapcore.Core, error) {
client, err := raven.New(c.DSN)
if err != nil {
return zapcore.NewNopCore(), err
}
client.SetRelease(c.Release)
return newCore(c, client, zapcore.ErrorLevel), nil
}
type core struct {
client
zapcore.LevelEnabler
trace
fields map[string]interface{}
tags map[string]string
}
func newCore(cfg Configuration, c client, enab zapcore.LevelEnabler) *core {
sentryCore := &core{
client: c,
LevelEnabler: enab,
trace: cfg.Trace,
fields: make(map[string]interface{}),
tags: cfg.Tags,
}
return sentryCore
}
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.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)
packet := &raven.Packet{
Message: ent.Message,
Timestamp: raven.Timestamp(ent.Time),
Level: ravenSeverity(ent.Level),
Platform: _platform,
Extra: clone.fields,
Fingerprint: []string{ent.Message},
}
if !c.trace.Disabled {
trace := raven.NewStacktrace(_traceSkipFrames, _traceContextLines, nil /* app prefixes */)
if trace != nil {
packet.Interfaces = append(packet.Interfaces, trace)
}
}
_, _ = c.Capture(packet, c.tags)
// We may be crashing the program, so should flush any buffered events.
if ent.Level > zapcore.ErrorLevel {
c.Wait()
}
return nil
}
func (c *core) Sync() error {
c.client.Wait()
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,
LevelEnabler: c.LevelEnabler,
trace: c.trace,
fields: m,
}
}