Skip to content

Commit

Permalink
链路追踪,增加WebSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Sep 2, 2024
1 parent 726f6b6 commit 91e0faf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
3 changes: 2 additions & 1 deletion eumTraceType/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package eumTraceType
type Enum int

const (
WebApi Enum = iota // Api
WebApi Enum = iota // WebApi
MqConsumer // MQ消费
QueueConsumer // 本地消费
FSchedule // 调度中心
Task // 本地任务
WatchKey // ETCD
EventConsumer // 事件消费
WebSocket // WebSocket
)

func (e Enum) ToString() string {
Expand Down
38 changes: 36 additions & 2 deletions traceManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (*traceManager) GetTraceId() string {
}

// EntryWebApi Webapi入口
func (*traceManager) EntryWebApi(domain string, path string, method string, contentType string, header map[string]string, requestBody string, requestIp string) trace.ITraceContext {
func (*traceManager) EntryWebApi(domain string, path string, method string, contentType string, header map[string]string, requestIp string) trace.ITraceContext {
headerDictionary := collections.NewDictionaryFromMap(header)
traceId := parse.ToString(headerDictionary.GetValue("Trace-Id"))
traceLevel := parse.ToInt(headerDictionary.GetValue("Trace-Level"))
Expand All @@ -51,7 +51,41 @@ func (*traceManager) EntryWebApi(domain string, path string, method string, cont
WebMethod: method,
WebContentType: contentType,
WebHeaders: headerDictionary.ToDictionary(),
WebRequestBody: requestBody,
WebRequestBody: "",
WebRequestIp: requestIp,
},
}
trace.CurTraceContext.Set(context)
trace.ScopeLevel.Set([]trace.BaseTraceDetail{})
return context
}

// EntryWebSocket WebSocket入口
func (*traceManager) EntryWebSocket(domain string, path string, method string, contentType string, header map[string]string, requestIp string) trace.ITraceContext {
headerDictionary := collections.NewDictionaryFromMap(header)
traceId := parse.ToString(headerDictionary.GetValue("Trace-Id"))
traceLevel := parse.ToInt(headerDictionary.GetValue("Trace-Level"))
if traceId == "" {
traceId = parse.ToString(sonyflake.GenerateId())
} else {
traceLevel++ // 来自上游的请求,自动+1层
}
context := &TraceContext{
AppId: parse.ToString(core.AppId),
AppName: core.AppName,
AppIp: core.AppIp,
ParentAppName: headerDictionary.GetValue("Trace-App-Name"),
TraceId: traceId,
TraceLevel: traceLevel,
StartTs: time.Now().UnixMicro(),
TraceType: eumTraceType.WebSocket,
WebContext: WebContext{
WebDomain: domain,
WebPath: path,
WebMethod: method,
WebContentType: contentType,
WebHeaders: headerDictionary.ToDictionary(),
WebRequestBody: "Conn",
WebRequestIp: requestIp,
},
}
Expand Down

0 comments on commit 91e0faf

Please sign in to comment.