From 9201d2140400669ee27f6726848c715144052972 Mon Sep 17 00:00:00 2001 From: zhanghan2021 Date: Thu, 8 Jun 2023 10:39:45 +0800 Subject: [PATCH] add mysql service --- prometheus/server/config.yml.templete | 8 ++- prometheus/server/config/config.go | 15 ++++-- prometheus/server/db/db.go | 74 +++++++++++++++++++++++++++ prometheus/server/global/global.go | 11 ++++ prometheus/server/main.go | 14 +++-- prometheus/server/router/router.go | 55 ++++++++------------ 6 files changed, 136 insertions(+), 41 deletions(-) create mode 100644 prometheus/server/db/db.go create mode 100644 prometheus/server/global/global.go diff --git a/prometheus/server/config.yml.templete b/prometheus/server/config.yml.templete index b2c847a4..d200ff31 100644 --- a/prometheus/server/config.yml.templete +++ b/prometheus/server/config.yml.templete @@ -8,4 +8,10 @@ log: driver: stdout #可选stdout和file。stdout:输出到终端控制台;file:输出到path下的指定文件。 path: ./log/plugin_prometheus.log max_file: 1 - max_size: 10485760 \ No newline at end of file + max_size: 10485760 +mysql: + host: localhost + port: 3306 + user: '' + password: '' + database: PluginPrometheus \ No newline at end of file diff --git a/prometheus/server/config/config.go b/prometheus/server/config/config.go index 2fefee61..55f96334 100644 --- a/prometheus/server/config/config.go +++ b/prometheus/server/config/config.go @@ -17,10 +17,19 @@ type HttpConf struct { Addr string `yaml:"addr"` } +type MysqlDBInfo struct { + HostName string `yaml:"host"` + Port int `yaml:"port"` + UserName string `yaml:"user"` + Password string `yaml:"password"` + DataBase string `yaml:"database"` +} + type ServerConfig struct { - Prometheus *Prometheus `yaml:"prometheus"` - Http *HttpConf `yaml:"http"` - Logopts logger.LogOpts `yaml:"log"` + Prometheus *Prometheus `yaml:"prometheus"` + Http *HttpConf `yaml:"http"` + Logopts *logger.LogOpts `yaml:"log"` + Mysql *MysqlDBInfo `yaml:"mysql"` } const config_file = "./config.yml" diff --git a/prometheus/server/db/db.go b/prometheus/server/db/db.go new file mode 100644 index 00000000..de58a3ef --- /dev/null +++ b/prometheus/server/db/db.go @@ -0,0 +1,74 @@ +package db + +import ( + "database/sql" + "fmt" + + "gorm.io/driver/mysql" + "gorm.io/gorm" + "gorm.io/gorm/schema" + "openeuler.org/PilotGo/prometheus-plugin/config" + "openeuler.org/PilotGo/prometheus-plugin/global" +) + +var Url string + +type MysqlManager struct { + ip string + port int + userName string + passWord string + dbName string + db *gorm.DB +} + +func MysqldbInit(conf *config.MysqlDBInfo) error { + _, err := mysqlInit( + conf.HostName, + conf.UserName, + conf.Password, + conf.DataBase, + conf.Port) + if err != nil { + return err + } + + return nil +} + +func mysqlInit(ip, username, password, dbname string, port int) (*MysqlManager, error) { + m := &MysqlManager{ + ip: ip, + port: port, + userName: username, + passWord: password, + dbName: dbname, + } + Url = fmt.Sprintf("%s:%s@(%s:%d)/%s?charset=utf8mb4&parseTime=true", + m.userName, + m.passWord, + m.ip, + m.port, + m.dbName) + + var err error + m.db, err = gorm.Open(mysql.Open(Url), &gorm.Config{ + NamingStrategy: schema.NamingStrategy{ + SingularTable: true, + }, + }) + if err != nil { + return nil, err + } + global.GlobalDB = m.db + + var db *sql.DB + if db, err = m.db.DB(); err != nil { + return nil, err + } + + db.SetMaxIdleConns(10) + db.SetMaxOpenConns(100) + + return m, nil +} diff --git a/prometheus/server/global/global.go b/prometheus/server/global/global.go new file mode 100644 index 00000000..a96e4dde --- /dev/null +++ b/prometheus/server/global/global.go @@ -0,0 +1,11 @@ +package global + +import ( + "gitee.com/openeuler/PilotGo-plugins/sdk/plugin/client" + "gorm.io/gorm" +) + +var ( + GlobalClient *client.Client + GlobalDB *gorm.DB +) diff --git a/prometheus/server/main.go b/prometheus/server/main.go index 4cb4c021..e6e92116 100644 --- a/prometheus/server/main.go +++ b/prometheus/server/main.go @@ -7,6 +7,8 @@ import ( "gitee.com/openeuler/PilotGo-plugins/sdk/logger" "gitee.com/openeuler/PilotGo-plugins/sdk/plugin/client" "openeuler.org/PilotGo/prometheus-plugin/config" + "openeuler.org/PilotGo/prometheus-plugin/db" + "openeuler.org/PilotGo/prometheus-plugin/global" "openeuler.org/PilotGo/prometheus-plugin/plugin" "openeuler.org/PilotGo/prometheus-plugin/router" ) @@ -16,15 +18,21 @@ func main() { config.Init() - if err := logger.Init(&config.Config().Logopts); err != nil { + if err := logger.Init(config.Config().Logopts); err != nil { fmt.Printf("logger init failed, please check the config file: %s", err) os.Exit(-1) } + if err := db.MysqldbInit(config.Config().Mysql); err != nil { + logger.Error("mysql db init failed, please check again: %s", err) + os.Exit(-1) + } + server := router.InitRouter() - client := client.DefaultClient(plugin.Init(config.Config().Prometheus)) - client.RegisterHandlers(server) + global.GlobalClient = client.DefaultClient(plugin.Init(config.Config().Prometheus)) + router.RegisterAPIs(server) + global.GlobalClient.Server = config.Config().Http.Addr if err := server.Run(config.Config().Http.Addr); err != nil { logger.Fatal("failed to run server") diff --git a/prometheus/server/router/router.go b/prometheus/server/router/router.go index 81daf90e..b77e32f3 100644 --- a/prometheus/server/router/router.go +++ b/prometheus/server/router/router.go @@ -8,6 +8,7 @@ import ( "gitee.com/openeuler/PilotGo-plugins/sdk/logger" "github.com/gin-gonic/gin" + "openeuler.org/PilotGo/prometheus-plugin/global" ) func InitRouter() *gin.Engine { @@ -16,45 +17,31 @@ func InitRouter() *gin.Engine { router.Use(logger.LoggerDebug()) router.Use(gin.Recovery()) - registerAPIs(router) return router } -func registerAPIs(router *gin.Engine) { + +func RegisterAPIs(router *gin.Engine) { + global.GlobalClient.RegisterHandlers(router) + + pg := router.Group("/plugin/" + global.GlobalClient.PluginInfo.Name) + { + pg.GET("/query", func(c *gin.Context) { + c.Set("query", global.GlobalClient.PluginInfo.ReverseDest) + Query(c) + }) + pg.GET("/query_range", func(c *gin.Context) { + c.Set("query_range", global.GlobalClient.PluginInfo.ReverseDest) + QueryRange(c) + }) + pg.GET("/targets", func(c *gin.Context) { + c.Set("targets", global.GlobalClient.PluginInfo.ReverseDest) + Targets(c) + }) + + } router.GET("/ping", func(c *gin.Context) { c.String(http.StatusOK, "pong") }) } -// func DefaultClient(desc *splugin.PluginInfo) *splugin.Client { -// splugin.BaseInfo = desc -// dest := desc.ReverseDest - -// router := gin.Default() -// mg := router.Group("plugin_manage/") -// { -// mg.GET("/info", splugin.InfoHandler) -// } - -// pg := router.Group("/plugin/" + desc.Name) -// { -// pg.GET("/query", func(c *gin.Context) { -// c.Set("query", dest) -// Query(c) -// }) -// pg.GET("/query_range", func(c *gin.Context) { -// c.Set("query_range", dest) -// QueryRange(c) -// }) -// pg.GET("/targets", func(c *gin.Context) { -// c.Set("targets", dest) -// Targets(c) -// }) - -// } - -// return &splugin.Client{ -// Router: router, -// } -// } - func Query(c *gin.Context) { remote := c.GetString("query") if remote == "" { -- Gitee