Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update middleware hijack. #113

Merged
merged 6 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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