作者 yangfu

add prometheus

... ... @@ -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(
... ...
... ... @@ -37,6 +37,7 @@ func main() {
protocol.InitMessageCode()
log.Info("app on start!")
log.Info("Beego Run Mode:",beego.BConfig.RunMode)
beego.Run()
}
... ...
... ... @@ -45,17 +45,17 @@ func Image(request *protocol.ImageRequest)(rsp *protocol.ImageResponse,err error
}
filename =fmt.Sprintf("%v_%v%v",time.Now().Unix(),common.RandomString(32),subfix)
src,err=f.Open()
defer src.Close()
if err!=nil{
log.Error(err)
return
}
defer src.Close()
dst,err =os.OpenFile(filepath.Join(sourcePath,filename), os.O_RDWR | os.O_CREATE |os.O_TRUNC,0777) //file/ab/ 静态文件目录
defer dst.Close()
if err!=nil{
log.Error(err)
return
}
defer dst.Close()
if _,err =io.Copy(dst,src);err!=nil{
log.Error(err)
return
... ...