From ca9cb8e1f95e2b6e3391e3570a4385b08446e878 Mon Sep 17 00:00:00 2001 From: "teamgram.io" Date: Sat, 3 Dec 2022 21:31:16 +0800 Subject: [PATCH 1/3] fix: fix client side in zeromicro#2109 (zeromicro#2116) --- zrpc/client.go | 5 ++++ zrpc/client_test.go | 1 + .../clientinterceptors/durationinterceptor.go | 30 +++++++++++++++---- .../durationinterceptor_test.go | 1 + 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/zrpc/client.go b/zrpc/client.go index 5240a4790595..7fe631281bdb 100644 --- a/zrpc/client.go +++ b/zrpc/client.go @@ -90,6 +90,11 @@ func (rc *RpcClient) Conn() *grpc.ClientConn { return rc.client.Conn() } +// DontLogClientContentForMethod disable logging content for given method. +func DontLogClientContentForMethod(method string) { + clientinterceptors.DontLogContentForMethod(method) +} + // SetClientSlowThreshold sets the slow threshold on client side. func SetClientSlowThreshold(threshold time.Duration) { clientinterceptors.SetSlowThreshold(threshold) diff --git a/zrpc/client_test.go b/zrpc/client_test.go index 9b1319024c2b..c4f8ac14119c 100644 --- a/zrpc/client_test.go +++ b/zrpc/client_test.go @@ -125,6 +125,7 @@ func TestDepositServer_Deposit(t *testing.T) { tarConfClient, targetClient, } + DontLogContentForMethod("foo") SetClientSlowThreshold(time.Second) for _, tt := range tests { diff --git a/zrpc/internal/clientinterceptors/durationinterceptor.go b/zrpc/internal/clientinterceptors/durationinterceptor.go index 3f65a8bf5a5e..ffdbf0002d7f 100644 --- a/zrpc/internal/clientinterceptors/durationinterceptor.go +++ b/zrpc/internal/clientinterceptors/durationinterceptor.go @@ -2,7 +2,9 @@ package clientinterceptors import ( "context" + "github.com/zeromicro/go-zero/core/lang" "path" + "sync" "time" "github.com/zeromicro/go-zero/core/logx" @@ -13,7 +15,10 @@ import ( const defaultSlowThreshold = time.Millisecond * 500 -var slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold) +var ( + notLoggingContentMethods sync.Map + slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold) +) // DurationInterceptor is an interceptor that logs the processing time. func DurationInterceptor(ctx context.Context, method string, req, reply interface{}, @@ -22,19 +27,34 @@ func DurationInterceptor(ctx context.Context, method string, req, reply interfac start := timex.Now() err := invoker(ctx, method, req, reply, cc, opts...) if err != nil { - logx.WithContext(ctx).WithDuration(timex.Since(start)).Errorf("fail - %s - %v - %s", - serverName, req, err.Error()) + logger := logx.WithContext(ctx).WithDuration(timex.Since(start)) + _, ok := notLoggingContentMethods.Load(method) + if ok { + logger.Errorf("fail - %s - %s", serverName, err.Error()) + } else { + logger.Errorf("fail - %s - %v - %s", serverName, req, err.Error()) + } } else { elapsed := timex.Since(start) if elapsed > slowThreshold.Load() { - logx.WithContext(ctx).WithDuration(elapsed).Slowf("[RPC] ok - slowcall - %s - %v - %v", - serverName, req, reply) + logger := logx.WithContext(ctx).WithDuration(elapsed) + _, ok := notLoggingContentMethods.Load(method) + if ok { + logger.Slowf("[RPC] ok - slowcall - %s", serverName) + } else { + logger.Slowf("[RPC] ok - slowcall - %s - %v - %v", serverName, req, reply) + } } } return err } +// DontLogContentForMethod disable logging content for given method. +func DontLogContentForMethod(method string) { + notLoggingContentMethods.Store(method, lang.Placeholder) +} + // SetSlowThreshold sets the slow threshold. func SetSlowThreshold(threshold time.Duration) { slowThreshold.Set(threshold) diff --git a/zrpc/internal/clientinterceptors/durationinterceptor_test.go b/zrpc/internal/clientinterceptors/durationinterceptor_test.go index 9acd0efbe4b0..844a7a6d78ea 100644 --- a/zrpc/internal/clientinterceptors/durationinterceptor_test.go +++ b/zrpc/internal/clientinterceptors/durationinterceptor_test.go @@ -24,6 +24,7 @@ func TestDurationInterceptor(t *testing.T) { err: errors.New("mock"), }, } + DontLogContentForMethod("/foo") for _, test := range tests { t.Run(test.name, func(t *testing.T) { cc := new(grpc.ClientConn) From 6ca114d79dd487b298f3a2ad8cf1dde666b78c2d Mon Sep 17 00:00:00 2001 From: "teamgram.io" Date: Sat, 3 Dec 2022 21:35:19 +0800 Subject: [PATCH 2/3] fix: fix client side in zeromicro#2109 (zeromicro#2116) --- zrpc/internal/clientinterceptors/durationinterceptor.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zrpc/internal/clientinterceptors/durationinterceptor.go b/zrpc/internal/clientinterceptors/durationinterceptor.go index ffdbf0002d7f..eac73af55a5f 100644 --- a/zrpc/internal/clientinterceptors/durationinterceptor.go +++ b/zrpc/internal/clientinterceptors/durationinterceptor.go @@ -2,11 +2,11 @@ package clientinterceptors import ( "context" - "github.com/zeromicro/go-zero/core/lang" "path" "sync" "time" + "github.com/zeromicro/go-zero/core/lang" "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/syncx" "github.com/zeromicro/go-zero/core/timex" From 826213cbb8e37c265f5916baecbf0980900be99b Mon Sep 17 00:00:00 2001 From: "teamgram.io" Date: Sat, 3 Dec 2022 21:42:08 +0800 Subject: [PATCH 3/3] fix: fix client side in zeromicro#2109 (zeromicro#2116) --- zrpc/client_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zrpc/client_test.go b/zrpc/client_test.go index c4f8ac14119c..80fb39e9b365 100644 --- a/zrpc/client_test.go +++ b/zrpc/client_test.go @@ -125,7 +125,7 @@ func TestDepositServer_Deposit(t *testing.T) { tarConfClient, targetClient, } - DontLogContentForMethod("foo") + DontLogClientContentForMethod("foo") SetClientSlowThreshold(time.Second) for _, tt := range tests {