This commit is contained in:
Kevin Wan
2022-07-09 15:05:59 +08:00
committed by GitHub
parent 8afe68f3f1
commit 1410f7dc20
8 changed files with 117 additions and 29 deletions

View File

@@ -3,8 +3,10 @@ package serverinterceptors
import (
"context"
"encoding/json"
"sync"
"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"
@@ -15,7 +17,15 @@ import (
const defaultSlowThreshold = time.Millisecond * 500
var slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
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) {
@@ -45,13 +55,24 @@ func logDuration(ctx context.Context, method string, req interface{}, duration t
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)
}
}
content, err := json.Marshal(req)
if err != nil {
logx.WithContext(ctx).Errorf("%s - %s", addr, err.Error())
} else if duration > slowThreshold.Load() {
logx.WithContext(ctx).WithDuration(duration).Slowf("[RPC] slowcall - %s - %s - %s",
logger.Slowf("[RPC] slowcall - %s - %s - %s",
addr, method, string(content))
} else {
logx.WithContext(ctx).WithDuration(duration).Infof("%s - %s - %s", addr, method, string(content))
logger.Infof("%s - %s - %s", addr, method, string(content))
}
}

View File

@@ -83,3 +83,58 @@ func TestLogDuration(t *testing.T) {
})
}
}
func TestLogDurationWithoutContent(t *testing.T) {
addrs, err := net.InterfaceAddrs()
assert.Nil(t, err)
assert.True(t, len(addrs) > 0)
tests := []struct {
name string
ctx context.Context
req interface{}
duration time.Duration
}{
{
name: "normal",
ctx: context.Background(),
req: "foo",
},
{
name: "bad req",
ctx: context.Background(),
req: make(chan lang.PlaceholderType), // not marshalable
},
{
name: "timeout",
ctx: context.Background(),
req: "foo",
duration: time.Second,
},
{
name: "timeout",
ctx: peer.NewContext(context.Background(), &peer.Peer{
Addr: addrs[0],
}),
req: "foo",
},
{
name: "timeout",
ctx: context.Background(),
req: "foo",
duration: slowThreshold.Load() + time.Second,
},
}
DontLogContentForMethod("foo")
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
assert.NotPanics(t, func() {
logDuration(test.ctx, "foo", test.req, test.duration)
})
})
}
}

View File

@@ -97,6 +97,11 @@ func (rs *RpcServer) Stop() {
logx.Close()
}
// DontLogContentForMethod disable logging content for given method.
func DontLogContentForMethod(method string) {
serverinterceptors.DontLogContentForMethod(method)
}
// SetServerSlowThreshold sets the slow threshold on server side.
func SetServerSlowThreshold(threshold time.Duration) {
serverinterceptors.SetSlowThreshold(threshold)

View File

@@ -35,6 +35,7 @@ func TestServer_setupInterceptors(t *testing.T) {
}
func TestServer(t *testing.T) {
DontLogContentForMethod("foo")
SetServerSlowThreshold(time.Second)
svr := MustNewServer(RpcServerConf{
ServiceConf: service.ServiceConf{
@@ -121,7 +122,7 @@ type mockedServer struct {
streamInterceptors []grpc.StreamServerInterceptor
}
func (m *mockedServer) AddOptions(options ...grpc.ServerOption) {
func (m *mockedServer) AddOptions(_ ...grpc.ServerOption) {
}
func (m *mockedServer) AddStreamInterceptors(interceptors ...grpc.StreamServerInterceptor) {
@@ -132,9 +133,9 @@ func (m *mockedServer) AddUnaryInterceptors(interceptors ...grpc.UnaryServerInte
m.unaryInterceptors = append(m.unaryInterceptors, interceptors...)
}
func (m *mockedServer) SetName(s string) {
func (m *mockedServer) SetName(_ string) {
}
func (m *mockedServer) Start(register internal.RegisterFn) error {
func (m *mockedServer) Start(_ internal.RegisterFn) error {
return nil
}