forked from evalphobia/logrus_sentry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_field.go
137 lines (117 loc) · 2.93 KB
/
data_field.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
package logrus_sentry
import (
"net/http"
"github.com/getsentry/raven-go"
"github.com/sirupsen/logrus"
)
const (
fieldEventID = "event_id"
fieldFingerprint = "fingerprint"
fieldLogger = "logger"
fieldServerName = "server_name"
fieldTags = "tags"
fieldHTTPRequest = "http_request"
fieldUser = "user"
)
type dataField struct {
data logrus.Fields
omitList map[string]struct{}
}
func newDataField(data logrus.Fields) *dataField {
return &dataField{
data: data,
omitList: make(map[string]struct{}),
}
}
func (d *dataField) len() int {
return len(d.data)
}
func (d *dataField) isOmit(key string) bool {
_, ok := d.omitList[key]
return ok
}
func (d *dataField) getLogger() (string, bool) {
if logger, ok := d.data[fieldLogger].(string); ok {
d.omitList[fieldLogger] = struct{}{}
return logger, true
}
return "", false
}
func (d *dataField) getServerName() (string, bool) {
if serverName, ok := d.data[fieldServerName].(string); ok {
d.omitList[fieldServerName] = struct{}{}
return serverName, true
}
return "", false
}
func (d *dataField) getTags() (raven.Tags, bool) {
if tags, ok := d.data[fieldTags].(raven.Tags); ok {
d.omitList[fieldTags] = struct{}{}
return tags, true
}
return nil, false
}
func (d *dataField) getFingerprint() ([]string, bool) {
if fingerprint, ok := d.data[fieldFingerprint].([]string); ok {
d.omitList[fieldFingerprint] = struct{}{}
return fingerprint, true
}
return nil, false
}
func (d *dataField) getError() (error, bool) {
if err, ok := d.data[logrus.ErrorKey].(error); ok {
d.omitList[logrus.ErrorKey] = struct{}{}
return err, true
}
return nil, false
}
func (d *dataField) getHTTPRequest() (*raven.Http, bool) {
if req, ok := d.data[fieldHTTPRequest].(*http.Request); ok {
d.omitList[fieldHTTPRequest] = struct{}{}
return raven.NewHttp(req), true
}
if req, ok := d.data[fieldHTTPRequest].(*raven.Http); ok {
d.omitList[fieldHTTPRequest] = struct{}{}
return req, true
}
return nil, false
}
func (d *dataField) getEventID() (string, bool) {
eventID, ok := d.data[fieldEventID].(string)
if !ok {
return "", false
}
//verify eventID is 32 characters hexadecimal string (UUID4)
uuid := parseUUID(eventID)
if uuid == nil {
return "", false
}
d.omitList[fieldEventID] = struct{}{}
return uuid.noDashString(), true
}
func (d *dataField) getUser() (*raven.User, bool) {
data := d.data
if v, ok := data[fieldUser]; ok {
switch val := v.(type) {
case *raven.User:
d.omitList[fieldUser] = struct{}{}
return val, true
case raven.User:
d.omitList[fieldUser] = struct{}{}
return &val, true
}
}
username, _ := data["user_name"].(string)
email, _ := data["user_email"].(string)
id, _ := data["user_id"].(string)
ip, _ := data["user_ip"].(string)
if username == "" && email == "" && id == "" && ip == "" {
return nil, false
}
return &raven.User{
ID: id,
Username: username,
Email: email,
IP: ip,
}, true
}