forked from bugsnag/bugsnag-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata.go
192 lines (153 loc) · 4.44 KB
/
metadata.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
package bugsnag
import (
"fmt"
"reflect"
"strings"
)
// MetaData is added to the Bugsnag dashboard in tabs. Each tab is
// a map of strings -> values. You can pass MetaData to Notify, Recover
// and AutoNotify as rawData.
type MetaData map[string]map[string]interface{}
// Update the meta-data with more information. Tabs are merged together such
// that unique keys from both sides are preserved, and duplicate keys end up
// with the provided values.
func (meta MetaData) Update(other MetaData) {
for name, tab := range other {
if meta[name] == nil {
meta[name] = make(map[string]interface{})
}
for key, value := range tab {
meta[name][key] = value
}
}
}
// Add creates a tab of Bugsnag meta-data.
// If the tab doesn't yet exist it will be created.
// If the key already exists, it will be overwritten.
func (meta MetaData) Add(tab string, key string, value interface{}) {
if meta[tab] == nil {
meta[tab] = make(map[string]interface{})
}
meta[tab][key] = value
}
// AddStruct creates a tab of Bugsnag meta-data.
// The struct will be converted to an Object using the
// reflect library so any private fields will not be exported.
// As a safety measure, if you pass a non-struct the value will be
// sent to Bugsnag under the "Extra data" tab.
func (meta MetaData) AddStruct(tab string, obj interface{}) {
val := sanitizer{}.Sanitize(obj)
content, ok := val.(map[string]interface{})
if ok {
meta[tab] = content
} else {
// Wasn't a struct
meta.Add("Extra data", tab, obj)
}
}
// Remove any values from meta-data that have keys matching the filters,
// and any that are recursive data-structures
func (meta MetaData) sanitize(filters []string) interface{} {
return sanitizer{
Filters: filters,
Seen: make([]interface{}, 0),
}.Sanitize(meta)
}
// The sanitizer is used to remove filtered params and recursion from meta-data.
type sanitizer struct {
Filters []string
Seen []interface{}
}
func (s sanitizer) Sanitize(data interface{}) interface{} {
for _, s := range s.Seen {
// TODO: we don't need deep equal here, just type-ignoring equality
if reflect.DeepEqual(data, s) {
return "[RECURSION]"
}
}
// Sanitizers are passed by value, so we can modify s and it only affects
// s.Seen for nested calls.
s.Seen = append(s.Seen, data)
t := reflect.TypeOf(data)
v := reflect.ValueOf(data)
if t == nil {
return "<nil>"
}
switch t.Kind() {
case reflect.Bool,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
reflect.Float32, reflect.Float64:
return data
case reflect.String:
return data
case reflect.Interface, reflect.Ptr:
if v.IsNil() {
return "<nil>"
}
return s.Sanitize(v.Elem().Interface())
case reflect.Array, reflect.Slice:
ret := make([]interface{}, v.Len())
for i := 0; i < v.Len(); i++ {
ret[i] = s.Sanitize(v.Index(i).Interface())
}
return ret
case reflect.Map:
return s.sanitizeMap(v)
case reflect.Struct:
return s.sanitizeStruct(v, t)
// Things JSON can't serialize:
// case t.Chan, t.Func, reflect.Complex64, reflect.Complex128, reflect.UnsafePointer:
default:
return "[" + t.String() + "]"
}
}
func (s sanitizer) sanitizeMap(v reflect.Value) interface{} {
ret := make(map[string]interface{})
for _, key := range v.MapKeys() {
val := s.Sanitize(v.MapIndex(key).Interface())
newKey := fmt.Sprintf("%v", key.Interface())
if s.shouldRedact(newKey) {
val = "[FILTERED]"
}
ret[newKey] = val
}
return ret
}
func (s sanitizer) sanitizeStruct(v reflect.Value, t reflect.Type) interface{} {
ret := make(map[string]interface{})
for i := 0; i < v.NumField(); i++ {
val := v.Field(i)
// Don't export private fields
if !val.CanInterface() {
continue
}
name := t.Field(i).Name
var opts tagOptions
// Parse JSON tags. Supports name and "omitempty"
if jsonTag := t.Field(i).Tag.Get("json"); len(jsonTag) != 0 {
name, opts = parseTag(jsonTag)
}
if s.shouldRedact(name) {
ret[name] = "[FILTERED]"
} else {
sanitized := s.Sanitize(val.Interface())
if str, ok := sanitized.(string); ok {
if !(opts.Contains("omitempty") && len(str) == 0) {
ret[name] = str
}
} else {
ret[name] = sanitized
}
}
}
return ret
}
func (s sanitizer) shouldRedact(key string) bool {
for _, filter := range s.Filters {
if strings.Contains(strings.ToLower(key), strings.ToLower(filter)) {
return true
}
}
return false
}