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