metrics.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. // SizeMetric observes client response size partitioned by verb and host.
  36. type SizeMetric interface {
  37. Observe(ctx context.Context, verb string, host string, size float64)
  38. }
  39. // ResultMetric counts response codes partitioned by method and host.
  40. type ResultMetric interface {
  41. Increment(ctx context.Context, code string, method string, host string)
  42. }
  43. // CallsMetric counts calls that take place for a specific exec plugin.
  44. type CallsMetric interface {
  45. // Increment increments a counter per exitCode and callStatus.
  46. Increment(exitCode int, callStatus string)
  47. }
  48. var (
  49. // ClientCertExpiry is the expiry time of a client certificate
  50. ClientCertExpiry ExpiryMetric = noopExpiry{}
  51. // ClientCertRotationAge is the age of a certificate that has just been rotated.
  52. ClientCertRotationAge DurationMetric = noopDuration{}
  53. // RequestLatency is the latency metric that rest clients will update.
  54. RequestLatency LatencyMetric = noopLatency{}
  55. // RequestSize is the request size metric that rest clients will update.
  56. RequestSize SizeMetric = noopSize{}
  57. // ResponseSize is the response size metric that rest clients will update.
  58. ResponseSize SizeMetric = noopSize{}
  59. // RateLimiterLatency is the client side rate limiter latency metric.
  60. RateLimiterLatency LatencyMetric = noopLatency{}
  61. // RequestResult is the result metric that rest clients will update.
  62. RequestResult ResultMetric = noopResult{}
  63. // ExecPluginCalls is the number of calls made to an exec plugin, partitioned by
  64. // exit code and call status.
  65. ExecPluginCalls CallsMetric = noopCalls{}
  66. )
  67. // RegisterOpts contains all the metrics to register. Metrics may be nil.
  68. type RegisterOpts struct {
  69. ClientCertExpiry ExpiryMetric
  70. ClientCertRotationAge DurationMetric
  71. RequestLatency LatencyMetric
  72. RequestSize SizeMetric
  73. ResponseSize SizeMetric
  74. RateLimiterLatency LatencyMetric
  75. RequestResult ResultMetric
  76. ExecPluginCalls CallsMetric
  77. }
  78. // Register registers metrics for the rest client to use. This can
  79. // only be called once.
  80. func Register(opts RegisterOpts) {
  81. registerMetrics.Do(func() {
  82. if opts.ClientCertExpiry != nil {
  83. ClientCertExpiry = opts.ClientCertExpiry
  84. }
  85. if opts.ClientCertRotationAge != nil {
  86. ClientCertRotationAge = opts.ClientCertRotationAge
  87. }
  88. if opts.RequestLatency != nil {
  89. RequestLatency = opts.RequestLatency
  90. }
  91. if opts.RequestSize != nil {
  92. RequestSize = opts.RequestSize
  93. }
  94. if opts.ResponseSize != nil {
  95. ResponseSize = opts.ResponseSize
  96. }
  97. if opts.RateLimiterLatency != nil {
  98. RateLimiterLatency = opts.RateLimiterLatency
  99. }
  100. if opts.RequestResult != nil {
  101. RequestResult = opts.RequestResult
  102. }
  103. if opts.ExecPluginCalls != nil {
  104. ExecPluginCalls = opts.ExecPluginCalls
  105. }
  106. })
  107. }
  108. type noopDuration struct{}
  109. func (noopDuration) Observe(time.Duration) {}
  110. type noopExpiry struct{}
  111. func (noopExpiry) Set(*time.Time) {}
  112. type noopLatency struct{}
  113. func (noopLatency) Observe(context.Context, string, url.URL, time.Duration) {}
  114. type noopSize struct{}
  115. func (noopSize) Observe(context.Context, string, string, float64) {}
  116. type noopResult struct{}
  117. func (noopResult) Increment(context.Context, string, string, string) {}
  118. type noopCalls struct{}
  119. func (noopCalls) Increment(int, string) {}