1
16
17 package kuberuntime
18
19 import (
20 "context"
21 "time"
22
23 internalapi "k8s.io/cri-api/pkg/apis"
24 runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
25 "k8s.io/kubernetes/pkg/kubelet/metrics"
26 )
27
28
29
30 type instrumentedRuntimeService struct {
31 service internalapi.RuntimeService
32 }
33
34
35 func newInstrumentedRuntimeService(service internalapi.RuntimeService) internalapi.RuntimeService {
36 return &instrumentedRuntimeService{service: service}
37 }
38
39
40
41 type instrumentedImageManagerService struct {
42 service internalapi.ImageManagerService
43 }
44
45
46 func newInstrumentedImageManagerService(service internalapi.ImageManagerService) internalapi.ImageManagerService {
47 return &instrumentedImageManagerService{service: service}
48 }
49
50
51 func recordOperation(operation string, start time.Time) {
52 metrics.RuntimeOperations.WithLabelValues(operation).Inc()
53 metrics.RuntimeOperationsDuration.WithLabelValues(operation).Observe(metrics.SinceInSeconds(start))
54 }
55
56
57 func recordError(operation string, err error) {
58 if err != nil {
59 metrics.RuntimeOperationsErrors.WithLabelValues(operation).Inc()
60 }
61 }
62
63 func (in instrumentedRuntimeService) Version(ctx context.Context, apiVersion string) (*runtimeapi.VersionResponse, error) {
64 const operation = "version"
65 defer recordOperation(operation, time.Now())
66
67 out, err := in.service.Version(ctx, apiVersion)
68 recordError(operation, err)
69 return out, err
70 }
71
72 func (in instrumentedRuntimeService) Status(ctx context.Context, verbose bool) (*runtimeapi.StatusResponse, error) {
73 const operation = "status"
74 defer recordOperation(operation, time.Now())
75
76 out, err := in.service.Status(ctx, verbose)
77 recordError(operation, err)
78 return out, err
79 }
80
81 func (in instrumentedRuntimeService) CreateContainer(ctx context.Context, podSandboxID string, config *runtimeapi.ContainerConfig, sandboxConfig *runtimeapi.PodSandboxConfig) (string, error) {
82 const operation = "create_container"
83 defer recordOperation(operation, time.Now())
84
85 out, err := in.service.CreateContainer(ctx, podSandboxID, config, sandboxConfig)
86 recordError(operation, err)
87 return out, err
88 }
89
90 func (in instrumentedRuntimeService) StartContainer(ctx context.Context, containerID string) error {
91 const operation = "start_container"
92 defer recordOperation(operation, time.Now())
93
94 err := in.service.StartContainer(ctx, containerID)
95 recordError(operation, err)
96 return err
97 }
98
99 func (in instrumentedRuntimeService) StopContainer(ctx context.Context, containerID string, timeout int64) error {
100 const operation = "stop_container"
101 defer recordOperation(operation, time.Now())
102
103 err := in.service.StopContainer(ctx, containerID, timeout)
104 recordError(operation, err)
105 return err
106 }
107
108 func (in instrumentedRuntimeService) RemoveContainer(ctx context.Context, containerID string) error {
109 const operation = "remove_container"
110 defer recordOperation(operation, time.Now())
111
112 err := in.service.RemoveContainer(ctx, containerID)
113 recordError(operation, err)
114 return err
115 }
116
117 func (in instrumentedRuntimeService) ListContainers(ctx context.Context, filter *runtimeapi.ContainerFilter) ([]*runtimeapi.Container, error) {
118 const operation = "list_containers"
119 defer recordOperation(operation, time.Now())
120
121 out, err := in.service.ListContainers(ctx, filter)
122 recordError(operation, err)
123 return out, err
124 }
125
126 func (in instrumentedRuntimeService) ContainerStatus(ctx context.Context, containerID string, verbose bool) (*runtimeapi.ContainerStatusResponse, error) {
127 const operation = "container_status"
128 defer recordOperation(operation, time.Now())
129
130 out, err := in.service.ContainerStatus(ctx, containerID, verbose)
131 recordError(operation, err)
132 return out, err
133 }
134
135 func (in instrumentedRuntimeService) UpdateContainerResources(ctx context.Context, containerID string, resources *runtimeapi.ContainerResources) error {
136 const operation = "update_container"
137 defer recordOperation(operation, time.Now())
138
139 err := in.service.UpdateContainerResources(ctx, containerID, resources)
140 recordError(operation, err)
141 return err
142 }
143
144 func (in instrumentedRuntimeService) ReopenContainerLog(ctx context.Context, containerID string) error {
145 const operation = "reopen_container_log"
146 defer recordOperation(operation, time.Now())
147
148 err := in.service.ReopenContainerLog(ctx, containerID)
149 recordError(operation, err)
150 return err
151 }
152
153 func (in instrumentedRuntimeService) ExecSync(ctx context.Context, containerID string, cmd []string, timeout time.Duration) ([]byte, []byte, error) {
154 const operation = "exec_sync"
155 defer recordOperation(operation, time.Now())
156
157 stdout, stderr, err := in.service.ExecSync(ctx, containerID, cmd, timeout)
158 recordError(operation, err)
159 return stdout, stderr, err
160 }
161
162 func (in instrumentedRuntimeService) Exec(ctx context.Context, req *runtimeapi.ExecRequest) (*runtimeapi.ExecResponse, error) {
163 const operation = "exec"
164 defer recordOperation(operation, time.Now())
165
166 resp, err := in.service.Exec(ctx, req)
167 recordError(operation, err)
168 return resp, err
169 }
170
171 func (in instrumentedRuntimeService) Attach(ctx context.Context, req *runtimeapi.AttachRequest) (*runtimeapi.AttachResponse, error) {
172 const operation = "attach"
173 defer recordOperation(operation, time.Now())
174
175 resp, err := in.service.Attach(ctx, req)
176 recordError(operation, err)
177 return resp, err
178 }
179
180 func (in instrumentedRuntimeService) RunPodSandbox(ctx context.Context, config *runtimeapi.PodSandboxConfig, runtimeHandler string) (string, error) {
181 const operation = "run_podsandbox"
182 startTime := time.Now()
183 defer recordOperation(operation, startTime)
184 defer metrics.RunPodSandboxDuration.WithLabelValues(runtimeHandler).Observe(metrics.SinceInSeconds(startTime))
185
186 out, err := in.service.RunPodSandbox(ctx, config, runtimeHandler)
187 recordError(operation, err)
188 if err != nil {
189 metrics.RunPodSandboxErrors.WithLabelValues(runtimeHandler).Inc()
190 }
191 return out, err
192 }
193
194 func (in instrumentedRuntimeService) StopPodSandbox(ctx context.Context, podSandboxID string) error {
195 const operation = "stop_podsandbox"
196 defer recordOperation(operation, time.Now())
197
198 err := in.service.StopPodSandbox(ctx, podSandboxID)
199 recordError(operation, err)
200 return err
201 }
202
203 func (in instrumentedRuntimeService) RemovePodSandbox(ctx context.Context, podSandboxID string) error {
204 const operation = "remove_podsandbox"
205 defer recordOperation(operation, time.Now())
206
207 err := in.service.RemovePodSandbox(ctx, podSandboxID)
208 recordError(operation, err)
209 return err
210 }
211
212 func (in instrumentedRuntimeService) PodSandboxStatus(ctx context.Context, podSandboxID string, verbose bool) (*runtimeapi.PodSandboxStatusResponse, error) {
213 const operation = "podsandbox_status"
214 defer recordOperation(operation, time.Now())
215
216 out, err := in.service.PodSandboxStatus(ctx, podSandboxID, verbose)
217 recordError(operation, err)
218 return out, err
219 }
220
221 func (in instrumentedRuntimeService) ListPodSandbox(ctx context.Context, filter *runtimeapi.PodSandboxFilter) ([]*runtimeapi.PodSandbox, error) {
222 const operation = "list_podsandbox"
223 defer recordOperation(operation, time.Now())
224
225 out, err := in.service.ListPodSandbox(ctx, filter)
226 recordError(operation, err)
227 return out, err
228 }
229
230 func (in instrumentedRuntimeService) ContainerStats(ctx context.Context, containerID string) (*runtimeapi.ContainerStats, error) {
231 const operation = "container_stats"
232 defer recordOperation(operation, time.Now())
233
234 out, err := in.service.ContainerStats(ctx, containerID)
235 recordError(operation, err)
236 return out, err
237 }
238
239 func (in instrumentedRuntimeService) ListContainerStats(ctx context.Context, filter *runtimeapi.ContainerStatsFilter) ([]*runtimeapi.ContainerStats, error) {
240 const operation = "list_container_stats"
241 defer recordOperation(operation, time.Now())
242
243 out, err := in.service.ListContainerStats(ctx, filter)
244 recordError(operation, err)
245 return out, err
246 }
247
248 func (in instrumentedRuntimeService) PodSandboxStats(ctx context.Context, podSandboxID string) (*runtimeapi.PodSandboxStats, error) {
249 const operation = "podsandbox_stats"
250 defer recordOperation(operation, time.Now())
251
252 out, err := in.service.PodSandboxStats(ctx, podSandboxID)
253 recordError(operation, err)
254 return out, err
255 }
256
257 func (in instrumentedRuntimeService) ListPodSandboxStats(ctx context.Context, filter *runtimeapi.PodSandboxStatsFilter) ([]*runtimeapi.PodSandboxStats, error) {
258 const operation = "list_podsandbox_stats"
259 defer recordOperation(operation, time.Now())
260
261 out, err := in.service.ListPodSandboxStats(ctx, filter)
262 recordError(operation, err)
263 return out, err
264 }
265
266 func (in instrumentedRuntimeService) PortForward(ctx context.Context, req *runtimeapi.PortForwardRequest) (*runtimeapi.PortForwardResponse, error) {
267 const operation = "port_forward"
268 defer recordOperation(operation, time.Now())
269
270 resp, err := in.service.PortForward(ctx, req)
271 recordError(operation, err)
272 return resp, err
273 }
274
275 func (in instrumentedRuntimeService) UpdateRuntimeConfig(ctx context.Context, runtimeConfig *runtimeapi.RuntimeConfig) error {
276 const operation = "update_runtime_config"
277 defer recordOperation(operation, time.Now())
278
279 err := in.service.UpdateRuntimeConfig(ctx, runtimeConfig)
280 recordError(operation, err)
281 return err
282 }
283
284 func (in instrumentedImageManagerService) ListImages(ctx context.Context, filter *runtimeapi.ImageFilter) ([]*runtimeapi.Image, error) {
285 const operation = "list_images"
286 defer recordOperation(operation, time.Now())
287
288 out, err := in.service.ListImages(ctx, filter)
289 recordError(operation, err)
290 return out, err
291 }
292
293 func (in instrumentedImageManagerService) ImageStatus(ctx context.Context, image *runtimeapi.ImageSpec, verbose bool) (*runtimeapi.ImageStatusResponse, error) {
294 const operation = "image_status"
295 defer recordOperation(operation, time.Now())
296
297 out, err := in.service.ImageStatus(ctx, image, verbose)
298 recordError(operation, err)
299 return out, err
300 }
301
302 func (in instrumentedImageManagerService) PullImage(ctx context.Context, image *runtimeapi.ImageSpec, auth *runtimeapi.AuthConfig, podSandboxConfig *runtimeapi.PodSandboxConfig) (string, error) {
303 const operation = "pull_image"
304 defer recordOperation(operation, time.Now())
305
306 imageRef, err := in.service.PullImage(ctx, image, auth, podSandboxConfig)
307 recordError(operation, err)
308 return imageRef, err
309 }
310
311 func (in instrumentedImageManagerService) RemoveImage(ctx context.Context, image *runtimeapi.ImageSpec) error {
312 const operation = "remove_image"
313 defer recordOperation(operation, time.Now())
314
315 err := in.service.RemoveImage(ctx, image)
316 recordError(operation, err)
317 return err
318 }
319
320 func (in instrumentedImageManagerService) ImageFsInfo(ctx context.Context) (*runtimeapi.ImageFsInfoResponse, error) {
321 const operation = "image_fs_info"
322 defer recordOperation(operation, time.Now())
323
324 fsInfo, err := in.service.ImageFsInfo(ctx)
325 recordError(operation, err)
326 return fsInfo, nil
327 }
328
329 func (in instrumentedRuntimeService) CheckpointContainer(ctx context.Context, options *runtimeapi.CheckpointContainerRequest) error {
330 const operation = "checkpoint_container"
331 defer recordOperation(operation, time.Now())
332
333 err := in.service.CheckpointContainer(ctx, options)
334 recordError(operation, err)
335 return err
336 }
337
338 func (in instrumentedRuntimeService) GetContainerEvents(containerEventsCh chan *runtimeapi.ContainerEventResponse) error {
339 const operation = "get_container_events"
340 defer recordOperation(operation, time.Now())
341
342 err := in.service.GetContainerEvents(containerEventsCh)
343 recordError(operation, err)
344 return err
345 }
346
347 func (in instrumentedRuntimeService) ListMetricDescriptors(ctx context.Context) ([]*runtimeapi.MetricDescriptor, error) {
348 const operation = "list_metric_descriptors"
349 defer recordOperation(operation, time.Now())
350
351 out, err := in.service.ListMetricDescriptors(ctx)
352 recordError(operation, err)
353 return out, err
354 }
355
356 func (in instrumentedRuntimeService) ListPodSandboxMetrics(ctx context.Context) ([]*runtimeapi.PodSandboxMetrics, error) {
357 const operation = "list_podsandbox_metrics"
358 defer recordOperation(operation, time.Now())
359
360 out, err := in.service.ListPodSandboxMetrics(ctx)
361 recordError(operation, err)
362 return out, err
363 }
364
365 func (in instrumentedRuntimeService) RuntimeConfig(ctx context.Context) (*runtimeapi.RuntimeConfigResponse, error) {
366 const operation = "runtime_config"
367 defer recordOperation(operation, time.Now())
368
369 out, err := in.service.RuntimeConfig(ctx)
370 recordError(operation, err)
371 return out, err
372 }
373
View as plain text