mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 19:31:44 +00:00
Merge pull request #87740 from jennybuckley/rate-limit-log
Log when client rate limiter latency is very high at a lower log level
This commit is contained in:
commit
9de5763473
@ -30,6 +30,7 @@ import (
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/http2"
|
||||
@ -51,6 +52,9 @@ var (
|
||||
// throttled (via the provided rateLimiter) for more than longThrottleLatency will
|
||||
// be logged.
|
||||
longThrottleLatency = 50 * time.Millisecond
|
||||
|
||||
// extraLongThrottleLatency defines the threshold for logging requests at log level 2.
|
||||
extraLongThrottleLatency = 1 * time.Second
|
||||
)
|
||||
|
||||
// HTTPClient is an interface for testing a request object.
|
||||
@ -547,13 +551,44 @@ func (r *Request) tryThrottle(ctx context.Context) error {
|
||||
|
||||
err := r.rateLimiter.Wait(ctx)
|
||||
|
||||
if latency := time.Since(now); latency > longThrottleLatency {
|
||||
latency := time.Since(now)
|
||||
if latency > longThrottleLatency {
|
||||
klog.V(3).Infof("Throttling request took %v, request: %s:%s", latency, r.verb, r.URL().String())
|
||||
}
|
||||
if latency > extraLongThrottleLatency {
|
||||
globalThrottledLogger.Log(2, fmt.Sprintf("Throttling request took %v, request: %s:%s", latency, r.verb, r.URL().String()))
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
type throttledLogger struct {
|
||||
logTimeLock sync.RWMutex
|
||||
lastLogTime time.Time
|
||||
minLogInterval time.Duration
|
||||
}
|
||||
|
||||
var globalThrottledLogger = &throttledLogger{
|
||||
minLogInterval: 1 * time.Second,
|
||||
}
|
||||
|
||||
func (b *throttledLogger) Log(level klog.Level, message string) {
|
||||
if bool(klog.V(level)) {
|
||||
if func() bool {
|
||||
b.logTimeLock.RLock()
|
||||
defer b.logTimeLock.RUnlock()
|
||||
return time.Since(b.lastLogTime) > b.minLogInterval
|
||||
}() {
|
||||
b.logTimeLock.Lock()
|
||||
defer b.logTimeLock.Unlock()
|
||||
if time.Since(b.lastLogTime) > b.minLogInterval {
|
||||
klog.V(level).Info(message)
|
||||
b.lastLogTime = time.Now()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Watch attempts to begin watching the requested location.
|
||||
// Returns a watch.Interface, or an error.
|
||||
func (r *Request) Watch(ctx context.Context) (watch.Interface, error) {
|
||||
|
Loading…
Reference in New Issue
Block a user