instrumented_services.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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.DeprecatedRuntimeOperations.WithLabelValues(operation).Inc()
  42. metrics.RuntimeOperationsDuration.WithLabelValues(operation).Observe(metrics.SinceInSeconds(start))
  43. metrics.DeprecatedRuntimeOperationsLatency.WithLabelValues(operation).Observe(metrics.SinceInMicroseconds(start))
  44. }
  45. // recordError records error for metric if an error occurred.
  46. func recordError(operation string, err error) {
  47. if err != nil {
  48. metrics.RuntimeOperationsErrors.WithLabelValues(operation).Inc()
  49. metrics.DeprecatedRuntimeOperationsErrors.WithLabelValues(operation).Inc()
  50. }
  51. }
  52. func (in instrumentedRuntimeService) Version(apiVersion string) (*runtimeapi.VersionResponse, error) {
  53. const operation = "version"
  54. defer recordOperation(operation, time.Now())
  55. out, err := in.service.Version(apiVersion)
  56. recordError(operation, err)
  57. return out, err
  58. }
  59. func (in instrumentedRuntimeService) Status() (*runtimeapi.RuntimeStatus, error) {
  60. const operation = "status"
  61. defer recordOperation(operation, time.Now())
  62. out, err := in.service.Status()
  63. recordError(operation, err)
  64. return out, err
  65. }
  66. func (in instrumentedRuntimeService) CreateContainer(podSandboxID string, config *runtimeapi.ContainerConfig, sandboxConfig *runtimeapi.PodSandboxConfig) (string, error) {
  67. const operation = "create_container"
  68. defer recordOperation(operation, time.Now())
  69. out, err := in.service.CreateContainer(podSandboxID, config, sandboxConfig)
  70. recordError(operation, err)
  71. return out, err
  72. }
  73. func (in instrumentedRuntimeService) StartContainer(containerID string) error {
  74. const operation = "start_container"
  75. defer recordOperation(operation, time.Now())
  76. err := in.service.StartContainer(containerID)
  77. recordError(operation, err)
  78. return err
  79. }
  80. func (in instrumentedRuntimeService) StopContainer(containerID string, timeout int64) error {
  81. const operation = "stop_container"
  82. defer recordOperation(operation, time.Now())
  83. err := in.service.StopContainer(containerID, timeout)
  84. recordError(operation, err)
  85. return err
  86. }
  87. func (in instrumentedRuntimeService) RemoveContainer(containerID string) error {
  88. const operation = "remove_container"
  89. defer recordOperation(operation, time.Now())
  90. err := in.service.RemoveContainer(containerID)
  91. recordError(operation, err)
  92. return err
  93. }
  94. func (in instrumentedRuntimeService) ListContainers(filter *runtimeapi.ContainerFilter) ([]*runtimeapi.Container, error) {
  95. const operation = "list_containers"
  96. defer recordOperation(operation, time.Now())
  97. out, err := in.service.ListContainers(filter)
  98. recordError(operation, err)
  99. return out, err
  100. }
  101. func (in instrumentedRuntimeService) ContainerStatus(containerID string) (*runtimeapi.ContainerStatus, error) {
  102. const operation = "container_status"
  103. defer recordOperation(operation, time.Now())
  104. out, err := in.service.ContainerStatus(containerID)
  105. recordError(operation, err)
  106. return out, err
  107. }
  108. func (in instrumentedRuntimeService) UpdateContainerResources(containerID string, resources *runtimeapi.LinuxContainerResources) error {
  109. const operation = "update_container"
  110. defer recordOperation(operation, time.Now())
  111. err := in.service.UpdateContainerResources(containerID, resources)
  112. recordError(operation, err)
  113. return err
  114. }
  115. func (in instrumentedRuntimeService) ReopenContainerLog(containerID string) error {
  116. const operation = "reopen_container_log"
  117. defer recordOperation(operation, time.Now())
  118. err := in.service.ReopenContainerLog(containerID)
  119. recordError(operation, err)
  120. return err
  121. }
  122. func (in instrumentedRuntimeService) ExecSync(containerID string, cmd []string, timeout time.Duration) ([]byte, []byte, error) {
  123. const operation = "exec_sync"
  124. defer recordOperation(operation, time.Now())
  125. stdout, stderr, err := in.service.ExecSync(containerID, cmd, timeout)
  126. recordError(operation, err)
  127. return stdout, stderr, err
  128. }
  129. func (in instrumentedRuntimeService) Exec(req *runtimeapi.ExecRequest) (*runtimeapi.ExecResponse, error) {
  130. const operation = "exec"
  131. defer recordOperation(operation, time.Now())
  132. resp, err := in.service.Exec(req)
  133. recordError(operation, err)
  134. return resp, err
  135. }
  136. func (in instrumentedRuntimeService) Attach(req *runtimeapi.AttachRequest) (*runtimeapi.AttachResponse, error) {
  137. const operation = "attach"
  138. defer recordOperation(operation, time.Now())
  139. resp, err := in.service.Attach(req)
  140. recordError(operation, err)
  141. return resp, err
  142. }
  143. func (in instrumentedRuntimeService) RunPodSandbox(config *runtimeapi.PodSandboxConfig, runtimeHandler string) (string, error) {
  144. const operation = "run_podsandbox"
  145. startTime := time.Now()
  146. defer recordOperation(operation, startTime)
  147. defer metrics.RunPodSandboxDuration.WithLabelValues(runtimeHandler).Observe(metrics.SinceInSeconds(startTime))
  148. out, err := in.service.RunPodSandbox(config, runtimeHandler)
  149. recordError(operation, err)
  150. if err != nil {
  151. metrics.RunPodSandboxErrors.WithLabelValues(runtimeHandler).Inc()
  152. }
  153. return out, err
  154. }
  155. func (in instrumentedRuntimeService) StopPodSandbox(podSandboxID string) error {
  156. const operation = "stop_podsandbox"
  157. defer recordOperation(operation, time.Now())
  158. err := in.service.StopPodSandbox(podSandboxID)
  159. recordError(operation, err)
  160. return err
  161. }
  162. func (in instrumentedRuntimeService) RemovePodSandbox(podSandboxID string) error {
  163. const operation = "remove_podsandbox"
  164. defer recordOperation(operation, time.Now())
  165. err := in.service.RemovePodSandbox(podSandboxID)
  166. recordError(operation, err)
  167. return err
  168. }
  169. func (in instrumentedRuntimeService) PodSandboxStatus(podSandboxID string) (*runtimeapi.PodSandboxStatus, error) {
  170. const operation = "podsandbox_status"
  171. defer recordOperation(operation, time.Now())
  172. out, err := in.service.PodSandboxStatus(podSandboxID)
  173. recordError(operation, err)
  174. return out, err
  175. }
  176. func (in instrumentedRuntimeService) ListPodSandbox(filter *runtimeapi.PodSandboxFilter) ([]*runtimeapi.PodSandbox, error) {
  177. const operation = "list_podsandbox"
  178. defer recordOperation(operation, time.Now())
  179. out, err := in.service.ListPodSandbox(filter)
  180. recordError(operation, err)
  181. return out, err
  182. }
  183. func (in instrumentedRuntimeService) ContainerStats(containerID string) (*runtimeapi.ContainerStats, error) {
  184. const operation = "container_stats"
  185. defer recordOperation(operation, time.Now())
  186. out, err := in.service.ContainerStats(containerID)
  187. recordError(operation, err)
  188. return out, err
  189. }
  190. func (in instrumentedRuntimeService) ListContainerStats(filter *runtimeapi.ContainerStatsFilter) ([]*runtimeapi.ContainerStats, error) {
  191. const operation = "list_container_stats"
  192. defer recordOperation(operation, time.Now())
  193. out, err := in.service.ListContainerStats(filter)
  194. recordError(operation, err)
  195. return out, err
  196. }
  197. func (in instrumentedRuntimeService) PortForward(req *runtimeapi.PortForwardRequest) (*runtimeapi.PortForwardResponse, error) {
  198. const operation = "port_forward"
  199. defer recordOperation(operation, time.Now())
  200. resp, err := in.service.PortForward(req)
  201. recordError(operation, err)
  202. return resp, err
  203. }
  204. func (in instrumentedRuntimeService) UpdateRuntimeConfig(runtimeConfig *runtimeapi.RuntimeConfig) error {
  205. const operation = "update_runtime_config"
  206. defer recordOperation(operation, time.Now())
  207. err := in.service.UpdateRuntimeConfig(runtimeConfig)
  208. recordError(operation, err)
  209. return err
  210. }
  211. func (in instrumentedImageManagerService) ListImages(filter *runtimeapi.ImageFilter) ([]*runtimeapi.Image, error) {
  212. const operation = "list_images"
  213. defer recordOperation(operation, time.Now())
  214. out, err := in.service.ListImages(filter)
  215. recordError(operation, err)
  216. return out, err
  217. }
  218. func (in instrumentedImageManagerService) ImageStatus(image *runtimeapi.ImageSpec) (*runtimeapi.Image, error) {
  219. const operation = "image_status"
  220. defer recordOperation(operation, time.Now())
  221. out, err := in.service.ImageStatus(image)
  222. recordError(operation, err)
  223. return out, err
  224. }
  225. func (in instrumentedImageManagerService) PullImage(image *runtimeapi.ImageSpec, auth *runtimeapi.AuthConfig, podSandboxConfig *runtimeapi.PodSandboxConfig) (string, error) {
  226. const operation = "pull_image"
  227. defer recordOperation(operation, time.Now())
  228. imageRef, err := in.service.PullImage(image, auth, podSandboxConfig)
  229. recordError(operation, err)
  230. return imageRef, err
  231. }
  232. func (in instrumentedImageManagerService) RemoveImage(image *runtimeapi.ImageSpec) error {
  233. const operation = "remove_image"
  234. defer recordOperation(operation, time.Now())
  235. err := in.service.RemoveImage(image)
  236. recordError(operation, err)
  237. return err
  238. }
  239. func (in instrumentedImageManagerService) ImageFsInfo() ([]*runtimeapi.FilesystemUsage, error) {
  240. const operation = "image_fs_info"
  241. defer recordOperation(operation, time.Now())
  242. fsInfo, err := in.service.ImageFsInfo()
  243. recordError(operation, err)
  244. return fsInfo, nil
  245. }