性能指标、健康检查、default log配置生效

This commit is contained in:
linwenxiang
2020-10-21 18:03:39 +08:00
parent e53cc4a9b5
commit e1965be2ab
3 changed files with 48 additions and 16 deletions
+31
View File
@@ -0,0 +1,31 @@
package router
import (
"github.com/gin-gonic/gin"
"github.com/go-admin-team/go-admin-core/transfer"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go-admin/common/global"
"go-admin/common/log"
"net/http"
)
func Monitor() {
var r *gin.Engine
h := global.Cfg.GetEngine()
if h == nil {
h = gin.New()
global.Cfg.SetEngine(h)
}
switch h.(type) {
case *gin.Engine:
r = h.(*gin.Engine)
default:
log.Fatal("not support other engine")
}
//开发环境启动监控指标
r.GET("/metrics", transfer.Handler(promhttp.Handler()))
//健康检查
r.GET("/health", func(c *gin.Context) {
c.Status(http.StatusOK)
})
}
+6 -14
View File
@@ -10,8 +10,6 @@ import (
"time" "time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/go-admin-team/go-admin-core/transfer"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
@@ -76,18 +74,12 @@ func run() error {
} }
engine := global.Cfg.GetEngine() engine := global.Cfg.GetEngine()
if engine == nil { if engine == nil {
if mode == "dev" { engine = gin.New()
r := gin.New() }
//开发环境启动监控指标
r.GET("/metrics", transfer.Handler(promhttp.Handler())) if mode == "dev" {
//健康检查 //监控
r.GET("/health", func(c *gin.Context) { AppRouters = append(AppRouters, router.Monitor)
c.Status(http.StatusOK)
})
engine = r
} else {
engine = gin.New()
}
} }
for _, f := range AppRouters { for _, f := range AppRouters {
+11 -2
View File
@@ -3,6 +3,7 @@ package logger
import ( import (
"context" "context"
"fmt" "fmt"
"log"
"os" "os"
"runtime" "runtime"
"sort" "sort"
@@ -114,7 +115,11 @@ func (l *defaultLogger) Log(level Level, v ...interface{}) {
} }
t := rec.Timestamp.Format("2006-01-02 15:04:05") t := rec.Timestamp.Format("2006-01-02 15:04:05")
fmt.Printf("%s %s %v\n", t, metadata, rec.Message) _, err := l.opts.Out.Write([]byte(fmt.Sprintf("%s %s %v\n", t, metadata, rec.Message)))
if err != nil {
log.Printf("log [Log] write error: %s \n", err.Error())
}
//fmt.Printf("%s %s %v\n", t, metadata, rec.Message)
} }
func (l *defaultLogger) Logf(level Level, format string, v ...interface{}) { func (l *defaultLogger) Logf(level Level, format string, v ...interface{}) {
@@ -153,7 +158,11 @@ func (l *defaultLogger) Logf(level Level, format string, v ...interface{}) {
} }
t := rec.Timestamp.Format("2006-01-02 15:04:05") t := rec.Timestamp.Format("2006-01-02 15:04:05")
fmt.Printf("%s %s %v\n", t, metadata, rec.Message) //fmt.Printf("%s %s %v\n", t, metadata, rec.Message)
_, err := l.opts.Out.Write([]byte(fmt.Sprintf("%s %s %v\n", t, metadata, rec.Message)))
if err != nil {
log.Printf("log [Logf] write error: %s \n", err.Error())
}
} }
func (l *defaultLogger) Options() Options { func (l *defaultLogger) Options() Options {