|
|
|
@ -6,11 +6,14 @@ import (
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/tal-tech/go-zero/core/logx"
|
|
|
|
|
"github.com/tal-tech/go-zero/core/syncx"
|
|
|
|
|
"github.com/tal-tech/go-zero/core/timex"
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const slowThreshold = time.Millisecond * 500
|
|
|
|
|
const defaultSlowThreshold = time.Millisecond * 500
|
|
|
|
|
|
|
|
|
|
var slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
|
|
|
|
|
|
|
|
|
|
// DurationInterceptor is an interceptor that logs the processing time.
|
|
|
|
|
func DurationInterceptor(ctx context.Context, method string, req, reply interface{},
|
|
|
|
@ -23,7 +26,7 @@ func DurationInterceptor(ctx context.Context, method string, req, reply interfac
|
|
|
|
|
serverName, req, err.Error())
|
|
|
|
|
} else {
|
|
|
|
|
elapsed := timex.Since(start)
|
|
|
|
|
if elapsed > slowThreshold {
|
|
|
|
|
if elapsed > slowThreshold.Load() {
|
|
|
|
|
logx.WithContext(ctx).WithDuration(elapsed).Slowf("[RPC] ok - slowcall - %s - %v - %v",
|
|
|
|
|
serverName, req, reply)
|
|
|
|
|
}
|
|
|
|
@ -31,3 +34,8 @@ func DurationInterceptor(ctx context.Context, method string, req, reply interfac
|
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetSlowThreshold sets the slow threshold.
|
|
|
|
|
func SetSlowThreshold(threshold time.Duration) {
|
|
|
|
|
slowThreshold.Set(threshold)
|
|
|
|
|
}
|
|
|
|
|