Skip to content

Commit

Permalink
feat: Better source code location resolution and more useful inapp fr…
Browse files Browse the repository at this point in the history
…ames (#26)
  • Loading branch information
kamilogorek authored Jul 10, 2019
1 parent 3a8117d commit 39e034f
Showing 1 changed file with 22 additions and 9 deletions.
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)):
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

0 comments on commit 39e034f

Please sign in to comment.