Skip to content

Commit

Permalink
修复MqConsumer没有追加traceLevel
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Oct 26, 2024
1 parent c9b588d commit d4e8bf1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
10 changes: 8 additions & 2 deletions traceDetailRedis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package linkTrace

import (
"fmt"

"github.com/farseer-go/fs/flog"
"github.com/farseer-go/fs/trace"
)

type TraceDetailRedis struct {
trace.BaseTraceDetail
Key string // redis key
Field string // hash field
Key string // redis key
Field string // hash field
RowsAffected int // 影响行数
}

func (receiver *TraceDetailRedis) GetTraceDetail() *trace.BaseTraceDetail {
Expand All @@ -19,3 +21,7 @@ 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) SetRows(rows int) {
receiver.RowsAffected = rows
}
16 changes: 11 additions & 5 deletions traceManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package linkTrace

import (
"fmt"
"time"

"github.com/farseer-go/collections"
"github.com/farseer-go/fs/core"
"github.com/farseer-go/fs/parse"
"github.com/farseer-go/fs/sonyflake"
"github.com/farseer-go/fs/trace"
"github.com/farseer-go/fs/trace/eumCallType"
"github.com/farseer-go/linkTrace/eumTraceType"
"time"
)

type traceManager struct {
Expand Down Expand Up @@ -63,10 +64,10 @@ func (*traceManager) EntryWebApi(domain string, path string, method string, cont
// EntryWebSocket WebSocket入口
func (*traceManager) EntryWebSocket(domain string, path string, header map[string]string, requestIp string) trace.ITraceContext {
headerDictionary := collections.NewDictionaryFromMap(header)
traceId := parse.ToString(headerDictionary.GetValue("Trace-Id"))
parentTraceId := parse.ToString(headerDictionary.GetValue("Trace-Id"))
traceLevel := parse.ToInt(headerDictionary.GetValue("Trace-Level"))
if traceId == "" {
traceId = parse.ToString(sonyflake.GenerateId())
if parentTraceId == "" {
parentTraceId = parse.ToString(sonyflake.GenerateId())
} else {
traceLevel++ // 来自上游的请求,自动+1层
}
Expand All @@ -75,7 +76,7 @@ func (*traceManager) EntryWebSocket(domain string, path string, header map[strin
AppName: core.AppName,
AppIp: core.AppIp,
ParentAppName: headerDictionary.GetValue("Trace-App-Name"),
TraceId: traceId,
TraceId: parentTraceId,
TraceLevel: traceLevel,
StartTs: time.Now().UnixMicro(),
TraceType: eumTraceType.WebSocket,
Expand All @@ -96,15 +97,20 @@ func (*traceManager) EntryWebSocket(domain string, path string, header map[strin

// EntryMqConsumer mq 消费埋点
func (*traceManager) EntryMqConsumer(parentTraceId, parentAppName, server string, queueName string, routingKey string) trace.ITraceContext {
// 如果来自上游,则要自动+1层,默认为0
var traceLevel int
if parentTraceId == "" {
parentTraceId = parse.ToString(sonyflake.GenerateId())
} else {
traceLevel++ // 来自上游的请求,自动+1层
}
context := &TraceContext{
AppId: parse.ToString(core.AppId),
AppName: core.AppName,
AppIp: core.AppIp,
ParentAppName: parentAppName,
TraceId: parentTraceId,
TraceLevel: traceLevel,
StartTs: time.Now().UnixMicro(),
TraceType: eumTraceType.MqConsumer,
ConsumerContext: ConsumerContext{
Expand Down

0 comments on commit d4e8bf1

Please sign in to comment.