Show response time, status code etc.

https://github.com/thoas/stats

Fixed #103

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu
2016-06-26 12:21:32 +08:00
parent 82cb2f5246
commit f65ccaaa8a
8 changed files with 206 additions and 3 deletions

View File

@@ -70,10 +70,12 @@ func routerEngine() *gin.Engine {
r.Use(gin.Recovery())
r.Use(VersionMiddleware())
r.Use(LogMiddleware())
r.Use(StatMiddleware())
r.GET(PushConf.API.StatGoURI, api.StatusHandler)
r.GET(PushConf.API.StatAppURI, appStatusHandler)
r.GET(PushConf.API.ConfigURI, configHandler)
r.GET(PushConf.API.SysStatURI, sysStatsHandler)
r.POST(PushConf.API.PushURI, pushHandler)
r.GET("/", rootHandler)

View File

@@ -202,3 +202,14 @@ func TestSuccessPushHandler(t *testing.T) {
assert.Equal(t, http.StatusOK, r.Code)
})
}
func TestSysStatsHandler(t *testing.T) {
initTest()
r := gofight.New()
r.GET("/sys/stats").
Run(routerEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code)
})
}

View File

@@ -5,9 +5,13 @@ import (
"github.com/appleboy/gorush/storage/memory"
"github.com/appleboy/gorush/storage/redis"
"github.com/gin-gonic/gin"
"github.com/thoas/stats"
"net/http"
)
// Stats provide response time, status code count, etc.
var Stats = stats.New()
// StatusApp is app status structure
type StatusApp struct {
QueueMax int `json:"queue_max"`
@@ -66,3 +70,16 @@ func appStatusHandler(c *gin.Context) {
c.JSON(http.StatusOK, result)
}
func sysStatsHandler(c *gin.Context) {
c.JSON(http.StatusOK, Stats.Data())
}
// StatMiddleware response time, status code count, etc.
func StatMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
beginning, recorder := Stats.Begin(c.Writer)
c.Next()
Stats.End(beginning, recorder)
}
}