mirror of
https://github.com/go-kratos/kratos.git
synced 2025-01-07 23:02:12 +02:00
error.Code def support http , mw/log&metrics update (#1015)
* mw/metrics add tag
This commit is contained in:
parent
544e08f729
commit
c551448bbd
@ -72,7 +72,7 @@ func Errorf(code int, reason, format string, a ...interface{}) error {
|
|||||||
return New(code, reason, fmt.Sprintf(format, a...))
|
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.
|
// It supports wrapped errors.
|
||||||
func Code(err error) int {
|
func Code(err error) int {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/go-kratos/kratos/v2/transport/grpc"
|
"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) {
|
func grpcServerLog(logger log.Logger, ctx context.Context, args string, err error) {
|
||||||
info, ok := grpc.FromServerContext(ctx)
|
info, ok := grpc.FromServerContext(ctx)
|
||||||
if !ok {
|
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) {
|
func grpcClientLog(logger log.Logger, ctx context.Context, args string, err error) {
|
||||||
info, ok := grpc.FromClientContext(ctx)
|
info, ok := grpc.FromClientContext(ctx)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/go-kratos/kratos/v2/transport/http"
|
"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) {
|
func httpServerLog(logger log.Logger, ctx context.Context, args string, err error) {
|
||||||
info, ok := http.FromServerContext(ctx)
|
info, ok := http.FromServerContext(ctx)
|
||||||
if !ok {
|
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) {
|
func httpClientLog(logger log.Logger, ctx context.Context, args string, err error) {
|
||||||
info, ok := http.FromClientContext(ctx)
|
info, ok := http.FromClientContext(ctx)
|
||||||
if !ok {
|
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 {
|
func extractArgs(req interface{}) string {
|
||||||
if stringer, ok := req.(fmt.Stringer); ok {
|
if stringer, ok := req.(fmt.Stringer); ok {
|
||||||
return stringer.String()
|
return stringer.String()
|
||||||
@ -52,6 +53,7 @@ func extractArgs(req interface{}) string {
|
|||||||
return fmt.Sprintf("%+v", req)
|
return fmt.Sprintf("%+v", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// extractError returns the string of the error
|
||||||
func extractError(err error) (errMsg string) {
|
func extractError(err error) (errMsg string) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errMsg = fmt.Sprintf("%+v", err)
|
errMsg = fmt.Sprintf("%+v", err)
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-kratos/kratos/v2/errors"
|
"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/metrics"
|
||||||
"github.com/go-kratos/kratos/v2/middleware"
|
"github.com/go-kratos/kratos/v2/middleware"
|
||||||
"github.com/go-kratos/kratos/v2/transport/grpc"
|
"github.com/go-kratos/kratos/v2/transport/grpc"
|
||||||
@ -31,7 +32,7 @@ func WithSeconds(c metrics.Observer) Option {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type options struct {
|
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
|
requests metrics.Counter
|
||||||
// histogram: <kind>_<client/server>_requests_seconds_bucket{method, path}
|
// histogram: <kind>_<client/server>_requests_seconds_bucket{method, path}
|
||||||
seconds metrics.Observer
|
seconds metrics.Observer
|
||||||
@ -50,9 +51,13 @@ func Server(opts ...Option) middleware.Middleware {
|
|||||||
path string
|
path string
|
||||||
code uint32
|
code uint32
|
||||||
)
|
)
|
||||||
|
startTime := time.Now()
|
||||||
|
reply, err := handler(ctx, req)
|
||||||
|
|
||||||
if info, ok := grpc.FromServerContext(ctx); ok {
|
if info, ok := grpc.FromServerContext(ctx); ok {
|
||||||
method = "POST"
|
method = "POST"
|
||||||
path = info.FullMethod
|
path = info.FullMethod
|
||||||
|
code = uint32(httputil.GRPCCodeFromStatus(errors.Code(err)))
|
||||||
} else if info, ok := http.FromServerContext(ctx); ok {
|
} else if info, ok := http.FromServerContext(ctx); ok {
|
||||||
req := info.Request.WithContext(ctx)
|
req := info.Request.WithContext(ctx)
|
||||||
method = req.Method
|
method = req.Method
|
||||||
@ -62,14 +67,11 @@ func Server(opts ...Option) middleware.Middleware {
|
|||||||
} else {
|
} else {
|
||||||
path = req.RequestURI
|
path = req.RequestURI
|
||||||
}
|
}
|
||||||
}
|
|
||||||
startTime := time.Now()
|
|
||||||
reply, err := handler(ctx, req)
|
|
||||||
if err != nil {
|
|
||||||
code = uint32(errors.Code(err))
|
code = uint32(errors.Code(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
if options.requests != nil {
|
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 {
|
if options.seconds != nil {
|
||||||
options.seconds.With(method, path).Observe(time.Since(startTime).Seconds())
|
options.seconds.With(method, path).Observe(time.Since(startTime).Seconds())
|
||||||
@ -93,20 +95,21 @@ func Client(opts ...Option) middleware.Middleware {
|
|||||||
path string
|
path string
|
||||||
code uint32
|
code uint32
|
||||||
)
|
)
|
||||||
|
startTime := time.Now()
|
||||||
|
reply, err := handler(ctx, req)
|
||||||
|
|
||||||
if info, ok := grpc.FromClientContext(ctx); ok {
|
if info, ok := grpc.FromClientContext(ctx); ok {
|
||||||
method = "POST"
|
method = "POST"
|
||||||
path = info.FullMethod
|
path = info.FullMethod
|
||||||
|
code = uint32(httputil.GRPCCodeFromStatus(errors.Code(err)))
|
||||||
} else if info, ok := http.FromClientContext(ctx); ok {
|
} else if info, ok := http.FromClientContext(ctx); ok {
|
||||||
method = info.Request.Method
|
method = info.Request.Method
|
||||||
path = info.Request.RequestURI
|
path = info.Request.RequestURI
|
||||||
}
|
|
||||||
startTime := time.Now()
|
|
||||||
reply, err := handler(ctx, req)
|
|
||||||
if err != nil {
|
|
||||||
code = uint32(errors.Code(err))
|
code = uint32(errors.Code(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
if options.requests != nil {
|
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 {
|
if options.seconds != nil {
|
||||||
options.seconds.With(method, path).Observe(time.Since(startTime).Seconds())
|
options.seconds.With(method, path).Observe(time.Since(startTime).Seconds())
|
||||||
|
Loading…
Reference in New Issue
Block a user