소스 검색

fix: fix client side in zeromicro#2109 (zeromicro#2116) (#2659)

* fix: fix client side in zeromicro#2109 (zeromicro#2116)

* fix: fix client side in zeromicro#2109 (zeromicro#2116)

* fix: fix client side in zeromicro#2109 (zeromicro#2116)
benqi 2 년 전
부모
커밋
3604659027
4개의 변경된 파일32개의 추가작업 그리고 5개의 파일을 삭제
  1. 5 0
      zrpc/client.go
  2. 1 0
      zrpc/client_test.go
  3. 25 5
      zrpc/internal/clientinterceptors/durationinterceptor.go
  4. 1 0
      zrpc/internal/clientinterceptors/durationinterceptor_test.go

+ 5 - 0
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)

+ 1 - 0
zrpc/client_test.go

@@ -125,6 +125,7 @@ func TestDepositServer_Deposit(t *testing.T) {
 		tarConfClient,
 		targetClient,
 	}
+	DontLogClientContentForMethod("foo")
 	SetClientSlowThreshold(time.Second)
 
 	for _, tt := range tests {

+ 25 - 5
zrpc/internal/clientinterceptors/durationinterceptor.go

@@ -3,8 +3,10 @@ package clientinterceptors
 import (
 	"context"
 	"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"
@@ -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)

+ 1 - 0
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)