MCPcopy Create free account
hub / github.com/GoSimplicity/LinkMe / InitDB

Function InitDB

ioc/db.go:21–68  ·  view source on GitHub ↗

InitDB 初始化数据库

()

Source from the content-addressed store, hash-verified

19
20// InitDB 初始化数据库
21func InitDB() *gorm.DB {
22 var c config
23
24 if err := viper.UnmarshalKey("db", &c); err != nil {
25 panic(fmt.Errorf("init failed:%v", err))
26 }
27 db, err := gorm.Open(mysql.Open(c.DSN), &gorm.Config{
28 Logger: logger.Default.LogMode(logger.Info),
29 })
30
31 if err != nil {
32 panic(err)
33 }
34 // 初始化表
35
36 if err = dao.InitTables(db); err != nil {
37 panic(err)
38 }
39
40 // 注册 Prometheus 插件
41 if err = db.Use(prometheus.New(prometheus.Config{
42 DBName: "linkme", // Prometheus中标识数据库的名称
43 RefreshInterval: 5, // 监控数据刷新间隔,单位为秒
44 })); err != nil {
45 log.Println("register prometheus plugin failed")
46 }
47
48 // 创建并注册自定义的 PrometheusCallbacks 插件,用于监控gorm操作执行时间
49 prometheusPlugin := prometheus3.NewPrometheusCallbacks(prometheus2.SummaryOpts{
50 Namespace: "linkme", // 命名空间
51 Subsystem: "gorm", // 子系统
52 Name: "operation_duration_seconds", // 指标名称
53 Help: "Duration of GORM database operations in seconds", // 指标帮助信息
54 Objectives: map[float64]float64{
55 0.5: 0.01,
56 0.75: 0.01,
57 0.9: 0.01,
58 0.99: 0.001,
59 0.999: 0.0001,
60 },
61 })
62
63 if err = db.Use(prometheusPlugin); err != nil {
64 log.Println("register custom prometheus callbacks plugin failed:", err)
65 }
66
67 return db
68}

Callers 2

TestSendCodeFunction · 0.92
InitWebServerFunction · 0.85

Calls 1

InitTablesFunction · 0.92

Tested by 1

TestSendCodeFunction · 0.74