error.Code def support http , mw/log&metrics update (#1015)

* mw/metrics add tag
pull/1018/head
miya 3 years ago committed by GitHub
parent 544e08f729
commit c551448bbd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      errors/errors.go
  2. 2
      middleware/logging/grpc.go
  3. 2
      middleware/logging/http.go
  4. 2
      middleware/logging/logging.go
  5. 25
      middleware/metrics/metrics.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 {

@ -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 {

@ -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 {

@ -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)

@ -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: <kind>_<client/server>_requests_code_total{method, path, code}
// counter: <kind>_<client/server>_requests_code_total{method, path, code, reason}
requests metrics.Counter
// histogram: <kind>_<client/server>_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())

Loading…
Cancel
Save