Remove duration from prometheus label

This commit is contained in:
Ming Deng 2020-12-26 21:11:04 +08:00
parent b0406f107a
commit 73d81bafd9
8 changed files with 18 additions and 12 deletions

View File

@ -1,4 +1,5 @@
# developing # developing
- Remove `duration` from prometheus labels. [4391](https://github.com/beego/beego/pull/4391)
- Fix `unknown escape sequence` in generated code. [4385](https://github.com/beego/beego/pull/4385) - Fix `unknown escape sequence` in generated code. [4385](https://github.com/beego/beego/pull/4385)
- Using fixed name `commentRouter.go` as generated file name. [4385](https://github.com/beego/beego/pull/4385) - Using fixed name `commentRouter.go` as generated file name. [4385](https://github.com/beego/beego/pull/4385)
- Fix 4383: ORM Adapter produces panic when using orm.RegisterModelWithPrefix. [4386](https://github.com/beego/beego/pull/4386) - Fix 4383: ORM Adapter produces panic when using orm.RegisterModelWithPrefix. [4386](https://github.com/beego/beego/pull/4386)

View File

@ -38,7 +38,7 @@ func PrometheusMiddleWare(next http.Handler) http.Handler {
"appname": web.BConfig.AppName, "appname": web.BConfig.AppName,
}, },
Help: "The statics info for http request", Help: "The statics info for http request",
}, []string{"pattern", "method", "status", "duration"}) }, []string{"pattern", "method", "status"})
prometheus.MustRegister(summaryVec) prometheus.MustRegister(summaryVec)
@ -96,5 +96,5 @@ func report(dur time.Duration, writer http.ResponseWriter, q *http.Request, vec
logs.Warn("we can not find the router info for this request, so request will be recorded as UNKNOWN: " + q.URL.String()) logs.Warn("we can not find the router info for this request, so request will be recorded as UNKNOWN: " + q.URL.String())
} }
ms := dur / time.Millisecond ms := dur / time.Millisecond
vec.WithLabelValues(ptn, q.Method, strconv.Itoa(status), strconv.Itoa(int(ms))).Observe(float64(ms)) vec.WithLabelValues(ptn, q.Method, strconv.Itoa(status)).Observe(float64(ms))
} }

View File

@ -35,7 +35,7 @@ func TestPrometheusMiddleWare(t *testing.T) {
}, },
Method: "POST", Method: "POST",
} }
vec := prometheus.NewSummaryVec(prometheus.SummaryOpts{}, []string{"pattern", "method", "status", "duration"}) vec := prometheus.NewSummaryVec(prometheus.SummaryOpts{}, []string{"pattern", "method", "status"})
report(time.Second, writer, request, vec) report(time.Second, writer, request, vec)
middleware.ServeHTTP(writer, request) middleware.ServeHTTP(writer, request)

View File

@ -43,7 +43,7 @@ func (builder *FilterChainBuilder) FilterChain(next httplib.Filter) httplib.Filt
"appname": builder.AppName, "appname": builder.AppName,
}, },
Help: "The statics info for remote http requests", Help: "The statics info for remote http requests",
}, []string{"proto", "scheme", "method", "host", "path", "status", "duration", "isError"}) }, []string{"proto", "scheme", "method", "host", "path", "status", "isError"})
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()
@ -73,5 +73,5 @@ 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, builder.summaryVec.WithLabelValues(proto, scheme, method, host, path,
strconv.Itoa(status), strconv.Itoa(dur), strconv.FormatBool(err == nil)) strconv.Itoa(status), strconv.FormatBool(err != nil)).Observe(float64(dur))
} }

View File

@ -50,7 +50,7 @@ func (builder *FilterChainBuilder) FilterChain(next orm.Filter) orm.Filter {
"appname": builder.AppName, "appname": builder.AppName,
}, },
Help: "The statics info for orm operation", Help: "The statics info for orm operation",
}, []string{"method", "name", "duration", "insideTx", "txName"}) }, []string{"method", "name", "insideTx", "txName"})
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 +74,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(), strconv.Itoa(int(dur)), builder.summaryVec.WithLabelValues(inv.Method, inv.GetTableName(),
strconv.FormatBool(inv.InsideTx), inv.TxName) 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, strconv.Itoa(int(dur)), builder.summaryVec.WithLabelValues(inv.Method, inv.TxName,
strconv.FormatBool(inv.InsideTx), inv.TxName) strconv.FormatBool(inv.InsideTx), inv.TxName).Observe(float64(dur))
} }

View File

@ -43,7 +43,7 @@ func (builder *FilterChainBuilder) FilterChain(next web.FilterFunc) web.FilterFu
"appname": web.BConfig.AppName, "appname": web.BConfig.AppName,
}, },
Help: "The statics info for http request", Help: "The statics info for http request",
}, []string{"pattern", "method", "status", "duration"}) }, []string{"pattern", "method", "status"})
prometheus.MustRegister(summaryVec) prometheus.MustRegister(summaryVec)
@ -83,5 +83,5 @@ func report(dur time.Duration, ctx *context.Context, vec *prometheus.SummaryVec)
status := ctx.Output.Status status := ctx.Output.Status
ptn := ctx.Input.GetData("RouterPattern").(string) ptn := ctx.Input.GetData("RouterPattern").(string)
ms := dur / time.Millisecond ms := dur / time.Millisecond
vec.WithLabelValues(ptn, ctx.Input.Method(), strconv.Itoa(status), strconv.Itoa(int(ms))).Observe(float64(ms)) vec.WithLabelValues(ptn, ctx.Input.Method(), strconv.Itoa(status)).Observe(float64(ms))
} }

View File

@ -157,6 +157,9 @@ func (t *Task) GetSpec(context.Context) string {
func (t *Task) GetStatus(context.Context) string { func (t *Task) GetStatus(context.Context) string {
var str string var str string
for _, v := range t.Errlist { for _, v := range t.Errlist {
if v == nil {
continue
}
str += v.t.String() + ":" + v.errinfo + "<br>" str += v.t.String() + ":" + v.errinfo + "<br>"
} }
return str return str

View File

@ -36,9 +36,11 @@ func TestParse(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
assert.Equal(t, "0/30 * * * * *", tk.GetSpec(context.Background()))
m.AddTask("taska", tk) m.AddTask("taska", tk)
m.StartTask() m.StartTask()
time.Sleep(3 * time.Second) time.Sleep(3 * time.Second)
assert.True(t, len(tk.GetStatus(context.Background())) == 0)
m.StopTask() m.StopTask()
} }