diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a3068ed..9bf94fd1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ # 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) - 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) \ No newline at end of file diff --git a/adapter/metric/prometheus.go b/adapter/metric/prometheus.go index 7abd0e5a..6b276171 100644 --- a/adapter/metric/prometheus.go +++ b/adapter/metric/prometheus.go @@ -38,7 +38,7 @@ func PrometheusMiddleWare(next http.Handler) http.Handler { "appname": web.BConfig.AppName, }, Help: "The statics info for http request", - }, []string{"pattern", "method", "status", "duration"}) + }, []string{"pattern", "method", "status"}) 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()) } 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)) } diff --git a/adapter/metric/prometheus_test.go b/adapter/metric/prometheus_test.go index e87e06d6..53984845 100644 --- a/adapter/metric/prometheus_test.go +++ b/adapter/metric/prometheus_test.go @@ -35,7 +35,7 @@ func TestPrometheusMiddleWare(t *testing.T) { }, 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) middleware.ServeHTTP(writer, request) diff --git a/client/httplib/filter/prometheus/filter.go b/client/httplib/filter/prometheus/filter.go index a4de521b..3d5acf12 100644 --- a/client/httplib/filter/prometheus/filter.go +++ b/client/httplib/filter/prometheus/filter.go @@ -43,7 +43,7 @@ func (builder *FilterChainBuilder) FilterChain(next httplib.Filter) httplib.Filt "appname": builder.AppName, }, 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) { startTime := time.Now() @@ -73,5 +73,5 @@ func (builder *FilterChainBuilder) report(startTime time.Time, endTime time.Time dur := int(endTime.Sub(startTime) / time.Millisecond) 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)) } diff --git a/client/orm/filter/prometheus/filter.go b/client/orm/filter/prometheus/filter.go index 1f30f770..db60876e 100644 --- a/client/orm/filter/prometheus/filter.go +++ b/client/orm/filter/prometheus/filter.go @@ -50,7 +50,7 @@ func (builder *FilterChainBuilder) FilterChain(next orm.Filter) orm.Filter { "appname": builder.AppName, }, 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{} { startTime := time.Now() @@ -74,12 +74,12 @@ func (builder *FilterChainBuilder) report(ctx context.Context, inv *orm.Invocati builder.reportTxn(ctx, inv) return } - builder.summaryVec.WithLabelValues(inv.Method, inv.GetTableName(), strconv.Itoa(int(dur)), - strconv.FormatBool(inv.InsideTx), inv.TxName) + builder.summaryVec.WithLabelValues(inv.Method, inv.GetTableName(), + strconv.FormatBool(inv.InsideTx), inv.TxName).Observe(float64(dur)) } func (builder *FilterChainBuilder) reportTxn(ctx context.Context, inv *orm.Invocation) { dur := time.Now().Sub(inv.TxStartTime) / time.Millisecond - builder.summaryVec.WithLabelValues(inv.Method, inv.TxName, strconv.Itoa(int(dur)), - strconv.FormatBool(inv.InsideTx), inv.TxName) + builder.summaryVec.WithLabelValues(inv.Method, inv.TxName, + strconv.FormatBool(inv.InsideTx), inv.TxName).Observe(float64(dur)) } diff --git a/server/web/filter/prometheus/filter.go b/server/web/filter/prometheus/filter.go index 5a0db9a7..59a673ac 100644 --- a/server/web/filter/prometheus/filter.go +++ b/server/web/filter/prometheus/filter.go @@ -43,7 +43,7 @@ func (builder *FilterChainBuilder) FilterChain(next web.FilterFunc) web.FilterFu "appname": web.BConfig.AppName, }, Help: "The statics info for http request", - }, []string{"pattern", "method", "status", "duration"}) + }, []string{"pattern", "method", "status"}) prometheus.MustRegister(summaryVec) @@ -83,5 +83,5 @@ func report(dur time.Duration, ctx *context.Context, vec *prometheus.SummaryVec) status := ctx.Output.Status ptn := ctx.Input.GetData("RouterPattern").(string) 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)) } diff --git a/task/task.go b/task/task.go index 00cbbfa7..2ea34f24 100644 --- a/task/task.go +++ b/task/task.go @@ -157,6 +157,9 @@ func (t *Task) GetSpec(context.Context) string { func (t *Task) GetStatus(context.Context) string { var str string for _, v := range t.Errlist { + if v == nil { + continue + } str += v.t.String() + ":" + v.errinfo + "
" } return str diff --git a/task/task_test.go b/task/task_test.go index 2cb807ce..5e117cbd 100644 --- a/task/task_test.go +++ b/task/task_test.go @@ -36,9 +36,11 @@ func TestParse(t *testing.T) { if err != nil { t.Fatal(err) } + assert.Equal(t, "0/30 * * * * *", tk.GetSpec(context.Background())) m.AddTask("taska", tk) m.StartTask() time.Sleep(3 * time.Second) + assert.True(t, len(tk.GetStatus(context.Background())) == 0) m.StopTask() }