instrumented_services.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package kuberuntime
  14. import (
  15. "time"
  16. internalapi "k8s.io/cri-api/pkg/apis"
  17. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  18. "k8s.io/kubernetes/pkg/kubelet/metrics"
  19. )
  20. // instrumentedRuntimeService wraps the RuntimeService and records the operations
  21. // and errors metrics.
  22. type instrumentedRuntimeService struct {
  23. service internalapi.RuntimeService
  24. }
  25. // Creates an instrumented RuntimeInterface from an existing RuntimeService.
  26. func newInstrumentedRuntimeService(service internalapi.RuntimeService) internalapi.RuntimeService {
  27. return &instrumentedRuntimeService{service: service}
  28. }
  29. // instrumentedImageManagerService wraps the ImageManagerService and records the operations
  30. // and errors metrics.
  31. type instrumentedImageManagerService struct {
  32. service internalapi.ImageManagerService
  33. }
  34. // Creates an instrumented ImageManagerService from an existing ImageManagerService.
  35. func newInstrumentedImageManagerService(service internalapi.ImageManagerService) internalapi.ImageManagerService {
  36. return &instrumentedImageManagerService{service: service}
  37. }
  38. // recordOperation records the duration of the operation.
  39. func recordOperation(operation string, start time.Time) {
  40. metrics.RuntimeOperations.WithLabelValues(operation).Inc()
  41. metrics.RuntimeOperationsDuration.WithLabelValues(operation).Observe(metrics.SinceInSeconds(start))
  42. }
  43. // recordError records error for metric if an error occurred.
  44. func recordError(operation string, err error) {
  45. if err != nil {
  46. metrics.RuntimeOperationsErrors.WithLabelValues(operation).Inc()
  47. }
  48. }
  49. func (in instrumentedRuntimeService) Version(apiVersion string) (*runtimeapi.VersionResponse, error) {
  50. const operation = "version"
  51. defer recordOperation(operation, time.Now())
  52. out, err := in.service.Version(apiVersion)
  53. recordError(operation, err)
  54. return out, err
  55. }
  56. func (in instrumentedRuntimeService) Status() (*runtimeapi.RuntimeStatus, error) {
  57. const operation = "status"
  58. defer recordOperation(operation, time.Now())
  59. out, err := in.service.Status()
  60. recordError(operation, err)
  61. return out, err
  62. }
  63. func (in instrumentedRuntimeService) CreateContainer(podSandboxID string, config *runtimeapi.ContainerConfig, sandboxConfig *runtimeapi.PodSandboxConfig) (string, error) {
  64. const operation = "create_container"
  65. defer recordOperation(operation, time.Now())
  66. out, err := in.service.CreateContainer(podSandboxID, config, sandboxConfig)
  67. recordError(operation, err)
  68. return out, err
  69. }
  70. func (in instrumentedRuntimeService) StartContainer(containerID string) error {
  71. const operation = "start_container"
  72. defer recordOperation(operation, time.Now())
  73. err := in.service.StartContainer(containerID)
  74. recordError(operation, err)
  75. return err
  76. }
  77. func (in instrumentedRuntimeService) StopContainer(containerID string, timeout int64) error {
  78. const operation = "stop_container"
  79. defer recordOperation(operation, time.Now())
  80. err := in.service.StopContainer(containerID, timeout)
  81. recordError(operation, err)
  82. return err
  83. }
  84. func (in instrumentedRuntimeService) RemoveContainer(containerID string) error {
  85. const operation = "remove_container"
  86. defer recordOperation(operation, time.Now())
  87. err := in.service.RemoveContainer(containerID)
  88. recordError(operation, err)
  89. return err
  90. }
  91. func (in instrumentedRuntimeService) ListContainers(filter *runtimeapi.ContainerFilter) ([]*runtimeapi.Container, error) {
  92. const operation = "list_containers"
  93. defer recordOperation(operation, time.Now())
  94. out, err := in.service.ListContainers(filter)
  95. recordError(operation, err)
  96. return out, err
  97. }
  98. func (in instrumentedRuntimeService) ContainerStatus(containerID string) (*runtimeapi.ContainerStatus, error) {
  99. const operation = "container_status"
  100. defer recordOperation(operation, time.Now())
  101. out, err := in.service.ContainerStatus(containerID)
  102. recordError(operation, err)
  103. return out, err
  104. }
  105. func (in instrumentedRuntimeService) UpdateContainerResources(containerID string, resources *runtimeapi.LinuxContainerResources) error {
  106. const operation = "update_container"
  107. defer recordOperation(operation, time.Now())
  108. err := in.service.UpdateContainerResources(containerID, resources)
  109. recordError(operation, err)
  110. return err
  111. }
  112. func (in instrumentedRuntimeService) ReopenContainerLog(containerID string) error {
  113. const operation = "reopen_container_log"
  114. defer recordOperation(operation, time.Now())
  115. err := in.service.ReopenContainerLog(containerID)
  116. recordError(operation, err)
  117. return err
  118. }
  119. func (in instrumentedRuntimeService) ExecSync(containerID string, cmd []string, timeout time.Duration) ([]byte, []byte, error) {
  120. const operation = "exec_sync"
  121. defer recordOperation(operation, time.Now())
  122. stdout, stderr, err := in.service.ExecSync(containerID, cmd, timeout)
  123. recordError(operation, err)
  124. return stdout, stderr, err
  125. }
  126. func (in instrumentedRuntimeService) Exec(req *runtimeapi.ExecRequest) (*runtimeapi.ExecResponse, error) {
  127. const operation = "exec"
  128. defer recordOperation(operation, time.Now())
  129. resp, err := in.service.Exec(req)
  130. recordError(operation, err)
  131. return resp, err
  132. }
  133. func (in instrumentedRuntimeService) Attach(req *runtimeapi.AttachRequest) (*runtimeapi.AttachResponse, error) {
  134. const operation = "attach"
  135. defer recordOperation(operation, time.Now())
  136. resp, err := in.service.Attach(req)
  137. recordError(operation, err)
  138. return resp, err
  139. }
  140. func (in instrumentedRuntimeService) RunPodSandbox(config *runtimeapi.PodSandboxConfig, runtimeHandler string) (string, error) {
  141. const operation = "run_podsandbox"
  142. startTime := time.Now()
  143. defer recordOperation(operation, startTime)
  144. defer metrics.RunPodSandboxDuration.WithLabelValues(runtimeHandler).Observe(metrics.SinceInSeconds(startTime))
  145. out, err := in.service.RunPodSandbox(config, runtimeHandler)
  146. recordError(operation, err)
  147. if err != nil {
  148. metrics.RunPodSandboxErrors.WithLabelValues(runtimeHandler).Inc()
  149. }
  150. return out, err
  151. }
  152. func (in instrumentedRuntimeService) StopPodSandbox(podSandboxID string) error {
  153. const operation = "stop_podsandbox"
  154. defer recordOperation(operation, time.Now())
  155. err := in.service.StopPodSandbox(podSandboxID)
  156. recordError(operation, err)
  157. return err
  158. }
  159. func (in instrumentedRuntimeService) RemovePodSandbox(podSandboxID string) error {
  160. const operation = "remove_podsandbox"
  161. defer recordOperation(operation, time.Now())
  162. err := in.service.RemovePodSandbox(podSandboxID)
  163. recordError(operation, err)
  164. return err
  165. }
  166. func (in instrumentedRuntimeService) PodSandboxStatus(podSandboxID string) (*runtimeapi.PodSandboxStatus, error) {
  167. const operation = "podsandbox_status"
  168. defer recordOperation(operation, time.Now())
  169. out, err := in.service.PodSandboxStatus(podSandboxID)
  170. recordError(operation, err)
  171. return out, err
  172. }
  173. func (in instrumentedRuntimeService) ListPodSandbox(filter *runtimeapi.PodSandboxFilter) ([]*runtimeapi.PodSandbox, error) {
  174. const operation = "list_podsandbox"
  175. defer recordOperation(operation, time.Now())
  176. out, err := in.service.ListPodSandbox(filter)
  177. recordError(operation, err)
  178. return out, err
  179. }
  180. func (in instrumentedRuntimeService) ContainerStats(containerID string) (*runtimeapi.ContainerStats, error) {
  181. const operation = "container_stats"
  182. defer recordOperation(operation, time.Now())
  183. out, err := in.service.ContainerStats(containerID)
  184. recordError(operation, err)
  185. return out, err
  186. }
  187. func (in instrumentedRuntimeService) ListContainerStats(filter *runtimeapi.ContainerStatsFilter) ([]*runtimeapi.ContainerStats, error) {
  188. const operation = "list_container_stats"
  189. defer recordOperation(operation, time.Now())
  190. out, err := in.service.ListContainerStats(filter)
  191. recordError(operation, err)
  192. return out, err
  193. }
  194. func (in instrumentedRuntimeService) PortForward(req *runtimeapi.PortForwardRequest) (*runtimeapi.PortForwardResponse, error) {
  195. const operation = "port_forward"
  196. defer recordOperation(operation, time.Now())
  197. resp, err := in.service.PortForward(req)
  198. recordError(operation, err)
  199. return resp, err
  200. }
  201. func (in instrumentedRuntimeService) UpdateRuntimeConfig(runtimeConfig *runtimeapi.RuntimeConfig) error {
  202. const operation = "update_runtime_config"
  203. defer recordOperation(operation, time.Now())
  204. err := in.service.UpdateRuntimeConfig(runtimeConfig)
  205. recordError(operation, err)
  206. return err
  207. }
  208. func (in instrumentedImageManagerService) ListImages(filter *runtimeapi.ImageFilter) ([]*runtimeapi.Image, error) {
  209. const operation = "list_images"
  210. defer recordOperation(operation, time.Now())
  211. out, err := in.service.ListImages(filter)
  212. recordError(operation, err)
  213. return out, err
  214. }
  215. func (in instrumentedImageManagerService) ImageStatus(image *runtimeapi.ImageSpec) (*runtimeapi.Image, error) {
  216. const operation = "image_status"
  217. defer recordOperation(operation, time.Now())
  218. out, err := in.service.ImageStatus(image)
  219. recordError(operation, err)
  220. return out, err
  221. }
  222. func (in instrumentedImageManagerService) PullImage(image *runtimeapi.ImageSpec, auth *runtimeapi.AuthConfig, podSandboxConfig *runtimeapi.PodSandboxConfig) (string, error) {
  223. const operation = "pull_image"
  224. defer recordOperation(operation, time.Now())
  225. imageRef, err := in.service.PullImage(image, auth, podSandboxConfig)
  226. recordError(operation, err)
  227. return imageRef, err
  228. }
  229. func (in instrumentedImageManagerService) RemoveImage(image *runtimeapi.ImageSpec) error {
  230. const operation = "remove_image"
  231. defer recordOperation(operation, time.Now())
  232. err := in.service.RemoveImage(image)
  233. recordError(operation, err)
  234. return err
  235. }
  236. func (in instrumentedImageManagerService) ImageFsInfo() ([]*runtimeapi.FilesystemUsage, error) {
  237. const operation = "image_fs_info"
  238. defer recordOperation(operation, time.Now())
  239. fsInfo, err := in.service.ImageFsInfo()
  240. recordError(operation, err)
  241. return fsInfo, nil
  242. }