diff --git a/middleware/logging/logging.go b/middleware/logging/logging.go index 8595750bd1f..a4dff33862c 100644 --- a/middleware/logging/logging.go +++ b/middleware/logging/logging.go @@ -5,10 +5,13 @@ import ( "fmt" "time" + "google.golang.org/grpc/codes" + "github.com/go-kratos/kratos/v2/errors" "github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/middleware" "github.com/go-kratos/kratos/v2/transport" + "github.com/go-kratos/kratos/v2/transport/http/status" ) // Redacter defines how to log an object @@ -26,6 +29,10 @@ func Server(logger log.Logger) middleware.Middleware { kind string operation string ) + + // default code + code = int32(status.FromGRPCCode(codes.OK)) + startTime := time.Now() if info, ok := transport.FromServerContext(ctx); ok { kind = info.Kind().String() @@ -62,6 +69,10 @@ func Client(logger log.Logger) middleware.Middleware { kind string operation string ) + + // default code + code = int32(status.FromGRPCCode(codes.OK)) + startTime := time.Now() if info, ok := transport.FromClientContext(ctx); ok { kind = info.Kind().String() diff --git a/middleware/metrics/metrics.go b/middleware/metrics/metrics.go index 401c6377070..7a76c57ae78 100644 --- a/middleware/metrics/metrics.go +++ b/middleware/metrics/metrics.go @@ -4,13 +4,15 @@ import ( "context" "time" - "github.com/go-kratos/kratos/v2/errors" - "github.com/go-kratos/kratos/v2/middleware" - "github.com/go-kratos/kratos/v2/transport" - "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/metric" metricsdk "go.opentelemetry.io/otel/sdk/metric" + "google.golang.org/grpc/codes" + + "github.com/go-kratos/kratos/v2/errors" + "github.com/go-kratos/kratos/v2/middleware" + "github.com/go-kratos/kratos/v2/transport" + "github.com/go-kratos/kratos/v2/transport/http/status" ) const ( @@ -115,6 +117,10 @@ func Server(opts ...Option) middleware.Middleware { kind string operation string ) + + // default code + code = status.FromGRPCCode(codes.OK) + startTime := time.Now() if info, ok := transport.FromServerContext(ctx); ok { kind = info.Kind().String() @@ -164,6 +170,10 @@ func Client(opts ...Option) middleware.Middleware { kind string operation string ) + + // default code + code = status.FromGRPCCode(codes.OK) + startTime := time.Now() if info, ok := transport.FromClientContext(ctx); ok { kind = info.Kind().String()