-
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
[AU-217] Make NCError implement StackTracer interface from aws-xray-sdk-go #6
Changes from all commits
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 |
---|---|---|
|
@@ -5,26 +5,35 @@ import ( | |
"regexp" | ||
"runtime" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
var ( | ||
regexFuncName = regexp.MustCompile(`(([^/]+/)+)?([^/.]+)((\.[^/.]+)+)?`) | ||
) | ||
|
||
// GetTrace return the simplified stack trace in the format file_name(func_name):line. It also contains the current goroutine entrypoint. | ||
func GetTrace() []string { | ||
var stack []string | ||
// getStackTraces returns custom-formatted and raw (in the form of program counters) stack trace | ||
// for the purpose of initializing NCError struct | ||
func getStackTraces() ([]string, *stack) { | ||
var formattedStack []string | ||
callStack := *callers() | ||
st := callStack[:len(callStack)-1] | ||
for _, f := range st { | ||
frame := frame(f) | ||
stack = append(stack, frame.formatContext()) | ||
formattedStack = append(formattedStack, frame.formatContext()) | ||
} | ||
|
||
return formattedStack, &callStack | ||
} | ||
|
||
// GetTrace return the simplified stack trace in the format file_name(func_name):line. It also contains the current goroutine entrypoint. | ||
func GetTrace() []string { | ||
stack, _ := getStackTraces() | ||
|
||
return stack | ||
} | ||
|
||
type stack []uintptr | ||
type frame uintptr | ||
|
||
func (f frame) pc() uintptr { return uintptr(f) - 1 } | ||
|
@@ -71,6 +80,16 @@ func GetRuntimeContext() (fileName, funcName string, line int) { | |
return | ||
} | ||
|
||
type stack []uintptr | ||
|
||
func (s *stack) StackTrace() errors.StackTrace { | ||
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. Somewhat off-topic, but I wonder what's the point of declaring exported method on unexported type (I copied it over from 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. The fact that the type itself is not exported doesn't change the fact that the method can be called on that type. Unexported type just means you can not use the name of that type, but if you have a variable of that type you still can doe whatever is exported on that type. Also |
||
f := make([]errors.Frame, len(*s)) | ||
for i := 0; i < len(f); i++ { | ||
f[i] = errors.Frame((*s)[i]) | ||
} | ||
return f | ||
} | ||
|
||
func callers() *stack { | ||
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. Don't see the point of it returning a pointer and neither does the author |
||
const depth = 32 | ||
var pcs [depth]uintptr | ||
|
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.
Could be embedded instead? Dunno if it would work with the type being unexported 🤔
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.
Not sure what would not work here, but if there is no clear reason for embedding then I'd leave it as it is
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.
Rationale would be no need for the
StackTrace
method (re)definition. Currently it's more clear, though, I guess