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.
go-zero/zrpc/internal/serverinterceptors/statinterceptor.go

79 lines
2.1 KiB
Go

4 years ago
package serverinterceptors
import (
"context"
"encoding/json"
"sync"
4 years ago
"time"
"github.com/zeromicro/go-zero/core/lang"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stat"
"github.com/zeromicro/go-zero/core/syncx"
"github.com/zeromicro/go-zero/core/timex"
4 years ago
"google.golang.org/grpc"
"google.golang.org/grpc/peer"
)
const defaultSlowThreshold = time.Millisecond * 500
var (
notLoggingContentMethods sync.Map
slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
)
// 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)
}
4 years ago
// UnaryStatInterceptor returns a func that uses given metrics to report stats.
4 years ago
func UnaryStatInterceptor(metrics *stat.Metrics) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (resp interface{}, err error) {
startTime := timex.Now()
defer func() {
duration := timex.Since(startTime)
metrics.Add(stat.Task{
Duration: duration,
})
logDuration(ctx, info.FullMethod, req, duration)
}()
return handler(ctx, req)
}
}
func logDuration(ctx context.Context, method string, req interface{}, duration time.Duration) {
var addr string
client, ok := peer.FromContext(ctx)
if ok {
addr = client.Addr.String()
}
logger := logx.WithContext(ctx).WithDuration(duration)
_, ok = notLoggingContentMethods.Load(method)
if ok {
if duration > slowThreshold.Load() {
logger.Slowf("[RPC] slowcall - %s - %s - %s", addr, method)
} else {
logger.Infof("%s - %s - %s", addr, method)
}
4 years ago
} else {
content, err := json.Marshal(req)
if err != nil {
logx.WithContext(ctx).Errorf("%s - %s", addr, err.Error())
} else if duration > slowThreshold.Load() {
logger.Slowf("[RPC] slowcall - %s - %s - %s",
addr, method, string(content))
} else {
logger.Infof("%s - %s - %s", addr, method, string(content))
}
4 years ago
}
}