Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2015 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | // Package metrics provides abstractions for registering which metrics |
| 18 | // to record. |
| 19 | package metrics |
| 20 | |
| 21 | import ( |
| 22 | "net/url" |
| 23 | "sync" |
| 24 | "time" |
| 25 | ) |
| 26 | |
| 27 | var registerMetrics sync.Once |
| 28 | |
| 29 | // DurationMetric is a measurement of some amount of time. |
| 30 | type DurationMetric interface { |
| 31 | Observe(duration time.Duration) |
| 32 | } |
| 33 | |
| 34 | // ExpiryMetric sets some time of expiry. If nil, assume not relevant. |
| 35 | type ExpiryMetric interface { |
| 36 | Set(expiry *time.Time) |
| 37 | } |
| 38 | |
| 39 | // LatencyMetric observes client latency partitioned by verb and url. |
| 40 | type LatencyMetric interface { |
| 41 | Observe(verb string, u url.URL, latency time.Duration) |
| 42 | } |
| 43 | |
| 44 | // ResultMetric counts response codes partitioned by method and host. |
| 45 | type ResultMetric interface { |
| 46 | Increment(code string, method string, host string) |
| 47 | } |
| 48 | |
| 49 | var ( |
| 50 | // ClientCertExpiry is the expiry time of a client certificate |
| 51 | ClientCertExpiry ExpiryMetric = noopExpiry{} |
| 52 | // ClientCertRotationAge is the age of a certificate that has just been rotated. |
| 53 | ClientCertRotationAge DurationMetric = noopDuration{} |
| 54 | // RequestLatency is the latency metric that rest clients will update. |
| 55 | RequestLatency LatencyMetric = noopLatency{} |
| 56 | // RateLimiterLatency is the client side rate limiter latency metric. |
| 57 | RateLimiterLatency LatencyMetric = noopLatency{} |
| 58 | // RequestResult is the result metric that rest clients will update. |
| 59 | RequestResult ResultMetric = noopResult{} |
| 60 | ) |
| 61 | |
| 62 | // RegisterOpts contains all the metrics to register. Metrics may be nil. |
| 63 | type RegisterOpts struct { |
| 64 | ClientCertExpiry ExpiryMetric |
| 65 | ClientCertRotationAge DurationMetric |
| 66 | RequestLatency LatencyMetric |
| 67 | RateLimiterLatency LatencyMetric |
| 68 | RequestResult ResultMetric |
| 69 | } |
| 70 | |
| 71 | // Register registers metrics for the rest client to use. This can |
| 72 | // only be called once. |
| 73 | func Register(opts RegisterOpts) { |
| 74 | registerMetrics.Do(func() { |
| 75 | if opts.ClientCertExpiry != nil { |
| 76 | ClientCertExpiry = opts.ClientCertExpiry |
| 77 | } |
| 78 | if opts.ClientCertRotationAge != nil { |
| 79 | ClientCertRotationAge = opts.ClientCertRotationAge |
| 80 | } |
| 81 | if opts.RequestLatency != nil { |
| 82 | RequestLatency = opts.RequestLatency |
| 83 | } |
| 84 | if opts.RateLimiterLatency != nil { |
| 85 | RateLimiterLatency = opts.RateLimiterLatency |
| 86 | } |
| 87 | if opts.RequestResult != nil { |
| 88 | RequestResult = opts.RequestResult |
| 89 | } |
| 90 | }) |
| 91 | } |
| 92 | |
| 93 | type noopDuration struct{} |
| 94 | |
| 95 | func (noopDuration) Observe(time.Duration) {} |
| 96 | |
| 97 | type noopExpiry struct{} |
| 98 | |
| 99 | func (noopExpiry) Set(*time.Time) {} |
| 100 | |
| 101 | type noopLatency struct{} |
| 102 | |
| 103 | func (noopLatency) Observe(string, url.URL, time.Duration) {} |
| 104 | |
| 105 | type noopResult struct{} |
| 106 | |
| 107 | func (noopResult) Increment(string, string, string) {} |