Skip to content

Commit

Permalink
fix: segfault on Takesnapshot when go build -race (#252)
Browse files Browse the repository at this point in the history
* fix: segfault on Takesnapshot when go build -race

* update exetension readme and fix tracecontext

* update fasthttp version

---------

Co-authored-by: zhb192729 <[email protected]>
  • Loading branch information
y1yang0 and NameHaibinZhang authored Dec 11, 2024
1 parent d71d610 commit fec9612
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 25 deletions.
5 changes: 3 additions & 2 deletions example/extension/nethttp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ Replace the `Path` in `config.json` with the actual absolute path of `rules` dir
"OnEnter":"httpClientEnterHook",
"ReceiverType": "*Transport",
"OnExit": "httpClientExitHook",
"Path": "/Users/zhanghaibin/Desktop/opentelemetry-go-auto-instrumentation/example/extension/nethttp/rules"
"Path": "/path/opentelemetry-go-auto-instrumentation/example/extension/nethttp/rules"
}]
```

## Step2: Compile the target binary with otel
Use `otel` to build the binary with `config.json`:
```
cd example/extension/netHttp
cd example/extension/nethttp
../../../otel set -rule=config.json
../../../otel -rule=config.json go build demo/net_http.go
```
Users can get the `otel` according to [documentation](../../../README.md)
Expand Down
2 changes: 1 addition & 1 deletion example/extension/nethttp/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"OnEnter":"httpClientEnterHook",
"ReceiverType": "*Transport",
"OnExit": "httpClientExitHook",
"Path": "/path/to/nethttp/rules"
"Path": "/path/opentelemetry-go-auto-instrumentation/example/extension/nethttp/rules"
}
]
3 changes: 2 additions & 1 deletion example/extension/sqlinject/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ Replace the `Path` in `config.json` with the actual absolute path of `rules` dir
Use `otel` to build the binary with `config.json`:
```
cd example/extension/sqlinject
../../../otel -rule=config.json go build demo/sql_inject.go
../../../otel set -rule=config.json
../../../otel go build demo/sql_inject.go
```
Users can get the `otel` according to [documentation](../../../README.md)

Expand Down
4 changes: 2 additions & 2 deletions pkg/data/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@
"Path": "github.com/alibaba/opentelemetry-go-auto-instrumentation/pkg/rules/hertz/client"
},
{
"Version": "[1.45.0,1.57.1)",
"Version": "[1.45.0,1.58.1)",
"ImportPath": "github.com/valyala/fasthttp",
"Function": "Do",
"ReceiverType": "*HostClient",
Expand All @@ -530,7 +530,7 @@
"Path": "github.com/alibaba/opentelemetry-go-auto-instrumentation/pkg/rules/fasthttp"
},
{
"Version": "[1.45.0,1.57.1)",
"Version": "[1.45.0,1.58.1)",
"ImportPath": "github.com/valyala/fasthttp",
"Function": "ListenAndServe",
"ReceiverType": "*Server",
Expand Down
1 change: 1 addition & 0 deletions pkg/rules/otel-sdk/otel_baggage_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type BaggageContainer struct {
baggage interface{}
}

//go:norace
func (bc *BaggageContainer) TakeSnapShot() interface{} {
return &BaggageContainer{bc.baggage}
}
Expand Down
32 changes: 13 additions & 19 deletions pkg/rules/otel-sdk/trace-context/otel_trace_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ import (
const maxSpans = 300

type traceContext struct {
sw *spanWrapper
n int
Data map[string]interface{}
lcs trace.Span
sw *spanWrapper
n int
lcs trace.Span
}

type spanWrapper struct {
Expand Down Expand Up @@ -54,6 +53,7 @@ func (tc *traceContext) add(span trace.Span) bool {
return true
}

//go:norace
func (tc *traceContext) tail() trace.Span {
if tc.n == 0 {
return nil
Expand Down Expand Up @@ -92,43 +92,37 @@ func (tc *traceContext) del(span trace.Span) {
func (tc *traceContext) clear() {
tc.sw = nil
tc.n = 0
tc.Data = nil
SetBaggageContainerToGLS(nil)
}

//go:norace
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, dataCopy, nil}
return &traceContext{nil, 0, nil}
}
last := tc.tail()
sw := &spanWrapper{last, nil}
return &traceContext{sw, 1, dataCopy, nil}
return &traceContext{sw, 1, nil}
}

func GetGLocalData(key string) interface{} {
t := getOrInitTraceContext()
r := t.Data[key]
return r
//todo set key into traceContext struct
//t := getOrInitTraceContext()

return nil
}

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

setTraceContext(t)
}

func getOrInitTraceContext() *traceContext {
tc := GetTraceContextFromGLS()
if tc == nil {
newTc := &traceContext{nil, 0, nil, nil}
newTc := &traceContext{nil, 0, nil}
setTraceContext(newTc)
return newTc
} else {
Expand Down

0 comments on commit fec9612

Please sign in to comment.