-
Notifications
You must be signed in to change notification settings - Fork 40
/
filter.go
146 lines (123 loc) · 2.96 KB
/
filter.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
package gobrake
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
func newNotifierFilter(notifier *Notifier) func(*Notice) *Notice {
opt := notifier.opt
return func(notice *Notice) *Notice {
if opt.Environment != "" {
notice.Context["environment"] = opt.Environment
}
if opt.Revision != "" {
notice.Context["revision"] = opt.Revision
}
return notice
}
}
func NewBlocklistKeysFilter(keys ...interface{}) func(*Notice) *Notice {
return func(notice *Notice) *Notice {
for _, key := range keys {
notice.Env = filterByKey(notice.Env, key)
notice.Context = filterByKey(notice.Context, key)
notice.Session = filterByKey(notice.Session, key)
}
return notice
}
}
func filterByKey(values map[string]interface{}, key interface{}) map[string]interface{} {
const filtered = "[Filtered]"
switch key := key.(type) {
case string:
for k := range values {
if k == key {
values[k] = filtered
}
}
case *regexp.Regexp:
for k := range values {
if key.MatchString(k) {
values[k] = filtered
}
}
default:
panic(fmt.Errorf("unsupported blacklist key type: %T", key))
}
return values
}
func gopathFilter(notice *Notice) *Notice {
s, ok := notice.Context["gopath"].(string)
if !ok {
return notice
}
dirs := filepath.SplitList(s)
for i := range notice.Errors {
backtrace := notice.Errors[i].Backtrace
for j := range backtrace {
frame := &backtrace[j]
for _, dir := range dirs {
dir = filepath.Join(dir, "src")
if strings.HasPrefix(frame.File, dir) {
frame.File = strings.Replace(frame.File, dir, "/GOPATH", 1)
break
}
}
}
}
return notice
}
func gitFilter(notice *Notice) *Notice {
rootDir, _ := notice.Context["rootDirectory"].(string)
if rootDir == "" {
return notice
}
gitDir, ok := findGitDir(rootDir)
if !ok {
return notice
}
info := getGitInfo(gitDir)
if notice.Context == nil {
notice.Context = make(map[string]interface{})
}
if notice.Context["repository"] == nil && info.Repository != "" {
notice.Context["repository"] = info.Repository
}
if notice.Context["revision"] == nil && info.Revision != "" {
notice.Context["revision"] = info.Revision
}
if info.LastCheckout != nil {
notice.Context["lastCheckout"] = info.LastCheckout
}
return notice
}
func httpUnsolicitedResponseFilter(notice *Notice) *Notice {
if notice.Errors[0].Type != "string" {
return notice
}
prefix := "Unsolicited response received on idle HTTP channel starting with"
if !strings.HasPrefix(notice.Errors[0].Message, prefix) {
return notice
}
return nil
}
func codeHunksFilter(notice *Notice) *Notice {
for i := range notice.Errors {
error := ¬ice.Errors[i]
for j := range error.Backtrace {
frame := &error.Backtrace[j]
code, err := getCode(frame.File, frame.Line)
if err != nil {
if !os.IsNotExist(err) {
logger.Printf("getCode file=%q line=%d failed: %s",
frame.File, frame.Line, err)
}
continue
}
frame.Code = code
}
}
return notice
}