-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create a virtual stack trace by merging multiple stack traces #13
Changes from all commits
33a06b9
8af36cf
7938008
d9b114b
b2f08eb
206509c
5811a7e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,27 @@ type Frame struct { | |
Line int64 | ||
} | ||
|
||
// newFrameFrom creates Frame from the specified program counter | ||
func newFrameFrom(pc uintptr) (f Frame, ok bool) { | ||
fpc := runtime.FuncForPC(pc) | ||
if fpc == nil { | ||
return | ||
} | ||
|
||
file, line := fpc.FileLine(pc) | ||
|
||
f.Func = funcname(fpc.Name()) | ||
f.File = trimGOPATH(fpc.Name(), file) | ||
f.Line = int64(line) | ||
|
||
if strings.HasPrefix(f.File, "runtime/") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ignores runtime functions. |
||
return | ||
} | ||
|
||
ok = true | ||
return | ||
} | ||
|
||
// newStackTrace creates StackTrace by callers | ||
func newStackTrace(offset int) StackTrace { | ||
pcs := make([]uintptr, stackMaxSize) | ||
|
@@ -29,19 +50,10 @@ func newStackTrace(offset int) StackTrace { | |
frames := make([]Frame, n) | ||
|
||
for _, pc := range pcs[0:n] { | ||
f := runtime.FuncForPC(pc) | ||
if f == nil { | ||
continue | ||
} | ||
|
||
file, line := f.FileLine(pc) | ||
|
||
frames[i] = Frame{ | ||
Func: funcname(f.Name()), | ||
File: trimGOPATH(f.Name(), file), | ||
Line: int64(line), | ||
if f, ok := newFrameFrom(pc); ok { | ||
frames[i] = f | ||
i++ | ||
} | ||
i++ | ||
} | ||
|
||
return frames[:i] | ||
|
@@ -96,3 +108,34 @@ func trimGOPATH(name, file string) string { | |
file = file[i+len(sep):] | ||
return file | ||
} | ||
|
||
// mergeStackTraces merges two stack traces | ||
func mergeStackTraces(inner StackTrace, outer StackTrace) StackTrace { | ||
innerLen := len(inner) | ||
outerLen := len(outer) | ||
|
||
if innerLen > outerLen { | ||
overlap := 0 | ||
for overlap < outerLen { | ||
if inner[innerLen-overlap-1] != outer[outerLen-overlap-1] { | ||
break | ||
} | ||
overlap++ | ||
} | ||
|
||
if overlap > 0 { | ||
return append(inner[:innerLen-overlap], outer...) | ||
} | ||
} | ||
|
||
return append(inner, outer...) | ||
} | ||
|
||
// reduceStackTraces incrementally merges multiple stack traces | ||
// and returns a merged stack trace | ||
func reduceStackTraces(stackTraces []StackTrace) (merged StackTrace) { | ||
for i := len(stackTraces) - 1; i >= 0; i-- { | ||
merged = mergeStackTraces(merged, stackTraces[i]) | ||
} | ||
return | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,3 +57,113 @@ func TestTrimGOPATH(t *testing.T) { | |
|
||
assert.Equal(t, "pkg/sub/file.go", trimGOPATH(funcName, file)) | ||
} | ||
|
||
func TestMergeStackTraces(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is the strategy for merging multiple stacks. |
||
t.Run("empty", func(t *testing.T) { | ||
inner := StackTrace{} | ||
outer := StackTrace{ | ||
{Func: "init", File: "main.go", Line: 154}, | ||
} | ||
result := StackTrace{ | ||
{Func: "init", File: "main.go", Line: 154}, | ||
} | ||
|
||
assert.Equal(t, result, mergeStackTraces(inner, outer)) | ||
}) | ||
|
||
t.Run("inner < outer", func(t *testing.T) { | ||
inner := StackTrace{ | ||
{Func: "init", File: "main.go", Line: 154}, | ||
} | ||
outer := StackTrace{ | ||
{Func: "f1", File: "main.go", Line: 157}, | ||
{Func: "f2", File: "main.go", Line: 161}, | ||
{Func: "f3.func1", File: "main.go", Line: 167}, | ||
} | ||
result := StackTrace{ | ||
{Func: "init", File: "main.go", Line: 154}, | ||
{Func: "f1", File: "main.go", Line: 157}, | ||
{Func: "f2", File: "main.go", Line: 161}, | ||
{Func: "f3.func1", File: "main.go", Line: 167}, | ||
} | ||
|
||
assert.Equal(t, result, mergeStackTraces(inner, outer)) | ||
}) | ||
|
||
t.Run("inner > outer (overlapping)", func(t *testing.T) { | ||
inner := StackTrace{ | ||
{Func: "init", File: "main.go", Line: 154}, | ||
{Func: "f1", File: "main.go", Line: 157}, | ||
{Func: "f2", File: "main.go", Line: 161}, | ||
{Func: "f3.func1", File: "main.go", Line: 167}, | ||
} | ||
outer := StackTrace{ | ||
{Func: "f2", File: "main.go", Line: 161}, | ||
{Func: "f3.func1", File: "main.go", Line: 167}, | ||
} | ||
result := StackTrace{ | ||
{Func: "init", File: "main.go", Line: 154}, | ||
{Func: "f1", File: "main.go", Line: 157}, | ||
{Func: "f2", File: "main.go", Line: 161}, | ||
{Func: "f3.func1", File: "main.go", Line: 167}, | ||
} | ||
|
||
assert.Equal(t, result, mergeStackTraces(inner, outer)) | ||
}) | ||
|
||
t.Run("inner > outer (no overlapping frames)", func(t *testing.T) { | ||
inner := StackTrace{ | ||
{Func: "init", File: "main.go", Line: 154}, | ||
{Func: "f1", File: "main.go", Line: 157}, | ||
{Func: "f2", File: "main.go", Line: 161}, | ||
{Func: "f3.func1", File: "main.go", Line: 167}, | ||
} | ||
outer := StackTrace{ | ||
{Func: "g2", File: "main.go", Line: 1061}, | ||
{Func: "g3.func1", File: "main.go", Line: 1067}, | ||
} | ||
result := StackTrace{ | ||
{Func: "init", File: "main.go", Line: 154}, | ||
{Func: "f1", File: "main.go", Line: 157}, | ||
{Func: "f2", File: "main.go", Line: 161}, | ||
{Func: "f3.func1", File: "main.go", Line: 167}, | ||
{Func: "g2", File: "main.go", Line: 1061}, | ||
{Func: "g3.func1", File: "main.go", Line: 1067}, | ||
} | ||
|
||
assert.Equal(t, result, mergeStackTraces(inner, outer)) | ||
}) | ||
} | ||
|
||
func TestReduceStackTraces(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. var origin = New("test")
func f1() error {
return Wrap(origin)
}
func f2() error {
return Wrap(f1())
}
func f3() chan error {
c := make(chan error)
go func() {
c <- Wrap(f2())
}()
return c
}
func main() {
// init
// f1
// f2
// f3.func1
// main
Wrap(<-f3())
} |
||
input := []StackTrace{ | ||
{ | ||
{Func: "main", File: "main.go", Line: 179}, | ||
}, | ||
{ | ||
{Func: "f3.func1", File: "main.go", Line: 168}, | ||
}, | ||
{ | ||
{Func: "f2", File: "main.go", Line: 162}, | ||
{Func: "f3.func1", File: "main.go", Line: 168}, | ||
}, | ||
{ | ||
{Func: "f1", File: "main.go", Line: 158}, | ||
{Func: "f2", File: "main.go", Line: 162}, | ||
{Func: "f3.func1", File: "main.go", Line: 168}, | ||
}, | ||
{ | ||
{Func: "init", File: "main.go", Line: 155}, | ||
}, | ||
{}, | ||
} | ||
result := StackTrace{ | ||
{Func: "init", File: "main.go", Line: 155}, | ||
{Func: "f1", File: "main.go", Line: 158}, | ||
{Func: "f2", File: "main.go", Line: 162}, | ||
{Func: "f3.func1", File: "main.go", Line: 168}, | ||
{Func: "main", File: "main.go", Line: 179}, | ||
} | ||
|
||
assert.Equal(t, result, reduceStackTraces(input)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even when the error is passed between goroutines, a virtual stack trace contains all information.