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

[AU-217] Make NCError implement StackTracer interface from aws-xray-sdk-go #6

Merged
merged 2 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 17 additions & 4 deletions errors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ type NCError struct {
// Contains stack trace from the initial place when the error
// was raised.
Stack []string
// Raw stack trace in the form of program counters, as returned by the runtime.Callers
RawStack []uintptr
//The root error at the base level.
RootError error
}
Expand All @@ -85,6 +87,12 @@ func (n NCError) Error() string {
return strings.Join(messages, ": ")
}

// StackTrace returns stack trace in the form of program counters (result from runtime.Callers)
// to implement StackTracer interface from aws-xray-sdk-go module
func (n NCError) StackTrace() []uintptr {
return n.RawStack
}

// New error with context.
func New(message string, fields Fields) error {
fileName, funcName, lineNumber := GetRuntimeContext()
Expand All @@ -96,8 +104,10 @@ func New(message string, fields Fields) error {
Line: lineNumber,
Severity: ERROR}
return NCError{
Causes: []Cause{newCause},
Stack: GetTrace()}
Causes: []Cause{newCause},
Stack: GetTrace(),
RawStack: callers(),
}
}

func NewWithSeverity(message string, fields Fields, severity LogSeverity) error {
Expand All @@ -111,8 +121,9 @@ func NewWithSeverity(message string, fields Fields, severity LogSeverity) error
Severity: severity,
}
return NCError{
Causes: []Cause{newCause},
Stack: GetTrace(),
Causes: []Cause{newCause},
Stack: GetTrace(),
RawStack: callers(),
}
}

Expand All @@ -138,6 +149,7 @@ func WithContext(err error, message string, fields Fields) error {
return NCError{
Causes: []Cause{newCause, Cause{Message: err.Error()}},
Stack: GetTrace(),
RawStack: callers(),
okonos marked this conversation as resolved.
Show resolved Hide resolved
RootError: err}
}

Expand All @@ -163,6 +175,7 @@ func WithContextAndSeverity(err error, message string, severity LogSeverity, fie
return NCError{
Causes: []Cause{newCause, Cause{Message: err.Error()}},
Stack: GetTrace(),
RawStack: callers(),
RootError: err,
}
}
Expand Down
10 changes: 4 additions & 6 deletions errors/stack_trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var (
// 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
callStack := *callers()
callStack := callers()
st := callStack[:len(callStack)-1]
for _, f := range st {
frame := frame(f)
Expand All @@ -24,7 +24,6 @@ func GetTrace() []string {
return stack
}

type stack []uintptr
type frame uintptr

func (f frame) pc() uintptr { return uintptr(f) - 1 }
Expand Down Expand Up @@ -65,16 +64,15 @@ func (f frame) formatContext() string {

// GetRuntimeContext returns function name and code line.
func GetRuntimeContext() (fileName, funcName string, line int) {
st := *callers()
st := callers()
frame := frame(st[1])
fileName, funcName, line = frame.getContext()
return
}

func callers() *stack {
func callers() []uintptr {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function (and probably a few others) is copied directly from the pkg/errors: https://github.com/pkg/errors/blob/master/stack.go#L163
The x-ray SDK supports both the generic []uintptr as well as the pkg/errors stacktrace format directly, so it seems strange that we need to change the code from pkg/errors to support the x-ray SDK. Any thoughts on this? 🤔

Copy link
Contributor Author

@okonos okonos Mar 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unaware of this having been copied, I've just removed it considering it was redundant (a custom type without any methods declared seemed so).

I reworked the solution to implement the pkg/errors "StackTrace interface" instead, since this package depends on it anyway. We could possibly update the pkg/errors to the latest version or to one matching that of aws-xray-sdk-go to avoid possible issues.

const depth = 32
var pcs [depth]uintptr
n := runtime.Callers(3, pcs[:])
var st stack = pcs[0:n]
return &st
return pcs[0:n]
}