1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
func_perf.go 1.93 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2022-09-08 20:26 . 换日志到zap
package perf
import (
"gitee.com/h79/goutils/common/debug"
"gitee.com/h79/goutils/common/logger"
"gitee.com/h79/goutils/common/result"
"gitee.com/h79/goutils/common/timer"
"gitee.com/h79/goutils/plugins"
"go.uber.org/zap"
"path/filepath"
"runtime"
)
type Perf struct {
model string
funName string
file string
line int
warn int64
start int64
}
func (p *Perf) WithModel(model string) *Perf {
p.model = model
return p
}
func (p *Perf) WithWarn(t int64) *Perf {
p.warn = t
return p
}
func (p *Perf) Start() *Perf {
p.start = timer.CurrentMS()
logger.Context().Debug("Exec",
zap.String("model", p.model),
zap.Int64("Start", p.start),
zap.String("funcName", p.funName),
zap.String("fileName", p.file),
zap.Int("line", p.line))
return p
}
func (p *Perf) End() {
end := timer.CurrentMS()
dif := end - p.start
logger.Context().Debug("Exec",
zap.String("model", p.model),
zap.Int64("End", end),
zap.Int64("dif", dif))
//执行时间太长,告警
wa := p.warn
if wa <= 0 {
wa = 2000
}
if dif >= p.warn+1000 {
d := debug.New(result.ErrTimeout).
WithDetail("'%s' call %s function, run time too long= %d, in %v line= %d", p.model, p.funName, dif, p.file, p.line).
WithLevel(debug.DTightLevel)
_ = plugins.DoWithError(plugins.KAlarm, d)
}
if dif >= p.warn {
d := debug.New(result.ErrTimeout).
WithDetail("'%s' call %s function, run time too long= %d,in %v line= %d", p.model, p.funName, dif, p.file, p.line).
WithLevel(debug.DImportantLevel)
_ = plugins.DoWithError(plugins.KAlarm, d)
}
}
func This() *Perf {
pc, file, line, _ := runtime.Caller(1)
return &Perf{
warn: 2000,
funName: filepath.Base(runtime.FuncForPC(pc).Name()),
file: filepath.Base(file),
line: line,
}
}
func Caller() *Perf {
pc, file, line, _ := runtime.Caller(2)
return &Perf{
warn: 2000,
funName: filepath.Base(runtime.FuncForPC(pc).Name()),
file: filepath.Base(file),
line: line,
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.1.30

搜索帮助