diff --git a/define.go b/define.go index ea02271..5b03057 100644 --- a/define.go +++ b/define.go @@ -20,9 +20,10 @@ const ( // // Date : 23:10 2022/10/14 type Behavior struct { - ID int `json:"id"` // 行为ID - Action string `json:"action"` // 行为 - Type string `json:"type"` // 行为记录类型 - Timestamp int64 `json:"timestamp"` // 触发行为的时间,纳秒时间戳 - Data map[string]interface{} `json:"data"` // 本次行为附带的数据 + ID int `json:"id"` // 行为ID + StartBehaviorID int `json:"start_behavior_id"` // 开始行为的ID,仅对 finish 类型的行为有效 + Action string `json:"action"` // 行为 + Type string `json:"type"` // 行为记录类型 + Timestamp int64 `json:"timestamp"` // 触发行为的时间,纳秒时间戳 + Data map[string]interface{} `json:"data"` // 本次行为附带的数据 } diff --git a/trace.go b/trace.go index bc19aca..40eab19 100644 --- a/trace.go +++ b/trace.go @@ -33,7 +33,7 @@ func NewRuntime(traceID string) *Runtime { type Runtime struct { lock *sync.Mutex // 锁 traceID string // 日志追踪ID - behaviorList []*Behavior // 行为列表 + behaviorList []Behavior // 行为列表 } // StartBehavior 开始一个行为 @@ -47,7 +47,7 @@ func (r *Runtime) StartBehavior(action string, data map[string]interface{}) int } r.lock.Lock() defer r.lock.Unlock() - b := &Behavior{ + b := Behavior{ ID: len(r.behaviorList), Action: action, Type: BehaviorActionTypeStart, @@ -63,6 +63,32 @@ 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) { - +func (r *Runtime) FinishBehavior(behaviorID int, data map[string]interface{}) { + if nil == data { + data = make(map[string]interface{}) + } + r.lock.Lock() + defer r.lock.Unlock() + b := Behavior{ + ID: len(r.behaviorList), + StartBehaviorID: behaviorID, + Action: r.behaviorList[behaviorID].Action, + Type: BehaviorActionTypeFinish, + Timestamp: time.Now().UnixNano(), + Data: data, + } + r.behaviorList = append(r.behaviorList, b) +} + +// WrapRun ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 00:17 2022/10/15 +func (r *Runtime) WrapRun(action string, startData map[string]interface{}, logic func()) { + if nil == logic { + return + } + behaviorID := r.StartBehavior(action, startData) + defer r.FinishBehavior(behaviorID, nil) }