这个模块主要是参考了 Dropwizard 框架,是一位用户提醒我说有这么一个框架,然后里面实现一些很酷的东西。那个 issue 详细描述了该功能的雏形,然后就在参考该功能的情况下增加了一些额外的很酷的功能,接下来我将一一介绍这个模块中的几个功能:健康检查、性能调试、访问统计、计划任务。

在 v2.x 里面,我们将原本的toolbox拆分为两块,一块是admin,即治理模块;另外一块是task

我们的设计哲学认为,task是一个和server,client平级的东西。

如何安装

  1. go get github.com/beego/beego/v2/core/admin

healthcheck

监控检查是用于当你应用于产品环境中进程,检查当前的状态是否正常,例如你要检查当前数据库是否可用,如下例子所示:

  1. type DatabaseCheck struct {
  2. }
  3. func (dc *DatabaseCheck) Check() error {
  4. if dc.isConnected() {
  5. return nil
  6. } else {
  7. return errors.New("can't connect database")
  8. }
  9. }

然后就可以通过如下方式增加检测项:

  1. admin.AddHealthCheck("database",&DatabaseCheck{})

加入之后,你可以往你的管理端口 /healthcheck 发送GET请求:

  1. $ curl http://beego.me:8088/healthcheck
  2. * deadlocks: OK
  3. * database: OK

如果检测显示是正确的,那么输出 OK,如果检测出错,显示出错的信息。

profile

对于运行中的进程的性能监控是我们进行程序调优和查找问题的最佳方法,例如 GC、goroutine 等基础信息。profile 提供了方便的入口方便用户来调试程序,他主要是通过入口函数 ProcessInput 来进行处理各类请求,主要包括以下几种调试:

  • lookup goroutine

    打印出来当前全部的 goroutine 执行的情况,非常方便查找各个 goroutine 在做的事情:

    1. goroutine 3 [running]:
    2. runtime/pprof.writeGoroutineStacks(0x634238, 0xc210000008, 0x62b000, 0xd200000000000000)
    3. /Users/astaxie/go/src/pkg/runtime/pprof/pprof.go:511 +0x7c
    4. runtime/pprof.writeGoroutine(0x634238, 0xc210000008, 0x2, 0xd2676410957b30fd, 0xae98)
    5. /Users/astaxie/go/src/pkg/runtime/pprof/pprof.go:500 +0x3c
    6. runtime/pprof.(*Profile).WriteTo(0x52ebe0, 0x634238, 0xc210000008, 0x2, 0x1, ...)
    7. /Users/astaxie/go/src/pkg/runtime/pprof/pprof.go:229 +0xb4
    8. _/Users/astaxie/github/beego/admin.ProcessInput(0x2c89f0, 0x10, 0x634238, 0xc210000008)
    9. /Users/astaxie/github/beego/admin/profile.go:26 +0x256
    10. _/Users/astaxie/github/beego/admin.TestProcessInput(0xc21004e090)
    11. /Users/astaxie/github/beego/admin/profile_test.go:9 +0x5a
    12. testing.tRunner(0xc21004e090, 0x532320)
    13. /Users/astaxie/go/src/pkg/testing/testing.go:391 +0x8b
    14. created by testing.RunTests
    15. /Users/astaxie/go/src/pkg/testing/testing.go:471 +0x8b2
    16. goroutine 1 [chan receive]:
    17. testing.RunTests(0x315668, 0x532320, 0x4, 0x4, 0x1)
    18. /Users/astaxie/go/src/pkg/testing/testing.go:472 +0x8d5
    19. testing.Main(0x315668, 0x532320, 0x4, 0x4, 0x537700, ...)
    20. /Users/astaxie/go/src/pkg/testing/testing.go:403 +0x84
    21. main.main()
    22. _/Users/astaxie/github/beego/admin/_test/_testmain.go:53 +0x9c
  • lookup heap

    用来打印当前 heap 的信息:

    1. heap profile: 1: 288 [2: 296] @ heap/1048576
    2. 1: 288 [2: 296] @
  1. # runtime.MemStats
  2. # Alloc = 275504
  3. # TotalAlloc = 275512
  4. # Sys = 4069608
  5. # Lookups = 5
  6. # Mallocs = 469
  7. # Frees = 1
  8. # HeapAlloc = 275504
  9. # HeapSys = 1048576
  10. # HeapIdle = 647168
  11. # HeapInuse = 401408
  12. # HeapReleased = 0
  13. # HeapObjects = 468
  14. # Stack = 24576 / 131072
  15. # MSpan = 4472 / 16384
  16. # MCache = 1504 / 16384
  17. # BuckHashSys = 1476472
  18. # NextGC = 342976
  19. # 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]
  20. # NumGC = 2
  21. # EnableGC = true
  22. # DebugGC = false
  • lookup threadcreate

    查看创建线程的信息:

    1. threadcreate profile: total 4
    2. 1 @ 0x17f68 0x183c7 0x186a8 0x188cc 0x19ca9 0xcf41 0x139a3 0x196c0
    3. # 0x183c7 newm+0x27 /Users/astaxie/go/src/pkg/runtime/proc.c:896
    4. # 0x186a8 startm+0xb8 /Users/astaxie/go/src/pkg/runtime/proc.c:974
    5. # 0x188cc handoffp+0x1ac /Users/astaxie/go/src/pkg/runtime/proc.c:992
    6. # 0x19ca9 runtime.entersyscallblock+0x129 /Users/astaxie/go/src/pkg/runtime/proc.c:1514
    7. # 0xcf41 runtime.notetsleepg+0x71 /Users/astaxie/go/src/pkg/runtime/lock_sema.c:253
    8. # 0x139a3 runtime.MHeap_Scavenger+0xa3 /Users/astaxie/go/src/pkg/runtime/mheap.c:463
    9. 1 @ 0x17f68 0x183c7 0x186a8 0x188cc 0x189c3 0x1969b 0x2618b
    10. # 0x183c7 newm+0x27 /Users/astaxie/go/src/pkg/runtime/proc.c:896
    11. # 0x186a8 startm+0xb8 /Users/astaxie/go/src/pkg/runtime/proc.c:974
    12. # 0x188cc handoffp+0x1ac /Users/astaxie/go/src/pkg/runtime/proc.c:992
    13. # 0x189c3 stoplockedm+0x83 /Users/astaxie/go/src/pkg/runtime/proc.c:1049
    14. # 0x1969b runtime.gosched0+0x8b /Users/astaxie/go/src/pkg/runtime/proc.c:1382
    15. # 0x2618b runtime.mcall+0x4b /Users/astaxie/go/src/pkg/runtime/asm_amd64.s:178
    16. 1 @ 0x17f68 0x183c7 0x170bc 0x196c0
    17. # 0x183c7 newm+0x27 /Users/astaxie/go/src/pkg/runtime/proc.c:896
    18. # 0x170bc runtime.main+0x3c /Users/astaxie/go/src/pkg/runtime/proc.c:191
    19. 1 @
  • lookup block

    查看 block 信息:

    1. --- contention:
    2. cycles/second=2294781025
  • start cpuprof

    开始记录 cpuprof 信息,生产一个文件 cpu-pid.pprof,开始记录当前进程的 CPU 处理信息

  • stop cpuprof

    关闭记录信息

  • get memprof

    开启记录 memprof,生产一个文件 mem-pid.memprof

  • gc summary

    查看 GC 信息

    1. 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

statistics

注意!在 v2.x 里面,我们实际上并不建议直接使用admin来收集这种统计信息。只有在你的应用是单机应用,并且只部署少量实例的时候,可以考虑使用这种方式。

我们推荐的是使用专门的中间件来完成统计的功能,例如opentracingprometheus

我们在 web, httplib, orm 上都提供了对应的Filter来接入这种中间件。

admin 模块 - 图1

如何使用这个统计呢?如下所示添加统计:

  1. admin.StatisticsMap.AddStatistics("POST", "/api/user", "&admin.user", time.Duration(2000))
  2. admin.StatisticsMap.AddStatistics("POST", "/api/user", "&admin.user", time.Duration(120000))
  3. admin.StatisticsMap.AddStatistics("GET", "/api/user", "&admin.user", time.Duration(13000))
  4. admin.StatisticsMap.AddStatistics("POST", "/api/admin", "&admin.user", time.Duration(14000))
  5. admin.StatisticsMap.AddStatistics("POST", "/api/user/astaxie", "&admin.user", time.Duration(12000))
  6. admin.StatisticsMap.AddStatistics("POST", "/api/user/xiemengjun", "&admin.user", time.Duration(13000))
  7. admin.StatisticsMap.AddStatistics("DELETE", "/api/user", "&admin.user", time.Duration(1400))

获取统计信息

  1. admin.StatisticsMap.GetMap(os.Stdout)

输出如下格式的信息:

  1. | requestUrl | method | times | used | max used | min used | avg used |
  2. | /api/user | POST | 2 | 122.00us | 120.00us | 2.00us | 61.00us |
  3. | /api/user | GET | 1 | 13.00us | 13.00us | 13.00us | 13.00us |
  4. | /api/user | DELETE | 1 | 1.40us | 1.40us | 1.40us | 1.40us |
  5. | /api/admin | POST | 1 | 14.00us | 14.00us | 14.00us | 14.00us |
  6. | /api/user/astaxie | POST | 1 | 12.00us | 12.00us | 12.00us | 12.00us |
  7. | /api/user/xiemengjun | POST | 1 | 13.00us | 13.00us | 13.00us | 13.00us |