From e851cc32ef773455061907ba12c78ec1a66e6b58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 15 Oct 2022 00:23:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=B8=80=E7=89=88trace?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define.go | 11 ++++++----- trace.go | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 9 deletions(-) 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) }