完成一版trace逻辑
This commit is contained in:
parent
f7a9e60be1
commit
e851cc32ef
@ -21,6 +21,7 @@ const (
|
|||||||
// Date : 23:10 2022/10/14
|
// Date : 23:10 2022/10/14
|
||||||
type Behavior struct {
|
type Behavior struct {
|
||||||
ID int `json:"id"` // 行为ID
|
ID int `json:"id"` // 行为ID
|
||||||
|
StartBehaviorID int `json:"start_behavior_id"` // 开始行为的ID,仅对 finish 类型的行为有效
|
||||||
Action string `json:"action"` // 行为
|
Action string `json:"action"` // 行为
|
||||||
Type string `json:"type"` // 行为记录类型
|
Type string `json:"type"` // 行为记录类型
|
||||||
Timestamp int64 `json:"timestamp"` // 触发行为的时间,纳秒时间戳
|
Timestamp int64 `json:"timestamp"` // 触发行为的时间,纳秒时间戳
|
||||||
|
34
trace.go
34
trace.go
@ -33,7 +33,7 @@ func NewRuntime(traceID string) *Runtime {
|
|||||||
type Runtime struct {
|
type Runtime struct {
|
||||||
lock *sync.Mutex // 锁
|
lock *sync.Mutex // 锁
|
||||||
traceID string // 日志追踪ID
|
traceID string // 日志追踪ID
|
||||||
behaviorList []*Behavior // 行为列表
|
behaviorList []Behavior // 行为列表
|
||||||
}
|
}
|
||||||
|
|
||||||
// StartBehavior 开始一个行为
|
// StartBehavior 开始一个行为
|
||||||
@ -47,7 +47,7 @@ func (r *Runtime) StartBehavior(action string, data map[string]interface{}) int
|
|||||||
}
|
}
|
||||||
r.lock.Lock()
|
r.lock.Lock()
|
||||||
defer r.lock.Unlock()
|
defer r.lock.Unlock()
|
||||||
b := &Behavior{
|
b := Behavior{
|
||||||
ID: len(r.behaviorList),
|
ID: len(r.behaviorList),
|
||||||
Action: action,
|
Action: action,
|
||||||
Type: BehaviorActionTypeStart,
|
Type: BehaviorActionTypeStart,
|
||||||
@ -63,6 +63,32 @@ func (r *Runtime) StartBehavior(action string, data map[string]interface{}) int
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 23:53 2022/10/14
|
// 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)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user