Skip to content
Open
Show file tree
Hide file tree
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
16 changes: 10 additions & 6 deletions dynamic_sampling_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,11 @@ func TestDynamicSamplingContextFromScope(t *testing.T) {
}{
"Valid input": {
scope: &Scope{
propagationContext: PropagationContext{
TraceID: TraceIDFromHex("d49d9bf66f13450b81f65bc51cf49c03"),
SpanID: SpanIDFromHex("a9f442f9330b4e09"),
scopeData: scopeData{
propagationContext: PropagationContext{
TraceID: TraceIDFromHex("d49d9bf66f13450b81f65bc51cf49c03"),
SpanID: SpanIDFromHex("a9f442f9330b4e09"),
},
},
},
client: func() *Client {
Expand All @@ -215,9 +217,11 @@ func TestDynamicSamplingContextFromScope(t *testing.T) {
},
"Nil client": {
scope: &Scope{
propagationContext: PropagationContext{
TraceID: TraceIDFromHex("d49d9bf66f13450b81f65bc51cf49c03"),
SpanID: SpanIDFromHex("a9f442f9330b4e09"),
scopeData: scopeData{
propagationContext: PropagationContext{
TraceID: TraceIDFromHex("d49d9bf66f13450b81f65bc51cf49c03"),
SpanID: SpanIDFromHex("a9f442f9330b4e09"),
},
},
},
client: nil,
Expand Down
58 changes: 36 additions & 22 deletions scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ import (
// an event for reporting, the current client adds information from the current
// scope into the event.
type Scope struct {
mu sync.RWMutex
mu sync.RWMutex
eventProcessors []EventProcessor

// scopeData keeps track of all scope specific data
scopeData
}

type scopeData struct {
attributes map[string]attribute.Value
breadcrumbs []*Breadcrumb
attachments []*Attachment
Expand All @@ -49,15 +56,18 @@ type Scope struct {
// size.
Overflow() bool
}
eventProcessors []EventProcessor

propagationContext PropagationContext
span *Span
}

// NewScope creates a new Scope.
func NewScope() *Scope {
return &Scope{
return &Scope{scopeData: newScopeData()}
}

func newScopeData() scopeData {
return scopeData{
attributes: make(map[string]attribute.Value),
breadcrumbs: make([]*Breadcrumb, 0),
attachments: make([]*Attachment, 0),
Expand Down Expand Up @@ -280,29 +290,33 @@ func (scope *Scope) Clone() *Scope {
scope.mu.RLock()
defer scope.mu.RUnlock()

clone := NewScope()
clone.user = scope.user
clone.breadcrumbs = make([]*Breadcrumb, len(scope.breadcrumbs))
copy(clone.breadcrumbs, scope.breadcrumbs)
clone.attachments = make([]*Attachment, len(scope.attachments))
copy(clone.attachments, scope.attachments)
clone.attributes = maps.Clone(scope.attributes)
clone.contexts = maps.Clone(scope.contexts)
clone.tags = maps.Clone(scope.tags)
clone.fingerprint = make([]string, len(scope.fingerprint))
copy(clone.fingerprint, scope.fingerprint)
clone.level = scope.level
clone.request = scope.request
clone.requestBody = scope.requestBody
clone.eventProcessors = scope.eventProcessors[:len(scope.eventProcessors):len(scope.eventProcessors)]
clone.propagationContext = scope.propagationContext
clone.span = scope.span
data := scope.scopeData
return &Scope{
scopeData: data.clone(),
eventProcessors: scope.eventProcessors[:len(scope.eventProcessors):len(scope.eventProcessors)],
}
}

func (data scopeData) clone() scopeData {
Comment thread
sentry[bot] marked this conversation as resolved.
clone := data
clone.breadcrumbs = make([]*Breadcrumb, len(data.breadcrumbs))
copy(clone.breadcrumbs, data.breadcrumbs)
clone.attachments = make([]*Attachment, len(data.attachments))
copy(clone.attachments, data.attachments)
clone.attributes = maps.Clone(data.attributes)
clone.contexts = maps.Clone(data.contexts)
clone.tags = maps.Clone(data.tags)
clone.fingerprint = make([]string, len(data.fingerprint))
copy(clone.fingerprint, data.fingerprint)
return clone
}

// Clear removes the data from the current scope. Not safe for concurrent use.
// Clear removes data from the scope while retaining event processors.
func (scope *Scope) Clear() {
*scope = *NewScope()
scope.mu.Lock()
defer scope.mu.Unlock()

scope.scopeData = newScopeData()
}

// AddEventProcessor adds an event processor to the current scope.
Expand Down
3 changes: 3 additions & 0 deletions scope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,8 @@ func TestScopeChildOverrideInheritance(t *testing.T) {

func TestClear(t *testing.T) {
scope := fillScopeWithData(NewScope())
processor := func(event *Event, _ *EventHint) *Event { return event }
scope.AddEventProcessor(processor)
scope.Clear()

assertEqual(t, []*Breadcrumb{}, scope.breadcrumbs)
Expand All @@ -551,6 +553,7 @@ func TestClear(t *testing.T) {
assertEqual(t, Level(""), scope.level)
assertEqual(t, (*http.Request)(nil), scope.request)
assertEqual(t, (*Span)(nil), scope.GetSpan())
assertEqual(t, 1, len(scope.eventProcessors))
}

func TestClearAndReconfigure(t *testing.T) {
Expand Down
Loading