Skip to content

Commit 2b025e6

Browse files
authored
Merge pull request kubernetes#130571 from hakuna-matatah/debug-upstream
Help debug latencies in authn and authz phase of Request
2 parents c496aef + 186e6ee commit 2b025e6

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

staging/src/k8s.io/apiserver/pkg/endpoints/filters/authentication.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func withAuthentication(handler http.Handler, auth authenticator.Request, failed
6868
authenticationFinish := time.Now()
6969
defer func() {
7070
metrics(req.Context(), resp, ok, err, apiAuds, authenticationStart, authenticationFinish)
71+
genericapirequest.TrackAuthenticationLatency(req.Context(), authenticationFinish.Sub(authenticationStart))
7172
}()
7273
if err != nil || !ok {
7374
if err != nil {
@@ -118,7 +119,6 @@ func withAuthentication(handler http.Handler, auth authenticator.Request, failed
118119
// https://github.com/golang/net/commit/97aa3a539ec716117a9d15a4659a911f50d13c3c
119120
w.Header().Set("Connection", "close")
120121
}
121-
122122
req = req.WithContext(genericapirequest.WithUser(req.Context(), resp.User))
123123
handler.ServeHTTP(w, req)
124124
})

staging/src/k8s.io/apiserver/pkg/endpoints/filters/authorization.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func withAuthorization(handler http.Handler, a authorizer.Authorizer, s runtime.
7373
authorizationFinish := time.Now()
7474
defer func() {
7575
metrics(ctx, authorized, err, authorizationStart, authorizationFinish)
76+
request.TrackAuthorizationLatency(ctx, authorizationFinish.Sub(authorizationStart))
7677
}()
7778

7879
// an authorizer like RBAC could encounter evaluation errors and still allow the request, so authorizer decision is checked before error here.

staging/src/k8s.io/apiserver/pkg/endpoints/request/webhook_duration.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ type LatencyTrackers struct {
116116
// Validate webhooks are done in parallel, so max function is used.
117117
ValidatingWebhookTracker DurationTracker
118118

119+
// AuthenticationTracker tracks the latency incurred by Authentication of request
120+
AuthenticationTracker DurationTracker
121+
122+
// AuthorizationTracker tracks the latency incurred by Authorization of request
123+
AuthorizationTracker DurationTracker
124+
119125
// APFQueueWaitTracker tracks the latency incurred by queue wait times
120126
// from priority & fairness.
121127
APFQueueWaitTracker DurationTracker
@@ -179,6 +185,8 @@ func WithLatencyTrackersAndCustomClock(parent context.Context, c clock.Clock) co
179185
return WithValue(parent, latencyTrackersKey, &LatencyTrackers{
180186
MutatingWebhookTracker: newSumLatencyTracker(c),
181187
ValidatingWebhookTracker: newMaxLatencyTracker(c),
188+
AuthenticationTracker: newSumLatencyTracker(c),
189+
AuthorizationTracker: newMaxLatencyTracker(c),
182190
APFQueueWaitTracker: newMaxLatencyTracker(c),
183191
StorageTracker: newSumLatencyTracker(c),
184192
TransformTracker: newSumLatencyTracker(c),
@@ -243,6 +251,22 @@ func TrackResponseWriteLatency(ctx context.Context, d time.Duration) {
243251
}
244252
}
245253

254+
// TrackAuthenticationLatency is used to track latency incurred
255+
// by Authentication phase of request.
256+
func TrackAuthenticationLatency(ctx context.Context, d time.Duration) {
257+
if tracker, ok := LatencyTrackersFrom(ctx); ok {
258+
tracker.AuthenticationTracker.TrackDuration(d)
259+
}
260+
}
261+
262+
// TrackAuthorizationLatency is used to track latency incurred
263+
// by Authorization phase of request.
264+
func TrackAuthorizationLatency(ctx context.Context, d time.Duration) {
265+
if tracker, ok := LatencyTrackersFrom(ctx); ok {
266+
tracker.AuthorizationTracker.TrackDuration(d)
267+
}
268+
}
269+
246270
// TrackAPFQueueWaitLatency is used to track latency incurred
247271
// by priority and fairness queues.
248272
func TrackAPFQueueWaitLatency(ctx context.Context, d time.Duration) {
@@ -275,6 +299,8 @@ func AuditAnnotationsFromLatencyTrackers(ctx context.Context) map[string]string
275299
validatingWebhookLatencyKey = "apiserver.latency.k8s.io/validating-webhook"
276300
decodeLatencyKey = "apiserver.latency.k8s.io/decode-response-object"
277301
apfQueueWaitLatencyKey = "apiserver.latency.k8s.io/apf-queue-wait"
302+
authenticationLatencyKey = "apiserver.latency.k8s.io/authentication"
303+
authorizationLatencyKey = "apiserver.latency.k8s.io/authorization"
278304
)
279305

280306
tracker, ok := LatencyTrackersFrom(ctx)
@@ -307,5 +333,11 @@ func AuditAnnotationsFromLatencyTrackers(ctx context.Context) map[string]string
307333
if latency := tracker.APFQueueWaitTracker.GetLatency(); latency != 0 {
308334
annotations[apfQueueWaitLatencyKey] = latency.String()
309335
}
336+
if latency := tracker.AuthenticationTracker.GetLatency(); latency != 0 {
337+
annotations[authenticationLatencyKey] = latency.String()
338+
}
339+
if latency := tracker.AuthorizationTracker.GetLatency(); latency != 0 {
340+
annotations[authorizationLatencyKey] = latency.String()
341+
}
310342
return annotations
311343
}

0 commit comments

Comments
 (0)