Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [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 | // LatencyMetric observes client latency partitioned by verb and url. |
| 30 | type LatencyMetric interface { |
| 31 | Observe(verb string, u url.URL, latency time.Duration) |
| 32 | } |
| 33 | |
| 34 | // ResultMetric counts response codes partitioned by method and host. |
| 35 | type ResultMetric interface { |
| 36 | Increment(code string, method string, host string) |
| 37 | } |
| 38 | |
| 39 | var ( |
| 40 | // RequestLatency is the latency metric that rest clients will update. |
| 41 | RequestLatency LatencyMetric = noopLatency{} |
| 42 | // RequestResult is the result metric that rest clients will update. |
| 43 | RequestResult ResultMetric = noopResult{} |
| 44 | ) |
| 45 | |
| 46 | // Register registers metrics for the rest client to use. This can |
| 47 | // only be called once. |
| 48 | func Register(lm LatencyMetric, rm ResultMetric) { |
| 49 | registerMetrics.Do(func() { |
| 50 | RequestLatency = lm |
| 51 | RequestResult = rm |
| 52 | }) |
| 53 | } |
| 54 | |
| 55 | type noopLatency struct{} |
| 56 | |
| 57 | func (noopLatency) Observe(string, url.URL, time.Duration) {} |
| 58 | |
| 59 | type noopResult struct{} |
| 60 | |
| 61 | func (noopResult) Increment(string, string, string) {} |