Skip to content

Commit

Permalink
新增:Trace-Level链路层级字段
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Jan 11, 2024
1 parent 54bb408 commit c507aca
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 65 deletions.
1 change: 1 addition & 0 deletions saveTraceContextConsumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func uploadTrace(lstTraceContext any) error {
// 链路追踪
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-App-Name", core.AppName)
}
client := &http.Client{}
Expand Down
134 changes: 70 additions & 64 deletions traceManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,21 @@ func (*traceManager) GetCurTrace() trace.ITraceContext {
func (*traceManager) EntryWebApi(domain string, path string, method string, contentType string, header map[string]string, requestBody 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())
}
// 默认从0层开始
if traceLevel > 0 {
traceLevel++
}
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.WebApi,
WebContext: WebContext{
Expand All @@ -49,70 +55,6 @@ func (*traceManager) EntryWebApi(domain string, path string, method string, cont
return context
}

// TraceDatabase 数据库埋点
func (*traceManager) TraceDatabase() trace.ITraceDetail {
detail := &TraceDetailDatabase{
BaseTraceDetail: newTraceDetail(eumCallType.Database, ""),
}
add(detail)
return detail
}

// TraceDatabaseOpen 数据库埋点
func (*traceManager) TraceDatabaseOpen(dbName string, connectString string) trace.ITraceDetail {
detail := &TraceDetailDatabase{
BaseTraceDetail: newTraceDetail(eumCallType.Database, ""),
DbName: dbName,
ConnectionString: connectString,
}
add(detail)
return detail
}

// TraceElasticsearch Elasticsearch埋点
func (*traceManager) TraceElasticsearch(method string, IndexName string, AliasesName string) trace.ITraceDetail {
detail := &TraceDetailEs{
BaseTraceDetail: newTraceDetail(eumCallType.Elasticsearch, method),
IndexName: IndexName,
AliasesName: AliasesName,
}
add(detail)
return detail
}

// TraceEtcd etcd埋点
func (*traceManager) TraceEtcd(method string, key string, leaseID int64) trace.ITraceDetail {
detail := &TraceDetailEtcd{
BaseTraceDetail: newTraceDetail(eumCallType.Etcd, method),
Key: key,
LeaseID: leaseID,
}
add(detail)
return detail
}

// TraceHand 手动埋点
func (*traceManager) TraceHand(name string) trace.ITraceDetail {
detail := &TraceDetailHand{
BaseTraceDetail: newTraceDetail(eumCallType.Hand, ""),
Name: name,
}
add(detail)
return detail
}

// TraceMqSend mq发送埋点
func (*traceManager) TraceMqSend(method string, server string, exchange string, routingKey string) trace.ITraceDetail {
detail := &TraceDetailMq{
BaseTraceDetail: newTraceDetail(eumCallType.Mq, method),
Server: server,
Exchange: exchange,
RoutingKey: routingKey,
}
add(detail)
return detail
}

// EntryMqConsumer mq 消费埋点
func (*traceManager) EntryMqConsumer(server string, queueName string, routingKey string) trace.ITraceContext {
traceId := parse.ToString(sonyflake.GenerateId())
Expand Down Expand Up @@ -241,6 +183,70 @@ func (*traceManager) EntryWatchKey(key string) trace.ITraceContext {
return context
}

// TraceDatabase 数据库埋点
func (*traceManager) TraceDatabase() trace.ITraceDetail {
detail := &TraceDetailDatabase{
BaseTraceDetail: newTraceDetail(eumCallType.Database, ""),
}
add(detail)
return detail
}

// TraceDatabaseOpen 数据库埋点
func (*traceManager) TraceDatabaseOpen(dbName string, connectString string) trace.ITraceDetail {
detail := &TraceDetailDatabase{
BaseTraceDetail: newTraceDetail(eumCallType.Database, ""),
DbName: dbName,
ConnectionString: connectString,
}
add(detail)
return detail
}

// TraceElasticsearch Elasticsearch埋点
func (*traceManager) TraceElasticsearch(method string, IndexName string, AliasesName string) trace.ITraceDetail {
detail := &TraceDetailEs{
BaseTraceDetail: newTraceDetail(eumCallType.Elasticsearch, method),
IndexName: IndexName,
AliasesName: AliasesName,
}
add(detail)
return detail
}

// TraceEtcd etcd埋点
func (*traceManager) TraceEtcd(method string, key string, leaseID int64) trace.ITraceDetail {
detail := &TraceDetailEtcd{
BaseTraceDetail: newTraceDetail(eumCallType.Etcd, method),
Key: key,
LeaseID: leaseID,
}
add(detail)
return detail
}

// TraceHand 手动埋点
func (*traceManager) TraceHand(name string) trace.ITraceDetail {
detail := &TraceDetailHand{
BaseTraceDetail: newTraceDetail(eumCallType.Hand, ""),
Name: name,
}
add(detail)
return detail
}

// TraceMqSend mq发送埋点
func (*traceManager) TraceMqSend(method string, server string, exchange string, routingKey string) trace.ITraceDetail {
detail := &TraceDetailMq{
BaseTraceDetail: newTraceDetail(eumCallType.Mq, method),
Server: server,
Exchange: exchange,
RoutingKey: routingKey,
}
add(detail)
return detail
}

// TraceMq open、create埋点
func (*traceManager) TraceMq(method string, server string, exchange string) trace.ITraceDetail {
detail := &TraceDetailMq{
Expand Down
3 changes: 2 additions & 1 deletion trackContext.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type TraceContext struct {
AppName string // 应用名称
AppIp string // 应用IP
ParentAppName string // 上游应用
TraceLevel int // 逐层递增(显示上下游顺序)
StartTs int64 // 调用开始时间戳(微秒)
EndTs int64 // 调用结束时间戳(微秒)
UseTs time.Duration // 总共使用时间(微秒)
Expand Down Expand Up @@ -88,7 +89,7 @@ func (receiver *TraceContext) SetBody(requestBody string, statusCode int, respon
func (receiver *TraceContext) GetTraceId() string {
return receiver.TraceId
}

func (receiver *TraceContext) GetTraceLevel() int { return receiver.TraceLevel }
func (receiver *TraceContext) GetStartTs() int64 {
return receiver.StartTs
}
Expand Down

0 comments on commit c507aca

Please sign in to comment.