Skip to content

Commit

Permalink
Merge branch 'master' into nop-client
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilogorek committed Jul 10, 2019
2 parents 5bb0647 + 39e034f commit 091467d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 19 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Changelog

## v0.1.2

- fix: Use `NewScope` instead of literal struct inside a `scope.Clear` call

## v0.1.1

- fix: Check for initialized Client in AddBreadcrumbs (#20)
- fix: Check for initialized `Client` in `AddBreadcrumbs` (#20)
- build: Bump version when releasing with Craft (#19)

## v0.1.0
Expand Down
2 changes: 1 addition & 1 deletion scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (scope *Scope) Clone() *Scope {

// Clear removed the data from the current scope.
func (scope *Scope) Clear() {
*scope = Scope{}
*scope = *NewScope()
}

// AddEventProcessor adds an event processor to the current scope.
Expand Down
37 changes: 30 additions & 7 deletions scope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
func fillScopeWithData(scope *Scope) *Scope {
scope.breadcrumbs = []*Breadcrumb{{Timestamp: 1337, Message: "scopeBreadcrumbMessage"}}
scope.user = User{ID: "1337"}
scope.contexts = map[string]interface{}{"scopeContextsKey": "scopeContextsValue"}
scope.tags = map[string]string{"scopeTagKey": "scopeTagValue"}
scope.contexts = map[string]interface{}{"scopeContextsKey": "scopeContextsValue"}
scope.extra = map[string]interface{}{"scopeExtraKey": "scopeExtraValue"}
scope.fingerprint = []string{"scopeFingerprintOne", "scopeFingerprintTwo"}
scope.level = LevelDebug
Expand All @@ -20,8 +20,8 @@ func fillScopeWithData(scope *Scope) *Scope {
func fillEventWithData(event *Event) *Event {
event.Breadcrumbs = []*Breadcrumb{{Timestamp: 1337, Message: "eventBreadcrumbMessage"}}
event.User = User{ID: "42"}
event.Contexts = map[string]interface{}{"eventContextsKey": "eventContextsValue"}
event.Tags = map[string]string{"eventTagKey": "eventTagValue"}
event.Contexts = map[string]interface{}{"eventContextsKey": "eventContextsValue"}
event.Extra = map[string]interface{}{"eventExtraKey": "eventExtraValue"}
event.Fingerprint = []string{"eventFingerprintOne", "eventFingerprintTwo"}
event.Level = LevelInfo
Expand Down Expand Up @@ -436,16 +436,39 @@ func TestClear(t *testing.T) {
scope := fillScopeWithData(NewScope())
scope.Clear()

assertEqual(t, []*Breadcrumb(nil), scope.breadcrumbs)
assertEqual(t, []*Breadcrumb{}, scope.breadcrumbs)
assertEqual(t, User{}, scope.user)
assertEqual(t, map[string]string(nil), scope.tags)
assertEqual(t, map[string]interface{}(nil), scope.contexts)
assertEqual(t, map[string]interface{}(nil), scope.extra)
assertEqual(t, []string(nil), scope.fingerprint)
assertEqual(t, map[string]string{}, scope.tags)
assertEqual(t, map[string]interface{}{}, scope.contexts)
assertEqual(t, map[string]interface{}{}, scope.extra)
assertEqual(t, []string{}, scope.fingerprint)
assertEqual(t, Level(""), scope.level)
assertEqual(t, Request{}, scope.request)
}

func TestClearAndReconfigure(t *testing.T) {
scope := fillScopeWithData(NewScope())
scope.Clear()

scope.SetTag("foo", "bar")
scope.SetContext("foo", "bar")
scope.SetExtra("foo", "bar")
scope.SetLevel(LevelDebug)
scope.SetFingerprint([]string{"foo"})
scope.AddBreadcrumb(&Breadcrumb{Timestamp: 1337, Message: "foo"}, maxBreadcrumbs)
scope.SetUser(User{ID: "foo"})
scope.SetRequest(Request{URL: "foo"})

assertEqual(t, map[string]string{"foo": "bar"}, scope.tags)
assertEqual(t, map[string]interface{}{"foo": "bar"}, scope.contexts)
assertEqual(t, map[string]interface{}{"foo": "bar"}, scope.extra)
assertEqual(t, LevelDebug, scope.level)
assertEqual(t, []string{"foo"}, scope.fingerprint)
assertEqual(t, []*Breadcrumb{{Timestamp: 1337, Message: "foo"}}, scope.breadcrumbs)
assertEqual(t, User{ID: "foo"}, scope.user)
assertEqual(t, Request{URL: "foo"}, scope.request)
}

func TestClearBreadcrumbs(t *testing.T) {
scope := fillScopeWithData(NewScope())
scope.ClearBreadcrumbs()
Expand Down
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
4 changes: 3 additions & 1 deletion transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ func (t *HTTPTransport) SendEvent(event *Event) {
request.Header.Set(headerKey, headerValue)
}

t.wg.Add(1)

select {
case t.buffer <- request:
Logger.Printf(
Expand All @@ -152,8 +154,8 @@ func (t *HTTPTransport) SendEvent(event *Event) {
t.dsn.host,
t.dsn.projectID,
)
t.wg.Add(1)
default:
t.wg.Done()
Logger.Println("Event dropped due to transport buffer being full.")
// worker would block, drop the packet
}
Expand Down

0 comments on commit 091467d

Please sign in to comment.