在 v2.x 里面,我们将原本的toolbox
拆分为两块,一块是admin
,即治理模块;另外一块是task
。
安装:
go get github.com/beego/beego/v2/core/admin
1 监控检查
安装需要检查的mysql库
go get -u github.com/beego/beego/v2/client/orm
go get -u github.com/go-sql-driver/mysql
在程序启动处设置MySQL驱动
import (
"github.com/beego/beego/v2/client/orm"
beego "github.com/beego/beego/v2/server/web"
_ "github.com/go-sql-driver/mysql"
_ "quickstart/routers"
)
func init() {
orm.RegisterDriver("mysql", orm.DRMySQL)
}
func main() {
beego.Run()
}
新建一个health.go
import (
"database/sql"
"github.com/beego/beego/v2/core/admin"
"github.com/pkg/errors"
)
type DatabaseCheck struct {
}
func (dc *DatabaseCheck) Check() error {
dsn := "leellun:liulun666@tcp(localhost:3308)/test"
//open函数只能检查格式是否正确,不正确立即中断程序
db, err := sql.Open("mysql", dsn)
if err != nil {
return errors.New("mysql connect fail1")
}
//判断数据库是否连接成功,可使用db中的Ping参数
err = db.Ping()
if err != nil {
return errors.New("mysql connect fail")
} else {
return nil
}
}
func init() {
admin.AddHealthCheck("database", &DatabaseCheck{})
}
通过bee run启动应用
PS C:\Users\leell\go\src\quickstart> bee run
______
| ___ \
| |_/ / ___ ___
| ___ \ / _ \ / _ \
| |_/ /| __/| __/
\____/ \___| \___| v2.1.0
2024/01/19 21:11:47 INFO ▶ 0001 Using 'quickstart' as 'appname'
2024/01/19 21:11:47 INFO ▶ 0002 Initializing watcher...
cannot install, GOBIN must be an absolute path
2024/01/19 21:11:49 SUCCESS ▶ 0003 Built Successfully!
2024/01/19 21:11:49 INFO ▶ 0004 Restarting 'quickstart.exe'...
2024/01/19 21:11:49 SUCCESS ▶ 0005 './quickstart.exe' is running...
2024/01/19 21:11:50.198 [I] [server.go:280] http server Running on http://:8080
2024/01/19 21:11:50.198 [D] [admin.go:87] now we don't start tasks here, if you use task module, please invoke task.StartTask, or task will not be executed
2024/01/19 21:11:50.225 [I] [admin.go:93] Admin server Running on :8088
2024/01/19 21:11:50.225 [I] [server.go:280] http server Running on http://:8088
打开浏览器查看数据库连接状态
2 性能监控
对于运行中的进程的性能监控是我们进行程序调优和查找问题的最佳方法,例如 GC、goroutine 等基础信息。profile 提供了方便的入口方便用户来调试程序,他主要是通过入口函数 ProcessInput
来进行处理各类请求,主要包括以下几种调试:
-
lookup goroutine
打印出来当前全部的 goroutine 执行的情况,非常方便查找各个 goroutine 在做的事情:
goroutine 3 [running]: runtime/pprof.writeGoroutineStacks(0x634238, 0xc210000008, 0x62b000, 0xd200000000000000) /Users/astaxie/go/src/pkg/runtime/pprof/pprof.go:511 +0x7c runtime/pprof.writeGoroutine(0x634238, 0xc210000008, 0x2, 0xd2676410957b30fd, 0xae98) /Users/astaxie/go/src/pkg/runtime/pprof/pprof.go:500 +0x3c runtime/pprof.(*Profile).WriteTo(0x52ebe0, 0x634238, 0xc210000008, 0x2, 0x1, ...) /Users/astaxie/go/src/pkg/runtime/pprof/pprof.go:229 +0xb4 _/Users/astaxie/github/beego/admin.ProcessInput(0x2c89f0, 0x10, 0x634238, 0xc210000008) /Users/astaxie/github/beego/admin/profile.go:26 +0x256 _/Users/astaxie/github/beego/admin.TestProcessInput(0xc21004e090) /Users/astaxie/github/beego/admin/profile_test.go:9 +0x5a testing.tRunner(0xc21004e090, 0x532320) /Users/astaxie/go/src/pkg/testing/testing.go:391 +0x8b created by testing.RunTests /Users/astaxie/go/src/pkg/testing/testing.go:471 +0x8b2 goroutine 1 [chan receive]: testing.RunTests(0x315668, 0x532320, 0x4, 0x4, 0x1) /Users/astaxie/go/src/pkg/testing/testing.go:472 +0x8d5 testing.Main(0x315668, 0x532320, 0x4, 0x4, 0x537700, ...) /Users/astaxie/go/src/pkg/testing/testing.go:403 +0x84 main.main() _/Users/astaxie/github/beego/admin/_test/_testmain.go:53 +0x9c
-
lookup heap
用来打印当前 heap 的信息:
heap profile: 1: 288 [2: 296] @ heap/1048576 1: 288 [2: 296] @ # runtime.MemStats # Alloc = 275504 # TotalAlloc = 275512 # Sys = 4069608 # Lookups = 5 # Mallocs = 469 # Frees = 1 # HeapAlloc = 275504 # HeapSys = 1048576 # HeapIdle = 647168 # HeapInuse = 401408 # HeapReleased = 0 # HeapObjects = 468 # Stack = 24576 / 131072 # MSpan = 4472 / 16384 # MCache = 1504 / 16384 # BuckHashSys = 1476472 # NextGC = 342976 # PauseNs = [370712 77378 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] # NumGC = 2 # EnableGC = true # DebugGC = false
-
lookup threadcreate
查看创建线程的信息:
threadcreate profile: total 4 1 @ 0x17f68 0x183c7 0x186a8 0x188cc 0x19ca9 0xcf41 0x139a3 0x196c0 # 0x183c7 newm+0x27 /Users/astaxie/go/src/pkg/runtime/proc.c:896 # 0x186a8 startm+0xb8 /Users/astaxie/go/src/pkg/runtime/proc.c:974 # 0x188cc handoffp+0x1ac /Users/astaxie/go/src/pkg/runtime/proc.c:992 # 0x19ca9 runtime.entersyscallblock+0x129 /Users/astaxie/go/src/pkg/runtime/proc.c:1514 # 0xcf41 runtime.notetsleepg+0x71 /Users/astaxie/go/src/pkg/runtime/lock_sema.c:253 # 0x139a3 runtime.MHeap_Scavenger+0xa3 /Users/astaxie/go/src/pkg/runtime/mheap.c:463 1 @ 0x17f68 0x183c7 0x186a8 0x188cc 0x189c3 0x1969b 0x2618b # 0x183c7 newm+0x27 /Users/astaxie/go/src/pkg/runtime/proc.c:896 # 0x186a8 startm+0xb8 /Users/astaxie/go/src/pkg/runtime/proc.c:974 # 0x188cc handoffp+0x1ac /Users/astaxie/go/src/pkg/runtime/proc.c:992 # 0x189c3 stoplockedm+0x83 /Users/astaxie/go/src/pkg/runtime/proc.c:1049 # 0x1969b runtime.gosched0+0x8b /Users/astaxie/go/src/pkg/runtime/proc.c:1382 # 0x2618b runtime.mcall+0x4b /Users/astaxie/go/src/pkg/runtime/asm_amd64.s:178 1 @ 0x17f68 0x183c7 0x170bc 0x196c0 # 0x183c7 newm+0x27 /Users/astaxie/go/src/pkg/runtime/proc.c:896 # 0x170bc runtime.main+0x3c /Users/astaxie/go/src/pkg/runtime/proc.c:191 1 @
-
lookup block
查看 block 信息:
--- contention: cycles/second=2294781025
-
start cpuprof
开始记录 cpuprof 信息,生产一个文件 cpu-pid.pprof,开始记录当前进程的 CPU 处理信息
-
stop cpuprof
关闭记录信息
-
get memprof
开启记录 memprof,生产一个文件 mem-pid.memprof
-
gc summary
查看 GC 信息
NumGC:2 Pause:54.54us Pause(Avg):170.82us Overhead:177.49% Alloc:248.97K Sys:3.88M Alloc(Rate):1.23G/s Histogram:287.09us 287.09us 287.09us
参考文章:
https://www.fansimao.com/861037.html
https://www.xichangyou.com/861063.html