-
Notifications
You must be signed in to change notification settings - Fork 23
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
Use []runtime.Frame instead of exporting Stack (#45) #52
Changes from 1 commit
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 |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
"net/url" | ||
"reflect" | ||
"regexp" | ||
"runtime" | ||
"strings" | ||
"time" | ||
) | ||
|
@@ -191,10 +192,10 @@ func filterIp(ip string, captureIp captureIp) string { | |
// method, the causes will be traversed until nil. | ||
func errorBody(configuration configuration, err error, skip int) (map[string]interface{}, string) { | ||
var parent error | ||
traceChain := []map[string]interface{}{} | ||
var traceChain []map[string]interface{} | ||
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. Why this change? 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. Sorry it was just a reflex to change that due to my IDE complaining about declaring an empty slice with a literal. I believe it's considered a good convention to use nil declarations (https://stackoverflow.com/a/29164565/998919), but by all means, I can revert it if this project prefers the other (previous) style. 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. I guess that we always append something to it so this change is okay. The one difference is that for JSON serialization the nil slice becomes 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. True. I did pay attention to this in at least one other spot, to make sure we get |
||
fingerprint := "" | ||
for { | ||
stack := getOrBuildStack(err, parent, skip) | ||
stack := buildStack(getOrBuildFrames(err, parent, 1 + skip)) | ||
traceChain = append(traceChain, buildTrace(err, stack)) | ||
if configuration.fingerprint { | ||
fingerprint = fingerprint + stack.Fingerprint() | ||
|
@@ -210,7 +211,7 @@ func errorBody(configuration configuration, err error, skip int) (map[string]int | |
} | ||
|
||
// builds one trace element in trace_chain | ||
func buildTrace(err error, stack Stack) map[string]interface{} { | ||
func buildTrace(err error, stack stack) map[string]interface{} { | ||
message := nilErrTitle | ||
if err != nil { | ||
message = err.Error() | ||
|
@@ -231,20 +232,33 @@ func getCause(err error) error { | |
return nil | ||
} | ||
|
||
// gets Stack from errors that provide one of their own | ||
// otherwise, builds a new stack | ||
func getOrBuildStack(err error, parent error, skip int) Stack { | ||
// gets stack frames from errors that provide one of their own | ||
// otherwise, builds a new stack trace | ||
func getOrBuildFrames(err error, parent error, skip int) []runtime.Frame { | ||
if cs, ok := err.(CauseStacker); ok { | ||
if s := cs.Stack(); s != nil { | ||
return s | ||
} | ||
} else { | ||
if _, ok := parent.(CauseStacker); !ok { | ||
return BuildStack(4 + skip) | ||
return cs.Stack() | ||
} else if _, ok := parent.(CauseStacker); !ok { | ||
return getCallersFrames(1 + skip) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func getCallersFrames(skip int) []runtime.Frame { | ||
pc := make([]uintptr, 100) | ||
runtime.Callers(2 + skip, pc) | ||
fr := runtime.CallersFrames(pc) | ||
frames := make([]runtime.Frame, 0) | ||
|
||
for frame, more := fr.Next(); frame != (runtime.Frame{}); frame, more = fr.Next() { | ||
frames = append(frames, frame) | ||
|
||
if !more { | ||
break | ||
} | ||
} | ||
|
||
return make(Stack, 0) | ||
return frames | ||
} | ||
|
||
// Build a message inner-body for the given message string. | ||
|
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.
You can get rid of the "optionally skipping ..." as we are not taking a skip argument anymore.