Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Oct 17, 2023
1 parent d0a659f commit 5bb77b9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 24 deletions.
9 changes: 2 additions & 7 deletions traceDatabaseDetail.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,10 @@ func (receiver *TraceDatabaseDetail) ToString() string {
return fmt.Sprintf("[%s]耗时:%s, %s", flog.Yellow(receiver.CallType.ToString()), flog.Red(receiver.UseTs.String()), sql)
}

// TraceDatabase 数据库埋点
func TraceDatabase() *TraceDatabaseDetail {
detail := &TraceDatabaseDetail{
TraceDetail: TraceDetail{
//CallStackTrace: CallStackTrace{},
CallMethod: "",
CallType: eumCallType.Database,
StartTs: time.Now().UnixMicro(),
EndTs: time.Now().UnixMicro(),
},
TraceDetail: newTraceDetail(eumCallType.Database),
}

if trace := GetCurTrace(); trace != nil && defConfig.Enable {
Expand Down
10 changes: 10 additions & 0 deletions traceDetail.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ type CallStackTrace struct {
type ExceptionDetail struct {
}

func newTraceDetail(callType eumCallType.Enum) TraceDetail {
return TraceDetail{
//CallStackTrace: CallStackTrace{},
CallMethod: "",
CallType: callType,
StartTs: time.Now().UnixMicro(),
EndTs: time.Now().UnixMicro(),
}
}

// End 链路明细执行完后,统计用时
func (receiver *TraceDetail) End(err error) {
receiver.EndTs = time.Now().UnixMicro()
Expand Down
31 changes: 23 additions & 8 deletions traceHandDetail.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,31 @@ func (receiver *TraceHandDetail) ToString() string {
return fmt.Sprintf("[%s]耗时:%s, %s", flog.Yellow(receiver.CallType.ToString()), flog.Red(receiver.UseTs.String()), receiver.Name)
}

// TraceHand 手动埋点
func TraceHand(name string) *TraceHandDetail {
detail := &TraceHandDetail{
TraceDetail: TraceDetail{
//CallStackTrace: CallStackTrace{},
CallMethod: "",
CallType: eumCallType.Hand,
StartTs: time.Now().UnixMicro(),
EndTs: time.Now().UnixMicro(),
},
Name: name,
TraceDetail: newTraceDetail(eumCallType.Hand),
Name: name,
}

if trace := GetCurTrace(); trace != nil && defConfig.Enable {
// 时间轴:上下文入口起点时间到本次开始时间
detail.Timeline = time.Duration(detail.StartTs-trace.StartTs) * time.Microsecond
if trace.List.Count() > 0 {
detail.UnTraceTs = time.Duration(detail.StartTs-trace.List.Last().GetEndTs()) * time.Microsecond
} else {
detail.UnTraceTs = time.Duration(detail.StartTs-trace.StartTs) * time.Microsecond
}
trace.List.Add(detail)
}
return detail
}

// TraceKeyLocation 关键位置埋点
func TraceKeyLocation(name string) *TraceHandDetail {
detail := &TraceHandDetail{
TraceDetail: newTraceDetail(eumCallType.KeyLocation),
Name: name,
}

if trace := GetCurTrace(); trace != nil && defConfig.Enable {
Expand Down
13 changes: 4 additions & 9 deletions traceRedisDetail.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,12 @@ func (receiver *TraceRedisDetail) ToString() string {
return fmt.Sprintf("[%s]耗时:%s,%s Key=%s,FieldKey=%s", flog.Yellow(receiver.CallType.ToString()), flog.Red(receiver.UseTs.String()), receiver.CallMethod, receiver.Key, receiver.Field)
}

// TraceRedis Redis埋点
func TraceRedis(redisMethod string, key string, field string) *TraceRedisDetail {
detail := &TraceRedisDetail{
TraceDetail: TraceDetail{
//CallStackTrace: CallStackTrace{},
CallMethod: redisMethod,
CallType: eumCallType.Redis,
StartTs: time.Now().UnixMicro(),
EndTs: time.Now().UnixMicro(),
},
Key: key,
Field: field,
TraceDetail: newTraceDetail(eumCallType.Redis),
Key: key,
Field: field,
}

if trace := GetCurTrace(); trace != nil && defConfig.Enable {
Expand Down

0 comments on commit 5bb77b9

Please sign in to comment.