1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 00:40:56 +00:00

seperate orm alone

This commit is contained in:
Anker Jam 2020-10-04 22:16:19 +08:00
parent 325a0821c1
commit c5d43e87fe
2 changed files with 16 additions and 19 deletions

View File

@ -23,7 +23,6 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/astaxie/beego/pkg/client/orm"
"github.com/astaxie/beego/pkg/server/web"
)
// FilterChainBuilder is an extension point,
@ -35,27 +34,24 @@ import (
// actually we only records metrics of invoking "QueryTable" and "QueryTableWithCtx"
type FilterChainBuilder struct {
summaryVec prometheus.ObserverVec
AppName string
ServerName string
RunMode string
}
func NewFilterChainBuilder() *FilterChainBuilder {
summaryVec := prometheus.NewSummaryVec(prometheus.SummaryOpts{
func (builder *FilterChainBuilder) FilterChain(next orm.Filter) orm.Filter {
builder.summaryVec = prometheus.NewSummaryVec(prometheus.SummaryOpts{
Name: "beego",
Subsystem: "orm_operation",
ConstLabels: map[string]string{
"server": web.BConfig.ServerName,
"env": web.BConfig.RunMode,
"appname": web.BConfig.AppName,
"server": builder.ServerName,
"env": builder.RunMode,
"appname": builder.AppName,
},
Help: "The statics info for orm operation",
}, []string{"method", "name", "duration", "insideTx", "txName"})
prometheus.MustRegister(summaryVec)
return &FilterChainBuilder{
summaryVec: summaryVec,
}
}
func (builder *FilterChainBuilder) FilterChain(next orm.Filter) orm.Filter {
return func(ctx context.Context, inv *orm.Invocation) []interface{} {
startTime := time.Now()
res := next(ctx, inv)

View File

@ -24,14 +24,15 @@ import (
"github.com/astaxie/beego/pkg/client/orm"
)
func TestFilterChainBuilder_FilterChain(t *testing.T) {
builder := NewFilterChainBuilder()
assert.NotNil(t, builder.summaryVec)
filter := builder.FilterChain(func(ctx context.Context, inv *orm.Invocation) []interface{} {
func TestFilterChainBuilder_FilterChain1(t *testing.T) {
next := func(ctx context.Context, inv *orm.Invocation) []interface{} {
inv.Method = "coming"
return []interface{}{}
})
}
builder := &FilterChainBuilder{}
filter := builder.FilterChain(next)
assert.NotNil(t, builder.summaryVec)
assert.NotNil(t, filter)
inv := &orm.Invocation{}