Skip to content

Commit

Permalink
完成链路追踪调整
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Dec 31, 2023
1 parent 69f964f commit 684c590
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 83 deletions.
6 changes: 0 additions & 6 deletions TraceDetailMq.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,3 @@ func (receiver *TraceDetailMq) GetTraceDetail() *trace.BaseTraceDetail {
func (receiver *TraceDetailMq) ToString() string {
return fmt.Sprintf("[%s]耗时:%s,%s Server=%s,Exchange=%s,RoutingKey=%s", flog.Yellow(receiver.CallType.ToString()), flog.Red(receiver.UseTs.String()), receiver.MethodName, receiver.Server, receiver.Exchange, receiver.RoutingKey)
}

func (receiver *TraceDetailMq) Desc() (caption string, desc string) {
caption = fmt.Sprintf("发送MQ消息 => %s %s %s", receiver.Server, receiver.Exchange, receiver.RoutingKey)
desc = fmt.Sprintf("%s %s %s", receiver.Server, receiver.Exchange, receiver.RoutingKey)
return
}
10 changes: 0 additions & 10 deletions traceDetailDatabase.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,3 @@ func (receiver *TraceDetailDatabase) SetSql(connectionString string, DbName stri
receiver.Sql = sql
receiver.RowsAffected = rowsAffected
}

func (receiver *TraceDetailDatabase) Desc() (caption string, desc string) {
if receiver.TableName == "" && receiver.Sql == "" {
caption = fmt.Sprintf("打开数据库 => %s %s", receiver.DbName, receiver.ConnectionString)
} else {
caption = fmt.Sprintf("执行数据库 => %s %s 影响%v行", receiver.DbName, receiver.TableName, receiver.RowsAffected)
}
desc = receiver.Sql
return
}
6 changes: 0 additions & 6 deletions traceDetailEs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,3 @@ func (receiver *TraceDetailEs) GetTraceDetail() *trace.BaseTraceDetail {
func (receiver *TraceDetailEs) ToString() string {
return fmt.Sprintf("[%s]耗时:%s,%s IndexName=%s,AliasesName=%s", flog.Yellow(receiver.CallType.ToString()), flog.Red(receiver.UseTs.String()), receiver.MethodName, receiver.IndexName, receiver.AliasesName)
}

func (receiver *TraceDetailEs) Desc() (caption string, desc string) {
caption = fmt.Sprintf("执行ES => %s %s", receiver.IndexName, receiver.AliasesName)
desc = fmt.Sprintf("%s %s", receiver.IndexName, receiver.AliasesName)
return
}
6 changes: 0 additions & 6 deletions traceDetailEtcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,3 @@ func (receiver *TraceDetailEtcd) GetTraceDetail() *trace.BaseTraceDetail {
func (receiver *TraceDetailEtcd) ToString() string {
return fmt.Sprintf("[%s]耗时:%s,%s Key=%s LeaseID=%v", flog.Yellow(receiver.CallType.ToString()), flog.Red(receiver.UseTs.String()), receiver.MethodName, receiver.Key, receiver.LeaseID)
}

func (receiver *TraceDetailEtcd) Desc() (caption string, desc string) {
caption = fmt.Sprintf("执行Etcd => %s %v", receiver.Key, receiver.LeaseID)
desc = fmt.Sprintf("%s %v", receiver.Key, receiver.LeaseID)
return
}
10 changes: 0 additions & 10 deletions traceDetailGrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,3 @@ func (receiver *TraceDetailGrpc) SetHttpRequest(url string, head map[string]any,
receiver.Headers.Add(k, parse.ToString(v))
}
}

func (receiver *TraceDetailGrpc) Desc() (caption string, desc string) {
caption = fmt.Sprintf("调用http => %v %s %s", receiver.StatusCode, receiver.Method, receiver.Url)
lstHeader := collections.NewList[string]()
for k, v := range receiver.Headers.ToMap() {
lstHeader.Add(fmt.Sprintf("%s=%v", k, v))
}
desc = fmt.Sprintf("头部:%s 入参:%s 出参:%s", lstHeader.ToString(","), receiver.RequestBody, receiver.ResponseBody)
return
}
6 changes: 0 additions & 6 deletions traceDetailHand.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,3 @@ func (receiver *TraceDetailHand) GetTraceDetail() *trace.BaseTraceDetail {
func (receiver *TraceDetailHand) ToString() string {
return fmt.Sprintf("[%s]耗时:%s, %s", flog.Yellow(receiver.CallType.ToString()), flog.Red(receiver.UseTs.String()), receiver.Name)
}

func (receiver *TraceDetailHand) Desc() (caption string, desc string) {
caption = fmt.Sprintf("手动埋点 => %s", receiver.Name)
desc = fmt.Sprintf("%s", receiver.Name)
return
}
10 changes: 0 additions & 10 deletions traceDetailHttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,3 @@ func (receiver *TraceDetailHttp) SetHttpRequest(url string, head map[string]any,
receiver.Headers.Add(k, parse.ToString(v))
}
}

func (receiver *TraceDetailHttp) Desc() (caption string, desc string) {
caption = fmt.Sprintf("调用http => %v %s %s", receiver.StatusCode, receiver.Method, receiver.Url)
lstHeader := collections.NewList[string]()
for k, v := range receiver.Headers.ToMap() {
lstHeader.Add(fmt.Sprintf("%s=%v", k, v))
}
desc = fmt.Sprintf("头部:%s 入参:%s 出参:%s", lstHeader.ToString(","), receiver.RequestBody, receiver.ResponseBody)
return
}
6 changes: 0 additions & 6 deletions traceDetailRedis.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,3 @@ func (receiver *TraceDetailRedis) GetTraceDetail() *trace.BaseTraceDetail {
func (receiver *TraceDetailRedis) ToString() string {
return fmt.Sprintf("[%s]耗时:%s,%s Key=%s,Field=%s", flog.Yellow(receiver.CallType.ToString()), flog.Red(receiver.UseTs.String()), receiver.MethodName, receiver.Key, receiver.Field)
}

func (receiver *TraceDetailRedis) Desc() (caption string, desc string) {
caption = fmt.Sprintf("执行Redis => %s %s %s", receiver.MethodName, receiver.Key, receiver.Field)
desc = fmt.Sprintf("%s %s", receiver.Key, receiver.Field)
return
}
2 changes: 1 addition & 1 deletion traceManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func add(traceDetail trace.ITraceDetail) {
detail.Timeline = time.Duration(detail.StartTs-t.GetStartTs()) * time.Microsecond
details := t.GetList()
if len(details) > 0 {
detail.UnTraceTs = time.Duration(detail.StartTs-details[len(details)-1].GetTraceDetail().EndTs) * time.Microsecond
detail.UnTraceTs = time.Duration(detail.StartTs-details[len(details)-1].(trace.ITraceDetail).GetTraceDetail().EndTs) * time.Microsecond
} else {
detail.UnTraceTs = time.Duration(detail.StartTs-t.GetStartTs()) * time.Microsecond
}
Expand Down
49 changes: 27 additions & 22 deletions trackContext.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package linkTrace
import (
"fmt"
"github.com/farseer-go/collections"
"github.com/farseer-go/fs/dateTime"
"github.com/farseer-go/fs/flog"
"github.com/farseer-go/fs/parse"
"github.com/farseer-go/fs/trace"
Expand All @@ -13,22 +14,24 @@ import (
)

type TraceContext struct {
TraceId int64 // 上下文ID
AppId int64 // 应用ID
AppName string // 应用名称
AppIp string // 应用IP
ParentAppName string // 上游应用
StartTs int64 // 调用开始时间戳(微秒)
EndTs int64 // 调用结束时间戳(微秒)
UseTs time.Duration // 总共使用时间微秒
TraceType eumTraceType.Enum // 状态码
List []trace.ITraceDetail // 调用的上下文
ignore bool // 忽略这次的链路追踪
Exception trace.ExceptionStack // 异常信息
TraceId int64 // 上下文ID
AppId int64 // 应用ID
AppName string // 应用名称
AppIp string // 应用IP
ParentAppName string // 上游应用
StartTs int64 // 调用开始时间戳(微秒)
EndTs int64 // 调用结束时间戳(微秒)
UseTs time.Duration // 总共使用时间微秒
UseDesc string // 总共使用时间(描述)
TraceType eumTraceType.Enum // 状态码
List []any // 调用的上下文trace.ITraceDetail
ignore bool // 忽略这次的链路追踪
Exception *trace.ExceptionStack // 异常信息
WebContext
ConsumerContext
TaskContext
WatchKeyContext
CreateAt dateTime.DateTime // 请求时间
}

type WebContext struct {
Expand Down Expand Up @@ -97,9 +100,9 @@ func (receiver *TraceContext) End() {
receiver.EndTs = time.Now().UnixMicro()
receiver.UseTs = time.Duration(receiver.EndTs-receiver.StartTs) * time.Microsecond
// 移除忽略的明细
var newList []trace.ITraceDetail
var newList []any
for _, detail := range receiver.List {
if !detail.GetTraceDetail().IsIgnore() {
if !detail.(trace.ITraceDetail).GetTraceDetail().IsIgnore() {
newList = append(newList, detail)
}
}
Expand All @@ -118,7 +121,7 @@ func (receiver *TraceContext) Ignore() {
}

// GetList 获取链路明细
func (receiver *TraceContext) GetList() []trace.ITraceDetail {
func (receiver *TraceContext) GetList() []any {
return receiver.List
}

Expand All @@ -133,17 +136,17 @@ func (receiver *TraceContext) printLog() {
if defConfig.PrintLog {
lst := collections.NewList[string]()
for i := 0; i < len(receiver.List); i++ {
tab := strings.Repeat("\t", receiver.List[i].GetLevel()-1)
detail := receiver.List[i].GetTraceDetail()
log := fmt.Sprintf("%s%s (%s):%s", tab, flog.Blue(i+1), flog.Green(detail.UnTraceTs.String()), receiver.List[i].ToString())
tab := strings.Repeat("\t", receiver.List[i].(trace.ITraceDetail).GetLevel()-1)
detail := receiver.List[i].(trace.ITraceDetail).GetTraceDetail()
log := fmt.Sprintf("%s%s (%s):%s", tab, flog.Blue(i+1), flog.Green(detail.UnTraceTs.String()), receiver.List[i].(trace.ITraceDetail).ToString())
lst.Add(log)

if detail.Exception.ExceptionIsException {
if detail.Exception != nil && detail.Exception.ExceptionIsException {
lst.Add(fmt.Sprintf("%s:%s %s 出错了:%s", detail.Exception.ExceptionCallFile, flog.Blue(detail.Exception.ExceptionCallLine), flog.Red(detail.Exception.ExceptionCallFuncName), flog.Red(detail.Exception.ExceptionMessage)))
}
}

if receiver.Exception.ExceptionIsException {
if receiver.Exception != nil && receiver.Exception.ExceptionIsException {
lst.Add(fmt.Sprintf("%s%s:%s %s %s", flog.Red("【异常】"), flog.Blue(receiver.Exception.ExceptionCallFile), flog.Blue(receiver.Exception.ExceptionCallLine), flog.Green(receiver.Exception.ExceptionCallFuncName), flog.Red(receiver.Exception.ExceptionMessage)))
}

Expand All @@ -164,8 +167,10 @@ func (receiver *TraceContext) printLog() {

func (receiver *TraceContext) Error(err error) {
if err != nil {
receiver.Exception.ExceptionIsException = true
receiver.Exception.ExceptionMessage = err.Error()
receiver.Exception = &trace.ExceptionStack{
ExceptionIsException: true,
ExceptionMessage: err.Error(),
}
receiver.Exception.ExceptionCallFile, receiver.Exception.ExceptionCallFuncName, receiver.Exception.ExceptionCallLine = trace.GetCallerInfo()
}
}

0 comments on commit 684c590

Please sign in to comment.