@ -3,8 +3,10 @@ package clientinterceptors
import (
import (
"context"
"context"
"path"
"path"
"sync"
"time"
"time"
"github.com/zeromicro/go-zero/core/lang"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/syncx"
"github.com/zeromicro/go-zero/core/syncx"
"github.com/zeromicro/go-zero/core/timex"
"github.com/zeromicro/go-zero/core/timex"
@ -13,7 +15,10 @@ import (
const defaultSlowThreshold = time . Millisecond * 500
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.
// DurationInterceptor is an interceptor that logs the processing time.
func DurationInterceptor ( ctx context . Context , method string , req , reply interface { } ,
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 ( )
start := timex . Now ( )
err := invoker ( ctx , method , req , reply , cc , opts ... )
err := invoker ( ctx , method , req , reply , cc , opts ... )
if err != nil {
if err != nil {
logx . WithContext ( ctx ) . WithDuration ( timex . Since ( start ) ) . Errorf ( "fail - %s - %v - %s" ,
logger := logx . WithContext ( ctx ) . WithDuration ( timex . Since ( start ) )
serverName , req , err . Error ( ) )
_ , 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 {
} else {
elapsed := timex . Since ( start )
elapsed := timex . Since ( start )
if elapsed > slowThreshold . Load ( ) {
if elapsed > slowThreshold . Load ( ) {
logx . WithContext ( ctx ) . WithDuration ( elapsed ) . Slowf ( "[RPC] ok - slowcall - %s - %v - %v" ,
logger := logx . WithContext ( ctx ) . WithDuration ( elapsed )
serverName , req , reply )
_ , 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
return err
}
}
// DontLogContentForMethod disable logging content for given method.
func DontLogContentForMethod ( method string ) {
notLoggingContentMethods . Store ( method , lang . Placeholder )
}
// SetSlowThreshold sets the slow threshold.
// SetSlowThreshold sets the slow threshold.
func SetSlowThreshold ( threshold time . Duration ) {
func SetSlowThreshold ( threshold time . Duration ) {
slowThreshold . Set ( threshold )
slowThreshold . Set ( threshold )