instrumented_client.go 8.4 KB

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