-
Notifications
You must be signed in to change notification settings - Fork 15
/
errors.go
146 lines (125 loc) · 2.86 KB
/
errors.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 pinpoint
import (
"runtime"
"strings"
"sync/atomic"
"time"
"unsafe"
pkgError "github.com/pkg/errors"
)
type pkgErrorStackTracer interface {
StackTrace() pkgError.StackTrace
}
type causer interface {
Cause() error
}
type errorWithCallStack struct {
err error
errorTime time.Time
callstack []uintptr
}
func (e *errorWithCallStack) stackTrace() []frame {
f := make([]frame, len(e.callstack))
for i := 0; i < len(f); i++ {
f[i] = newFrame(e.callstack[i])
}
return f
}
type frame struct {
moduleName string
funcName string
file string
line int32
}
func newFrame(f uintptr) frame {
moduleName := "unknown"
funcName := "unknown"
file := "unknown"
line := 0
pc := uintptr(f) - 1
if fn := runtime.FuncForPC(pc); fn != nil {
file, line = fn.FileLine(pc)
moduleName, funcName = splitName(fn.Name())
}
return frame{moduleName, funcName, file, int32(line)}
}
func splitName(fullName string) (string, string) {
lastIdx := strings.LastIndex(fullName, ".")
return fullName[:lastIdx], fullName[lastIdx+1:]
}
func (span *span) findError(err error) *exception {
for _, chain := range span.errorChains {
if chain.callstack.err == err {
return chain
}
}
return nil
}
func (span *span) getExceptionChainId(err error) (int64, bool) {
if _, ok := err.(pkgErrorStackTracer); ok {
if ec := span.findError(err); ec != nil {
return ec.exceptionId, false
}
for e := err; e != nil; {
if c, ok := e.(causer); ok {
e = c.Cause()
if ec := span.findError(e); ec != nil {
return ec.exceptionId, true
}
} else {
break
}
}
}
return atomic.AddInt64(&exceptionIdGen, 1), true
}
func (span *span) addCauserCallStack(err error, eid int64) {
for e := err; e != nil; {
c, ok := e.(causer)
if !ok {
break
}
e = c.Cause()
if t := span.findError(e); t == nil {
if pkgErr, ok := e.(pkgErrorStackTracer); ok {
st := pkgErr.StackTrace()
chain := &exception{
callstack: &errorWithCallStack{
err: e,
errorTime: time.Now(),
callstack: *(*[]uintptr)(unsafe.Pointer(&st)),
},
exceptionId: eid,
}
span.errorChains = append(span.errorChains, chain)
}
}
}
}
func (span *span) traceCallStack(err error, depth int) int64 {
var callstack []uintptr
span.errorChainsLock.Lock()
defer span.errorChainsLock.Unlock()
eid, newId := span.getExceptionChainId(err)
if newId {
if pkgErr, ok := err.(pkgErrorStackTracer); ok {
span.addCauserCallStack(err, eid)
st := pkgErr.StackTrace()
callstack = *(*[]uintptr)(unsafe.Pointer(&st))
} else {
pcs := make([]uintptr, depth+3)
n := runtime.Callers(3, pcs)
callstack = pcs[0:n]
}
chain := &exception{
callstack: &errorWithCallStack{
err: err,
errorTime: time.Now(),
callstack: callstack,
},
exceptionId: eid,
}
span.errorChains = append(span.errorChains, chain)
}
return eid
}