Skip to content

fix: context data deep copy #41

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

Merged
merged 3 commits into from
Aug 14, 2024
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
23 changes: 16 additions & 7 deletions pkg/rules/otsdk/trace-context/ot_trace_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const maxSpans = 300
type traceContext struct {
sw *spanWrapper
n int
Data interface{}
Data map[string]interface{}
}

type spanWrapper struct {
Expand Down Expand Up @@ -71,22 +71,31 @@ func (tc *traceContext) clear() {
}

func (tc *traceContext) TakeSnapShot() interface{} {
// take a deep copy to avoid reading & writing the same map at the same time
var dataCopy = make(map[string]interface{})
for key, value := range tc.Data {
dataCopy[key] = value
}
if tc.n == 0 {
return &traceContext{nil, 0, tc.Data}
return &traceContext{nil, 0, dataCopy}
}
last := tc.tail()
sw := &spanWrapper{last, nil}
return &traceContext{sw, 1, tc.Data}
return &traceContext{sw, 1, dataCopy}
}

func GetGLocalData() interface{} {
func GetGLocalData(key string) interface{} {
t := getOrInitTraceContext()
return t.Data
r := t.Data[key]
return r
}

func SetGLocalData(data interface{}) {
func SetGLocalData(key string, value interface{}) {
t := getOrInitTraceContext()
t.Data = data
if t.Data == nil {
t.Data = make(map[string]interface{})
}
t.Data[key] = value
setTraceContext(t)
}

Expand Down
Loading