You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
776 B
Go
32 lines
776 B
Go
4 years ago
|
package clientinterceptors
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"path"
|
||
|
"time"
|
||
|
|
||
|
"zero/core/logx"
|
||
|
"zero/core/timex"
|
||
|
|
||
|
"google.golang.org/grpc"
|
||
|
)
|
||
|
|
||
|
const slowThreshold = time.Millisecond * 500
|
||
|
|
||
|
func DurationInterceptor(ctx context.Context, method string, req, reply interface{},
|
||
|
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
|
||
|
serverName := path.Join(cc.Target(), method)
|
||
|
start := timex.Now()
|
||
|
err := invoker(ctx, method, req, reply, cc, opts...)
|
||
|
if err != nil {
|
||
|
logx.WithDuration(timex.Since(start)).Infof("fail - %s - %v - %s", serverName, req, err.Error())
|
||
|
} else {
|
||
|
elapsed := timex.Since(start)
|
||
|
if elapsed > slowThreshold {
|
||
|
logx.WithDuration(elapsed).Slowf("[RPC] ok - slowcall - %s - %v - %v", serverName, req, reply)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return err
|
||
|
}
|