...
|
...
|
@@ -2,6 +2,7 @@ package controllers |
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
"crypto/sha256"
|
...
|
...
|
@@ -19,12 +20,29 @@ import ( |
|
|
)
|
|
|
|
|
|
var(
|
|
|
//prometheus 监控endpoint
|
|
|
HTTPReqTotal *prometheus.CounterVec
|
|
|
auth s_auth.IAuthService = &s_auth.AuthService{}
|
|
|
)
|
|
|
|
|
|
type BaseController struct {
|
|
|
mybeego.BaseController
|
|
|
}
|
|
|
|
|
|
func init(){
|
|
|
// HistogramVec 是一组Histogram
|
|
|
HTTPReqTotal= prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
|
Name: "request_count_vec",//http_requests_total
|
|
|
Help: "total number of http requests made.",
|
|
|
}, []string{"method", "path"})
|
|
|
// 这里的"method"、"path"、"status" 都是label , "status"
|
|
|
prometheus.MustRegister(
|
|
|
HTTPReqTotal,
|
|
|
)
|
|
|
//if err:=prometheus.Register(HTTPReqTotal);err!=nil{
|
|
|
// log.Error(err)
|
|
|
//}
|
|
|
}
|
|
|
var DefaultController *BaseController = &BaseController{}
|
|
|
//Valid valid struct
|
|
|
func (this *BaseController)Valid(obj interface{})(result bool ,msg *mybeego.Message){
|
...
|
...
|
@@ -80,9 +98,14 @@ func FilterComm(ctx *context.Context){ |
|
|
//if strings.HasSuffix(ctx.Request.RequestURI,"login"){
|
|
|
// return
|
|
|
//}
|
|
|
|
|
|
//统计
|
|
|
MetricCounter(ctx)
|
|
|
|
|
|
if beego.BConfig.RunMode!="prod"{
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//1.检查签名
|
|
|
if !CheckSign(ctx){
|
|
|
return
|
...
|
...
|
@@ -97,6 +120,15 @@ func FilterComm(ctx *context.Context){ |
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
func MetricCounter(ctx *context.Context){
|
|
|
// 请求数加1
|
|
|
HTTPReqTotal.With(prometheus.Labels{
|
|
|
"method": ctx.Request.Method,
|
|
|
"path": ctx.Request.RequestURI,
|
|
|
//"status": strconv.Itoa(c.Writer.Status()),
|
|
|
}).Inc()
|
|
|
}
|
|
|
//检查签名
|
|
|
func CheckSign(ctx *context.Context)(result bool){
|
|
|
var(
|
...
|
...
|
|