metrics.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 metrics provides abstractions for registering which metrics
  14. // to record.
  15. package metrics
  16. import (
  17. "context"
  18. "net/url"
  19. "sync"
  20. "time"
  21. )
  22. var registerMetrics sync.Once
  23. // DurationMetric is a measurement of some amount of time.
  24. type DurationMetric interface {
  25. Observe(duration time.Duration)
  26. }
  27. // ExpiryMetric sets some time of expiry. If nil, assume not relevant.
  28. type ExpiryMetric interface {
  29. Set(expiry *time.Time)
  30. }
  31. // LatencyMetric observes client latency partitioned by verb and url.
  32. type LatencyMetric interface {
  33. Observe(ctx context.Context, verb string, u url.URL, latency time.Duration)
  34. }
  35. type ResolverLatencyMetric interface {
  36. Observe(ctx context.Context, host string, latency time.Duration)
  37. }
  38. // SizeMetric observes client response size partitioned by verb and host.
  39. type SizeMetric interface {
  40. Observe(ctx context.Context, verb string, host string, size float64)
  41. }
  42. // ResultMetric counts response codes partitioned by method and host.
  43. type ResultMetric interface {
  44. Increment(ctx context.Context, code string, method string, host string)
  45. }
  46. // CallsMetric counts calls that take place for a specific exec plugin.
  47. type CallsMetric interface {
  48. // Increment increments a counter per exitCode and callStatus.
  49. Increment(exitCode int, callStatus string)
  50. }
  51. // RetryMetric counts the number of retries sent to the server
  52. // partitioned by code, method, and host.
  53. type RetryMetric interface {
  54. IncrementRetry(ctx context.Context, code string, method string, host string)
  55. }
  56. // TransportCacheMetric shows the number of entries in the internal transport cache
  57. type TransportCacheMetric interface {
  58. Observe(value int)
  59. }
  60. // TransportCreateCallsMetric counts the number of times a transport is created
  61. // partitioned by the result of the cache: hit, miss, uncacheable
  62. type TransportCreateCallsMetric interface {
  63. Increment(result string)
  64. }
  65. var (
  66. // ClientCertExpiry is the expiry time of a client certificate
  67. ClientCertExpiry ExpiryMetric = noopExpiry{}
  68. // ClientCertRotationAge is the age of a certificate that has just been rotated.
  69. ClientCertRotationAge DurationMetric = noopDuration{}
  70. // RequestLatency is the latency metric that rest clients will update.
  71. RequestLatency LatencyMetric = noopLatency{}
  72. // ResolverLatency is the latency metric that DNS resolver will update
  73. ResolverLatency ResolverLatencyMetric = noopResolverLatency{}
  74. // RequestSize is the request size metric that rest clients will update.
  75. RequestSize SizeMetric = noopSize{}
  76. // ResponseSize is the response size metric that rest clients will update.
  77. ResponseSize SizeMetric = noopSize{}
  78. // RateLimiterLatency is the client side rate limiter latency metric.
  79. RateLimiterLatency LatencyMetric = noopLatency{}
  80. // RequestResult is the result metric that rest clients will update.
  81. RequestResult ResultMetric = noopResult{}
  82. // ExecPluginCalls is the number of calls made to an exec plugin, partitioned by
  83. // exit code and call status.
  84. ExecPluginCalls CallsMetric = noopCalls{}
  85. // RequestRetry is the retry metric that tracks the number of
  86. // retries sent to the server.
  87. RequestRetry RetryMetric = noopRetry{}
  88. // TransportCacheEntries is the metric that tracks the number of entries in the
  89. // internal transport cache.
  90. TransportCacheEntries TransportCacheMetric = noopTransportCache{}
  91. // TransportCreateCalls is the metric that counts the number of times a new transport
  92. // is created
  93. TransportCreateCalls TransportCreateCallsMetric = noopTransportCreateCalls{}
  94. )
  95. // RegisterOpts contains all the metrics to register. Metrics may be nil.
  96. type RegisterOpts struct {
  97. ClientCertExpiry ExpiryMetric
  98. ClientCertRotationAge DurationMetric
  99. RequestLatency LatencyMetric
  100. ResolverLatency ResolverLatencyMetric
  101. RequestSize SizeMetric
  102. ResponseSize SizeMetric
  103. RateLimiterLatency LatencyMetric
  104. RequestResult ResultMetric
  105. ExecPluginCalls CallsMetric
  106. RequestRetry RetryMetric
  107. TransportCacheEntries TransportCacheMetric
  108. TransportCreateCalls TransportCreateCallsMetric
  109. }
  110. // Register registers metrics for the rest client to use. This can
  111. // only be called once.
  112. func Register(opts RegisterOpts) {
  113. registerMetrics.Do(func() {
  114. if opts.ClientCertExpiry != nil {
  115. ClientCertExpiry = opts.ClientCertExpiry
  116. }
  117. if opts.ClientCertRotationAge != nil {
  118. ClientCertRotationAge = opts.ClientCertRotationAge
  119. }
  120. if opts.RequestLatency != nil {
  121. RequestLatency = opts.RequestLatency
  122. }
  123. if opts.ResolverLatency != nil {
  124. ResolverLatency = opts.ResolverLatency
  125. }
  126. if opts.RequestSize != nil {
  127. RequestSize = opts.RequestSize
  128. }
  129. if opts.ResponseSize != nil {
  130. ResponseSize = opts.ResponseSize
  131. }
  132. if opts.RateLimiterLatency != nil {
  133. RateLimiterLatency = opts.RateLimiterLatency
  134. }
  135. if opts.RequestResult != nil {
  136. RequestResult = opts.RequestResult
  137. }
  138. if opts.ExecPluginCalls != nil {
  139. ExecPluginCalls = opts.ExecPluginCalls
  140. }
  141. if opts.RequestRetry != nil {
  142. RequestRetry = opts.RequestRetry
  143. }
  144. if opts.TransportCacheEntries != nil {
  145. TransportCacheEntries = opts.TransportCacheEntries
  146. }
  147. if opts.TransportCreateCalls != nil {
  148. TransportCreateCalls = opts.TransportCreateCalls
  149. }
  150. })
  151. }
  152. type noopDuration struct{}
  153. func (noopDuration) Observe(time.Duration) {}
  154. type noopExpiry struct{}
  155. func (noopExpiry) Set(*time.Time) {}
  156. type noopLatency struct{}
  157. func (noopLatency) Observe(context.Context, string, url.URL, time.Duration) {}
  158. type noopResolverLatency struct{}
  159. func (n noopResolverLatency) Observe(ctx context.Context, host string, latency time.Duration) {
  160. }
  161. type noopSize struct{}
  162. func (noopSize) Observe(context.Context, string, string, float64) {}
  163. type noopResult struct{}
  164. func (noopResult) Increment(context.Context, string, string, string) {}
  165. type noopCalls struct{}
  166. func (noopCalls) Increment(int, string) {}
  167. type noopRetry struct{}
  168. func (noopRetry) IncrementRetry(context.Context, string, string, string) {}
  169. type noopTransportCache struct{}
  170. func (noopTransportCache) Observe(int) {}
  171. type noopTransportCreateCalls struct{}
  172. func (noopTransportCreateCalls) Increment(string) {}