|
@@ -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(
|