增加读取行为列表方法

This commit is contained in:
白茶清欢 2022-10-15 00:35:18 +08:00
parent 110a9afca6
commit e37a26f4ad
3 changed files with 69 additions and 4 deletions

13
go.mod
View File

@ -1,3 +1,16 @@
module git.zhangdeman.cn/zhangdeman/trace module git.zhangdeman.cn/zhangdeman/trace
go 1.17 go 1.17
require (
git.zhangdeman.cn/zhangdeman/util v0.0.0-20221014054633-9931590d801d // indirect
github.com/Jeffail/gabs v1.4.0 // indirect
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect
github.com/go-ini/ini v1.67.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/tidwall/gjson v1.14.3 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

22
go.sum Normal file
View File

@ -0,0 +1,22 @@
git.zhangdeman.cn/zhangdeman/util v0.0.0-20221014054633-9931590d801d h1:JH8+12c4ZPcR1Wsvdwz7OvAw+BrAvMXV1AGf1M1DJ4Y=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20221014054633-9931590d801d/go.mod h1:zTir/0IWdK3E7n0GiaogyWHADAQnBtTdl2I6Z2/OPqw=
github.com/Jeffail/gabs v1.4.0 h1://5fYRRTq1edjfIrQGvdkcd22pkYUrHZ5YC/H2GJVAo=
github.com/Jeffail/gabs v1.4.0/go.mod h1:6xMvQMK4k33lb7GUUpaAPh6nKMmemQeg5d4gn7/bOXc=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg=
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/tidwall/gjson v1.14.3 h1:9jvXn7olKEHU1S9vwoMGliaT8jq1vJ7IH/n9zD9Dnlw=
github.com/tidwall/gjson v1.14.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -10,6 +10,8 @@ package trace
import ( import (
"sync" "sync"
"time" "time"
"git.zhangdeman.cn/zhangdeman/util"
) )
// NewRuntime 获取runtime实例 // NewRuntime 获取runtime实例
@ -18,8 +20,12 @@ import (
// //
// Date : 23:26 2022/10/14 // Date : 23:26 2022/10/14
func NewRuntime(traceID string) *Runtime { func NewRuntime(traceID string) *Runtime {
if len(traceID) == 0 {
// 若不指定 trace id , 随机生成
traceID = util.String.Md5(util.String.GenRandomMd5())
}
return &Runtime{ return &Runtime{
lock: &sync.Mutex{}, lock: &sync.RWMutex{},
traceID: traceID, traceID: traceID,
behaviorList: make([]Behavior, 0), behaviorList: make([]Behavior, 0),
} }
@ -31,7 +37,7 @@ func NewRuntime(traceID string) *Runtime {
// //
// Date : 23:32 2022/10/14 // Date : 23:32 2022/10/14
type Runtime struct { type Runtime struct {
lock *sync.Mutex // 锁 lock *sync.RWMutex // 锁
traceID string // 日志追踪ID traceID string // 日志追踪ID
behaviorList []Behavior // 行为列表 behaviorList []Behavior // 行为列表
} }
@ -92,3 +98,27 @@ func (r *Runtime) WrapRun(action string, startData map[string]interface{}, logic
behaviorID := r.StartBehavior(action, startData) behaviorID := r.StartBehavior(action, startData)
defer r.FinishBehavior(behaviorID, nil) defer r.FinishBehavior(behaviorID, nil)
} }
// GetTraceID 获取 trace id
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 00:28 2022/10/15
func (r *Runtime) GetTraceID() string {
return r.traceID
}
// GetBehaviorList 获取行为列表
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 00:29 2022/10/15
func (r *Runtime) GetBehaviorList() []Behavior {
r.lock.RLock()
defer r.lock.RUnlock()
behaviorList := make([]Behavior, 0)
for _, item := range r.behaviorList {
behaviorList = append(behaviorList, item)
}
return behaviorList
}