-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace.go
152 lines (134 loc) · 2.66 KB
/
trace.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
package process
import (
"context"
"fmt"
"runtime"
"strings"
"time"
"github.com/pkg/errors"
)
type Frame struct {
errors.Frame
Name string
File string
Line int
}
func externalFrame() *Frame {
// get the call stack excluding the runtime call
const depth = 32
var pcs [depth]uintptr
n := runtime.Callers(1, pcs[:])
st := pcs[0:n]
// get the current package name
pkgFrame := frameDetails(st[0])
pkg := pkgFrame.Name[0:strings.LastIndex(pkgFrame.Name, ".")]
// find the frame first frame excluding the current package and go runtime
for _, s := range st[1:] {
fr := frameDetails(s)
if !strings.HasPrefix(fr.Name, pkg+".") && !strings.HasPrefix(fr.Name, "runtime.") {
return &fr
}
}
return nil
}
func frameDetails(pc uintptr) Frame {
fn := runtime.FuncForPC(pc - 1)
name := fn.Name()
file, line := fn.FileLine(pc - 1)
return Frame{
Frame: errors.Frame(pc),
Name: name,
File: file,
Line: line,
}
}
type traceError struct {
cause error
frame Frame
}
func WrapTrace(err error) error {
if err == nil {
return nil
}
frame := externalFrame()
if frame == nil {
return err
}
return &traceError{
cause: err,
frame: *frame,
}
}
func (t *traceError) Error() string {
return t.cause.Error()
}
func (t *traceError) Cause() error {
return t.cause
}
func (t *traceError) Unwrap() error {
return t.cause
}
func (t *traceError) Trace() []Frame {
err := t.cause
for err != nil {
if trace, ok := err.(*traceError); ok {
return append([]Frame{t.frame}, trace.Trace()...)
}
if errors.Unwrap(err) != nil {
err = errors.Unwrap(err)
continue
}
if errors.Cause(err) != err {
err = errors.Cause(err)
continue
}
break
}
return []Frame{t.frame}
}
func (t *traceError) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
fmt.Fprintln(s, t.frame.Name)
fmt.Fprintf(s, "\t%s:%d\n", t.frame.File, t.frame.Line)
fmt.Fprintf(s, "%+v", t.cause)
return
}
fallthrough
case 's':
fmt.Fprintf(s, "%s", t.cause)
case 'q':
fmt.Fprintf(s, "%q", t.cause)
}
}
type traceContext struct {
parent context.Context
frame Frame
}
func WithTrace(ctx context.Context) context.Context {
frame := externalFrame()
if frame == nil {
return ctx
}
t := &traceContext{
parent: ctx,
frame: *frame,
}
return t
}
func (t *traceContext) Done() <-chan struct{} {
return t.parent.Done()
}
func (t *traceContext) Err() error {
if t.parent.Err() == nil {
return nil
}
return &traceError{cause: WrapTrace(t.parent.Err()), frame: t.frame}
}
func (t *traceContext) Deadline() (time.Time, bool) {
return t.parent.Deadline()
}
func (t *traceContext) Value(key interface{}) interface{} {
return t.parent.Value(key)
}