Skip to content

Commit

Permalink
update middleware hijack. (#113)
Browse files Browse the repository at this point in the history
* Format when rpc is to long.

* update kv format

* update kv Append logic.

* update zapLogger new field to control log.

* update mw hijack.
  • Loading branch information
mo3et authored Jul 23, 2024
1 parent d962b38 commit b5bbb74
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
17 changes: 14 additions & 3 deletions log/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ import (
"go.uber.org/zap/zapcore"
)

type LogFormatter interface {
Format() any
}

var (
pkgLogger Logger
osStdout Logger
Expand Down Expand Up @@ -376,9 +380,8 @@ func (l *ZapLogger) kvAppend(ctx context.Context, keysAndValues []any) []any {
if l.isSimplify {
if len(keysAndValues)%2 == 0 {
for i := 1; i <= len(keysAndValues); i += 2 {
if val, ok := keysAndValues[i].(interface {
Format() any
}); ok && val != nil {

if val, ok := keysAndValues[i].(LogFormatter); ok && val != nil {
keysAndValues[i] = val.Format()
}
}
Expand All @@ -387,6 +390,14 @@ func (l *ZapLogger) kvAppend(ctx context.Context, keysAndValues []any) []any {
}
}

for i := 1; i <= len(keysAndValues); i += 2 {
if s, ok := keysAndValues[i].(interface{ String() string }); ok {
keysAndValues[i] = s.String()
} else {
keysAndValues[i] = fmt.Sprintf("%+v", keysAndValues[i])
}
}

if opUserID != "" {
keysAndValues = append([]any{constant.OpUserID, opUserID}, keysAndValues...)
}
Expand Down
2 changes: 1 addition & 1 deletion mw/rpc_client_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func RpcClientInterceptor(ctx context.Context, method string, req, resp any, cc
log.ZDebug(ctx, fmt.Sprintf("RPC Client Request - %s", extractFunctionName(method)), "funcName", method, "req", req, "conn target", cc.Target())
err = invoker(ctx, method, req, resp, cc, opts...)
if err == nil {
log.ZInfo(ctx, fmt.Sprintf("RPC Client Response Success - %s", extractFunctionName(method)), "funcName", method, "resp", rpcString(resp))
log.ZInfo(ctx, fmt.Sprintf("RPC Client Response Success - %s", extractFunctionName(method)), "funcName", method, "resp", resp)
return nil
}
log.ZError(ctx, fmt.Sprintf("RPC Client Response Error - %s", extractFunctionName(method)), err, "funcName", method)
Expand Down
16 changes: 5 additions & 11 deletions mw/rpc_server_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ package mw
import (
"context"
"fmt"
"github.com/openimsdk/tools/checker"
"github.com/pkg/errors"
"math"
"runtime"
"strings"

"github.com/openimsdk/tools/checker"
"github.com/pkg/errors"

"github.com/openimsdk/protocol/constant"
"github.com/openimsdk/protocol/errinfo"
"github.com/openimsdk/tools/errs"
Expand All @@ -34,13 +35,6 @@ import (
"google.golang.org/grpc/status"
)

func rpcString(v any) string {
if s, ok := v.(interface{ String() string }); ok {
return s.String()
}
return fmt.Sprintf("%+v", v)
}

func RpcServerInterceptor(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
funcName := info.FullMethod
md, err := validateMetadata(ctx)
Expand All @@ -51,7 +45,7 @@ func RpcServerInterceptor(ctx context.Context, req any, info *grpc.UnaryServerIn
if err != nil {
return nil, err
}
log.ZInfo(ctx, fmt.Sprintf("RPC Server Request - %s", extractFunctionName(funcName)), "funcName", funcName, "req", rpcString(req))
log.ZInfo(ctx, fmt.Sprintf("RPC Server Request - %s", extractFunctionName(funcName)), "funcName", funcName, "req", req)
if err := checker.Validate(req); err != nil {
return nil, err
}
Expand All @@ -60,7 +54,7 @@ func RpcServerInterceptor(ctx context.Context, req any, info *grpc.UnaryServerIn
if err != nil {
return nil, handleError(ctx, funcName, req, err)
}
log.ZInfo(ctx, fmt.Sprintf("RPC Server Response Success - %s", extractFunctionName(funcName)), "funcName", funcName, "resp", rpcString(resp))
log.ZInfo(ctx, fmt.Sprintf("RPC Server Response Success - %s", extractFunctionName(funcName)), "funcName", funcName, "resp", resp)
return resp, nil
}

Expand Down

0 comments on commit b5bbb74

Please sign in to comment.