beego的模块篇 - 监控检查、性能监控

news2024/10/6 22:26:33

在 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1400338.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【图解数据结构】顺序表实战指南:手把手教你详细实现(超详细解析)

🌈个人主页:聆风吟 🔥系列专栏:图解数据结构、算法模板 🔖少年有梦不应止于心动,更要付诸行动。 文章目录 一. ⛳️线性表1.1 🔔线性表的定义1.2 🔔线性表的存储结构 二. ⛳️顺序表…

linux的PXE服务(进阶知识)

一、批量部署概述 什么是PXE 预启动执行环境(PXE)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持工作站通过网络从远端服务器下载映像,并由此支持通过网络启动操作系统,在启动过程中&am…

使用vscode在wsl2中配置clangd环境

在vscode中安装这三个插件(clangd需要科学上网或者从VSIX安装) 之后创建一个空目录并进去。 使用快捷键ctrlshiftp,输入命令 Cmake:Quick Start 根据步骤选择。注意在创建CMakeLists.txt这一步选择跳过,直接输入enter&#xff0c…

多线程(看这一篇就够了,超详细,满满的干货)

多线程 一.认识线程(Thread)1. 1) 线程是什么1. 2) 为啥要有线程1.3) 进程和线程的区别标题1.4) Java的线程和操作系统线程的关系 二.创建线程方法1:继承Thread类方法2:实现Runnable接口方法3:匿名内部类创建Thread子类对象标题方法4:匿名内部类创建Runn…

(循环依赖问题)学习spring的第九天

Bean实例的属性填充 Spring在属性注入时 , 分为如下几种情况 : 注入单向对象引用 : 如usersevice里注入userdao , userdao里没有注入其他属性 注入双向对象引用 : 如usersevice里注入userdao , userdao也注入usersevice属性 二 . 着重看循环依赖问题 (搞清原理即可) 问题提出…

基于Spring+mybatis+vue的在线课后测试系统(Java毕业设计)

大家好,我是DeBug,很高兴你能来阅读!作为一名热爱编程的程序员,我希望通过这些教学笔记与大家分享我的编程经验和知识。在这里,我将会结合实际项目经验,分享编程技巧、最佳实践以及解决问题的方法。无论你是…

C语言第四弹---printf和scanf详解

✨个人主页: 熬夜学编程的小林 💗系列专栏: 【C语言详解】 【数据结构详解】 printf和scanf详解 1、printf和scanf详解介绍1.1 printf1.1.1 基本用法1.1.2 占位符1.1.3 占位符列举1.1.4 输出格式1.1.4.1 限定宽度1.1.4.2 总是显示正负号1.1…

响应式Web开发项目教程(HTML5+CSS3+Bootstrap)第2版 例4-6 fieldset

代码 <!doctype html> <html> <head> <meta charset"utf-8"> <title>fieldset</title> </head><body> <form action"#"><fieldset><legend>学生信息</legend>姓名&#xff1a;&…

svg矢量图标在wpf中的使用

在wpf应用程序开发中&#xff0c;为支持图标的矢量缩放&#xff0c;及在不同分辨率下界面中图标元素的矢量无损缩放&#xff0c;所以常常用到svg图标&#xff0c;那么如果完 美的将svg图标运用到wpf日常的项目开发中呢&#xff0c;这里分享一下我的个人使用经验和详细步骤。 步…

互联网加竞赛 基于机器视觉的手势检测和识别算法

0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 基于深度学习的手势检测与识别算法 该项目较为新颖&#xff0c;适合作为竞赛课题方向&#xff0c;学长非常推荐&#xff01; &#x1f9ff; 更多资料, 项目分享&#xff1a; https://gitee.com/dancheng…

1.10马原,总复习PART2

马克思主义鲜明特征 革命性&#xff0c;科学性&#xff0c;实践性 人民性&#xff0c;发展性 革命性、科学性&#xff0c;实践性&#xff0c;人民性&#xff0c;发展性 革命性&#xff0c;科学性&#xff0c;实践性&#xff0c;人 革命性&#xff0c;发展性&#xff0c…

Java设计模式-原型模式(3)

大家好,我是馆长!从今天开始馆长开始对java设计模式的创建型模式中的单例模式、原型模式、工厂方法、抽象工厂、建造者的原型模式进行讲解和说明。 原型模式(Prototype Pattern) 定义 原型模式是一种创建型设计模式,Prototype模式允许一个对象再创建另外一个可定制的对…

Mac book air 重新安装系统验证显示 untrusted_cert_title

环境&#xff1a; Mac Book Air macOS Sierra 问题描述&#xff1a; Mac book air 重新安装系统验证显示 untrusted_cert_title 解决方案&#xff1a; 1.终端输入命令行输入 date 会看到一个非常旧的日期 2.更改日期为当前时间 使用以下命令来设置日期和时间&#xff1a…

流式湖仓增强,Hologres + Flink构建企业级实时数仓

云布道师 2023 年 12 月&#xff0c;由阿里云主办的实时计算闭门会在北京举行&#xff0c;阿里云实时数仓Hologres 研发负责人姜伟华现场分享 HologresFlink 构建的企业级实时数仓&#xff0c;实现全链路的数据实时计算、实时写入、实时更新、实时查询。同时&#xff0c;随着流…

backtrader策略库:强化学习一: 梯度提升( Gradient Ascent)

本文来自博客文章&#xff0c;文末含源码链接。 In the next few posts, I will be going over a strategy that uses Machine Learning to determine what trades to execute. Before we start going over the strategy, we will go over one of the algorithms it uses: Gra…

数据结构一:算法效率分析(时间复杂度和空间复杂度)-重点

在学习具体的数据结构和算法之前&#xff0c;每一位初学者都要掌握一个技能&#xff0c;即善于运用时间复杂度和空间复杂度来衡量一个算法的运行效率。所谓算法&#xff0c;即解决问题的方法。同一个问题&#xff0c;使用不同的算法&#xff0c;虽然得到的结果相同&#xff0c;…

【c++leetcode】1913.Maximum Product Difference Between Two Pairs

问题入口 这个问题很容易解决。只要将数组排序&#xff0c;返回 最大元素*第二大元素-最小元素*第二小元素 即可。通过这道题顺便复习一些排序算法 。 直接使用sort函数 class Solution {public:int maxProductDifference(vector<int>& nums) {sort(nums.begin(),…

《WebKit 技术内幕》之三(2): WebKit 架构和模块

2.基于 Blink 的 Chrominum 浏览器结构 2.1 Chrominum 浏览器的架构及模块 Chromium也是基于WebKit&#xff08;Blink&#xff09;开发的&#xff0c;并且在WebKit的移植部分中&#xff0c;Chromium也做了很多有趣的事&#xff0c;所以通过Chromium可以了解如何基于WebKit构建浏…

尝试解决githubclone失败问题

BV1qV4y1m7PB 根据这个视频 似乎是我的linux的github似乎下好了 我没有配置好 比如我的ssh-key 现在根据视频试试 首先需要跳转到ssh的文件夹&#xff1a; cd ~/.ssh 然后生成一个ssh-key&#xff1a; ssh-keygen -t rsa -C "<github资料里的邮箱>" 然后…

Linux之进程间通信(管道)

目录 一、进程间通信 1、进程间通信的概念 2、进程间通信的目的 3、进程间通信的分类 二、管道 1、管道基本介绍 2、匿名管道 3、命名管道 一、进程间通信 1、进程间通信的概念 什么是进程间通信&#xff1f; 我们在学习了进程的相关知识后&#xff0c;知道&#xff…