Skip to content
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

Merged
merged 7 commits into from
Aug 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,23 @@ func Wrap(err error, opts ...Option) error {
return appErr
}

func wrap(err error) *Error {
pkgErr := extractPkgError(err)
func wrap(err error) (wrappedErr *Error) {
stackTrace := newStackTrace(1)

pkgErr := extractPkgError(err)
if appErr, ok := pkgErr.Err.(*Error); ok {
return appErr.Copy()
wrappedErr = appErr.Copy()
} else {
wrappedErr = &Error{
Err: pkgErr.Err,
StackTrace: pkgErr.StackTrace,
}
WithMessage(pkgErr.Message)(wrappedErr)
}

stackTrace := pkgErr.StackTrace
if stackTrace == nil {
stackTrace = newStackTrace(1)
}
wrappedErr.StackTrace = mergeStackTraces(wrappedErr.StackTrace, stackTrace)

wrappedErr := &Error{
Err: pkgErr.Err,
StackTrace: stackTrace,
}
WithMessage(pkgErr.Message)(wrappedErr)
return wrappedErr
return
}

// Unwrap extracts an underlying *fail.Error from an error.
Expand Down
52 changes: 48 additions & 4 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,17 +326,43 @@ func TestAll(t *testing.T) {
assert.Equal(t, "e2: e1: e0", appErr.FullMessage())
assert.Equal(t, nil, appErr.Code)
assert.Equal(t, false, appErr.Ignorable)
assert.NotEmpty(t, appErr.StackTrace)
assert.Equal(t, "errFunc1", appErr.StackTrace[0].Func)
assert.Equal(t, []string{
"errFunc1",
"errFunc2",
"errFunc3",
"TestAll",
"tRunner",
}, funcNamesFromStackTrace(appErr.StackTrace))
}

{
appErr := Unwrap(errFunc4())
assert.Equal(t, "e4: e2: e1: e0", appErr.FullMessage())
assert.Equal(t, 500, appErr.Code)
assert.Equal(t, true, appErr.Ignorable)
assert.NotEmpty(t, appErr.StackTrace)
assert.Equal(t, "errFunc1", appErr.StackTrace[0].Func)
assert.Equal(t, []string{
"errFunc1",
"errFunc2",
"errFunc3",
"errFunc4",
"TestAll",
"tRunner",
}, funcNamesFromStackTrace(appErr.StackTrace))
}

{
appErr := Unwrap(errFunc4Goroutine())
assert.Equal(t, "e4: e2: e1: e0", appErr.FullMessage())
assert.Equal(t, 500, appErr.Code)
assert.Equal(t, true, appErr.Ignorable)
assert.Equal(t, []string{
"errFunc1",
"errFunc2",
"errFunc3Goroutine.func1",
Copy link
Member Author

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.

"errFunc4Goroutine",
"TestAll",
"tRunner",
}, funcNamesFromStackTrace(appErr.StackTrace))
}
}

Expand All @@ -359,3 +385,21 @@ func errFunc3() error {
func errFunc4() error {
return Wrap(errFunc3(), WithMessage("e4"), WithCode(500), WithIgnorable())
}

func errFunc3Goroutine() chan error {
c := make(chan error)
go func() {
c <- Wrap(errFunc2())
}()
return c
}
func errFunc4Goroutine() error {
return Wrap(<-errFunc3Goroutine(), WithMessage("e4"), WithCode(500), WithIgnorable())
}

func funcNamesFromStackTrace(stackTrace StackTrace) (funcNames []string) {
for _, frame := range stackTrace {
funcNames = append(funcNames, frame.Func)
}
return
}
37 changes: 15 additions & 22 deletions pkgerrors.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package fail

import (
"fmt"
"strconv"

pkgerrors "github.com/pkg/errors"
)

Expand All @@ -26,10 +23,11 @@ func extractPkgError(err error) pkgError {
}

rootErr := err
var st pkgerrors.StackTrace
var stackTraces []StackTrace
for {
if stackTrace, ok := rootErr.(traceable); ok {
st = stackTrace.StackTrace()
if t, ok := rootErr.(traceable); ok {
stackTrace := convertStackTrace(t.StackTrace())
stackTraces = append(stackTraces, stackTrace)
}

if cause, ok := rootErr.(causer); ok {
Expand All @@ -40,21 +38,6 @@ func extractPkgError(err error) pkgError {
break
}

var frames []Frame
if st != nil {
for _, t := range st {
file := fmt.Sprintf("%s", t)
line, _ := strconv.ParseInt(fmt.Sprintf("%d", t), 10, 64)
funcName := fmt.Sprintf("%n", t)

frames = append(frames, Frame{
Func: funcName,
Line: line,
File: file,
})
}
}

var msg string
if err.Error() != rootErr.Error() {
msg = err.Error()
Expand All @@ -63,6 +46,16 @@ func extractPkgError(err error) pkgError {
return pkgError{
Err: rootErr,
Message: msg,
StackTrace: frames,
StackTrace: reduceStackTraces(stackTraces),
}
}

// convertStackTrace converts pkg/errors.StackTrace into fail.StackTrace
func convertStackTrace(stackTrace pkgerrors.StackTrace) (frames StackTrace) {
for _, t := range stackTrace {
if frame, ok := newFrameFrom(uintptr(t)); ok {
frames = append(frames, frame)
}
}
return
}
67 changes: 55 additions & 12 deletions stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/") {
Copy link
Member Author

Choose a reason for hiding this comment

The 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)
Expand All @@ -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]
Expand Down Expand Up @@ -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
}
110 changes: 110 additions & 0 deletions stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,113 @@ func TestTrimGOPATH(t *testing.T) {

assert.Equal(t, "pkg/sub/file.go", trimGOPATH(funcName, file))
}

func TestMergeStackTraces(t *testing.T) {
Copy link
Member Author

Choose a reason for hiding this comment

The 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) {
Copy link
Member Author

Choose a reason for hiding this comment

The 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))
}