Skip to content

Commit

Permalink
fix: context data deep copy (#41)
Browse files Browse the repository at this point in the history
* fix: fix #16

* fix: make data in traceContext deep copy
  • Loading branch information
123liuziming authored Aug 14, 2024
1 parent 3268163 commit c2561eb
Showing 1 changed file with 16 additions and 7 deletions.
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

0 comments on commit c2561eb

Please sign in to comment.