From 684c590c92f6e4d74f37271ed7756f66fb6306c8 Mon Sep 17 00:00:00 2001 From: steden <1470804@qq.com> Date: Sun, 31 Dec 2023 15:42:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E9=93=BE=E8=B7=AF=E8=BF=BD?= =?UTF-8?q?=E8=B8=AA=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TraceDetailMq.go | 6 ------ traceDetailDatabase.go | 10 --------- traceDetailEs.go | 6 ------ traceDetailEtcd.go | 6 ------ traceDetailGrpc.go | 10 --------- traceDetailHand.go | 6 ------ traceDetailHttp.go | 10 --------- traceDetailRedis.go | 6 ------ traceManager.go | 2 +- trackContext.go | 49 +++++++++++++++++++++++------------------- 10 files changed, 28 insertions(+), 83 deletions(-) diff --git a/TraceDetailMq.go b/TraceDetailMq.go index 557671f..0c78cf9 100644 --- a/TraceDetailMq.go +++ b/TraceDetailMq.go @@ -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 -} diff --git a/traceDetailDatabase.go b/traceDetailDatabase.go index 80e3c6f..3dda040 100644 --- a/traceDetailDatabase.go +++ b/traceDetailDatabase.go @@ -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 -} diff --git a/traceDetailEs.go b/traceDetailEs.go index a1e1df6..dc4c6b2 100644 --- a/traceDetailEs.go +++ b/traceDetailEs.go @@ -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 -} diff --git a/traceDetailEtcd.go b/traceDetailEtcd.go index b228c3b..48b826b 100644 --- a/traceDetailEtcd.go +++ b/traceDetailEtcd.go @@ -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 -} diff --git a/traceDetailGrpc.go b/traceDetailGrpc.go index a5f1fcf..2ee256c 100644 --- a/traceDetailGrpc.go +++ b/traceDetailGrpc.go @@ -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 -} diff --git a/traceDetailHand.go b/traceDetailHand.go index 616c073..7dd4fcc 100644 --- a/traceDetailHand.go +++ b/traceDetailHand.go @@ -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 -} diff --git a/traceDetailHttp.go b/traceDetailHttp.go index feb5c32..5ea9348 100644 --- a/traceDetailHttp.go +++ b/traceDetailHttp.go @@ -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 -} diff --git a/traceDetailRedis.go b/traceDetailRedis.go index 79315bf..8721e5f 100644 --- a/traceDetailRedis.go +++ b/traceDetailRedis.go @@ -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 -} diff --git a/traceManager.go b/traceManager.go index fe74d1b..a830b9d 100644 --- a/traceManager.go +++ b/traceManager.go @@ -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 } diff --git a/trackContext.go b/trackContext.go index 573317c..77e380d 100644 --- a/trackContext.go +++ b/trackContext.go @@ -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" @@ -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 { @@ -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) } } @@ -118,7 +121,7 @@ func (receiver *TraceContext) Ignore() { } // GetList 获取链路明细 -func (receiver *TraceContext) GetList() []trace.ITraceDetail { +func (receiver *TraceContext) GetList() []any { return receiver.List } @@ -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))) } @@ -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() } }