diff --git a/core/stores/redis/hook.go b/core/stores/redis/hook.go index 856c5d5d..b7a0a332 100644 --- a/core/stores/redis/hook.go +++ b/core/stores/redis/hook.go @@ -56,7 +56,7 @@ func (h hook) AfterProcess(ctx context.Context, cmd red.Cmder) error { logDuration(ctx, []red.Cmder{cmd}, duration) } - metricReqDur.Observe(int64(duration/time.Millisecond), cmd.Name()) + metricReqDur.Observe(duration.Milliseconds(), cmd.Name()) if msg := formatError(err); len(msg) > 0 { metricReqErr.Inc(cmd.Name(), msg) } @@ -103,7 +103,7 @@ func (h hook) AfterProcessPipeline(ctx context.Context, cmds []red.Cmder) error logDuration(ctx, cmds, duration) } - metricReqDur.Observe(int64(duration/time.Millisecond), "Pipeline") + metricReqDur.Observe(duration.Milliseconds(), "Pipeline") if msg := formatError(batchError.Err()); len(msg) > 0 { metricReqErr.Inc("Pipeline", msg) } diff --git a/core/stores/sqlx/stmt.go b/core/stores/sqlx/stmt.go index 5dc206a5..cdaece6f 100644 --- a/core/stores/sqlx/stmt.go +++ b/core/stores/sqlx/stmt.go @@ -136,7 +136,7 @@ func (e *realSqlGuard) finish(ctx context.Context, err error) { logSqlError(ctx, e.stmt, err) } - metricReqDur.Observe(int64(duration/time.Millisecond), e.command) + metricReqDur.Observe(duration.Milliseconds(), e.command) } func (e *realSqlGuard) start(q string, args ...any) error { diff --git a/rest/handler/prometheushandler.go b/rest/handler/prometheushandler.go index c1aa7ae7..cf0d91fc 100644 --- a/rest/handler/prometheushandler.go +++ b/rest/handler/prometheushandler.go @@ -3,7 +3,6 @@ package handler import ( "net/http" "strconv" - "time" "github.com/zeromicro/go-zero/core/metric" "github.com/zeromicro/go-zero/core/timex" @@ -38,7 +37,7 @@ func PrometheusHandler(path, method string) func(http.Handler) http.Handler { startTime := timex.Now() cw := &response.WithCodeResponseWriter{Writer: w} defer func() { - metricServerReqDur.Observe(int64(timex.Since(startTime)/time.Millisecond), path, method) + metricServerReqDur.Observe(timex.Since(startTime).Milliseconds(), path, method) metricServerReqCodeTotal.Inc(path, strconv.Itoa(cw.Code), method) }() diff --git a/tools/goctl/api/gogen/genroutes.go b/tools/goctl/api/gogen/genroutes.go index 1d6e2df2..8243d5e4 100644 --- a/tools/goctl/api/gogen/genroutes.go +++ b/tools/goctl/api/gogen/genroutes.go @@ -129,7 +129,7 @@ rest.WithPrefix("%s"),`, g.prefix) return fmt.Errorf("timeout should not less than 1ms, now %v", duration) } - timeout = fmt.Sprintf("\n rest.WithTimeout(%d * time.Millisecond),", duration/time.Millisecond) + timeout = fmt.Sprintf("\n rest.WithTimeout(%d * time.Millisecond),", duration.Milliseconds()) hasTimeout = true } diff --git a/zrpc/internal/clientinterceptors/prometheusinterceptor.go b/zrpc/internal/clientinterceptors/prometheusinterceptor.go index c682937b..23ab3a75 100644 --- a/zrpc/internal/clientinterceptors/prometheusinterceptor.go +++ b/zrpc/internal/clientinterceptors/prometheusinterceptor.go @@ -3,7 +3,6 @@ package clientinterceptors import ( "context" "strconv" - "time" "github.com/zeromicro/go-zero/core/metric" "github.com/zeromicro/go-zero/core/timex" @@ -37,7 +36,7 @@ func PrometheusInterceptor(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { startTime := timex.Now() err := invoker(ctx, method, req, reply, cc, opts...) - metricClientReqDur.Observe(int64(timex.Since(startTime)/time.Millisecond), method) + metricClientReqDur.Observe(timex.Since(startTime).Milliseconds(), method) metricClientReqCodeTotal.Inc(method, strconv.Itoa(int(status.Code(err)))) return err } diff --git a/zrpc/internal/serverinterceptors/prometheusinterceptor.go b/zrpc/internal/serverinterceptors/prometheusinterceptor.go index 5683dbd2..9fcaaef9 100644 --- a/zrpc/internal/serverinterceptors/prometheusinterceptor.go +++ b/zrpc/internal/serverinterceptors/prometheusinterceptor.go @@ -3,7 +3,6 @@ package serverinterceptors import ( "context" "strconv" - "time" "github.com/zeromicro/go-zero/core/metric" "github.com/zeromicro/go-zero/core/timex" @@ -37,7 +36,7 @@ func UnaryPrometheusInterceptor(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) { startTime := timex.Now() resp, err := handler(ctx, req) - metricServerReqDur.Observe(int64(timex.Since(startTime)/time.Millisecond), info.FullMethod) + metricServerReqDur.Observe(timex.Since(startTime).Milliseconds(), info.FullMethod) metricServerReqCodeTotal.Inc(info.FullMethod, strconv.Itoa(int(status.Code(err)))) return resp, err }