diff --git a/saveTraceContextConsumer.go b/saveTraceContextConsumer.go index 5ed6d95..5a76918 100644 --- a/saveTraceContextConsumer.go +++ b/saveTraceContextConsumer.go @@ -5,6 +5,7 @@ import ( "crypto/tls" "fmt" "net/http" + "strconv" "github.com/bytedance/sonic" "github.com/farseer-go/collections" @@ -12,7 +13,6 @@ import ( "github.com/farseer-go/fs/container" "github.com/farseer-go/fs/core" "github.com/farseer-go/fs/exception" - "github.com/farseer-go/fs/parse" "github.com/farseer-go/fs/trace" "github.com/farseer-go/linkTrace/eumTraceType" ) @@ -51,8 +51,8 @@ func uploadTrace(lstTraceContext any) error { newRequest.Header.Set("Content-Type", "application/json") // 链路追踪 if traceContext := container.Resolve[trace.IManager]().GetCurTrace(); traceContext != nil { - newRequest.Header.Set("Trace-Id", parse.ToString(traceContext.GetTraceId())) - newRequest.Header.Set("Trace-Level", parse.ToString(traceContext.GetTraceLevel())) + newRequest.Header.Set("Trace-Id", traceContext.GetTraceId()) + newRequest.Header.Set("Trace-Level", strconv.Itoa(traceContext.GetTraceLevel())) newRequest.Header.Set("Trace-App-Name", core.AppName) } client := &http.Client{ diff --git a/traceManager.go b/traceManager.go index ec6d6e0..d162d22 100644 --- a/traceManager.go +++ b/traceManager.go @@ -2,6 +2,7 @@ package linkTrace import ( "fmt" + "strconv" "time" "github.com/farseer-go/collections" @@ -33,12 +34,12 @@ func (*traceManager) EntryWebApi(domain string, path string, method string, cont traceId := parse.ToString(headerDictionary.GetValue("Trace-Id")) traceLevel := parse.ToInt(headerDictionary.GetValue("Trace-Level")) if traceId == "" { - traceId = parse.ToString(sonyflake.GenerateId()) + traceId = strconv.FormatInt(sonyflake.GenerateId(), 10) } else { traceLevel++ // 来自上游的请求,自动+1层 } context := &TraceContext{ - AppId: parse.ToString(core.AppId), + AppId: strconv.FormatInt(core.AppId, 10), AppName: core.AppName, AppIp: core.AppIp, ParentAppName: headerDictionary.GetValue("Trace-App-Name"), @@ -67,12 +68,12 @@ func (*traceManager) EntryWebSocket(domain string, path string, header map[strin parentTraceId := parse.ToString(headerDictionary.GetValue("Trace-Id")) traceLevel := parse.ToInt(headerDictionary.GetValue("Trace-Level")) if parentTraceId == "" { - parentTraceId = parse.ToString(sonyflake.GenerateId()) + parentTraceId = strconv.FormatInt(sonyflake.GenerateId(), 10) } else { traceLevel++ // 来自上游的请求,自动+1层 } context := &TraceContext{ - AppId: parse.ToString(core.AppId), + AppId: strconv.FormatInt(core.AppId, 10), AppName: core.AppName, AppIp: core.AppIp, ParentAppName: headerDictionary.GetValue("Trace-App-Name"), @@ -100,12 +101,12 @@ func (*traceManager) EntryMqConsumer(parentTraceId, parentAppName, server string // 如果来自上游,则要自动+1层,默认为0 var traceLevel int if parentTraceId == "" { - parentTraceId = parse.ToString(sonyflake.GenerateId()) + parentTraceId = strconv.FormatInt(sonyflake.GenerateId(), 10) } else { traceLevel++ // 来自上游的请求,自动+1层 } context := &TraceContext{ - AppId: parse.ToString(core.AppId), + AppId: strconv.FormatInt(core.AppId, 10), AppName: core.AppName, AppIp: core.AppIp, ParentAppName: parentAppName, @@ -127,11 +128,11 @@ func (*traceManager) EntryMqConsumer(parentTraceId, parentAppName, server string // EntryQueueConsumer queue 消费埋点 func (*traceManager) EntryQueueConsumer(queueName, subscribeName string) trace.ITraceContext { context := &TraceContext{ - AppId: parse.ToString(core.AppId), + AppId: strconv.FormatInt(core.AppId, 10), AppName: core.AppName, AppIp: core.AppIp, ParentAppName: "", - TraceId: parse.ToString(sonyflake.GenerateId()), + TraceId: strconv.FormatInt(sonyflake.GenerateId(), 10), StartTs: time.Now().UnixMicro(), TraceType: eumTraceType.QueueConsumer, ConsumerContext: ConsumerContext{ @@ -154,10 +155,10 @@ func (receiver *traceManager) EntryEventConsumer(server, eventName, subscribeNam traceId = cur.GetTraceId() traceLevel = cur.GetTraceLevel() + 1 } else { - traceId = parse.ToString(sonyflake.GenerateId()) + traceId = strconv.FormatInt(sonyflake.GenerateId(), 10) } context := &TraceContext{ - AppId: parse.ToString(core.AppId), + AppId: strconv.FormatInt(core.AppId, 10), AppName: core.AppName, AppIp: core.AppIp, ParentAppName: "", @@ -177,9 +178,9 @@ func (receiver *traceManager) EntryEventConsumer(server, eventName, subscribeNam // EntryTask 创建本地任务入口 func (*traceManager) EntryTask(taskName string) trace.ITraceContext { - traceId := parse.ToString(sonyflake.GenerateId()) + traceId := strconv.FormatInt(sonyflake.GenerateId(), 10) context := &TraceContext{ - AppId: parse.ToString(core.AppId), + AppId: strconv.FormatInt(core.AppId, 10), AppName: core.AppName, AppIp: core.AppIp, ParentAppName: "", @@ -197,13 +198,13 @@ func (*traceManager) EntryTask(taskName string) trace.ITraceContext { // EntryTaskGroup 创建本地任务入口(调度中心专用) func (*traceManager) EntryTaskGroup(taskName string, taskGroupName string, taskId int64) trace.ITraceContext { - traceId := parse.ToString(sonyflake.GenerateId()) + traceId := strconv.FormatInt(sonyflake.GenerateId(), 10) context := &TraceContext{ - AppId: parse.ToString(core.AppId), + AppId: strconv.FormatInt(core.AppId, 10), AppName: core.AppName, AppIp: core.AppIp, ParentAppName: "", - TraceId: parse.ToString(traceId), + TraceId: traceId, StartTs: time.Now().UnixMicro(), TraceType: eumTraceType.Task, TaskContext: TaskContext{ @@ -219,9 +220,9 @@ func (*traceManager) EntryTaskGroup(taskName string, taskGroupName string, taskI // EntryFSchedule 创建调度中心入口 func (*traceManager) EntryFSchedule(taskGroupName string, taskId int64, data map[string]string) trace.ITraceContext { - traceId := parse.ToString(sonyflake.GenerateId()) + traceId := strconv.FormatInt(sonyflake.GenerateId(), 10) context := &TraceContext{ - AppId: parse.ToString(core.AppId), + AppId: strconv.FormatInt(core.AppId, 10), AppName: core.AppName, AppIp: core.AppIp, ParentAppName: "", @@ -242,9 +243,9 @@ func (*traceManager) EntryFSchedule(taskGroupName string, taskId int64, data map // EntryWatchKey 创建etcd入口 func (*traceManager) EntryWatchKey(key string) trace.ITraceContext { - traceId := parse.ToString(sonyflake.GenerateId()) + traceId := strconv.FormatInt(sonyflake.GenerateId(), 10) context := &TraceContext{ - AppId: parse.ToString(core.AppId), + AppId: strconv.FormatInt(core.AppId, 10), AppName: core.AppName, AppIp: core.AppIp, ParentAppName: "", @@ -386,7 +387,7 @@ func newTraceDetail(callType eumCallType.Enum, methodName string) trace.BaseTrac parentDetailId = lstScope[len(lstScope)-1].DetailId } baseTraceDetail := trace.BaseTraceDetail{ - DetailId: parse.ToString(sonyflake.GenerateId()), + DetailId: strconv.FormatInt(sonyflake.GenerateId(), 10), Level: len(lstScope) + 1, ParentDetailId: parentDetailId, MethodName: methodName, diff --git a/trackContext.go b/trackContext.go index 891f754..850cbf8 100644 --- a/trackContext.go +++ b/trackContext.go @@ -2,6 +2,9 @@ package linkTrace import ( "fmt" + "strings" + "time" + "github.com/farseer-go/collections" "github.com/farseer-go/fs/dateTime" "github.com/farseer-go/fs/flog" @@ -9,8 +12,6 @@ import ( "github.com/farseer-go/fs/trace" "github.com/farseer-go/linkTrace/eumTraceType" "github.com/farseer-go/queue" - "strings" - "time" ) type TraceContext struct { @@ -165,13 +166,13 @@ func (receiver *TraceContext) printLog() { 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.WebContext.WebPath, logs) + flog.Printf("【%s链路追踪】TraceId:%s,耗时:%s,%s\n%s\n", receiver.TraceType.ToString(), flog.Green(receiver.TraceId), flog.Red(receiver.UseTs.String()), receiver.WebContext.WebPath, logs) case eumTraceType.MqConsumer, eumTraceType.QueueConsumer, eumTraceType.EventConsumer: - 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.ConsumerContext.ConsumerQueueName, logs) + flog.Printf("【%s链路追踪】TraceId:%s,耗时:%s,%s\n%s\n", receiver.TraceType.ToString(), flog.Green(receiver.TraceId), flog.Red(receiver.UseTs.String()), receiver.ConsumerContext.ConsumerQueueName, logs) case eumTraceType.Task, eumTraceType.FSchedule: - flog.Printf("【%s链路追踪】TraceId:%s,耗时:%s,%s %s\n%s\n", receiver.TraceType.ToString(), flog.Green(parse.ToString(receiver.TraceId)), flog.Red(receiver.UseTs.String()), receiver.TaskContext.TaskName, receiver.TaskContext.TaskGroupName, logs) + flog.Printf("【%s链路追踪】TraceId:%s,耗时:%s,%s %s\n%s\n", receiver.TraceType.ToString(), flog.Green(receiver.TraceId), flog.Red(receiver.UseTs.String()), receiver.TaskContext.TaskName, receiver.TaskContext.TaskGroupName, logs) default: - flog.Printf("【%s链路追踪】TraceId:%s,耗时:%s\n%s\n", receiver.TraceType.ToString(), flog.Green(parse.ToString(receiver.TraceId)), flog.Red(receiver.UseTs.String()), logs) + flog.Printf("【%s链路追踪】TraceId:%s,耗时:%s\n%s\n", receiver.TraceType.ToString(), flog.Green(receiver.TraceId), flog.Red(receiver.UseTs.String()), logs) } } } @@ -187,5 +188,5 @@ func (receiver *TraceContext) Error(err error) { } func (receiver *TraceContext) GetAppInfo() (string, string, string, string, string) { - return parse.ToString(receiver.TraceId), receiver.AppName, parse.ToString(receiver.AppId), receiver.AppIp, receiver.ParentAppName + return receiver.TraceId, receiver.AppName, parse.ToString(receiver.AppId), receiver.AppIp, receiver.ParentAppName }