Skip to content

Commit

Permalink
调整链路追踪的字段
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Dec 26, 2023
1 parent d9e46d3 commit 09ab6d7
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 84 deletions.
2 changes: 1 addition & 1 deletion TraceDetailMq.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ 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)
return fmt.Sprintf("[%s]耗时:%s,%s ConsumerServer=%s,Exchange=%s,ConsumerRoutingKey=%s", flog.Yellow(receiver.CallType.ToString()), flog.Red(receiver.UseTs.String()), receiver.MethodName, receiver.Server, receiver.Exchange, receiver.RoutingKey)
}
44 changes: 22 additions & 22 deletions driver/clickhouse/traceContextPO.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@ type TraceContextPO struct {
TraceType eumTraceType.Enum `gorm:"not null;comment:状态码"`
Exception *ExceptionStackPO `gorm:"json;not null;comment:异常信息"`
List []trace.ITraceDetail `gorm:"json;not null;comment:调用的上下文"`
WebContextPO `gorm:"embedded;embeddedPrefix:web_;not null;comment:Web请求上下文"`
ConsumerContextPO `gorm:"embedded;embeddedPrefix:consumer_;not null;comment:消费上下文"`
TaskContextPO `gorm:"embedded;embeddedPrefix:task_;not null;comment:任务上下文"`
WatchKeyContextPO `gorm:"embedded;embeddedPrefix:watchkey_;not null;comment:Etcd上下文"`
WebContextPO `gorm:"embedded;not null;comment:Web请求上下文"`
ConsumerContextPO `gorm:"embedded;not null;comment:消费上下文"`
TaskContextPO `gorm:"embedded;not null;comment:任务上下文"`
WatchKeyContextPO `gorm:"embedded;not null;comment:Etcd上下文"`
}

type WebContextPO struct {
Domain string `gorm:"not null;default:'';comment:请求域名"`
Path string `gorm:"not null;default:'';comment:请求地址"`
Method string `gorm:"not null;default:'';comment:请求方式"`
ContentType string `gorm:"not null;default:'';comment:请求内容类型"`
StatusCode int `gorm:"not null;default:0;comment:状态码"`
Headers collections.Dictionary[string, string] `gorm:"type:String;json;not null;comment:请求头部"`
RequestBody string `gorm:"not null;default:'';comment:请求参数"`
ResponseBody string `gorm:"not null;default:'';comment:输出参数"`
RequestIp string `gorm:"not null;default:'';comment:客户端IP"`
WebDomain string `gorm:"not null;default:'';comment:请求域名"`
WebPath string `gorm:"not null;default:'';comment:请求地址"`
WebMethod string `gorm:"not null;default:'';comment:请求方式"`
WebContentType string `gorm:"not null;default:'';comment:请求内容类型"`
WebStatusCode int `gorm:"not null;default:0;comment:状态码"`
WebHeaders collections.Dictionary[string, string] `gorm:"type:String;json;not null;comment:请求头部"`
WebRequestBody string `gorm:"not null;default:'';comment:请求参数"`
WebResponseBody string `gorm:"not null;default:'';comment:输出参数"`
WebRequestIp string `gorm:"not null;default:'';comment:客户端IP"`
}

type ConsumerContextPO struct {
Server string `gorm:"not null;default:'';comment:Server"`
QueueName string `gorm:"not null;default:'';comment:队列名称"`
RoutingKey string `gorm:"not null;default:'';comment:路由KEY"`
ConsumerServer string `gorm:"not null;default:'';comment:ConsumerServer"`
ConsumerQueueName string `gorm:"not null;default:'';comment:队列名称"`
ConsumerRoutingKey string `gorm:"not null;default:'';comment:路由KEY"`
}

type TaskContextPO struct {
Expand All @@ -50,13 +50,13 @@ type TaskContextPO struct {
}

type WatchKeyContextPO struct {
Key string `gorm:"not null;default:'';comment:KEY"`
WatchKey string `gorm:"not null;default:'';comment:KEY"`
}

type ExceptionStackPO struct {
CallFile string `gorm:"not null;default:'';comment:调用者文件路径"`
CallLine int `gorm:"not null;default:0;comment:调用者行号"`
CallFuncName string `gorm:"not null;default:'';comment:调用者函数名称"`
IsException bool `gorm:"not null;default:false;comment:是否执行异常"`
ExceptionMessage string `gorm:"not null;default:'';comment:异常信息"`
ExceptionCallFile string `gorm:"not null;default:'';comment:调用者文件路径"`
ExceptionCallLine int `gorm:"not null;default:0;comment:调用者行号"`
ExceptionCallFuncName string `gorm:"not null;default:'';comment:调用者函数名称"`
ExceptionIsException bool `gorm:"not null;default:false;comment:是否执行异常"`
ExceptionMessage string `gorm:"not null;default:'';comment:异常信息"`
}
36 changes: 18 additions & 18 deletions driver/elasticSearch/traceContextPO.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ type TraceContextPO struct {
}

type WebContextPO struct {
Domain string // 请求域名
Path string `es_type:"text"` // 请求地址
Method string // 请求方式
ContentType string // 请求内容类型
StatusCode int // 状态码
Headers collections.Dictionary[string, string] `es_type:"flattened"` // 请求头部
RequestBody string `es_type:"text"` // 请求参数
ResponseBody string `es_type:"text"` // 输出参数
RequestIp string // 客户端IP
WebDomain string // 请求域名
WebPath string `es_type:"text"` // 请求地址
WebMethod string // 请求方式
WebContentType string // 请求内容类型
WebStatusCode int // 状态码
WebHeaders collections.Dictionary[string, string] `es_type:"flattened"` // 请求头部
WebRequestBody string `es_type:"text"` // 请求参数
WebResponseBody string `es_type:"text"` // 输出参数
WebRequestIp string // 客户端IP
}

type ConsumerContextPO struct {
Server string
QueueName string
RoutingKey string
ConsumerServer string
ConsumerQueueName string
ConsumerRoutingKey string
}

type TaskContextPO struct {
Expand All @@ -50,13 +50,13 @@ type TaskContextPO struct {
}

type WatchKeyContextPO struct {
Key string
WatchKey string
}

type ExceptionStackPO struct {
CallFile string // 调用者文件路径
CallLine int // 调用者行号
CallFuncName string // 调用者函数名称
IsException bool // 是否执行异常
ExceptionMessage string // 异常信息
ExceptionCallFile string // 调用者文件路径
ExceptionCallLine int // 调用者行号
ExceptionCallFuncName string // 调用者函数名称
ExceptionIsException bool // 是否执行异常
ExceptionMessage string // 异常信息
}
2 changes: 1 addition & 1 deletion traceDetailEtcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ 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)
return fmt.Sprintf("[%s]耗时:%s,%s WatchKey=%s LeaseID=%v", flog.Yellow(receiver.CallType.ToString()), flog.Red(receiver.UseTs.String()), receiver.MethodName, receiver.Key, receiver.LeaseID)
}
2 changes: 1 addition & 1 deletion traceDetailRedis.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ 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)
return fmt.Sprintf("[%s]耗时:%s,%s WatchKey=%s,Field=%s", flog.Yellow(receiver.CallType.ToString()), flog.Red(receiver.UseTs.String()), receiver.MethodName, receiver.Key, receiver.Field)
}
26 changes: 13 additions & 13 deletions traceManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ func (*traceManager) EntryWebApi(domain string, path string, method string, cont
StartTs: time.Now().UnixMicro(),
TraceType: eumTraceType.WebApi,
Web: WebContext{
Domain: domain,
Path: path,
Method: method,
ContentType: contentType,
Headers: headerDictionary.ToDictionary(),
RequestBody: requestBody,
RequestIp: requestIp,
WebDomain: domain,
WebPath: path,
WebMethod: method,
WebContentType: contentType,
WebHeaders: headerDictionary.ToDictionary(),
WebRequestBody: requestBody,
WebRequestIp: requestIp,
},
}
trace.CurTraceContext.Set(context)
Expand Down Expand Up @@ -135,9 +135,9 @@ func (*traceManager) EntryMqConsumer(server string, queueName string, routingKey
StartTs: time.Now().UnixMicro(),
TraceType: eumTraceType.MqConsumer,
Consumer: ConsumerContext{
Server: server,
QueueName: queueName,
RoutingKey: routingKey,
ConsumerServer: server,
ConsumerQueueName: queueName,
ConsumerRoutingKey: routingKey,
},
}
trace.CurTraceContext.Set(context)
Expand All @@ -157,8 +157,8 @@ func (*traceManager) EntryQueueConsumer(subscribeName string) trace.ITraceContex
StartTs: time.Now().UnixMicro(),
TraceType: eumTraceType.QueueConsumer,
Consumer: ConsumerContext{
Server: fmt.Sprintf("%s/%s/%v", fs.AppName, fs.AppIp, fs.AppId),
QueueName: subscribeName,
ConsumerServer: fmt.Sprintf("%s/%s/%v", fs.AppName, fs.AppIp, fs.AppId),
ConsumerQueueName: subscribeName,
},
}
trace.CurTraceContext.Set(context)
Expand Down Expand Up @@ -220,7 +220,7 @@ func (*traceManager) EntryWatchKey(key string) trace.ITraceContext {
StartTs: time.Now().UnixMicro(),
TraceType: eumTraceType.WatchKey,
WatchKey: WatchKeyContext{
Key: key,
WatchKey: key,
},
}
trace.CurTraceContext.Set(context)
Expand Down
56 changes: 28 additions & 28 deletions trackContext.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type TraceContext struct {
AppIp string // 应用IP
ParentAppName string // 上游应用
StartTs int64 // 调用开始时间戳(微秒)
EndTs int64 // 调用结束时间戳
EndTs int64 // 调用结束时间戳(微秒)
UseTs time.Duration // 总共使用时间微秒
TraceType eumTraceType.Enum // 状态码
List []trace.ITraceDetail `es_type:"object"` // 调用的上下文
Expand All @@ -32,29 +32,29 @@ type TraceContext struct {
}

type WebContext struct {
Domain string // 请求域名
Path string `es_type:"text"` // 请求地址
Method string // 请求方式
ContentType string // 请求内容类型
StatusCode int // 状态码
Headers collections.Dictionary[string, string] `es_type:"flattened" gorm:"json;not null;comment:请求头部"`
RequestBody string `es_type:"text"` // 请求参数
ResponseBody string `es_type:"text"` // 输出参数
RequestIp string // 客户端IP
WebDomain string // 请求域名
WebPath string `es_type:"text"` // 请求地址
WebMethod string // 请求方式
WebContentType string // 请求内容类型
WebStatusCode int // 状态码
WebHeaders collections.Dictionary[string, string] `es_type:"flattened" gorm:"json;not null;comment:请求头部"`
WebRequestBody string `es_type:"text"` // 请求参数
WebResponseBody string `es_type:"text"` // 输出参数
WebRequestIp string // 客户端IP
}

func (receiver WebContext) IsNil() bool {
return receiver.Domain == "" && receiver.Path == "" && receiver.Method == "" && receiver.ContentType == "" && receiver.StatusCode == 0
return receiver.WebDomain == "" && receiver.WebPath == "" && receiver.WebMethod == "" && receiver.WebContentType == "" && receiver.WebStatusCode == 0
}

type ConsumerContext struct {
Server string
QueueName string
RoutingKey string
ConsumerServer string
ConsumerQueueName string
ConsumerRoutingKey string
}

func (receiver ConsumerContext) IsNil() bool {
return receiver.Server == "" && receiver.QueueName == "" && receiver.RoutingKey == ""
return receiver.ConsumerServer == "" && receiver.ConsumerQueueName == "" && receiver.ConsumerRoutingKey == ""
}

type TaskContext struct {
Expand All @@ -68,17 +68,17 @@ func (receiver TaskContext) IsNil() bool {
}

type WatchKeyContext struct {
Key string
WatchKey string
}

func (receiver WatchKeyContext) IsNil() bool {
return receiver.Key == ""
return receiver.WatchKey == ""
}

func (receiver *TraceContext) SetBody(requestBody string, statusCode int, responseBody string) {
receiver.Web.RequestBody = requestBody
receiver.Web.StatusCode = statusCode
receiver.Web.ResponseBody = responseBody
receiver.Web.WebRequestBody = requestBody
receiver.Web.WebStatusCode = statusCode
receiver.Web.WebResponseBody = responseBody
}

func (receiver *TraceContext) GetTraceId() int64 {
Expand Down Expand Up @@ -138,22 +138,22 @@ func (receiver *TraceContext) printLog() {
log := fmt.Sprintf("%s%s (%s):%s", tab, flog.Blue(i+1), flog.Green(detail.UnTraceTs.String()), receiver.List[i].ToString())
lst.Add(log)

if detail.Exception.IsException {
lst.Add(fmt.Sprintf("%s:%s %s 出错了:%s", detail.Exception.CallFile, flog.Blue(detail.Exception.CallLine), flog.Red(detail.Exception.CallFuncName), flog.Red(detail.Exception.ExceptionMessage)))
if 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.IsException {
lst.Add(fmt.Sprintf("%s%s:%s %s %s", flog.Red("【异常】"), flog.Blue(receiver.Exception.CallFile), flog.Blue(receiver.Exception.CallLine), flog.Green(receiver.Exception.CallFuncName), flog.Red(receiver.Exception.ExceptionMessage)))
if 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)))
}

lst.Add("-----------------------------------------------------------------")
logs := strings.Join(lst.ToArray(), "\n")
switch receiver.TraceType {
case eumTraceType.WebApi:
flog.Printf("【%s链路追踪】TraceId:%s,耗时:%s,%s\n%s\n", receiver.TraceType.ToString(), flog.Green(parse.ToString(receiver.TraceId)), flog.Red(receiver.UseTs.String()), receiver.Web.Path, logs)
flog.Printf("【%s链路追踪】TraceId:%s,耗时:%s,%s\n%s\n", receiver.TraceType.ToString(), flog.Green(parse.ToString(receiver.TraceId)), flog.Red(receiver.UseTs.String()), receiver.Web.WebPath, logs)
case eumTraceType.MqConsumer, eumTraceType.QueueConsumer:
flog.Printf("【%s链路追踪】TraceId:%s,耗时:%s,%s\n%s\n", receiver.TraceType.ToString(), flog.Green(parse.ToString(receiver.TraceId)), flog.Red(receiver.UseTs.String()), receiver.Consumer.QueueName, logs)
flog.Printf("【%s链路追踪】TraceId:%s,耗时:%s,%s\n%s\n", receiver.TraceType.ToString(), flog.Green(parse.ToString(receiver.TraceId)), flog.Red(receiver.UseTs.String()), receiver.Consumer.ConsumerQueueName, logs)
case eumTraceType.Task, eumTraceType.FSchedule:
flog.Printf("【%s链路追踪】TraceId:%s,耗时:%s,%s\n%s\n", receiver.TraceType.ToString(), flog.Green(parse.ToString(receiver.TraceId)), flog.Red(receiver.UseTs.String()), receiver.Task.TaskName, logs)
default:
Expand All @@ -164,8 +164,8 @@ func (receiver *TraceContext) printLog() {

func (receiver *TraceContext) Error(err error) {
if err != nil {
receiver.Exception.IsException = true
receiver.Exception.ExceptionIsException = true
receiver.Exception.ExceptionMessage = err.Error()
receiver.Exception.CallFile, receiver.Exception.CallFuncName, receiver.Exception.CallLine = trace.GetCallerInfo()
receiver.Exception.ExceptionCallFile, receiver.Exception.ExceptionCallFuncName, receiver.Exception.ExceptionCallLine = trace.GetCallerInfo()
}
}

0 comments on commit 09ab6d7

Please sign in to comment.