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

feat: Better source code location resolution and more useful inapp frames #26

Merged
merged 1 commit into from
Jul 10, 2019
Merged
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
31 changes: 22 additions & 9 deletions stacktrace.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sentry

import (
"go/build"
"path/filepath"
"reflect"
"regexp"
Expand Down Expand Up @@ -228,6 +229,7 @@ var sr = newSourceReader() // nolint: gochecknoglobals

func contextifyFrames(frames []Frame) []Frame {
contextifiedFrames := make([]Frame, 0, len(frames))
sourceCodeLocationPrefix := findSourceCodeLocationPrefix(frames)

for _, frame := range frames {
var path string
Expand All @@ -237,8 +239,8 @@ func contextifyFrames(frames []Frame) []Frame {
switch {
case fileExists(frame.AbsPath):
path = frame.AbsPath
case fileExists(frame.Filename):
path = frame.Filename
case fileExists(strings.TrimPrefix(frame.AbsPath, sourceCodeLocationPrefix)):
kamilogorek marked this conversation as resolved.
Show resolved Hide resolved
path = strings.TrimPrefix(frame.AbsPath, sourceCodeLocationPrefix)
default:
contextifiedFrames = append(contextifiedFrames, frame)
continue
Expand All @@ -263,21 +265,32 @@ func contextifyFrames(frames []Frame) []Frame {
return contextifiedFrames
}

func findSourceCodeLocationPrefix(frames []Frame) string {
mainModulePattern := regexp.MustCompile(`^main\.?`)

for _, frame := range frames {
if mainModulePattern.MatchString(frame.Module) {
dir, _ := filepath.Split(frame.AbsPath)
return dir
}
}

return ""
}

func extractFilename(path string) string {
_, file := filepath.Split(path)
return file
}

func isInAppFrame(frame Frame) bool {
if frame.Module == "main" {
return true
}

if !strings.Contains(frame.Module, "vendor") && !strings.Contains(frame.Module, "third_party") {
return true
if strings.HasPrefix(frame.AbsPath, build.Default.GOROOT) ||
strings.Contains(frame.Module, "vendor") ||
strings.Contains(frame.Module, "third_party") {
return false
}

return false
return true
}

// Transform `runtime/debug.*T·ptrmethod` into `{ module: runtime/debug, function: *T.ptrmethod }`
Expand Down