Debian Codegraph @

pkg/endpoints/request/webhook_duration.go

raw
1 /*
2 Copyright 2021 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package request
18
19 import (
20 "context"
21 "sync"
22 "time"
23
24 "k8s.io/utils/clock"
25 )
26
27 func sumDuration(d1 time.Duration, d2 time.Duration) time.Duration {
28 return d1 + d2
29 }
30
31 func maxDuration(d1 time.Duration, d2 time.Duration) time.Duration {
32 if d1 > d2 {
33 return d1
34 }
35 return d2
36 }
37
38 // DurationTracker is a simple interface for tracking functions duration,
39 // it is safe for concurrent use by multiple goroutines.
40 type DurationTracker interface {
41 // Track measures time spent in the given function f and
42 // aggregates measured duration using aggregateFunction.
43 // if Track is invoked with f from multiple goroutines concurrently,
44 // then f must be safe to be invoked concurrently by multiple goroutines.
45 Track(f func())
46
47 // TrackDuration tracks latency from the specified duration
48 // and aggregate it using aggregateFunction
49 TrackDuration(time.Duration)
50
51 // GetLatency returns the total latency incurred so far
52 GetLatency() time.Duration
53 }
54
55 // durationTracker implements DurationTracker by measuring function time
56 // using given clock and aggregates the duration using given aggregate function
57 type durationTracker struct {
58 clock clock.Clock
59 latency time.Duration
60 mu sync.Mutex
61 aggregateFunction func(time.Duration, time.Duration) time.Duration
62 }
63
64 // Track measures time spent in given function and aggregates measured
65 // duration using aggregateFunction
66 func (t *durationTracker) Track(f func()) {
67 startedAt := t.clock.Now()
68 defer func() {
69 duration := t.clock.Since(startedAt)
70 t.mu.Lock()
71 defer t.mu.Unlock()
72 t.latency = t.aggregateFunction(t.latency, duration)
73 }()
74
75 f()
76 }
77
78 // TrackDuration tracks latency from the given duration
79 // using aggregateFunction
80 func (t *durationTracker) TrackDuration(d time.Duration) {
81 t.mu.Lock()
82 defer t.mu.Unlock()
83 t.latency = t.aggregateFunction(t.latency, d)
84 }
85
86 // GetLatency returns aggregated latency tracked by a tracker
87 func (t *durationTracker) GetLatency() time.Duration {
88 t.mu.Lock()
89 defer t.mu.Unlock()
90 return t.latency
91 }
92
93 func newSumLatencyTracker(c clock.Clock) DurationTracker {
94 return &durationTracker{
95 clock: c,
96 aggregateFunction: sumDuration,
97 }
98 }
99
100 func newMaxLatencyTracker(c clock.Clock) DurationTracker {
101 return &durationTracker{
102 clock: c,
103 aggregateFunction: maxDuration,
104 }
105 }
106
107 // LatencyTrackers stores trackers used to measure latecny incurred in
108 // components within the apiserver.
109 type LatencyTrackers struct {
110 // MutatingWebhookTracker tracks the latency incurred in mutating webhook(s).
111 // Since mutating webhooks are done sequentially, latency
112 // is aggregated using sum function.
113 MutatingWebhookTracker DurationTracker
114
115 // ValidatingWebhookTracker tracks the latency incurred in validating webhook(s).
116 // Validate webhooks are done in parallel, so max function is used.
117 ValidatingWebhookTracker DurationTracker
118
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
125 // APFQueueWaitTracker tracks the latency incurred by queue wait times
126 // from priority & fairness.
127 APFQueueWaitTracker DurationTracker
128
129 // StorageTracker tracks the latency incurred inside the storage layer,
130 // it accounts for the time it takes to send data to the underlying
131 // storage layer (etcd) and get the complete response back.
132 // If a request involves N (N>=1) round trips to the underlying
133 // stogare layer, the latency will account for the total duration
134 // from these N round trips.
135 // It does not include the time incurred in admission, or validation.
136 StorageTracker DurationTracker
137
138 // TransformTracker tracks the latency incurred in transforming the
139 // response object(s) returned from the underlying storage layer.
140 // This includes transforming the object to user's desired form
141 // (ie. as Table), and also setting appropriate API level fields.
142 // This does not include the latency incurred in serialization
143 // (json or protobuf) of the response object or writing
144 // of it to the http ResponseWriter object.
145 TransformTracker DurationTracker
146
147 // SerializationTracker tracks the latency incurred in serialization
148 // (json or protobuf) of the response object.
149 // NOTE: serialization and writing of the serialized raw bytes to the
150 // associated http ResponseWriter object are interleaved, and hence
151 // the latency measured here will include the time spent writing the
152 // serialized raw bytes to the http ResponseWriter object.
153 SerializationTracker DurationTracker
154
155 // ResponseWriteTracker tracks the latency incurred in writing the
156 // serialized raw bytes to the http ResponseWriter object (via the
157 // Write method) associated with the request.
158 // The Write method can be invoked multiple times, so we use a
159 // latency tracker that sums up the duration from each call.
160 ResponseWriteTracker DurationTracker
161
162 // DecodeTracker is used to track latency incurred inside the function
163 // that takes an object returned from the underlying storage layer
164 // (etcd) and performs decoding of the response object.
165 // When called multiple times, the latency incurred inside to
166 // decode func each time will be summed up.
167 DecodeTracker DurationTracker
168 }
169
170 type latencyTrackersKeyType int
171
172 // latencyTrackersKey is the key that associates a LatencyTrackers
173 // instance with the request context.
174 const latencyTrackersKey latencyTrackersKeyType = iota
175
176 // WithLatencyTrackers returns a copy of parent context to which an
177 // instance of LatencyTrackers is added.
178 func WithLatencyTrackers(parent context.Context) context.Context {
179 return WithLatencyTrackersAndCustomClock(parent, clock.RealClock{})
180 }
181
182 // WithLatencyTrackersAndCustomClock returns a copy of parent context to which
183 // an instance of LatencyTrackers is added. Tracers use given clock.
184 func WithLatencyTrackersAndCustomClock(parent context.Context, c clock.Clock) context.Context {
185 return WithValue(parent, latencyTrackersKey, &LatencyTrackers{
186 MutatingWebhookTracker: newSumLatencyTracker(c),
187 ValidatingWebhookTracker: newMaxLatencyTracker(c),
188 AuthenticationTracker: newSumLatencyTracker(c),
189 AuthorizationTracker: newMaxLatencyTracker(c),
190 APFQueueWaitTracker: newMaxLatencyTracker(c),
191 StorageTracker: newSumLatencyTracker(c),
192 TransformTracker: newSumLatencyTracker(c),
193 SerializationTracker: newSumLatencyTracker(c),
194 ResponseWriteTracker: newSumLatencyTracker(c),
195 DecodeTracker: newSumLatencyTracker(c),
196 })
197 }
198
199 // LatencyTrackersFrom returns the associated LatencyTrackers instance
200 // from the specified context.
201 func LatencyTrackersFrom(ctx context.Context) (*LatencyTrackers, bool) {
202 wd, ok := ctx.Value(latencyTrackersKey).(*LatencyTrackers)
203 return wd, ok && wd != nil
204 }
205
206 // TrackTransformResponseObjectLatency is used to track latency incurred
207 // inside the function that takes an object returned from the underlying
208 // storage layer (etcd) and performs any necessary transformations
209 // of the response object. This does not include the latency incurred in
210 // serialization (json or protobuf) of the response object or writing of
211 // it to the http ResponseWriter object.
212 // When called multiple times, the latency incurred inside the
213 // transform func each time will be summed up.
214 func TrackTransformResponseObjectLatency(ctx context.Context, transform func()) {
215 if tracker, ok := LatencyTrackersFrom(ctx); ok {
216 tracker.TransformTracker.Track(transform)
217 return
218 }
219
220 transform()
221 }
222
223 // TrackStorageLatency is used to track latency incurred
224 // inside the underlying storage layer.
225 // When called multiple times, the latency provided will be summed up.
226 func TrackStorageLatency(ctx context.Context, d time.Duration) {
227 if tracker, ok := LatencyTrackersFrom(ctx); ok {
228 tracker.StorageTracker.TrackDuration(d)
229 }
230 }
231
232 // TrackSerializeResponseObjectLatency is used to track latency incurred in
233 // serialization (json or protobuf) of the response object.
234 // When called multiple times, the latency provided will be summed up.
235 func TrackSerializeResponseObjectLatency(ctx context.Context, f func()) {
236 if tracker, ok := LatencyTrackersFrom(ctx); ok {
237 tracker.SerializationTracker.Track(f)
238 return
239 }
240
241 f()
242 }
243
244 // TrackResponseWriteLatency is used to track latency incurred in writing
245 // the serialized raw bytes to the http ResponseWriter object (via the
246 // Write method) associated with the request.
247 // When called multiple times, the latency provided will be summed up.
248 func TrackResponseWriteLatency(ctx context.Context, d time.Duration) {
249 if tracker, ok := LatencyTrackersFrom(ctx); ok {
250 tracker.ResponseWriteTracker.TrackDuration(d)
251 }
252 }
253
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
270 // TrackAPFQueueWaitLatency is used to track latency incurred
271 // by priority and fairness queues.
272 func TrackAPFQueueWaitLatency(ctx context.Context, d time.Duration) {
273 if tracker, ok := LatencyTrackersFrom(ctx); ok {
274 tracker.APFQueueWaitTracker.TrackDuration(d)
275 }
276 }
277
278 // TrackDecodeLatency is used to track latency incurred inside the function
279 // that takes an object returned from the underlying storage layer
280 // (etcd) and performs decoding of the response object.
281 // When called multiple times, the latency incurred inside to
282 // decode func each time will be summed up.
283 func TrackDecodeLatency(ctx context.Context, d time.Duration) {
284 if tracker, ok := LatencyTrackersFrom(ctx); ok {
285 tracker.DecodeTracker.TrackDuration(d)
286 }
287 }
288
289 // AuditAnnotationsFromLatencyTrackers will inspect each latency tracker
290 // associated with the request context and return a set of audit
291 // annotations that can be added to the API audit entry.
292 func AuditAnnotationsFromLatencyTrackers(ctx context.Context) map[string]string {
293 const (
294 transformLatencyKey = "apiserver.latency.k8s.io/transform-response-object"
295 storageLatencyKey = "apiserver.latency.k8s.io/etcd"
296 serializationLatencyKey = "apiserver.latency.k8s.io/serialize-response-object"
297 responseWriteLatencyKey = "apiserver.latency.k8s.io/response-write"
298 mutatingWebhookLatencyKey = "apiserver.latency.k8s.io/mutating-webhook"
299 validatingWebhookLatencyKey = "apiserver.latency.k8s.io/validating-webhook"
300 decodeLatencyKey = "apiserver.latency.k8s.io/decode-response-object"
301 apfQueueWaitLatencyKey = "apiserver.latency.k8s.io/apf-queue-wait"
302 authenticationLatencyKey = "apiserver.latency.k8s.io/authentication"
303 authorizationLatencyKey = "apiserver.latency.k8s.io/authorization"
304 )
305
306 tracker, ok := LatencyTrackersFrom(ctx)
307 if !ok {
308 return nil
309 }
310
311 annotations := map[string]string{}
312 if latency := tracker.TransformTracker.GetLatency(); latency != 0 {
313 annotations[transformLatencyKey] = latency.String()
314 }
315 if latency := tracker.StorageTracker.GetLatency(); latency != 0 {
316 annotations[storageLatencyKey] = latency.String()
317 }
318 if latency := tracker.SerializationTracker.GetLatency(); latency != 0 {
319 annotations[serializationLatencyKey] = latency.String()
320 }
321 if latency := tracker.ResponseWriteTracker.GetLatency(); latency != 0 {
322 annotations[responseWriteLatencyKey] = latency.String()
323 }
324 if latency := tracker.MutatingWebhookTracker.GetLatency(); latency != 0 {
325 annotations[mutatingWebhookLatencyKey] = latency.String()
326 }
327 if latency := tracker.ValidatingWebhookTracker.GetLatency(); latency != 0 {
328 annotations[validatingWebhookLatencyKey] = latency.String()
329 }
330 if latency := tracker.DecodeTracker.GetLatency(); latency != 0 {
331 annotations[decodeLatencyKey] = latency.String()
332 }
333 if latency := tracker.APFQueueWaitTracker.GetLatency(); latency != 0 {
334 annotations[apfQueueWaitLatencyKey] = latency.String()
335 }
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 }
342 return annotations
343 }