初始化项目 + 数据结构定义

This commit is contained in:
白茶清欢 2022-10-15 00:08:32 +08:00
commit f7a9e60be1
4 changed files with 120 additions and 0 deletions

21
.gitignore vendored Normal file
View File

@ -0,0 +1,21 @@
# Created by .ignore support plugin (hsz.mobi)
### Go template
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Dependency directories (remove the comment below to include it)
# vendor/
.idea
.vscode
release
logs

28
define.go Normal file
View File

@ -0,0 +1,28 @@
// Package trace ...
//
// Description : trace ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2022-10-14 23:10
package trace
const (
// BehaviorActionTypeStart 开始某一行为
BehaviorActionTypeStart = "start"
// BehaviorActionTypeFinish 完成某一行为
BehaviorActionTypeFinish = "finish"
)
// Behavior 行为信息
//
// Author : go_developer@163.com<白茶清欢>
//
// 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"` // 本次行为附带的数据
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.zhangdeman.cn/zhangdeman/trace
go 1.17

68
trace.go Normal file
View File

@ -0,0 +1,68 @@
// Package trace ...
//
// Description : trace ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2022-10-14 23:25
package trace
import (
"sync"
"time"
)
// NewRuntime 获取runtime实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 23:26 2022/10/14
func NewRuntime(traceID string) *Runtime {
return &Runtime{
lock: &sync.Mutex{},
traceID: traceID,
behaviorList: make([]Behavior, 0),
}
}
// Runtime ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 23:32 2022/10/14
type Runtime struct {
lock *sync.Mutex // 锁
traceID string // 日志追踪ID
behaviorList []*Behavior // 行为列表
}
// StartBehavior 开始一个行为
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 23:41 2022/10/14
func (r *Runtime) StartBehavior(action string, data map[string]interface{}) int {
if nil == data {
data = make(map[string]interface{})
}
r.lock.Lock()
defer r.lock.Unlock()
b := &Behavior{
ID: len(r.behaviorList),
Action: action,
Type: BehaviorActionTypeStart,
Timestamp: time.Now().UnixNano(),
Data: data,
}
r.behaviorList = append(r.behaviorList, b)
return b.ID
}
// FinishBehavior 结束某一个行为
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 23:53 2022/10/14
func (r *Runtime) FinishBehavior(behaviorID int) {
}