refactor: simplify zrpc stat config (#3107)

This commit is contained in:
Kevin Wan
2023-04-08 22:45:05 +08:00
committed by GitHub
parent e13fd62d38
commit 98c9b5928a
6 changed files with 36 additions and 35 deletions

View File

@@ -15,6 +15,8 @@ type (
ClientMiddlewaresConf = internal.ClientMiddlewaresConf ClientMiddlewaresConf = internal.ClientMiddlewaresConf
// ServerMiddlewaresConf defines whether to use server middlewares. // ServerMiddlewaresConf defines whether to use server middlewares.
ServerMiddlewaresConf = internal.ServerMiddlewaresConf ServerMiddlewaresConf = internal.ServerMiddlewaresConf
// StatConf defines the stat config.
StatConf = internal.StatConf
// A RpcClientConf is a rpc client config. // A RpcClientConf is a rpc client config.
RpcClientConf struct { RpcClientConf struct {

View File

@@ -1,8 +1,11 @@
package internal package internal
import "time" import "github.com/zeromicro/go-zero/zrpc/internal/serverinterceptors"
type ( type (
// StatConf defines the stat config.
StatConf = serverinterceptors.StatConf
// ClientMiddlewaresConf defines whether to use client middlewares. // ClientMiddlewaresConf defines whether to use client middlewares.
ClientMiddlewaresConf struct { ClientMiddlewaresConf struct {
Trace bool `json:",default=true"` Trace bool `json:",default=true"`
@@ -14,12 +17,11 @@ type (
// ServerMiddlewaresConf defines whether to use server middlewares. // ServerMiddlewaresConf defines whether to use server middlewares.
ServerMiddlewaresConf struct { ServerMiddlewaresConf struct {
Trace bool `json:",default=true"` Trace bool `json:",default=true"`
Recover bool `json:",default=true"` Recover bool `json:",default=true"`
Stat bool `json:",default=true"` Stat bool `json:",default=true"`
StatSlowThreshold time.Duration `json:",default=500ms"` StatConf StatConf `json:",optional"`
NotLoggingContentMethods []string `json:",optional"` Prometheus bool `json:",default=true"`
Prometheus bool `json:",default=true"` Breaker bool `json:",default=true"`
Breaker bool `json:",default=true"`
} }
) )

View File

@@ -113,11 +113,8 @@ func (s *rpcServer) buildUnaryInterceptors() []grpc.UnaryServerInterceptor {
interceptors = append(interceptors, serverinterceptors.UnaryRecoverInterceptor) interceptors = append(interceptors, serverinterceptors.UnaryRecoverInterceptor)
} }
if s.middlewares.Stat { if s.middlewares.Stat {
interceptors = append(interceptors, serverinterceptors.UnaryStatInterceptor(s.metrics, interceptors = append(interceptors,
serverinterceptors.StatConf{ serverinterceptors.UnaryStatInterceptor(s.metrics, s.middlewares.StatConf))
SlowThreshold: s.middlewares.StatSlowThreshold,
NotLoggingContentMethods: s.middlewares.NotLoggingContentMethods,
}))
} }
if s.middlewares.Prometheus { if s.middlewares.Prometheus {
interceptors = append(interceptors, serverinterceptors.UnaryPrometheusInterceptor) interceptors = append(interceptors, serverinterceptors.UnaryPrometheusInterceptor)

View File

@@ -19,20 +19,20 @@ import (
const defaultSlowThreshold = time.Millisecond * 500 const defaultSlowThreshold = time.Millisecond * 500
var ( var (
notLoggingContentMethods sync.Map ignoreContentMethods sync.Map
slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold) slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
) )
// StatConf defines the static configuration for stat interceptor. // StatConf defines the static configuration for stat interceptor.
type StatConf struct { type StatConf struct {
SlowThreshold time.Duration SlowThreshold time.Duration `json:",default=500ms"`
NotLoggingContentMethods []string IgnoreContentMethods []string `json:",optional"`
} }
// DontLogContentForMethod disable logging content for given method. // DontLogContentForMethod disable logging content for given method.
// Deprecated: use StatConf instead. // Deprecated: use StatConf instead.
func DontLogContentForMethod(method string) { func DontLogContentForMethod(method string) {
notLoggingContentMethods.Store(method, lang.Placeholder) ignoreContentMethods.Store(method, lang.Placeholder)
} }
// SetSlowThreshold sets the slow threshold. // SetSlowThreshold sets the slow threshold.
@@ -44,7 +44,7 @@ func SetSlowThreshold(threshold time.Duration) {
// UnaryStatInterceptor returns a func that uses given metrics to report stats. // UnaryStatInterceptor returns a func that uses given metrics to report stats.
func UnaryStatInterceptor(metrics *stat.Metrics, conf StatConf) grpc.UnaryServerInterceptor { func UnaryStatInterceptor(metrics *stat.Metrics, conf StatConf) grpc.UnaryServerInterceptor {
staticNotLoggingContentMethods := collection.NewSet() staticNotLoggingContentMethods := collection.NewSet()
staticNotLoggingContentMethods.AddStr(conf.NotLoggingContentMethods...) staticNotLoggingContentMethods.AddStr(conf.IgnoreContentMethods...)
return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, return func(ctx context.Context, req any, info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (resp any, err error) { handler grpc.UnaryHandler) (resp any, err error) {
@@ -62,8 +62,13 @@ func UnaryStatInterceptor(metrics *stat.Metrics, conf StatConf) grpc.UnaryServer
} }
} }
func isSlow(duration, durationThreshold time.Duration) bool {
return duration > slowThreshold.Load() ||
(durationThreshold > 0 && duration > durationThreshold)
}
func logDuration(ctx context.Context, method string, req any, duration time.Duration, func logDuration(ctx context.Context, method string, req any, duration time.Duration,
staticNotLoggingContentMethods *collection.Set, staticSlowThreshold time.Duration) { ignoreMethods *collection.Set, durationThreshold time.Duration) {
var addr string var addr string
client, ok := peer.FromContext(ctx) client, ok := peer.FromContext(ctx)
if ok { if ok {
@@ -71,8 +76,8 @@ func logDuration(ctx context.Context, method string, req any, duration time.Dura
} }
logger := logx.WithContext(ctx).WithDuration(duration) logger := logx.WithContext(ctx).WithDuration(duration)
if !shouldLogContent(method, staticNotLoggingContentMethods) { if !shouldLogContent(method, ignoreMethods) {
if isSlow(duration, staticSlowThreshold) { if isSlow(duration, durationThreshold) {
logger.Slowf("[RPC] slowcall - %s - %s", addr, method) logger.Slowf("[RPC] slowcall - %s - %s", addr, method)
} }
} else { } else {
@@ -87,12 +92,7 @@ func logDuration(ctx context.Context, method string, req any, duration time.Dura
} }
} }
func shouldLogContent(method string, staticNotLoggingContentMethods *collection.Set) bool { func shouldLogContent(method string, ignoreMethods *collection.Set) bool {
_, ok := notLoggingContentMethods.Load(method) _, ok := ignoreContentMethods.Load(method)
return !ok && !staticNotLoggingContentMethods.Contains(method) return !ok && !ignoreMethods.Contains(method)
}
func isSlow(duration time.Duration, staticSlowThreshold time.Duration) bool {
return duration > slowThreshold.Load() ||
(staticSlowThreshold > 0 && duration > staticSlowThreshold)
} }

View File

@@ -135,9 +135,9 @@ func TestLogDurationWithoutContent(t *testing.T) {
} }
DontLogContentForMethod("foo") DontLogContentForMethod("foo")
// reset notLoggingContentMethods // reset ignoreContentMethods
t.Cleanup(func() { t.Cleanup(func() {
notLoggingContentMethods = sync.Map{} ignoreContentMethods = sync.Map{}
}) })
for _, test := range tests { for _, test := range tests {
test := test test := test
@@ -198,9 +198,9 @@ func Test_shouldLogContent(t *testing.T) {
if tt.setup != nil { if tt.setup != nil {
tt.setup() tt.setup()
} }
// reset notLoggingContentMethods // reset ignoreContentMethods
t.Cleanup(func() { t.Cleanup(func() {
notLoggingContentMethods = sync.Map{} ignoreContentMethods = sync.Map{}
}) })
set := collection.NewSet() set := collection.NewSet()
set.AddStr(tt.args.staticNotLoggingContentMethods...) set.AddStr(tt.args.staticNotLoggingContentMethods...)

View File

@@ -100,7 +100,7 @@ func (rs *RpcServer) Stop() {
} }
// DontLogContentForMethod disable logging content for given method. // DontLogContentForMethod disable logging content for given method.
// Deprecated: use ServerMiddlewaresConf.NotLoggingContentMethods instead. // Deprecated: use ServerMiddlewaresConf.IgnoreContentMethods instead.
func DontLogContentForMethod(method string) { func DontLogContentForMethod(method string) {
serverinterceptors.DontLogContentForMethod(method) serverinterceptors.DontLogContentForMethod(method)
} }