feat: slow threshold customizable in zrpc (#1191)
* feat: slow threshold customizable in rest * feat: slow threshold customizable in rest * feat: slow threshold customizable in rest * feat: slow threshold customizable in zrpc
This commit is contained in:
@@ -6,11 +6,14 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/logx"
|
||||
"github.com/tal-tech/go-zero/core/syncx"
|
||||
"github.com/tal-tech/go-zero/core/timex"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
const slowThreshold = time.Millisecond * 500
|
||||
const defaultSlowThreshold = time.Millisecond * 500
|
||||
|
||||
var slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
|
||||
|
||||
// DurationInterceptor is an interceptor that logs the processing time.
|
||||
func DurationInterceptor(ctx context.Context, method string, req, reply interface{},
|
||||
@@ -23,7 +26,7 @@ func DurationInterceptor(ctx context.Context, method string, req, reply interfac
|
||||
serverName, req, err.Error())
|
||||
} else {
|
||||
elapsed := timex.Since(start)
|
||||
if elapsed > slowThreshold {
|
||||
if elapsed > slowThreshold.Load() {
|
||||
logx.WithContext(ctx).WithDuration(elapsed).Slowf("[RPC] ok - slowcall - %s - %v - %v",
|
||||
serverName, req, reply)
|
||||
}
|
||||
@@ -31,3 +34,8 @@ func DurationInterceptor(ctx context.Context, method string, req, reply interfac
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// SetSlowThreshold sets the slow threshold.
|
||||
func SetSlowThreshold(threshold time.Duration) {
|
||||
slowThreshold.Set(threshold)
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"google.golang.org/grpc"
|
||||
@@ -35,3 +36,9 @@ func TestDurationInterceptor(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetSlowThreshold(t *testing.T) {
|
||||
assert.Equal(t, defaultSlowThreshold, slowThreshold.Load())
|
||||
SetSlowThreshold(time.Second)
|
||||
assert.Equal(t, time.Second, slowThreshold.Load())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user