作者 yangfu

add prometheus

@@ -2,6 +2,7 @@ package controllers @@ -2,6 +2,7 @@ package controllers
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
  5 + "github.com/prometheus/client_golang/prometheus"
5 "strconv" 6 "strconv"
6 "strings" 7 "strings"
7 "crypto/sha256" 8 "crypto/sha256"
@@ -19,12 +20,29 @@ import ( @@ -19,12 +20,29 @@ import (
19 ) 20 )
20 21
21 var( 22 var(
  23 + //prometheus 监控endpoint
  24 + HTTPReqTotal *prometheus.CounterVec
22 auth s_auth.IAuthService = &s_auth.AuthService{} 25 auth s_auth.IAuthService = &s_auth.AuthService{}
23 ) 26 )
24 27
25 type BaseController struct { 28 type BaseController struct {
26 mybeego.BaseController 29 mybeego.BaseController
27 } 30 }
  31 +
  32 +func init(){
  33 + // HistogramVec 是一组Histogram
  34 + HTTPReqTotal= prometheus.NewCounterVec(prometheus.CounterOpts{
  35 + Name: "request_count_vec",//http_requests_total
  36 + Help: "total number of http requests made.",
  37 + }, []string{"method", "path"})
  38 + // 这里的"method"、"path"、"status" 都是label , "status"
  39 + prometheus.MustRegister(
  40 + HTTPReqTotal,
  41 + )
  42 + //if err:=prometheus.Register(HTTPReqTotal);err!=nil{
  43 + // log.Error(err)
  44 + //}
  45 +}
28 var DefaultController *BaseController = &BaseController{} 46 var DefaultController *BaseController = &BaseController{}
29 //Valid valid struct 47 //Valid valid struct
30 func (this *BaseController)Valid(obj interface{})(result bool ,msg *mybeego.Message){ 48 func (this *BaseController)Valid(obj interface{})(result bool ,msg *mybeego.Message){
@@ -80,9 +98,14 @@ func FilterComm(ctx *context.Context){ @@ -80,9 +98,14 @@ func FilterComm(ctx *context.Context){
80 //if strings.HasSuffix(ctx.Request.RequestURI,"login"){ 98 //if strings.HasSuffix(ctx.Request.RequestURI,"login"){
81 // return 99 // return
82 //} 100 //}
  101 +
  102 + //统计
  103 + MetricCounter(ctx)
  104 +
83 if beego.BConfig.RunMode!="prod"{ 105 if beego.BConfig.RunMode!="prod"{
84 return 106 return
85 } 107 }
  108 +
86 //1.检查签名 109 //1.检查签名
87 if !CheckSign(ctx){ 110 if !CheckSign(ctx){
88 return 111 return
@@ -97,6 +120,15 @@ func FilterComm(ctx *context.Context){ @@ -97,6 +120,15 @@ func FilterComm(ctx *context.Context){
97 } 120 }
98 return 121 return
99 } 122 }
  123 +
  124 +func MetricCounter(ctx *context.Context){
  125 + // 请求数加1
  126 + HTTPReqTotal.With(prometheus.Labels{
  127 + "method": ctx.Request.Method,
  128 + "path": ctx.Request.RequestURI,
  129 + //"status": strconv.Itoa(c.Writer.Status()),
  130 + }).Inc()
  131 +}
100 //检查签名 132 //检查签名
101 func CheckSign(ctx *context.Context)(result bool){ 133 func CheckSign(ctx *context.Context)(result bool){
102 var( 134 var(
@@ -37,6 +37,7 @@ func main() { @@ -37,6 +37,7 @@ func main() {
37 protocol.InitMessageCode() 37 protocol.InitMessageCode()
38 log.Info("app on start!") 38 log.Info("app on start!")
39 log.Info("Beego Run Mode:",beego.BConfig.RunMode) 39 log.Info("Beego Run Mode:",beego.BConfig.RunMode)
  40 +
40 beego.Run() 41 beego.Run()
41 } 42 }
42 43
@@ -45,17 +45,17 @@ func Image(request *protocol.ImageRequest)(rsp *protocol.ImageResponse,err error @@ -45,17 +45,17 @@ func Image(request *protocol.ImageRequest)(rsp *protocol.ImageResponse,err error
45 } 45 }
46 filename =fmt.Sprintf("%v_%v%v",time.Now().Unix(),common.RandomString(32),subfix) 46 filename =fmt.Sprintf("%v_%v%v",time.Now().Unix(),common.RandomString(32),subfix)
47 src,err=f.Open() 47 src,err=f.Open()
48 - defer src.Close()  
49 if err!=nil{ 48 if err!=nil{
50 log.Error(err) 49 log.Error(err)
51 return 50 return
52 } 51 }
  52 + defer src.Close()
53 dst,err =os.OpenFile(filepath.Join(sourcePath,filename), os.O_RDWR | os.O_CREATE |os.O_TRUNC,0777) //file/ab/ 静态文件目录 53 dst,err =os.OpenFile(filepath.Join(sourcePath,filename), os.O_RDWR | os.O_CREATE |os.O_TRUNC,0777) //file/ab/ 静态文件目录
54 - defer dst.Close()  
55 if err!=nil{ 54 if err!=nil{
56 log.Error(err) 55 log.Error(err)
57 return 56 return
58 } 57 }
  58 + defer dst.Close()
59 if _,err =io.Copy(dst,src);err!=nil{ 59 if _,err =io.Copy(dst,src);err!=nil{
60 log.Error(err) 60 log.Error(err)
61 return 61 return