Skip to content

Commit

Permalink
调整:移除routine依赖,改用asyncLocal类型
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Oct 19, 2023
1 parent 19000b8 commit 34ac27d
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 18 deletions.
14 changes: 2 additions & 12 deletions global.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
package linkTrace

import (
"github.com/farseer-go/fs/asyncLocal"
"github.com/farseer-go/fs/trace"
"github.com/timandy/routine"
)

// CurTraceContext 当前请求的Trace上下文
var curTraceContext = routine.NewInheritableThreadLocal[trace.ITraceContext]()

// getCurTrace 获取当前TrackContext
func getCurTrace() trace.ITraceContext {
return curTraceContext.Get()
}

// setCurTrace 设置当前TrackContext
func setCurTrace(context trace.ITraceContext) {
curTraceContext.Set(context)
}
var curTraceContext = asyncLocal.New[trace.ITraceContext]()
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ require (
github.com/farseer-go/fs v0.8.0
github.com/farseer-go/mapper v0.8.0
github.com/farseer-go/queue v0.8.0
github.com/timandy/routine v1.1.1
)

require (
github.com/devfeel/mapper v0.7.13 // indirect
github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203 // indirect
github.com/farseer-go/utils v0.8.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/olivere/elastic/v7 v7.0.32 // indirect
Expand Down
2 changes: 1 addition & 1 deletion module.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (module Module) PreInitialize() {
defConfig = configure.ParseConfig[config]("LinkTrace")
// 使用了链路追踪组件,则要把空组件移除后,重新注册
container.Remove[trace.IManager]()
container.Register[trace.IManager](func() trace.IManager {
container.Register(func() trace.IManager {
return &traceManager{}
})
}
Expand Down
6 changes: 3 additions & 3 deletions traceManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type traceManager struct {
}

func (*traceManager) GetCurTrace() trace.ITraceContext {
return getCurTrace()
return curTraceContext.Get()
}

// TraceWebApi Webapi入口
Expand All @@ -42,7 +42,7 @@ func (*traceManager) TraceWebApi(domain string, path string, method string, cont
List: collections.NewList[trace.ITraceDetail](),
//ExceptionDetail: ExceptionDetail{},
}
setCurTrace(context)
curTraceContext.Set(context)
return context
}

Expand Down Expand Up @@ -152,7 +152,7 @@ func newTraceDetail(callType eumCallType.Enum, callMethod string) trace.BaseTrac
}

func add(traceDetail trace.ITraceDetail) {
if t := getCurTrace(); t != nil && defConfig.Enable {
if t := curTraceContext.Get(); t != nil && defConfig.Enable {
detail := traceDetail.GetTraceDetail()
// 时间轴:上下文入口起点时间到本次开始时间
detail.Timeline = time.Duration(detail.StartTs-t.GetStartTs()) * time.Microsecond
Expand Down
5 changes: 4 additions & 1 deletion update.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
go get github.com/farseer-go/collections
go get github.com/farseer-go/elasticSearch
go get github.com/farseer-go/fs
go get github.com/farseer-go/utils
go get github.com/farseer-go/mapper
go get github.com/farseer-go/queue
go get github.com/farseer-go/utils

0 comments on commit 34ac27d

Please sign in to comment.