升级trace逻辑

This commit is contained in:
2025-04-12 17:53:07 +08:00
parent 7b668e2e2f
commit 26e2419eba
2 changed files with 42 additions and 22 deletions

View File

@ -53,9 +53,9 @@ type Runtime struct {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 23:41 2022/10/14
func (r *Runtime) StartBehavior(action string, data map[string]interface{}) int {
func (r *Runtime) StartBehavior(action string, data map[string]any) *Behavior {
if nil == data {
data = make(map[string]interface{})
data = make(map[string]any)
}
r.lock.Lock()
defer r.lock.Unlock()
@ -68,7 +68,7 @@ func (r *Runtime) StartBehavior(action string, data map[string]interface{}) int
Data: data,
}
r.behaviorList = append(r.behaviorList, b)
return b.ID
return &b
}
// FinishBehavior 结束某一个行为
@ -76,22 +76,25 @@ func (r *Runtime) StartBehavior(action string, data map[string]interface{}) int
// Author : go_developer@163.com<白茶清欢>
//
// Date : 23:53 2022/10/14
func (r *Runtime) FinishBehavior(behaviorID int, data map[string]interface{}) {
func (r *Runtime) FinishBehavior(b *Behavior, data map[string]any) {
if nil == data {
data = make(map[string]interface{})
data = make(map[string]any)
}
r.lock.Lock()
defer r.lock.Unlock()
b := Behavior{
finishTimestamp := time.Now().UnixNano()
fb := Behavior{
ID: len(r.behaviorList),
StartBehaviorID: behaviorID,
StartBehaviorID: b.StartBehaviorID,
Stack: GetTraceFileInfo(r.stackOffset),
Action: r.behaviorList[behaviorID].Action,
Action: b.Action,
Type: BehaviorActionTypeFinish,
Timestamp: time.Now().UnixNano(),
Timestamp: finishTimestamp,
CostNano: finishTimestamp - b.Timestamp,
CostMs: (finishTimestamp - b.Timestamp) / 1e6,
Data: data,
}
r.behaviorList = append(r.behaviorList, b)
r.behaviorList = append(r.behaviorList, fb)
}
// WrapRun ...
@ -99,14 +102,27 @@ func (r *Runtime) FinishBehavior(behaviorID int, data map[string]interface{}) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 00:17 2022/10/15
func (r *Runtime) WrapRun(action string, startData map[string]interface{}, logic func()) {
func (r *Runtime) WrapRun(action string, startData map[string]any, logic LogicFunc) error {
if nil == logic {
return
return nil
}
behaviorID := r.StartBehavior(action, startData)
var (
err error
)
b := r.StartBehavior(action, startData)
// 执行逻辑
logic()
defer r.FinishBehavior(behaviorID, nil)
err = logic()
finishData := map[string]any{}
if nil != err {
finishData["error"] = err.Error()
finishData["success"] = false
} else {
finishData["success"] = true
finishData["error"] = "success"
}
r.FinishBehavior(b, finishData)
return err
}
// GetTraceID 获取 trace id