Merge master

This commit is contained in:
Ming Deng
2021-01-23 21:39:40 +08:00
8 changed files with 102 additions and 43 deletions

View File

@@ -108,8 +108,11 @@ func registerAdmin() error {
c := &adminController{
servers: make([]*HttpServer, 0, 2),
}
// copy config to avoid conflict
adminCfg := *BConfig
beeAdminApp = &adminApp{
HttpServer: NewHttpServerWithCfg(BConfig),
HttpServer: NewHttpServerWithCfg(&adminCfg),
}
// keep in mind that all data should be html escaped to avoid XSS attack
beeAdminApp.Router("/", c, "get:AdminIndex")

View File

@@ -17,23 +17,49 @@ package prometheus
import (
"strconv"
"strings"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/beego/beego/v2"
"github.com/beego/beego/v2/core/logs"
"github.com/beego/beego/v2/server/web"
"github.com/beego/beego/v2/server/web/context"
)
const unknownRouterPattern = "UnknownRouterPattern"
// FilterChainBuilder is an extension point,
// when we want to support some configuration,
// please use this structure
type FilterChainBuilder struct {
}
var summaryVec prometheus.ObserverVec
var initSummaryVec sync.Once
// FilterChain returns a FilterFunc. The filter will records some metrics
func (builder *FilterChainBuilder) FilterChain(next web.FilterFunc) web.FilterFunc {
initSummaryVec.Do(func() {
summaryVec = builder.buildVec()
err := prometheus.Register(summaryVec)
if _, ok := err.(*prometheus.AlreadyRegisteredError); err != nil && !ok {
logs.Error("web module register prometheus vector failed, %+v", err)
}
registerBuildInfo()
})
return func(ctx *context.Context) {
startTime := time.Now()
next(ctx)
endTime := time.Now()
go report(endTime.Sub(startTime), ctx, summaryVec)
}
}
func (builder *FilterChainBuilder) buildVec() *prometheus.SummaryVec {
summaryVec := prometheus.NewSummaryVec(prometheus.SummaryOpts{
Name: "beego",
Subsystem: "http_request",
@@ -44,17 +70,7 @@ func (builder *FilterChainBuilder) FilterChain(next web.FilterFunc) web.FilterFu
},
Help: "The statics info for http request",
}, []string{"pattern", "method", "status"})
prometheus.MustRegister(summaryVec)
registerBuildInfo()
return func(ctx *context.Context) {
startTime := time.Now()
next(ctx)
endTime := time.Now()
go report(endTime.Sub(startTime), ctx, summaryVec)
}
return summaryVec
}
func registerBuildInfo() {
@@ -75,13 +91,17 @@ func registerBuildInfo() {
},
}, []string{})
prometheus.MustRegister(buildInfo)
_ = prometheus.Register(buildInfo)
buildInfo.WithLabelValues().Set(1)
}
func report(dur time.Duration, ctx *context.Context, vec *prometheus.SummaryVec) {
func report(dur time.Duration, ctx *context.Context, vec prometheus.ObserverVec) {
status := ctx.Output.Status
ptn := ctx.Input.GetData("RouterPattern").(string)
ptnItf := ctx.Input.GetData("RouterPattern")
ptn := unknownRouterPattern
if ptnItf != nil {
ptn = ptnItf.(string)
}
ms := dur / time.Millisecond
vec.WithLabelValues(ptn, ctx.Input.Method(), strconv.Itoa(status)).Observe(float64(ms))
}

View File

@@ -40,3 +40,17 @@ func TestFilterChain(t *testing.T) {
assert.True(t, ctx.Input.GetData("invocation").(bool))
time.Sleep(1 * time.Second)
}
func TestFilterChainBuilder_report(t *testing.T) {
ctx := context.NewContext()
r, _ := http.NewRequest("GET", "/prometheus/user", nil)
w := httptest.NewRecorder()
ctx.Reset(w, r)
fb := &FilterChainBuilder{}
// without router info
report(time.Second, ctx, fb.buildVec())
ctx.Input.SetData("RouterPattern", "my-route")
report(time.Second, ctx, fb.buildVec())
}

View File

@@ -6,6 +6,8 @@ import (
"net/http"
"path/filepath"
"github.com/coreos/etcd/pkg/fileutil"
"github.com/beego/beego/v2/core/logs"
"github.com/beego/beego/v2/server/web/context"
"github.com/beego/beego/v2/server/web/session"
@@ -99,7 +101,12 @@ func registerGzip() error {
func registerCommentRouter() error {
if BConfig.RunMode == DEV {
if err := parserPkg(filepath.Join(WorkPath, BConfig.WebConfig.CommentRouterPath)); err != nil {
ctrlDir := filepath.Join(WorkPath, BConfig.WebConfig.CommentRouterPath)
if !fileutil.Exist(ctrlDir) {
logs.Warn("controller package not found, won't generate router: ", ctrlDir)
return nil
}
if err := parserPkg(ctrlDir); err != nil {
return err
}
}