Skip to content

Commit

Permalink
实现链路的scope范围功能
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Oct 21, 2023
1 parent daaac14 commit 72e5681
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
27 changes: 22 additions & 5 deletions traceManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (*traceManager) EntryWebApi(domain string, path string, method string, cont
List: collections.NewList[trace.ITraceDetail](),
}
curTraceContext.Set(context)
trace.ScopeLevel.Set(collections.NewList[trace.BaseTraceDetail]())
return context
}

Expand Down Expand Up @@ -141,6 +142,7 @@ func (*traceManager) EntryMqConsumer(server string, queueName string, routingKey
List: collections.NewList[trace.ITraceDetail](),
}
curTraceContext.Set(context)
trace.ScopeLevel.Set(collections.NewList[trace.BaseTraceDetail]())
return context
}

Expand All @@ -162,6 +164,7 @@ func (*traceManager) EntryQueueConsumer(subscribeName string) trace.ITraceContex
List: collections.NewList[trace.ITraceDetail](),
}
curTraceContext.Set(context)
trace.ScopeLevel.Set(collections.NewList[trace.BaseTraceDetail]())
return context
}

Expand All @@ -182,6 +185,7 @@ func (*traceManager) EntryTask(taskName string) trace.ITraceContext {
List: collections.NewList[trace.ITraceDetail](),
}
curTraceContext.Set(context)
trace.ScopeLevel.Set(collections.NewList[trace.BaseTraceDetail]())
return context
}

Expand All @@ -204,6 +208,7 @@ func (*traceManager) EntryFSchedule(taskGroupName string, taskGroupId int64, tas
List: collections.NewList[trace.ITraceDetail](),
}
curTraceContext.Set(context)
trace.ScopeLevel.Set(collections.NewList[trace.BaseTraceDetail]())
return context
}

Expand All @@ -224,6 +229,7 @@ func (*traceManager) EntryWatchKey(key string) trace.ITraceContext {
List: collections.NewList[trace.ITraceDetail](),
}
curTraceContext.Set(context)
trace.ScopeLevel.Set(collections.NewList[trace.BaseTraceDetail]())
return context
}

Expand Down Expand Up @@ -261,12 +267,23 @@ func (*traceManager) TraceHttp(method string, url string) trace.ITraceDetail {
}

func newTraceDetail(callType eumCallType.Enum, callMethod string) trace.BaseTraceDetail {
return trace.BaseTraceDetail{
CallMethod: callMethod,
CallType: callType,
StartTs: time.Now().UnixMicro(),
EndTs: time.Now().UnixMicro(),
// 获取当前层级列表
lstScope := trace.ScopeLevel.Get()
baseTraceDetail := trace.BaseTraceDetail{
DetailId: snowflake.GenerateId(),
Level: lstScope.Count() + 1,
ParentDetailId: lstScope.Last().DetailId,
CallMethod: callMethod,
CallType: callType,
StartTs: time.Now().UnixMicro(),
EndTs: time.Now().UnixMicro(),
}
// 加入到当前层级列表
if !lstScope.IsNil() {
lstScope.Add(baseTraceDetail)
trace.ScopeLevel.Set(lstScope)
}
return baseTraceDetail
}

func add(traceDetail trace.ITraceDetail) {
Expand Down
3 changes: 2 additions & 1 deletion trackContext.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ func (receiver *TraceContext) printLog() {
if defConfig.PrintLog {
lst := collections.NewList[string]()
for i := 0; i < receiver.List.Count(); i++ {
log := fmt.Sprintf("%s(%s):%s", flog.Blue(i+1), flog.Green(receiver.List.Index(i).GetTraceDetail().UnTraceTs.String()), receiver.List.Index(i).ToString())
tab := strings.Repeat("\t", receiver.List.Index(i).GetLevel()-1)
log := fmt.Sprintf("%s%s(%s):%s", tab, flog.Blue(i+1), flog.Green(receiver.List.Index(i).GetTraceDetail().UnTraceTs.String()), receiver.List.Index(i).ToString())
lst.Add(log)
}
flog.Printf("【链路追踪】TraceId:%s,耗时:%s,%s:\n%s\n", flog.Green(parse.ToString(receiver.TraceId)), flog.Red(receiver.UseTs.String()), receiver.Web.Path, strings.Join(lst.ToArray(), "\n"))
Expand Down

0 comments on commit 72e5681

Please sign in to comment.