增加调用堆栈的获取

This commit is contained in:
2022-10-15 00:57:39 +08:00
parent e37a26f4ad
commit 2bee255fcf
3 changed files with 41 additions and 1 deletions

View File

@ -19,15 +19,20 @@ import (
// Author : go_developer@163.com<白茶清欢>
//
// Date : 23:26 2022/10/14
func NewRuntime(traceID string) *Runtime {
func NewRuntime(traceID string, stackOffset int) *Runtime {
if len(traceID) == 0 {
// 若不指定 trace id , 随机生成
traceID = util.String.Md5(util.String.GenRandomMd5())
}
if stackOffset < 0 {
stackOffset = 0
}
return &Runtime{
lock: &sync.RWMutex{},
traceID: traceID,
behaviorList: make([]Behavior, 0),
stackOffset: stackOffset,
}
}
@ -40,6 +45,7 @@ type Runtime struct {
lock *sync.RWMutex // 锁
traceID string // 日志追踪ID
behaviorList []Behavior // 行为列表
stackOffset int // 堆栈回溯层级
}
// StartBehavior 开始一个行为
@ -55,6 +61,7 @@ func (r *Runtime) StartBehavior(action string, data map[string]interface{}) int
defer r.lock.Unlock()
b := Behavior{
ID: len(r.behaviorList),
Stack: GetTraceFileInfo(r.stackOffset),
Action: action,
Type: BehaviorActionTypeStart,
Timestamp: time.Now().UnixNano(),
@ -78,6 +85,7 @@ func (r *Runtime) FinishBehavior(behaviorID int, data map[string]interface{}) {
b := Behavior{
ID: len(r.behaviorList),
StartBehaviorID: behaviorID,
Stack: GetTraceFileInfo(r.stackOffset),
Action: r.behaviorList[behaviorID].Action,
Type: BehaviorActionTypeFinish,
Timestamp: time.Now().UnixNano(),