Merge pull request #4454 from flycash/mm
Merge master branch and resolve conflicts
This commit is contained in:
commit
7d3f4aa4f7
@ -12,3 +12,5 @@
|
|||||||
- Feature issue #4402 finish router get example. [4416](https://github.com/beego/beego/pull/4416)
|
- Feature issue #4402 finish router get example. [4416](https://github.com/beego/beego/pull/4416)
|
||||||
- Implement context.Context support and deprecate `QueryM2MWithCtx` and `QueryTableWithCtx` [4424](https://github.com/beego/beego/pull/4424)
|
- Implement context.Context support and deprecate `QueryM2MWithCtx` and `QueryTableWithCtx` [4424](https://github.com/beego/beego/pull/4424)
|
||||||
- Finish timeout option for tasks #4441 [4441](https://github.com/beego/beego/pull/4441)
|
- Finish timeout option for tasks #4441 [4441](https://github.com/beego/beego/pull/4441)
|
||||||
|
- Fix 4444: panic when 404 not found. [4446](https://github.com/beego/beego/pull/4446)
|
||||||
|
- Fix 4435: fix panic when controller dir not found. [4452](https://github.com/beego/beego/pull/4452)
|
||||||
@ -18,6 +18,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
@ -26,24 +27,30 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type FilterChainBuilder struct {
|
type FilterChainBuilder struct {
|
||||||
summaryVec prometheus.ObserverVec
|
|
||||||
AppName string
|
AppName string
|
||||||
ServerName string
|
ServerName string
|
||||||
RunMode string
|
RunMode string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var summaryVec prometheus.ObserverVec
|
||||||
|
var initSummaryVec sync.Once
|
||||||
|
|
||||||
func (builder *FilterChainBuilder) FilterChain(next httplib.Filter) httplib.Filter {
|
func (builder *FilterChainBuilder) FilterChain(next httplib.Filter) httplib.Filter {
|
||||||
|
|
||||||
builder.summaryVec = prometheus.NewSummaryVec(prometheus.SummaryOpts{
|
initSummaryVec.Do(func() {
|
||||||
Name: "beego",
|
summaryVec = prometheus.NewSummaryVec(prometheus.SummaryOpts{
|
||||||
Subsystem: "remote_http_request",
|
Name: "beego",
|
||||||
ConstLabels: map[string]string{
|
Subsystem: "remote_http_request",
|
||||||
"server": builder.ServerName,
|
ConstLabels: map[string]string{
|
||||||
"env": builder.RunMode,
|
"server": builder.ServerName,
|
||||||
"appname": builder.AppName,
|
"env": builder.RunMode,
|
||||||
},
|
"appname": builder.AppName,
|
||||||
Help: "The statics info for remote http requests",
|
},
|
||||||
}, []string{"proto", "scheme", "method", "host", "path", "status", "isError"})
|
Help: "The statics info for remote http requests",
|
||||||
|
}, []string{"proto", "scheme", "method", "host", "path", "status", "isError"})
|
||||||
|
|
||||||
|
prometheus.MustRegister(summaryVec)
|
||||||
|
})
|
||||||
|
|
||||||
return func(ctx context.Context, req *httplib.BeegoHTTPRequest) (*http.Response, error) {
|
return func(ctx context.Context, req *httplib.BeegoHTTPRequest) (*http.Response, error) {
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
@ -72,6 +79,6 @@ func (builder *FilterChainBuilder) report(startTime time.Time, endTime time.Time
|
|||||||
|
|
||||||
dur := int(endTime.Sub(startTime) / time.Millisecond)
|
dur := int(endTime.Sub(startTime) / time.Millisecond)
|
||||||
|
|
||||||
builder.summaryVec.WithLabelValues(proto, scheme, method, host, path,
|
summaryVec.WithLabelValues(proto, scheme, method, host, path,
|
||||||
strconv.Itoa(status), strconv.FormatBool(err != nil)).Observe(float64(dur))
|
strconv.Itoa(status), strconv.FormatBool(err != nil)).Observe(float64(dur))
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,6 +18,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
@ -33,24 +34,29 @@ import (
|
|||||||
// if we want to records the metrics of QuerySetter
|
// if we want to records the metrics of QuerySetter
|
||||||
// actually we only records metrics of invoking "QueryTable"
|
// actually we only records metrics of invoking "QueryTable"
|
||||||
type FilterChainBuilder struct {
|
type FilterChainBuilder struct {
|
||||||
summaryVec prometheus.ObserverVec
|
|
||||||
AppName string
|
AppName string
|
||||||
ServerName string
|
ServerName string
|
||||||
RunMode string
|
RunMode string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var summaryVec prometheus.ObserverVec
|
||||||
|
var initSummaryVec sync.Once
|
||||||
|
|
||||||
func (builder *FilterChainBuilder) FilterChain(next orm.Filter) orm.Filter {
|
func (builder *FilterChainBuilder) FilterChain(next orm.Filter) orm.Filter {
|
||||||
|
|
||||||
builder.summaryVec = prometheus.NewSummaryVec(prometheus.SummaryOpts{
|
initSummaryVec.Do(func() {
|
||||||
Name: "beego",
|
summaryVec = prometheus.NewSummaryVec(prometheus.SummaryOpts{
|
||||||
Subsystem: "orm_operation",
|
Name: "beego",
|
||||||
ConstLabels: map[string]string{
|
Subsystem: "orm_operation",
|
||||||
"server": builder.ServerName,
|
ConstLabels: map[string]string{
|
||||||
"env": builder.RunMode,
|
"server": builder.ServerName,
|
||||||
"appname": builder.AppName,
|
"env": builder.RunMode,
|
||||||
},
|
"appname": builder.AppName,
|
||||||
Help: "The statics info for orm operation",
|
},
|
||||||
}, []string{"method", "name", "insideTx", "txName"})
|
Help: "The statics info for orm operation",
|
||||||
|
}, []string{"method", "name", "insideTx", "txName"})
|
||||||
|
prometheus.MustRegister(summaryVec)
|
||||||
|
})
|
||||||
|
|
||||||
return func(ctx context.Context, inv *orm.Invocation) []interface{} {
|
return func(ctx context.Context, inv *orm.Invocation) []interface{} {
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
@ -74,12 +80,12 @@ func (builder *FilterChainBuilder) report(ctx context.Context, inv *orm.Invocati
|
|||||||
builder.reportTxn(ctx, inv)
|
builder.reportTxn(ctx, inv)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
builder.summaryVec.WithLabelValues(inv.Method, inv.GetTableName(),
|
summaryVec.WithLabelValues(inv.Method, inv.GetTableName(),
|
||||||
strconv.FormatBool(inv.InsideTx), inv.TxName).Observe(float64(dur))
|
strconv.FormatBool(inv.InsideTx), inv.TxName).Observe(float64(dur))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (builder *FilterChainBuilder) reportTxn(ctx context.Context, inv *orm.Invocation) {
|
func (builder *FilterChainBuilder) reportTxn(ctx context.Context, inv *orm.Invocation) {
|
||||||
dur := time.Now().Sub(inv.TxStartTime) / time.Millisecond
|
dur := time.Now().Sub(inv.TxStartTime) / time.Millisecond
|
||||||
builder.summaryVec.WithLabelValues(inv.Method, inv.TxName,
|
summaryVec.WithLabelValues(inv.Method, inv.TxName,
|
||||||
strconv.FormatBool(inv.InsideTx), inv.TxName).Observe(float64(dur))
|
strconv.FormatBool(inv.InsideTx), inv.TxName).Observe(float64(dur))
|
||||||
}
|
}
|
||||||
|
|||||||
@ -32,7 +32,7 @@ func TestFilterChainBuilder_FilterChain1(t *testing.T) {
|
|||||||
builder := &FilterChainBuilder{}
|
builder := &FilterChainBuilder{}
|
||||||
filter := builder.FilterChain(next)
|
filter := builder.FilterChain(next)
|
||||||
|
|
||||||
assert.NotNil(t, builder.summaryVec)
|
assert.NotNil(t, summaryVec)
|
||||||
assert.NotNil(t, filter)
|
assert.NotNil(t, filter)
|
||||||
|
|
||||||
inv := &orm.Invocation{}
|
inv := &orm.Invocation{}
|
||||||
|
|||||||
@ -108,8 +108,11 @@ func registerAdmin() error {
|
|||||||
c := &adminController{
|
c := &adminController{
|
||||||
servers: make([]*HttpServer, 0, 2),
|
servers: make([]*HttpServer, 0, 2),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// copy config to avoid conflict
|
||||||
|
adminCfg := *BConfig
|
||||||
beeAdminApp = &adminApp{
|
beeAdminApp = &adminApp{
|
||||||
HttpServer: NewHttpServerWithCfg(BConfig),
|
HttpServer: NewHttpServerWithCfg(&adminCfg),
|
||||||
}
|
}
|
||||||
// keep in mind that all data should be html escaped to avoid XSS attack
|
// keep in mind that all data should be html escaped to avoid XSS attack
|
||||||
beeAdminApp.Router("/", c, "get:AdminIndex")
|
beeAdminApp.Router("/", c, "get:AdminIndex")
|
||||||
|
|||||||
@ -17,23 +17,49 @@ package prometheus
|
|||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
|
||||||
"github.com/beego/beego/v2"
|
"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"
|
||||||
"github.com/beego/beego/v2/server/web/context"
|
"github.com/beego/beego/v2/server/web/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const unknownRouterPattern = "UnknownRouterPattern"
|
||||||
|
|
||||||
// FilterChainBuilder is an extension point,
|
// FilterChainBuilder is an extension point,
|
||||||
// when we want to support some configuration,
|
// when we want to support some configuration,
|
||||||
// please use this structure
|
// please use this structure
|
||||||
type FilterChainBuilder struct {
|
type FilterChainBuilder struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var summaryVec prometheus.ObserverVec
|
||||||
|
var initSummaryVec sync.Once
|
||||||
|
|
||||||
// FilterChain returns a FilterFunc. The filter will records some metrics
|
// FilterChain returns a FilterFunc. The filter will records some metrics
|
||||||
func (builder *FilterChainBuilder) FilterChain(next web.FilterFunc) web.FilterFunc {
|
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{
|
summaryVec := prometheus.NewSummaryVec(prometheus.SummaryOpts{
|
||||||
Name: "beego",
|
Name: "beego",
|
||||||
Subsystem: "http_request",
|
Subsystem: "http_request",
|
||||||
@ -44,17 +70,7 @@ func (builder *FilterChainBuilder) FilterChain(next web.FilterFunc) web.FilterFu
|
|||||||
},
|
},
|
||||||
Help: "The statics info for http request",
|
Help: "The statics info for http request",
|
||||||
}, []string{"pattern", "method", "status"})
|
}, []string{"pattern", "method", "status"})
|
||||||
|
return summaryVec
|
||||||
prometheus.MustRegister(summaryVec)
|
|
||||||
|
|
||||||
registerBuildInfo()
|
|
||||||
|
|
||||||
return func(ctx *context.Context) {
|
|
||||||
startTime := time.Now()
|
|
||||||
next(ctx)
|
|
||||||
endTime := time.Now()
|
|
||||||
go report(endTime.Sub(startTime), ctx, summaryVec)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func registerBuildInfo() {
|
func registerBuildInfo() {
|
||||||
@ -75,13 +91,17 @@ func registerBuildInfo() {
|
|||||||
},
|
},
|
||||||
}, []string{})
|
}, []string{})
|
||||||
|
|
||||||
prometheus.MustRegister(buildInfo)
|
_ = prometheus.Register(buildInfo)
|
||||||
buildInfo.WithLabelValues().Set(1)
|
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
|
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
|
ms := dur / time.Millisecond
|
||||||
vec.WithLabelValues(ptn, ctx.Input.Method(), strconv.Itoa(status)).Observe(float64(ms))
|
vec.WithLabelValues(ptn, ctx.Input.Method(), strconv.Itoa(status)).Observe(float64(ms))
|
||||||
}
|
}
|
||||||
|
|||||||
@ -40,3 +40,17 @@ func TestFilterChain(t *testing.T) {
|
|||||||
assert.True(t, ctx.Input.GetData("invocation").(bool))
|
assert.True(t, ctx.Input.GetData("invocation").(bool))
|
||||||
time.Sleep(1 * time.Second)
|
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())
|
||||||
|
}
|
||||||
@ -6,6 +6,8 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/coreos/etcd/pkg/fileutil"
|
||||||
|
|
||||||
"github.com/beego/beego/v2/core/logs"
|
"github.com/beego/beego/v2/core/logs"
|
||||||
"github.com/beego/beego/v2/server/web/context"
|
"github.com/beego/beego/v2/server/web/context"
|
||||||
"github.com/beego/beego/v2/server/web/session"
|
"github.com/beego/beego/v2/server/web/session"
|
||||||
@ -99,7 +101,12 @@ func registerGzip() error {
|
|||||||
|
|
||||||
func registerCommentRouter() error {
|
func registerCommentRouter() error {
|
||||||
if BConfig.RunMode == DEV {
|
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
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user