metrics.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. // CallsMetric counts the success or failure of execution for exec plugins.
  52. type PolicyCallsMetric interface {
  53. // Increment increments a counter per status { "allowed", "denied" }
  54. Increment(status string)
  55. }
  56. // RetryMetric counts the number of retries sent to the server
  57. // partitioned by code, method, and host.
  58. type RetryMetric interface {
  59. IncrementRetry(ctx context.Context, code string, method string, host string)
  60. }
  61. // TransportCacheMetric shows the number of entries in the internal transport cache
  62. type TransportCacheMetric interface {
  63. Observe(value int)
  64. }
  65. // TransportCreateCallsMetric counts the number of times a transport is created
  66. // partitioned by the result of the cache: hit, miss, uncacheable
  67. type TransportCreateCallsMetric interface {
  68. Increment(result string)
  69. }
  70. var (
  71. // ClientCertExpiry is the expiry time of a client certificate
  72. ClientCertExpiry ExpiryMetric = noopExpiry{}
  73. // ClientCertRotationAge is the age of a certificate that has just been rotated.
  74. ClientCertRotationAge DurationMetric = noopDuration{}
  75. // RequestLatency is the latency metric that rest clients will update.
  76. RequestLatency LatencyMetric = noopLatency{}
  77. // ResolverLatency is the latency metric that DNS resolver will update
  78. ResolverLatency ResolverLatencyMetric = noopResolverLatency{}
  79. // RequestSize is the request size metric that rest clients will update.
  80. RequestSize SizeMetric = noopSize{}
  81. // ResponseSize is the response size metric that rest clients will update.
  82. ResponseSize SizeMetric = noopSize{}
  83. // RateLimiterLatency is the client side rate limiter latency metric.
  84. RateLimiterLatency LatencyMetric = noopLatency{}
  85. // RequestResult is the result metric that rest clients will update.
  86. RequestResult ResultMetric = noopResult{}
  87. // ExecPluginCalls is the number of calls made to an exec plugin, partitioned by
  88. // exit code and call status.
  89. ExecPluginCalls CallsMetric = noopCalls{}
  90. // ExecPluginPolicyCalls is the number of plugin policy check calls, partitioned
  91. // by {"allowed", "denied"}
  92. ExecPluginPolicyCalls PolicyCallsMetric = noopPolicy{}
  93. // RequestRetry is the retry metric that tracks the number of
  94. // retries sent to the server.
  95. RequestRetry RetryMetric = noopRetry{}
  96. // TransportCacheEntries is the metric that tracks the number of entries in the
  97. // internal transport cache.
  98. TransportCacheEntries TransportCacheMetric = noopTransportCache{}
  99. // TransportCreateCalls is the metric that counts the number of times a new transport
  100. // is created
  101. TransportCreateCalls TransportCreateCallsMetric = noopTransportCreateCalls{}
  102. )
  103. // RegisterOpts contains all the metrics to register. Metrics may be nil.
  104. type RegisterOpts struct {
  105. ClientCertExpiry ExpiryMetric
  106. ClientCertRotationAge DurationMetric
  107. RequestLatency LatencyMetric
  108. ResolverLatency ResolverLatencyMetric
  109. RequestSize SizeMetric
  110. ResponseSize SizeMetric
  111. RateLimiterLatency LatencyMetric
  112. RequestResult ResultMetric
  113. ExecPluginCalls CallsMetric
  114. ExecPluginPolicyCalls PolicyCallsMetric
  115. RequestRetry RetryMetric
  116. TransportCacheEntries TransportCacheMetric
  117. TransportCreateCalls TransportCreateCallsMetric
  118. }
  119. // Register registers metrics for the rest client to use. This can
  120. // only be called once.
  121. func Register(opts RegisterOpts) {
  122. registerMetrics.Do(func() {
  123. if opts.ClientCertExpiry != nil {
  124. ClientCertExpiry = opts.ClientCertExpiry
  125. }
  126. if opts.ClientCertRotationAge != nil {
  127. ClientCertRotationAge = opts.ClientCertRotationAge
  128. }
  129. if opts.RequestLatency != nil {
  130. RequestLatency = opts.RequestLatency
  131. }
  132. if opts.ResolverLatency != nil {
  133. ResolverLatency = opts.ResolverLatency
  134. }
  135. if opts.RequestSize != nil {
  136. RequestSize = opts.RequestSize
  137. }
  138. if opts.ResponseSize != nil {
  139. ResponseSize = opts.ResponseSize
  140. }
  141. if opts.RateLimiterLatency != nil {
  142. RateLimiterLatency = opts.RateLimiterLatency
  143. }
  144. if opts.RequestResult != nil {
  145. RequestResult = opts.RequestResult
  146. }
  147. if opts.ExecPluginCalls != nil {
  148. ExecPluginCalls = opts.ExecPluginCalls
  149. }
  150. if opts.ExecPluginPolicyCalls != nil {
  151. ExecPluginCalls = opts.ExecPluginCalls
  152. }
  153. if opts.RequestRetry != nil {
  154. RequestRetry = opts.RequestRetry
  155. }
  156. if opts.TransportCacheEntries != nil {
  157. TransportCacheEntries = opts.TransportCacheEntries
  158. }
  159. if opts.TransportCreateCalls != nil {
  160. TransportCreateCalls = opts.TransportCreateCalls
  161. }
  162. })
  163. }
  164. type noopDuration struct{}
  165. func (noopDuration) Observe(time.Duration) {}
  166. type noopExpiry struct{}
  167. func (noopExpiry) Set(*time.Time) {}
  168. type noopLatency struct{}
  169. func (noopLatency) Observe(context.Context, string, url.URL, time.Duration) {}
  170. type noopResolverLatency struct{}
  171. func (n noopResolverLatency) Observe(ctx context.Context, host string, latency time.Duration) {
  172. }
  173. type noopSize struct{}
  174. func (noopSize) Observe(context.Context, string, string, float64) {}
  175. type noopResult struct{}
  176. func (noopResult) Increment(context.Context, string, string, string) {}
  177. type noopCalls struct{}
  178. func (noopCalls) Increment(int, string) {}
  179. type noopPolicy struct{}
  180. func (noopPolicy) Increment(string) {}
  181. type noopRetry struct{}
  182. func (noopRetry) IncrementRetry(context.Context, string, string, string) {}
  183. type noopTransportCache struct{}
  184. func (noopTransportCache) Observe(int) {}
  185. type noopTransportCreateCalls struct{}
  186. func (noopTransportCreateCalls) Increment(string) {}