Skip to content

Commit c2561eb

Browse files
authored
fix: context data deep copy (#41)
* fix: fix #16 * fix: make data in traceContext deep copy
1 parent 3268163 commit c2561eb

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

pkg/rules/otsdk/trace-context/ot_trace_context.go

+16-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const maxSpans = 300
1212
type traceContext struct {
1313
sw *spanWrapper
1414
n int
15-
Data interface{}
15+
Data map[string]interface{}
1616
}
1717

1818
type spanWrapper struct {
@@ -71,22 +71,31 @@ func (tc *traceContext) clear() {
7171
}
7272

7373
func (tc *traceContext) TakeSnapShot() interface{} {
74+
// take a deep copy to avoid reading & writing the same map at the same time
75+
var dataCopy = make(map[string]interface{})
76+
for key, value := range tc.Data {
77+
dataCopy[key] = value
78+
}
7479
if tc.n == 0 {
75-
return &traceContext{nil, 0, tc.Data}
80+
return &traceContext{nil, 0, dataCopy}
7681
}
7782
last := tc.tail()
7883
sw := &spanWrapper{last, nil}
79-
return &traceContext{sw, 1, tc.Data}
84+
return &traceContext{sw, 1, dataCopy}
8085
}
8186

82-
func GetGLocalData() interface{} {
87+
func GetGLocalData(key string) interface{} {
8388
t := getOrInitTraceContext()
84-
return t.Data
89+
r := t.Data[key]
90+
return r
8591
}
8692

87-
func SetGLocalData(data interface{}) {
93+
func SetGLocalData(key string, value interface{}) {
8894
t := getOrInitTraceContext()
89-
t.Data = data
95+
if t.Data == nil {
96+
t.Data = make(map[string]interface{})
97+
}
98+
t.Data[key] = value
9099
setTraceContext(t)
91100
}
92101

0 commit comments

Comments
 (0)