forked from trong/xlog-sentry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.go
243 lines (211 loc) · 6.3 KB
/
output.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package xlogsentry
import (
"fmt"
"net/http"
"os"
"runtime"
"time"
"github.com/getsentry/raven-go"
"github.com/rs/xlog"
)
type locationer interface {
Location() (string, int)
}
type causer interface {
Cause() error
}
var (
xlogSeverityMap = map[string]xlog.Level{
"debug": xlog.LevelDebug,
"info": xlog.LevelInfo,
"warn": xlog.LevelWarn,
"error": xlog.LevelError,
"fatal": xlog.LevelFatal,
}
severityMap = map[xlog.Level]raven.Severity{
xlog.LevelDebug: raven.DEBUG,
xlog.LevelInfo: raven.INFO,
xlog.LevelWarn: raven.WARNING,
xlog.LevelError: raven.ERROR,
xlog.LevelFatal: raven.FATAL,
}
)
// Output is a xlog to sentry output
type Output struct {
Timeout time.Duration
StacktraceConfiguration StackTraceConfiguration
FieldsToTag []string
client *raven.Client
host string
}
// StackTraceConfiguration allows for configuring stacktraces
type StackTraceConfiguration struct {
// whether stacktraces should be enabled
Enable bool
// the level at which to start capturing stacktraces
Level xlog.Level
// how many stack frames to skip before stacktrace starts recording
Skip int
// the number of lines to include around a stack frame for context
Context int
// the prefixes that will be matched against the stack frame.
// if the stack frame's package matches one of these prefixes
// sentry will identify the stack frame as "in_app"
InAppPrefixes []string
}
func NewSentryOutput(DSN string, tags map[string]string, fieldsToTag []string) *Output {
client, _ := raven.NewWithTags(DSN, tags)
return newOutput(client, fieldsToTag)
}
func NewSentryOutputWithClient(client *raven.Client, fieldsToTag []string) *Output {
return newOutput(client, fieldsToTag)
}
func newOutput(client *raven.Client, fieldsToTag []string) *Output {
hostname, _ := os.Hostname()
return &Output{
Timeout: 300 * time.Millisecond,
StacktraceConfiguration: StackTraceConfiguration{
Enable: false,
Level: xlog.LevelError,
Skip: 4,
Context: 0,
InAppPrefixes: nil,
},
FieldsToTag: fieldsToTag,
client: client,
host: hostname,
}
}
func getAndDel(fields map[string]interface{}, key string) (string, bool) {
var (
ok bool
v interface{}
val string
)
if v, ok = fields[key]; !ok {
return "", false
}
if val, ok = v.(string); !ok {
return "", false
}
delete(fields, key)
return val, true
}
func getAndDelRequest(fields map[string]interface{}, key string) (*http.Request, bool) {
var (
ok bool
v interface{}
req *http.Request
)
if v, ok = fields[key]; !ok {
return nil, false
}
if req, ok = v.(*http.Request); !ok || req == nil {
return nil, false
}
delete(fields, key)
return req, true
}
// Write implements xlog.Output interface
func (o Output) Write(fields map[string]interface{}) error {
packet := o.getPacket(fields)
_, errCh := o.client.Capture(packet, nil)
timeout := o.Timeout
if timeout != 0 {
timeoutCh := time.After(timeout)
select {
case err := <-errCh:
return err
case <-timeoutCh:
return fmt.Errorf("no response from sentry server in %s", timeout)
}
}
return nil
}
func (o Output) getPacket(fields map[string]interface{}) *raven.Packet {
level := xlogSeverityMap[fields[xlog.KeyLevel].(string)]
packet := raven.NewPacket(fields[xlog.KeyMessage].(string))
packet.Timestamp = raven.Timestamp(fields[xlog.KeyTime].(time.Time))
packet.Level = severityMap[level]
packet.Logger = "xlog"
fieldsCopy := make(map[string]interface{})
for k, v := range fields {
fieldsCopy[k] = v
}
fieldsCopy = o.mapFieldsToPacket(fieldsCopy, packet)
fieldsCopy = o.addDefaultFields(fieldsCopy)
fieldsCopy = o.mapFieldsToTag(fieldsCopy, packet)
fieldsCopy = o.cleanFields(fieldsCopy)
packet.Extra = fieldsCopy
return packet
}
func (o Output) addDefaultFields(fields map[string]interface{}) map[string]interface{} {
fields["runtime.Version"] = runtime.Version()
fields["runtime.NumCPU"] = runtime.NumCPU()
fields["runtime.GOMAXPROCS"] = runtime.GOMAXPROCS(0)
fields["runtime.NumGoroutine"] = runtime.NumGoroutine()
return fields
}
func (o Output) mapFieldsToPacket(fields map[string]interface{}, packet *raven.Packet) map[string]interface{} {
level := xlogSeverityMap[fields[xlog.KeyLevel].(string)]
if serverName, ok := getAndDel(fields, "host"); ok {
packet.ServerName = serverName
} else if serverName, ok := getAndDel(fields, "server_name"); ok {
packet.ServerName = serverName
} else {
packet.ServerName = o.host
}
if release, ok := getAndDel(fields, "release"); ok {
packet.Release = release
}
if fingerprint, ok := getAndDel(fields, "fingerprint"); ok {
packet.Fingerprint = append(packet.Fingerprint, fingerprint)
} else {
err := fields[xlog.KeyError]
if err, ok := err.(locationer); ok {
file, line := err.Location()
if file != "" {
packet.Fingerprint = append(packet.Fingerprint, fmt.Sprintf("%s:%d", file, line))
}
} else if err, ok := err.(causer); ok {
packet.Fingerprint = append(packet.Fingerprint, fmt.Sprint(err.Cause()))
} else {
packet.Fingerprint = append(packet.Fingerprint, fields[xlog.KeyMessage].(string))
}
}
if culprit, ok := getAndDel(fields, "culprit"); ok {
packet.Culprit = culprit
} else if role, ok := getAndDel(fields, "role"); ok {
packet.Culprit = role
}
if req, ok := getAndDelRequest(fields, "http_request"); ok {
packet.Interfaces = append(packet.Interfaces, raven.NewHttp(req))
}
stConfig := o.StacktraceConfiguration
if stConfig.Enable && level <= stConfig.Level {
currentStacktrace := raven.NewStacktrace(stConfig.Skip, stConfig.Context, stConfig.InAppPrefixes)
if currentStacktrace==nil {
currentStacktrace = raven.NewStacktrace(0, stConfig.Context, stConfig.InAppPrefixes)
}
if currentStacktrace != nil {
packet.Interfaces = append(packet.Interfaces, currentStacktrace)
}
}
return fields
}
func (o Output) mapFieldsToTag(fields map[string]interface{}, packet *raven.Packet) map[string]interface{} {
for _, tag := range o.FieldsToTag {
if value, ok := getAndDel(fields, tag); ok {
packet.AddTags(map[string]string{tag: value})
}
}
return fields
}
func (o Output) cleanFields(fields map[string]interface{}) map[string]interface{} {
delete(fields, xlog.KeyMessage)
delete(fields, xlog.KeyTime)
delete(fields, xlog.KeyLevel)
delete(fields, xlog.KeyFile)
delete(fields, xlog.KeyError)
return fields
}