From 790302b486ae30d79f7c3bb68d26b34eaaed1cc1 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Fri, 14 Feb 2025 23:14:57 +0800 Subject: [PATCH] fix: should not ignore slowThreshold (#4655) --- zrpc/internal/serverinterceptors/statinterceptor.go | 5 +++-- zrpc/internal/serverinterceptors/statinterceptor_test.go | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/zrpc/internal/serverinterceptors/statinterceptor.go b/zrpc/internal/serverinterceptors/statinterceptor.go index 874d84b09..b98721f55 100644 --- a/zrpc/internal/serverinterceptors/statinterceptor.go +++ b/zrpc/internal/serverinterceptors/statinterceptor.go @@ -25,7 +25,7 @@ var ( // StatConf defines the static configuration for stat interceptor. type StatConf struct { - SlowThreshold time.Duration `json:",optional"` + SlowThreshold time.Duration `json:",default=500ms"` IgnoreContentMethods []string `json:",optional"` } @@ -63,7 +63,8 @@ func UnaryStatInterceptor(metrics *stat.Metrics, conf StatConf) grpc.UnaryServer } func isSlow(duration, durationThreshold time.Duration) bool { - return durationThreshold > 0 && duration > durationThreshold + return duration > slowThreshold.Load() || + (durationThreshold > 0 && duration > durationThreshold) } func logDuration(ctx context.Context, method string, req any, duration time.Duration, diff --git a/zrpc/internal/serverinterceptors/statinterceptor_test.go b/zrpc/internal/serverinterceptors/statinterceptor_test.go index 8205155da..39fd8ba88 100644 --- a/zrpc/internal/serverinterceptors/statinterceptor_test.go +++ b/zrpc/internal/serverinterceptors/statinterceptor_test.go @@ -229,7 +229,7 @@ func Test_isSlow(t *testing.T) { args{ duration: time.Millisecond * 501, }, - false, + true, nil, }, { @@ -251,7 +251,8 @@ func Test_isSlow(t *testing.T) { t.Cleanup(func() { slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold) }) - assert.Equalf(t, tt.want, isSlow(tt.args.duration, tt.args.staticSlowThreshold), "isSlow(%v, %v)", tt.args.duration, tt.args.staticSlowThreshold) + assert.Equalf(t, tt.want, isSlow(tt.args.duration, tt.args.staticSlowThreshold), + "isSlow(%v, %v)", tt.args.duration, tt.args.staticSlowThreshold) }) } }