Skip to content

Commit

Permalink
fix: Scope values should not override Event values (#446)
Browse files Browse the repository at this point in the history
Co-authored-by: Michi Hoffmann <cleptric@users.noreply.github.com>
  • Loading branch information
Craig Pastro and cleptric committed Oct 31, 2022
1 parent bf68bd2 commit a62b88a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
6 changes: 5 additions & 1 deletion scope.go
Expand Up @@ -365,7 +365,11 @@ func (scope *Scope) ApplyToEvent(event *Event, hint *EventHint) *Event {
// to link errors and traces/spans in Sentry.
continue
}
event.Contexts[key] = value

// Ensure we are not overwriting event fields
if _, ok := event.Contexts[key]; !ok {
event.Contexts[key] = value
}
}
}

Expand Down
19 changes: 14 additions & 5 deletions scope_test.go
Expand Up @@ -7,13 +7,18 @@ import (
"time"
)

const sharedContextsKey = "sharedContextsKey"

var testNow = time.Now().UTC()

func fillScopeWithData(scope *Scope) *Scope {
scope.breadcrumbs = []*Breadcrumb{{Timestamp: testNow, Message: "scopeBreadcrumbMessage"}}
scope.user = User{ID: "1337"}
scope.tags = map[string]string{"scopeTagKey": "scopeTagValue"}
scope.contexts = map[string]Context{"scopeContextsKey": {"scopeContextKey": "scopeContextValue"}}
scope.contexts = map[string]Context{
"scopeContextsKey": {"scopeContextKey": "scopeContextValue"},
sharedContextsKey: {"scopeContextKey": "scopeContextValue"},
}
scope.extra = map[string]interface{}{"scopeExtraKey": "scopeExtraValue"}
scope.fingerprint = []string{"scopeFingerprintOne", "scopeFingerprintTwo"}
scope.level = LevelDebug
Expand All @@ -26,7 +31,10 @@ func fillEventWithData(event *Event) *Event {
event.Breadcrumbs = []*Breadcrumb{{Timestamp: testNow, Message: "eventBreadcrumbMessage"}}
event.User = User{ID: "42"}
event.Tags = map[string]string{"eventTagKey": "eventTagValue"}
event.Contexts = map[string]Context{"eventContextsKey": {"eventContextKey": "eventContextValue"}}
event.Contexts = map[string]Context{
"eventContextsKey": {"eventContextKey": "eventContextValue"},
sharedContextsKey: {"eventContextKey": "eventContextKey"},
}
event.Extra = map[string]interface{}{"eventExtraKey": "eventExtraValue"}
event.Fingerprint = []string{"eventFingerprintOne", "eventFingerprintTwo"}
event.Level = LevelInfo
Expand Down Expand Up @@ -541,7 +549,8 @@ func TestApplyToEventWithCorrectScopeAndEvent(t *testing.T) {

assertEqual(t, len(processedEvent.Breadcrumbs), 2, "should merge breadcrumbs")
assertEqual(t, len(processedEvent.Tags), 2, "should merge tags")
assertEqual(t, len(processedEvent.Contexts), 2, "should merge contexts")
assertEqual(t, len(processedEvent.Contexts), 3, "should merge contexts")
assertEqual(t, event.Contexts[sharedContextsKey], event.Contexts[sharedContextsKey], "should not override event context")
assertEqual(t, len(processedEvent.Extra), 2, "should merge extra")
assertEqual(t, processedEvent.Level, scope.level, "should use scope level if its set")
assertEqual(t, processedEvent.Transaction, scope.transaction, "should use scope transaction if its set")
Expand All @@ -558,7 +567,7 @@ func TestApplyToEventUsingEmptyScope(t *testing.T) {

assertEqual(t, len(processedEvent.Breadcrumbs), 1, "should use event breadcrumbs")
assertEqual(t, len(processedEvent.Tags), 1, "should use event tags")
assertEqual(t, len(processedEvent.Contexts), 1, "should use event contexts")
assertEqual(t, len(processedEvent.Contexts), 2, "should use event contexts")
assertEqual(t, len(processedEvent.Extra), 1, "should use event extra")
assertNotEqual(t, processedEvent.User, scope.user, "should use event user")
assertNotEqual(t, processedEvent.Fingerprint, scope.fingerprint, "should use event fingerprint")
Expand All @@ -575,7 +584,7 @@ func TestApplyToEventUsingEmptyEvent(t *testing.T) {

assertEqual(t, len(processedEvent.Breadcrumbs), 1, "should use scope breadcrumbs")
assertEqual(t, len(processedEvent.Tags), 1, "should use scope tags")
assertEqual(t, len(processedEvent.Contexts), 1, "should use scope contexts")
assertEqual(t, len(processedEvent.Contexts), 2, "should use scope contexts")
assertEqual(t, len(processedEvent.Extra), 1, "should use scope extra")
assertEqual(t, processedEvent.User, scope.user, "should use scope user")
assertEqual(t, processedEvent.Fingerprint, scope.fingerprint, "should use scope fingerprint")
Expand Down

0 comments on commit a62b88a

Please sign in to comment.