From aa60c81a2931a9c24c30fddd1b7be3fe8f5c331c Mon Sep 17 00:00:00 2001 From: zhanghan2021 Date: Wed, 7 Jun 2023 14:01:22 +0800 Subject: [PATCH] refactory prometheus plugin main func --- prometheus/server/config.yml.templete | 10 +-- prometheus/server/config/config.go | 14 +--- prometheus/server/main.go | 34 ++++++---- prometheus/server/plugin/plugin_manager.go | 20 ++++++ prometheus/server/router/router.go | 76 +++++++++++++--------- sdk/logger/logger.go | 39 +++++++++++ 6 files changed, 130 insertions(+), 63 deletions(-) create mode 100644 prometheus/server/plugin/plugin_manager.go diff --git a/prometheus/server/config.yml.templete b/prometheus/server/config.yml.templete index 53eee6f9..b2c847a4 100644 --- a/prometheus/server/config.yml.templete +++ b/prometheus/server/config.yml.templete @@ -1,14 +1,8 @@ -pluginInfo: - name: "Prometheus" - version: "0.0.1" - description: "Prometheus开源系统监视和警报工具包" - author: "zhanghan" - email: "zhanghan@kylinos.cn" -plugin: +prometheus: url: "http://localhost:8090/plugin/prometheus" reverseDest: "http://localhost:9090" http: - port: "8090" + addr: "0.0.0.0:8090" log: level: debug driver: stdout #可选stdout和file。stdout:输出到终端控制台;file:输出到path下的指定文件。 diff --git a/prometheus/server/config/config.go b/prometheus/server/config/config.go index 82be1d6e..2fefee61 100644 --- a/prometheus/server/config/config.go +++ b/prometheus/server/config/config.go @@ -9,24 +9,16 @@ import ( "gopkg.in/yaml.v2" ) -type PluginInfo struct { - Name string `yaml:"name"` - Version string `yaml:"version"` - Description string `yaml:"description"` - Author string `yaml:"author"` - Email string `yaml:"email"` -} -type Plugin struct { +type Prometheus struct { URL string `yaml:"url"` ReverseDest string `yaml:"reverseDest"` } type HttpConf struct { - Port string `yaml:"port"` + Addr string `yaml:"addr"` } type ServerConfig struct { - PluginInfo *PluginInfo `yaml:"pluginInfo"` - Plugin *Plugin `yaml:"plugin"` + Prometheus *Prometheus `yaml:"prometheus"` Http *HttpConf `yaml:"http"` Logopts logger.LogOpts `yaml:"log"` } diff --git a/prometheus/server/main.go b/prometheus/server/main.go index 3b644324..4cb4c021 100644 --- a/prometheus/server/main.go +++ b/prometheus/server/main.go @@ -2,23 +2,31 @@ package main import ( "fmt" + "os" - "gitee.com/openeuler/PilotGo-plugins/sdk/plugin" - Router "openeuler.org/PilotGo/prometheus-plugin/router" + "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/plugin" + "openeuler.org/PilotGo/prometheus-plugin/router" ) func main() { fmt.Println("hello prometheus") - client := Router.DefaultClient(&plugin.PluginInfo{ - Name: "Prometheus", - Version: "Version", - Description: "Prometheus开源系统监视和警报工具包", - Author: "zhanghan", - Email: "zhanghan@kylinos.cn", - Url: "http://localhost:8090", - ReverseDest: "http://localhost:9090", - }) - - client.Serve(":8090") + config.Init() + + if err := logger.Init(&config.Config().Logopts); err != nil { + fmt.Printf("logger init failed, please check the config file: %s", err) + os.Exit(-1) + } + + server := router.InitRouter() + + client := client.DefaultClient(plugin.Init(config.Config().Prometheus)) + client.RegisterHandlers(server) + + if err := server.Run(config.Config().Http.Addr); err != nil { + logger.Fatal("failed to run server") + } } diff --git a/prometheus/server/plugin/plugin_manager.go b/prometheus/server/plugin/plugin_manager.go new file mode 100644 index 00000000..c28d90dd --- /dev/null +++ b/prometheus/server/plugin/plugin_manager.go @@ -0,0 +1,20 @@ +package plugin + +import ( + "gitee.com/openeuler/PilotGo-plugins/sdk/plugin/client" + "openeuler.org/PilotGo/prometheus-plugin/config" +) + +func Init(conf *config.Prometheus) *client.PluginInfo { + PluginInfo := client.PluginInfo{ + Name: "Prometheus", + Version: "0.0.1", + Description: "Prometheus开源系统监视和警报工具包", + Author: "zhanghan", + Email: "zhanghan@kylinos.cn", + Url: conf.URL, + ReverseDest: conf.ReverseDest, + } + + return &PluginInfo +} diff --git a/prometheus/server/router/router.go b/prometheus/server/router/router.go index 5d9fbc74..81daf90e 100644 --- a/prometheus/server/router/router.go +++ b/prometheus/server/router/router.go @@ -1,45 +1,59 @@ -package plugin +package router import ( "fmt" + "net/http" "net/http/httputil" "net/url" - "gitee.com/openeuler/PilotGo-plugins/sdk/plugin" + "gitee.com/openeuler/PilotGo-plugins/sdk/logger" "github.com/gin-gonic/gin" ) -func DefaultClient(desc *plugin.PluginInfo) *plugin.Client { - plugin.BaseInfo = desc - dest := desc.ReverseDest +func InitRouter() *gin.Engine { + gin.SetMode(gin.ReleaseMode) + router := gin.New() + router.Use(logger.LoggerDebug()) + router.Use(gin.Recovery()) - router := gin.Default() - mg := router.Group("plugin_manage/") - { - mg.GET("/info", plugin.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 &plugin.Client{ - Router: router, - } + registerAPIs(router) + return router } +func registerAPIs(router *gin.Engine) { + 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") diff --git a/sdk/logger/logger.go b/sdk/logger/logger.go index db5beb60..6fbb8c57 100644 --- a/sdk/logger/logger.go +++ b/sdk/logger/logger.go @@ -17,7 +17,9 @@ package logger import ( "errors" "os" + "time" + "github.com/gin-gonic/gin" rotatelogs "github.com/lestrrat-go/file-rotatelogs" "github.com/sirupsen/logrus" ) @@ -90,6 +92,43 @@ func Init(conf *LogOpts) error { return nil } +func LoggerDebug() gin.HandlerFunc { + return func(c *gin.Context) { + // 开始时间 + startTime := time.Now() + + // 处理请求 + c.Next() + + // 结束时间 + endTime := time.Now() + + // 执行时间 + latencyTime := endTime.Sub(startTime) + + // 请求方式 + reqMethod := c.Request.Method + + // 请求路由 + reqUri := c.Request.RequestURI + + // 状态码 + statusCode := c.Writer.Status() + + // 请求IP + clientIP := c.ClientIP() + + // 日志格式 + Debug("status_code:%d latency_time:%s client_ip:%s req_method:%s req_uri:%s", + statusCode, + latencyTime, + clientIP, + reqMethod, + reqUri, + ) + } +} + func Trace(format string, args ...interface{}) { logrus.Tracef(format, args...) } -- Gitee