diff --git a/prometheus/server/config.yml.templete b/prometheus/server/config.yml.templete index 53eee6f95d82fda22fc863b48fa255ad22dc21a5..b2c847a4b73bb5222e01129fac4c63655e064607 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 82be1d6e619331a0007e0c73ce4bc557fed2133d..2fefee6144499850a19187e6f40f35540ab7beb5 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 3b6443240485be77fbed490b85a06f2fb76041af..4cb4c0215726b2511227099597f74c460f1f3763 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 0000000000000000000000000000000000000000..c28d90dd8b0dc2b5d61ff4734e5fb3ae75962eb4 --- /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 5d9fbc740dee26db3a380c85e83c3b1893d4b685..81daf90e30265a4ba783aafaeb3c5e63d0071539 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 db5beb601bbe123f2f13e59c980613351f329623..6fbb8c57ee818c1dec6db850d3d0cfe091a3be28 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...) }