Skip to content

Commit

Permalink
使用原生转换
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Nov 20, 2024
1 parent be13f65 commit f9d8af5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
6 changes: 3 additions & 3 deletions saveTraceContextConsumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"crypto/tls"
"fmt"
"net/http"
"strconv"

"github.com/bytedance/sonic"
"github.com/farseer-go/collections"
"github.com/farseer-go/fs/configure"
"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"
)
Expand Down Expand Up @@ -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{
Expand Down
41 changes: 21 additions & 20 deletions traceManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package linkTrace

import (
"fmt"
"strconv"
"time"

"github.com/farseer-go/collections"
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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,
Expand All @@ -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{
Expand All @@ -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: "",
Expand All @@ -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: "",
Expand All @@ -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{
Expand All @@ -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: "",
Expand All @@ -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: "",
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 8 additions & 7 deletions trackContext.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package linkTrace

import (
"fmt"
"strings"
"time"

"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"
"github.com/farseer-go/linkTrace/eumTraceType"
"github.com/farseer-go/queue"
"strings"
"time"
)

type TraceContext struct {
Expand Down Expand Up @@ -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)
}
}
}
Expand All @@ -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
}

0 comments on commit f9d8af5

Please sign in to comment.