instrumented_client.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 libdocker
  14. import (
  15. "time"
  16. dockertypes "github.com/docker/docker/api/types"
  17. dockercontainer "github.com/docker/docker/api/types/container"
  18. dockerimagetypes "github.com/docker/docker/api/types/image"
  19. "k8s.io/kubernetes/pkg/kubelet/dockershim/metrics"
  20. )
  21. // instrumentedInterface wraps the Interface and records the operations
  22. // and errors metrics.
  23. type instrumentedInterface struct {
  24. client Interface
  25. }
  26. // NewInstrumentedInterface creates an instrumented Interface from an existing Interface.
  27. func NewInstrumentedInterface(dockerClient Interface) Interface {
  28. return instrumentedInterface{
  29. client: dockerClient,
  30. }
  31. }
  32. // recordOperation records the duration of the operation.
  33. func recordOperation(operation string, start time.Time) {
  34. metrics.DockerOperations.WithLabelValues(operation).Inc()
  35. metrics.DeprecatedDockerOperations.WithLabelValues(operation).Inc()
  36. metrics.DockerOperationsLatency.WithLabelValues(operation).Observe(metrics.SinceInSeconds(start))
  37. metrics.DeprecatedDockerOperationsLatency.WithLabelValues(operation).Observe(metrics.SinceInMicroseconds(start))
  38. }
  39. // recordError records error for metric if an error occurred.
  40. func recordError(operation string, err error) {
  41. if err != nil {
  42. if _, ok := err.(operationTimeout); ok {
  43. metrics.DockerOperationsTimeout.WithLabelValues(operation).Inc()
  44. metrics.DeprecatedDockerOperationsTimeout.WithLabelValues(operation).Inc()
  45. }
  46. // Docker operation timeout error is also a docker error, so we don't add else here.
  47. metrics.DockerOperationsErrors.WithLabelValues(operation).Inc()
  48. metrics.DeprecatedDockerOperationsErrors.WithLabelValues(operation).Inc()
  49. }
  50. }
  51. func (in instrumentedInterface) ListContainers(options dockertypes.ContainerListOptions) ([]dockertypes.Container, error) {
  52. const operation = "list_containers"
  53. defer recordOperation(operation, time.Now())
  54. out, err := in.client.ListContainers(options)
  55. recordError(operation, err)
  56. return out, err
  57. }
  58. func (in instrumentedInterface) InspectContainer(id string) (*dockertypes.ContainerJSON, error) {
  59. const operation = "inspect_container"
  60. defer recordOperation(operation, time.Now())
  61. out, err := in.client.InspectContainer(id)
  62. recordError(operation, err)
  63. return out, err
  64. }
  65. func (in instrumentedInterface) InspectContainerWithSize(id string) (*dockertypes.ContainerJSON, error) {
  66. const operation = "inspect_container_withsize"
  67. defer recordOperation(operation, time.Now())
  68. out, err := in.client.InspectContainerWithSize(id)
  69. recordError(operation, err)
  70. return out, err
  71. }
  72. func (in instrumentedInterface) CreateContainer(opts dockertypes.ContainerCreateConfig) (*dockercontainer.ContainerCreateCreatedBody, error) {
  73. const operation = "create_container"
  74. defer recordOperation(operation, time.Now())
  75. out, err := in.client.CreateContainer(opts)
  76. recordError(operation, err)
  77. return out, err
  78. }
  79. func (in instrumentedInterface) StartContainer(id string) error {
  80. const operation = "start_container"
  81. defer recordOperation(operation, time.Now())
  82. err := in.client.StartContainer(id)
  83. recordError(operation, err)
  84. return err
  85. }
  86. func (in instrumentedInterface) StopContainer(id string, timeout time.Duration) error {
  87. const operation = "stop_container"
  88. defer recordOperation(operation, time.Now())
  89. err := in.client.StopContainer(id, timeout)
  90. recordError(operation, err)
  91. return err
  92. }
  93. func (in instrumentedInterface) RemoveContainer(id string, opts dockertypes.ContainerRemoveOptions) error {
  94. const operation = "remove_container"
  95. defer recordOperation(operation, time.Now())
  96. err := in.client.RemoveContainer(id, opts)
  97. recordError(operation, err)
  98. return err
  99. }
  100. func (in instrumentedInterface) UpdateContainerResources(id string, updateConfig dockercontainer.UpdateConfig) error {
  101. const operation = "update_container"
  102. defer recordOperation(operation, time.Now())
  103. err := in.client.UpdateContainerResources(id, updateConfig)
  104. recordError(operation, err)
  105. return err
  106. }
  107. func (in instrumentedInterface) InspectImageByRef(image string) (*dockertypes.ImageInspect, error) {
  108. const operation = "inspect_image"
  109. defer recordOperation(operation, time.Now())
  110. out, err := in.client.InspectImageByRef(image)
  111. recordError(operation, err)
  112. return out, err
  113. }
  114. func (in instrumentedInterface) InspectImageByID(image string) (*dockertypes.ImageInspect, error) {
  115. const operation = "inspect_image"
  116. defer recordOperation(operation, time.Now())
  117. out, err := in.client.InspectImageByID(image)
  118. recordError(operation, err)
  119. return out, err
  120. }
  121. func (in instrumentedInterface) ListImages(opts dockertypes.ImageListOptions) ([]dockertypes.ImageSummary, error) {
  122. const operation = "list_images"
  123. defer recordOperation(operation, time.Now())
  124. out, err := in.client.ListImages(opts)
  125. recordError(operation, err)
  126. return out, err
  127. }
  128. func (in instrumentedInterface) PullImage(imageID string, auth dockertypes.AuthConfig, opts dockertypes.ImagePullOptions) error {
  129. const operation = "pull_image"
  130. defer recordOperation(operation, time.Now())
  131. err := in.client.PullImage(imageID, auth, opts)
  132. recordError(operation, err)
  133. return err
  134. }
  135. func (in instrumentedInterface) RemoveImage(image string, opts dockertypes.ImageRemoveOptions) ([]dockertypes.ImageDeleteResponseItem, error) {
  136. const operation = "remove_image"
  137. defer recordOperation(operation, time.Now())
  138. imageDelete, err := in.client.RemoveImage(image, opts)
  139. recordError(operation, err)
  140. return imageDelete, err
  141. }
  142. func (in instrumentedInterface) Logs(id string, opts dockertypes.ContainerLogsOptions, sopts StreamOptions) error {
  143. const operation = "logs"
  144. defer recordOperation(operation, time.Now())
  145. err := in.client.Logs(id, opts, sopts)
  146. recordError(operation, err)
  147. return err
  148. }
  149. func (in instrumentedInterface) Version() (*dockertypes.Version, error) {
  150. const operation = "version"
  151. defer recordOperation(operation, time.Now())
  152. out, err := in.client.Version()
  153. recordError(operation, err)
  154. return out, err
  155. }
  156. func (in instrumentedInterface) Info() (*dockertypes.Info, error) {
  157. const operation = "info"
  158. defer recordOperation(operation, time.Now())
  159. out, err := in.client.Info()
  160. recordError(operation, err)
  161. return out, err
  162. }
  163. func (in instrumentedInterface) CreateExec(id string, opts dockertypes.ExecConfig) (*dockertypes.IDResponse, error) {
  164. const operation = "create_exec"
  165. defer recordOperation(operation, time.Now())
  166. out, err := in.client.CreateExec(id, opts)
  167. recordError(operation, err)
  168. return out, err
  169. }
  170. func (in instrumentedInterface) StartExec(startExec string, opts dockertypes.ExecStartCheck, sopts StreamOptions) error {
  171. const operation = "start_exec"
  172. defer recordOperation(operation, time.Now())
  173. err := in.client.StartExec(startExec, opts, sopts)
  174. recordError(operation, err)
  175. return err
  176. }
  177. func (in instrumentedInterface) InspectExec(id string) (*dockertypes.ContainerExecInspect, error) {
  178. const operation = "inspect_exec"
  179. defer recordOperation(operation, time.Now())
  180. out, err := in.client.InspectExec(id)
  181. recordError(operation, err)
  182. return out, err
  183. }
  184. func (in instrumentedInterface) AttachToContainer(id string, opts dockertypes.ContainerAttachOptions, sopts StreamOptions) error {
  185. const operation = "attach"
  186. defer recordOperation(operation, time.Now())
  187. err := in.client.AttachToContainer(id, opts, sopts)
  188. recordError(operation, err)
  189. return err
  190. }
  191. func (in instrumentedInterface) ImageHistory(id string) ([]dockerimagetypes.HistoryResponseItem, error) {
  192. const operation = "image_history"
  193. defer recordOperation(operation, time.Now())
  194. out, err := in.client.ImageHistory(id)
  195. recordError(operation, err)
  196. return out, err
  197. }
  198. func (in instrumentedInterface) ResizeExecTTY(id string, height, width uint) error {
  199. const operation = "resize_exec"
  200. defer recordOperation(operation, time.Now())
  201. err := in.client.ResizeExecTTY(id, height, width)
  202. recordError(operation, err)
  203. return err
  204. }
  205. func (in instrumentedInterface) ResizeContainerTTY(id string, height, width uint) error {
  206. const operation = "resize_container"
  207. defer recordOperation(operation, time.Now())
  208. err := in.client.ResizeContainerTTY(id, height, width)
  209. recordError(operation, err)
  210. return err
  211. }
  212. func (in instrumentedInterface) GetContainerStats(id string) (*dockertypes.StatsJSON, error) {
  213. const operation = "stats"
  214. defer recordOperation(operation, time.Now())
  215. out, err := in.client.GetContainerStats(id)
  216. recordError(operation, err)
  217. return out, err
  218. }