增加调用堆栈的获取
This commit is contained in:
parent
e37a26f4ad
commit
2bee255fcf
11
define.go
11
define.go
@ -22,8 +22,19 @@ const (
|
|||||||
type Behavior struct {
|
type Behavior struct {
|
||||||
ID int `json:"id"` // 行为ID
|
ID int `json:"id"` // 行为ID
|
||||||
StartBehaviorID int `json:"start_behavior_id"` // 开始行为的ID,仅对 finish 类型的行为有效
|
StartBehaviorID int `json:"start_behavior_id"` // 开始行为的ID,仅对 finish 类型的行为有效
|
||||||
|
Stack CallStack `json:"stack"` // 调用堆栈信息
|
||||||
Action string `json:"action"` // 行为
|
Action string `json:"action"` // 行为
|
||||||
Type string `json:"type"` // 行为记录类型
|
Type string `json:"type"` // 行为记录类型
|
||||||
Timestamp int64 `json:"timestamp"` // 触发行为的时间,纳秒时间戳
|
Timestamp int64 `json:"timestamp"` // 触发行为的时间,纳秒时间戳
|
||||||
Data map[string]interface{} `json:"data"` // 本次行为附带的数据
|
Data map[string]interface{} `json:"data"` // 本次行为附带的数据
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CallStack 调用堆栈
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 00:48 2022/10/15
|
||||||
|
type CallStack struct {
|
||||||
|
FilePath string `json:"file_path"` // 文件路径
|
||||||
|
LineNo int `json:"line_no"` // 所在行号
|
||||||
|
}
|
||||||
|
10
runtime.go
10
runtime.go
@ -19,15 +19,20 @@ import (
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 23:26 2022/10/14
|
// Date : 23:26 2022/10/14
|
||||||
func NewRuntime(traceID string) *Runtime {
|
func NewRuntime(traceID string, stackOffset int) *Runtime {
|
||||||
if len(traceID) == 0 {
|
if len(traceID) == 0 {
|
||||||
// 若不指定 trace id , 随机生成
|
// 若不指定 trace id , 随机生成
|
||||||
traceID = util.String.Md5(util.String.GenRandomMd5())
|
traceID = util.String.Md5(util.String.GenRandomMd5())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if stackOffset < 0 {
|
||||||
|
stackOffset = 0
|
||||||
|
}
|
||||||
return &Runtime{
|
return &Runtime{
|
||||||
lock: &sync.RWMutex{},
|
lock: &sync.RWMutex{},
|
||||||
traceID: traceID,
|
traceID: traceID,
|
||||||
behaviorList: make([]Behavior, 0),
|
behaviorList: make([]Behavior, 0),
|
||||||
|
stackOffset: stackOffset,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,6 +45,7 @@ type Runtime struct {
|
|||||||
lock *sync.RWMutex // 锁
|
lock *sync.RWMutex // 锁
|
||||||
traceID string // 日志追踪ID
|
traceID string // 日志追踪ID
|
||||||
behaviorList []Behavior // 行为列表
|
behaviorList []Behavior // 行为列表
|
||||||
|
stackOffset int // 堆栈回溯层级
|
||||||
}
|
}
|
||||||
|
|
||||||
// StartBehavior 开始一个行为
|
// StartBehavior 开始一个行为
|
||||||
@ -55,6 +61,7 @@ func (r *Runtime) StartBehavior(action string, data map[string]interface{}) int
|
|||||||
defer r.lock.Unlock()
|
defer r.lock.Unlock()
|
||||||
b := Behavior{
|
b := Behavior{
|
||||||
ID: len(r.behaviorList),
|
ID: len(r.behaviorList),
|
||||||
|
Stack: GetTraceFileInfo(r.stackOffset),
|
||||||
Action: action,
|
Action: action,
|
||||||
Type: BehaviorActionTypeStart,
|
Type: BehaviorActionTypeStart,
|
||||||
Timestamp: time.Now().UnixNano(),
|
Timestamp: time.Now().UnixNano(),
|
||||||
@ -78,6 +85,7 @@ func (r *Runtime) FinishBehavior(behaviorID int, data map[string]interface{}) {
|
|||||||
b := Behavior{
|
b := Behavior{
|
||||||
ID: len(r.behaviorList),
|
ID: len(r.behaviorList),
|
||||||
StartBehaviorID: behaviorID,
|
StartBehaviorID: behaviorID,
|
||||||
|
Stack: GetTraceFileInfo(r.stackOffset),
|
||||||
Action: r.behaviorList[behaviorID].Action,
|
Action: r.behaviorList[behaviorID].Action,
|
||||||
Type: BehaviorActionTypeFinish,
|
Type: BehaviorActionTypeFinish,
|
||||||
Timestamp: time.Now().UnixNano(),
|
Timestamp: time.Now().UnixNano(),
|
||||||
|
21
trace.go
Normal file
21
trace.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
// Package trace ...
|
||||||
|
//
|
||||||
|
// Description : trace ...
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 2022-10-15 00:37
|
||||||
|
package trace
|
||||||
|
|
||||||
|
import "runtime"
|
||||||
|
|
||||||
|
// GetTraceFileInfo 获取文件 trace 信息
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 00:44 2022/10/15
|
||||||
|
func GetTraceFileInfo(offset int) CallStack {
|
||||||
|
stack := CallStack{}
|
||||||
|
_, stack.FilePath, stack.LineNo, _ = runtime.Caller(offset)
|
||||||
|
return stack
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user