From c551448bbd648f6d5e9718230f1ad0ff45f4cdd4 Mon Sep 17 00:00:00 2001 From: miya Date: Mon, 7 Jun 2021 22:28:02 -0500 Subject: [PATCH] error.Code def support http , mw/log&metrics update (#1015) * mw/metrics add tag --- errors/errors.go | 2 +- middleware/logging/grpc.go | 2 ++ middleware/logging/http.go | 2 ++ middleware/logging/logging.go | 2 ++ middleware/metrics/metrics.go | 25 ++++++++++++++----------- 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/errors/errors.go b/errors/errors.go index 2f76d10d8..5b9d29032 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -72,7 +72,7 @@ func Errorf(code int, reason, format string, a ...interface{}) error { return New(code, reason, fmt.Sprintf(format, a...)) } -// Code returns the code for a particular error. +// Code returns the http code for a error. // It supports wrapped errors. func Code(err error) int { if err == nil { diff --git a/middleware/logging/grpc.go b/middleware/logging/grpc.go index 3fa46f5fa..66b41ba6e 100644 --- a/middleware/logging/grpc.go +++ b/middleware/logging/grpc.go @@ -9,6 +9,7 @@ import ( "github.com/go-kratos/kratos/v2/transport/grpc" ) +// grpcServerLog is a server handler when transport is KindGRPC func grpcServerLog(logger log.Logger, ctx context.Context, args string, err error) { info, ok := grpc.FromServerContext(ctx) if !ok { @@ -30,6 +31,7 @@ func grpcServerLog(logger log.Logger, ctx context.Context, args string, err erro ) } +// grpcClientLog is a client handler when transport is KindGRPC func grpcClientLog(logger log.Logger, ctx context.Context, args string, err error) { info, ok := grpc.FromClientContext(ctx) if !ok { diff --git a/middleware/logging/http.go b/middleware/logging/http.go index 31a3c6557..a8b647755 100644 --- a/middleware/logging/http.go +++ b/middleware/logging/http.go @@ -8,6 +8,7 @@ import ( "github.com/go-kratos/kratos/v2/transport/http" ) +// httpServerLog is a middleware server handler when transport is KindHTTP func httpServerLog(logger log.Logger, ctx context.Context, args string, err error) { info, ok := http.FromServerContext(ctx) if !ok { @@ -30,6 +31,7 @@ func httpServerLog(logger log.Logger, ctx context.Context, args string, err erro ) } +// httpClientLog is a middleware client handler when transport is KindHTTP func httpClientLog(logger log.Logger, ctx context.Context, args string, err error) { info, ok := http.FromClientContext(ctx) if !ok { diff --git a/middleware/logging/logging.go b/middleware/logging/logging.go index e39aab698..5dd58efaa 100644 --- a/middleware/logging/logging.go +++ b/middleware/logging/logging.go @@ -45,6 +45,7 @@ func Client(logger log.Logger) middleware.Middleware { } } +// extractArgs returns the string of the req func extractArgs(req interface{}) string { if stringer, ok := req.(fmt.Stringer); ok { return stringer.String() @@ -52,6 +53,7 @@ func extractArgs(req interface{}) string { return fmt.Sprintf("%+v", req) } +// extractError returns the string of the error func extractError(err error) (errMsg string) { if err != nil { errMsg = fmt.Sprintf("%+v", err) diff --git a/middleware/metrics/metrics.go b/middleware/metrics/metrics.go index 2f13b0b48..3982f36a2 100644 --- a/middleware/metrics/metrics.go +++ b/middleware/metrics/metrics.go @@ -6,6 +6,7 @@ import ( "time" "github.com/go-kratos/kratos/v2/errors" + "github.com/go-kratos/kratos/v2/internal/httputil" "github.com/go-kratos/kratos/v2/metrics" "github.com/go-kratos/kratos/v2/middleware" "github.com/go-kratos/kratos/v2/transport/grpc" @@ -31,7 +32,7 @@ func WithSeconds(c metrics.Observer) Option { } type options struct { - // counter: __requests_code_total{method, path, code} + // counter: __requests_code_total{method, path, code, reason} requests metrics.Counter // histogram: __requests_seconds_bucket{method, path} seconds metrics.Observer @@ -50,9 +51,13 @@ func Server(opts ...Option) middleware.Middleware { path string code uint32 ) + startTime := time.Now() + reply, err := handler(ctx, req) + if info, ok := grpc.FromServerContext(ctx); ok { method = "POST" path = info.FullMethod + code = uint32(httputil.GRPCCodeFromStatus(errors.Code(err))) } else if info, ok := http.FromServerContext(ctx); ok { req := info.Request.WithContext(ctx) method = req.Method @@ -62,14 +67,11 @@ func Server(opts ...Option) middleware.Middleware { } else { path = req.RequestURI } - } - startTime := time.Now() - reply, err := handler(ctx, req) - if err != nil { code = uint32(errors.Code(err)) } + if options.requests != nil { - options.requests.With(method, path, strconv.Itoa(int(code))).Inc() + options.requests.With(method, path, strconv.Itoa(int(code)), errors.Reason(err)).Inc() } if options.seconds != nil { options.seconds.With(method, path).Observe(time.Since(startTime).Seconds()) @@ -93,20 +95,21 @@ func Client(opts ...Option) middleware.Middleware { path string code uint32 ) + startTime := time.Now() + reply, err := handler(ctx, req) + if info, ok := grpc.FromClientContext(ctx); ok { method = "POST" path = info.FullMethod + code = uint32(httputil.GRPCCodeFromStatus(errors.Code(err))) } else if info, ok := http.FromClientContext(ctx); ok { method = info.Request.Method path = info.Request.RequestURI - } - startTime := time.Now() - reply, err := handler(ctx, req) - if err != nil { code = uint32(errors.Code(err)) } + if options.requests != nil { - options.requests.With(method, path, strconv.Itoa(int(code))).Inc() + options.requests.With(method, path, strconv.Itoa(int(code)), errors.Reason(err)).Inc() } if options.seconds != nil { options.seconds.With(method, path).Observe(time.Since(startTime).Seconds())