Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve rate limiter latency logging and add component-base metric #88134

Merged
merged 1 commit into from Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions staging/src/k8s.io/client-go/rest/BUILD
Expand Up @@ -65,6 +65,7 @@ go_library(
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime/serializer/streaming:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/clock:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/net:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/watch:go_default_library",
Expand Down
67 changes: 50 additions & 17 deletions staging/src/k8s.io/client-go/rest/request.go
Expand Up @@ -39,6 +39,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer/streaming"
utilclock "k8s.io/apimachinery/pkg/util/clock"
"k8s.io/apimachinery/pkg/util/net"
"k8s.io/apimachinery/pkg/watch"
restclientwatch "k8s.io/client-go/rest/watch"
Expand Down Expand Up @@ -556,37 +557,69 @@ func (r *Request) tryThrottle(ctx context.Context) error {
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()))
// If the rate limiter latency is very high, the log message should be printed at a higher log level,
// but we use a throttled logger to prevent spamming.
globalThrottledLogger.Infof("Throttling request took %v, request: %s:%s", latency, r.verb, r.URL().String())
}
metrics.RateLimiterLatency.Observe(r.verb, r.finalURLTemplate(), latency)

return err
}

type throttledLogger struct {
logTimeLock sync.RWMutex
lastLogTime time.Time
type throttleSettings struct {
logLevel klog.Level
minLogInterval time.Duration

lastLogTime time.Time
lock sync.RWMutex
}

type throttledLogger struct {
clock utilclock.PassiveClock
settings []*throttleSettings
}

var globalThrottledLogger = &throttledLogger{
minLogInterval: 1 * time.Second,
clock: utilclock.RealClock{},
settings: []*throttleSettings{
{
logLevel: 2,
minLogInterval: 1 * time.Second,
}, {
logLevel: 0,
minLogInterval: 10 * 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()
func (b *throttledLogger) attemptToLog() (klog.Level, bool) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a docstring on the return values. My first super quick skim, I was super confused by the -1 (it makes sense once I read the code though).

for _, setting := range b.settings {
if bool(klog.V(setting.logLevel)) {
// Return early without write locking if possible.
if func() bool {
setting.lock.RLock()
defer setting.lock.RUnlock()
return b.clock.Since(setting.lastLogTime) >= setting.minLogInterval
}() {
setting.lock.Lock()
defer setting.lock.Unlock()
if b.clock.Since(setting.lastLogTime) >= setting.minLogInterval {
setting.lastLogTime = b.clock.Now()
return setting.logLevel, true
}
}
return -1, false
}
}
return -1, false
}

// Infof will write a log message at each logLevel specified by the reciever's throttleSettings
// as long as it hasn't written a log message more recently than minLogInterval.
func (b *throttledLogger) Infof(message string, args ...interface{}) {
if logLevel, ok := b.attemptToLog(); ok {
klog.V(logLevel).Infof(message, args...)
}
}

// Watch attempts to begin watching the requested location.
Expand Down
28 changes: 28 additions & 0 deletions staging/src/k8s.io/client-go/rest/request_test.go
Expand Up @@ -31,6 +31,7 @@ import (
"os"
"reflect"
"strings"
"sync"
"syscall"
"testing"
"time"
Expand Down Expand Up @@ -2190,3 +2191,30 @@ func TestRequestPreflightCheck(t *testing.T) {
})
}
}

func TestThrottledLogger(t *testing.T) {
now := time.Now()
clock := clock.NewFakeClock(now)
globalThrottledLogger.clock = clock

logMessages := 0
for i := 0; i < 10000; i++ {
var wg sync.WaitGroup
wg.Add(100)
for j := 0; j < 100; j++ {
go func() {
if _, ok := globalThrottledLogger.attemptToLog(); ok {
logMessages++
}
wg.Done()
}()
}
wg.Wait()
now = now.Add(1 * time.Second)
clock.SetTime(now)
}

if a, e := logMessages, 1000; a != e {
t.Fatalf("expected %v log messages, but got %v", e, a)
}
}
6 changes: 6 additions & 0 deletions staging/src/k8s.io/client-go/tools/metrics/metrics.go
Expand Up @@ -53,6 +53,8 @@ var (
ClientCertRotationAge DurationMetric = noopDuration{}
// RequestLatency is the latency metric that rest clients will update.
RequestLatency LatencyMetric = noopLatency{}
// RateLimiterLatency is the client side rate limiter latency metric.
RateLimiterLatency LatencyMetric = noopLatency{}
// RequestResult is the result metric that rest clients will update.
RequestResult ResultMetric = noopResult{}
)
Expand All @@ -62,6 +64,7 @@ type RegisterOpts struct {
ClientCertExpiry ExpiryMetric
ClientCertRotationAge DurationMetric
RequestLatency LatencyMetric
RateLimiterLatency LatencyMetric
RequestResult ResultMetric
}

Expand All @@ -78,6 +81,9 @@ func Register(opts RegisterOpts) {
if opts.RequestLatency != nil {
RequestLatency = opts.RequestLatency
}
if opts.RateLimiterLatency != nil {
RateLimiterLatency = opts.RateLimiterLatency
}
if opts.RequestResult != nil {
RequestResult = opts.RequestResult
}
Expand Down
Expand Up @@ -38,6 +38,15 @@ var (
[]string{"verb", "url"},
)

rateLimiterLatency = k8smetrics.NewHistogramVec(
&k8smetrics.HistogramOpts{
Name: "rest_client_rate_limiter_duration_seconds",
Help: "Client side rate limiter latency in seconds. Broken down by verb and URL.",
Buckets: k8smetrics.ExponentialBuckets(0.001, 2, 10),
},
[]string{"verb", "url"},
)

requestResult = k8smetrics.NewCounterVec(
&k8smetrics.CounterOpts{
Name: "rest_client_requests_total",
Expand Down Expand Up @@ -106,6 +115,7 @@ func init() {
ClientCertExpiry: execPluginCertTTLAdapter,
ClientCertRotationAge: &rotationAdapter{m: execPluginCertRotation},
RequestLatency: &latencyAdapter{m: requestLatency},
RateLimiterLatency: &latencyAdapter{m: rateLimiterLatency},
RequestResult: &resultAdapter{requestResult},
})
}
Expand Down