fix: SlowThreshold configuration not taking effect (#4654)

This commit is contained in:
Nanosk07
2025-02-14 22:56:25 +08:00
committed by GitHub
parent 560c61612c
commit 6a0672b801
2 changed files with 26 additions and 33 deletions

View File

@@ -25,7 +25,7 @@ var (
// 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 `json:",default=500ms"` SlowThreshold time.Duration `json:",optional"`
IgnoreContentMethods []string `json:",optional"` IgnoreContentMethods []string `json:",optional"`
} }
@@ -63,8 +63,7 @@ func UnaryStatInterceptor(metrics *stat.Metrics, conf StatConf) grpc.UnaryServer
} }
func isSlow(duration, durationThreshold time.Duration) bool { func isSlow(duration, durationThreshold time.Duration) bool {
return duration > slowThreshold.Load() || return durationThreshold > 0 && duration > durationThreshold
(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,
@@ -84,7 +83,7 @@ func logDuration(ctx context.Context, method string, req any, duration time.Dura
content, err := json.Marshal(req) content, err := json.Marshal(req)
if err != nil { if err != nil {
logx.WithContext(ctx).Errorf("%s - %s", addr, err.Error()) logx.WithContext(ctx).Errorf("%s - %s", addr, err.Error())
} else if duration > slowThreshold.Load() { } else if isSlow(duration, durationThreshold) {
logger.Slowf("[RPC] slowcall - %s - %s - %s", addr, method, string(content)) logger.Slowf("[RPC] slowcall - %s - %s - %s", addr, method, string(content))
} else { } else {
logger.Infof("%s - %s - %s", addr, method, string(content)) logger.Infof("%s - %s - %s", addr, method, string(content))

View File

@@ -47,6 +47,7 @@ func TestLogDuration(t *testing.T) {
ctx context.Context ctx context.Context
req any req any
duration time.Duration duration time.Duration
durationThreshold time.Duration
}{ }{
{ {
name: "normal", name: "normal",
@@ -63,6 +64,7 @@ func TestLogDuration(t *testing.T) {
ctx: context.Background(), ctx: context.Background(),
req: "foo", req: "foo",
duration: time.Second, duration: time.Second,
durationThreshold: time.Millisecond * 500,
}, },
{ {
name: "timeout", name: "timeout",
@@ -86,7 +88,7 @@ func TestLogDuration(t *testing.T) {
assert.NotPanics(t, func() { assert.NotPanics(t, func() {
logDuration(test.ctx, "foo", test.req, test.duration, logDuration(test.ctx, "foo", test.req, test.duration,
collection.NewSet(), 0) collection.NewSet(), test.durationThreshold)
}) })
}) })
} }
@@ -102,6 +104,7 @@ func TestLogDurationWithoutContent(t *testing.T) {
ctx context.Context ctx context.Context
req any req any
duration time.Duration duration time.Duration
durationThreshold time.Duration
}{ }{
{ {
name: "normal", name: "normal",
@@ -118,6 +121,7 @@ func TestLogDurationWithoutContent(t *testing.T) {
ctx: context.Background(), ctx: context.Background(),
req: "foo", req: "foo",
duration: time.Second, duration: time.Second,
durationThreshold: time.Millisecond * 500,
}, },
{ {
name: "timeout", name: "timeout",
@@ -146,7 +150,7 @@ func TestLogDurationWithoutContent(t *testing.T) {
assert.NotPanics(t, func() { assert.NotPanics(t, func() {
logDuration(test.ctx, "foo", test.req, test.duration, logDuration(test.ctx, "foo", test.req, test.duration,
collection.NewSet(), 0) collection.NewSet(), test.durationThreshold)
}) })
}) })
} }
@@ -225,7 +229,7 @@ func Test_isSlow(t *testing.T) {
args{ args{
duration: time.Millisecond * 501, duration: time.Millisecond * 501,
}, },
true, false,
nil, nil,
}, },
{ {
@@ -237,16 +241,6 @@ func Test_isSlow(t *testing.T) {
true, true,
nil, nil,
}, },
{
"dynamic",
args{
duration: time.Millisecond * 200,
},
true,
func() {
SetSlowThreshold(time.Millisecond * 100)
},
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {