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