instrumented_docker.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. Copyright 2015 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 dockertools
  14. import (
  15. "time"
  16. dockertypes "github.com/docker/engine-api/types"
  17. "k8s.io/kubernetes/pkg/kubelet/metrics"
  18. )
  19. // instrumentedDockerInterface wraps the DockerInterface and records the operations
  20. // and errors metrics.
  21. type instrumentedDockerInterface struct {
  22. client DockerInterface
  23. }
  24. // Creates an instrumented DockerInterface from an existing DockerInterface.
  25. func NewInstrumentedDockerInterface(dockerClient DockerInterface) DockerInterface {
  26. return instrumentedDockerInterface{
  27. client: dockerClient,
  28. }
  29. }
  30. // recordOperation records the duration of the operation.
  31. func recordOperation(operation string, start time.Time) {
  32. metrics.DockerOperations.WithLabelValues(operation).Inc()
  33. metrics.DockerOperationsLatency.WithLabelValues(operation).Observe(metrics.SinceInMicroseconds(start))
  34. }
  35. // recordError records error for metric if an error occurred.
  36. func recordError(operation string, err error) {
  37. if err != nil {
  38. if _, ok := err.(operationTimeout); ok {
  39. metrics.DockerOperationsTimeout.WithLabelValues(operation).Inc()
  40. }
  41. // Docker operation timeout error is also a docker error, so we don't add else here.
  42. metrics.DockerOperationsErrors.WithLabelValues(operation).Inc()
  43. }
  44. }
  45. func (in instrumentedDockerInterface) ListContainers(options dockertypes.ContainerListOptions) ([]dockertypes.Container, error) {
  46. const operation = "list_containers"
  47. defer recordOperation(operation, time.Now())
  48. out, err := in.client.ListContainers(options)
  49. recordError(operation, err)
  50. return out, err
  51. }
  52. func (in instrumentedDockerInterface) InspectContainer(id string) (*dockertypes.ContainerJSON, error) {
  53. const operation = "inspect_container"
  54. defer recordOperation(operation, time.Now())
  55. out, err := in.client.InspectContainer(id)
  56. recordError(operation, err)
  57. return out, err
  58. }
  59. func (in instrumentedDockerInterface) CreateContainer(opts dockertypes.ContainerCreateConfig) (*dockertypes.ContainerCreateResponse, error) {
  60. const operation = "create_container"
  61. defer recordOperation(operation, time.Now())
  62. out, err := in.client.CreateContainer(opts)
  63. recordError(operation, err)
  64. return out, err
  65. }
  66. func (in instrumentedDockerInterface) StartContainer(id string) error {
  67. const operation = "start_container"
  68. defer recordOperation(operation, time.Now())
  69. err := in.client.StartContainer(id)
  70. recordError(operation, err)
  71. return err
  72. }
  73. func (in instrumentedDockerInterface) StopContainer(id string, timeout int) error {
  74. const operation = "stop_container"
  75. defer recordOperation(operation, time.Now())
  76. err := in.client.StopContainer(id, timeout)
  77. recordError(operation, err)
  78. return err
  79. }
  80. func (in instrumentedDockerInterface) RemoveContainer(id string, opts dockertypes.ContainerRemoveOptions) error {
  81. const operation = "remove_container"
  82. defer recordOperation(operation, time.Now())
  83. err := in.client.RemoveContainer(id, opts)
  84. recordError(operation, err)
  85. return err
  86. }
  87. func (in instrumentedDockerInterface) InspectImage(image string) (*dockertypes.ImageInspect, error) {
  88. const operation = "inspect_image"
  89. defer recordOperation(operation, time.Now())
  90. out, err := in.client.InspectImage(image)
  91. recordError(operation, err)
  92. return out, err
  93. }
  94. func (in instrumentedDockerInterface) ListImages(opts dockertypes.ImageListOptions) ([]dockertypes.Image, error) {
  95. const operation = "list_images"
  96. defer recordOperation(operation, time.Now())
  97. out, err := in.client.ListImages(opts)
  98. recordError(operation, err)
  99. return out, err
  100. }
  101. func (in instrumentedDockerInterface) PullImage(imageID string, auth dockertypes.AuthConfig, opts dockertypes.ImagePullOptions) error {
  102. const operation = "pull_image"
  103. defer recordOperation(operation, time.Now())
  104. err := in.client.PullImage(imageID, auth, opts)
  105. recordError(operation, err)
  106. return err
  107. }
  108. func (in instrumentedDockerInterface) RemoveImage(image string, opts dockertypes.ImageRemoveOptions) ([]dockertypes.ImageDelete, error) {
  109. const operation = "remove_image"
  110. defer recordOperation(operation, time.Now())
  111. imageDelete, err := in.client.RemoveImage(image, opts)
  112. recordError(operation, err)
  113. return imageDelete, err
  114. }
  115. func (in instrumentedDockerInterface) Logs(id string, opts dockertypes.ContainerLogsOptions, sopts StreamOptions) error {
  116. const operation = "logs"
  117. defer recordOperation(operation, time.Now())
  118. err := in.client.Logs(id, opts, sopts)
  119. recordError(operation, err)
  120. return err
  121. }
  122. func (in instrumentedDockerInterface) Version() (*dockertypes.Version, error) {
  123. const operation = "version"
  124. defer recordOperation(operation, time.Now())
  125. out, err := in.client.Version()
  126. recordError(operation, err)
  127. return out, err
  128. }
  129. func (in instrumentedDockerInterface) Info() (*dockertypes.Info, error) {
  130. const operation = "info"
  131. defer recordOperation(operation, time.Now())
  132. out, err := in.client.Info()
  133. recordError(operation, err)
  134. return out, err
  135. }
  136. func (in instrumentedDockerInterface) CreateExec(id string, opts dockertypes.ExecConfig) (*dockertypes.ContainerExecCreateResponse, error) {
  137. const operation = "create_exec"
  138. defer recordOperation(operation, time.Now())
  139. out, err := in.client.CreateExec(id, opts)
  140. recordError(operation, err)
  141. return out, err
  142. }
  143. func (in instrumentedDockerInterface) StartExec(startExec string, opts dockertypes.ExecStartCheck, sopts StreamOptions) error {
  144. const operation = "start_exec"
  145. defer recordOperation(operation, time.Now())
  146. err := in.client.StartExec(startExec, opts, sopts)
  147. recordError(operation, err)
  148. return err
  149. }
  150. func (in instrumentedDockerInterface) InspectExec(id string) (*dockertypes.ContainerExecInspect, error) {
  151. const operation = "inspect_exec"
  152. defer recordOperation(operation, time.Now())
  153. out, err := in.client.InspectExec(id)
  154. recordError(operation, err)
  155. return out, err
  156. }
  157. func (in instrumentedDockerInterface) AttachToContainer(id string, opts dockertypes.ContainerAttachOptions, sopts StreamOptions) error {
  158. const operation = "attach"
  159. defer recordOperation(operation, time.Now())
  160. err := in.client.AttachToContainer(id, opts, sopts)
  161. recordError(operation, err)
  162. return err
  163. }
  164. func (in instrumentedDockerInterface) ImageHistory(id string) ([]dockertypes.ImageHistory, error) {
  165. const operation = "image_history"
  166. defer recordOperation(operation, time.Now())
  167. out, err := in.client.ImageHistory(id)
  168. recordError(operation, err)
  169. return out, err
  170. }
  171. func (in instrumentedDockerInterface) ResizeExecTTY(id string, height, width int) error {
  172. const operation = "resize_exec"
  173. defer recordOperation(operation, time.Now())
  174. err := in.client.ResizeExecTTY(id, height, width)
  175. recordError(operation, err)
  176. return err
  177. }
  178. func (in instrumentedDockerInterface) ResizeContainerTTY(id string, height, width int) error {
  179. const operation = "resize_container"
  180. defer recordOperation(operation, time.Now())
  181. err := in.client.ResizeContainerTTY(id, height, width)
  182. recordError(operation, err)
  183. return err
  184. }