diff --git a/go.mod b/go.mod index 61749e41..09659af4 100644 --- a/go.mod +++ b/go.mod @@ -24,7 +24,7 @@ require ( golang.org/x/time v0.3.0 google.golang.org/protobuf v1.30.0 k8s.io/api v0.0.0-20230506223117-f3a0f2ed177a - k8s.io/apimachinery v0.0.0-20230508165628-e7958c5fe270 + k8s.io/apimachinery v0.0.0-20230510175910-f3f375c6bf8b k8s.io/klog/v2 v2.100.1 k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f k8s.io/utils v0.0.0-20230209194617-a36077c30491 @@ -60,5 +60,5 @@ require ( replace ( k8s.io/api => k8s.io/api v0.0.0-20230506223117-f3a0f2ed177a - k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20230508165628-e7958c5fe270 + k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20230510175910-f3f375c6bf8b ) diff --git a/go.sum b/go.sum index 9cf743b4..7915d33b 100644 --- a/go.sum +++ b/go.sum @@ -480,8 +480,8 @@ honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9 honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= k8s.io/api v0.0.0-20230506223117-f3a0f2ed177a h1:8sUE9zRxWwhC0FmKr0+Jtm6wgnL3ljlVJOT94jSVsO4= k8s.io/api v0.0.0-20230506223117-f3a0f2ed177a/go.mod h1:/fu24lnfAhrloAI7EhcGTa0fXXQH5r4rUqEQMW9endY= -k8s.io/apimachinery v0.0.0-20230508165628-e7958c5fe270 h1:0kz1rv3L87V/4KNEVPlst7yhT5RfAC+5JqeXR3rBXVc= -k8s.io/apimachinery v0.0.0-20230508165628-e7958c5fe270/go.mod h1:jF849JXyKVKRC0O62ZBSygt6qOSEYju8i90sKd1mx4g= +k8s.io/apimachinery v0.0.0-20230510175910-f3f375c6bf8b h1:WdIXsJtB+R/npU8V6XSRtCtBx0qSDvyrUhCQnHg/Fhw= +k8s.io/apimachinery v0.0.0-20230510175910-f3f375c6bf8b/go.mod h1:jF849JXyKVKRC0O62ZBSygt6qOSEYju8i90sKd1mx4g= k8s.io/klog/v2 v2.100.1 h1:7WCHKK6K8fNhTqfBhISHQ97KrnJNFZMcQvKp7gP/tmg= k8s.io/klog/v2 v2.100.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0= k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f h1:2kWPakN3i/k81b0gvD5C5FJ2kxm1WrQFanWchyKuqGg= diff --git a/tools/metrics/metrics.go b/tools/metrics/metrics.go index f36430dc..601c0733 100644 --- a/tools/metrics/metrics.go +++ b/tools/metrics/metrics.go @@ -64,6 +64,17 @@ type RetryMetric interface { IncrementRetry(ctx context.Context, code string, method string, host string) } +// TransportCacheMetric shows the number of entries in the internal transport cache +type TransportCacheMetric interface { + Observe(value int) +} + +// TransportCreateCallsMetric counts the number of times a transport is created +// partitioned by the result of the cache: hit, miss, uncacheable +type TransportCreateCallsMetric interface { + Increment(result string) +} + var ( // ClientCertExpiry is the expiry time of a client certificate ClientCertExpiry ExpiryMetric = noopExpiry{} @@ -85,6 +96,12 @@ var ( // RequestRetry is the retry metric that tracks the number of // retries sent to the server. RequestRetry RetryMetric = noopRetry{} + // TransportCacheEntries is the metric that tracks the number of entries in the + // internal transport cache. + TransportCacheEntries TransportCacheMetric = noopTransportCache{} + // TransportCreateCalls is the metric that counts the number of times a new transport + // is created + TransportCreateCalls TransportCreateCallsMetric = noopTransportCreateCalls{} ) // RegisterOpts contains all the metrics to register. Metrics may be nil. @@ -98,6 +115,8 @@ type RegisterOpts struct { RequestResult ResultMetric ExecPluginCalls CallsMetric RequestRetry RetryMetric + TransportCacheEntries TransportCacheMetric + TransportCreateCalls TransportCreateCallsMetric } // Register registers metrics for the rest client to use. This can @@ -131,6 +150,12 @@ func Register(opts RegisterOpts) { if opts.RequestRetry != nil { RequestRetry = opts.RequestRetry } + if opts.TransportCacheEntries != nil { + TransportCacheEntries = opts.TransportCacheEntries + } + if opts.TransportCreateCalls != nil { + TransportCreateCalls = opts.TransportCreateCalls + } }) } @@ -161,3 +186,11 @@ func (noopCalls) Increment(int, string) {} type noopRetry struct{} func (noopRetry) IncrementRetry(context.Context, string, string, string) {} + +type noopTransportCache struct{} + +func (noopTransportCache) Observe(int) {} + +type noopTransportCreateCalls struct{} + +func (noopTransportCreateCalls) Increment(string) {} diff --git a/transport/cache.go b/transport/cache.go index edcc6d1d..7c7f1b33 100644 --- a/transport/cache.go +++ b/transport/cache.go @@ -27,6 +27,7 @@ import ( utilnet "k8s.io/apimachinery/pkg/util/net" "k8s.io/apimachinery/pkg/util/wait" + "k8s.io/client-go/tools/metrics" ) // TlsTransportCache caches TLS http.RoundTrippers different configurations. The @@ -80,11 +81,16 @@ func (c *tlsTransportCache) get(config *Config) (http.RoundTripper, error) { // Ensure we only create a single transport for the given TLS options c.mu.Lock() defer c.mu.Unlock() + defer metrics.TransportCacheEntries.Observe(len(c.transports)) // See if we already have a custom transport for this config if t, ok := c.transports[key]; ok { + metrics.TransportCreateCalls.Increment("hit") return t, nil } + metrics.TransportCreateCalls.Increment("miss") + } else { + metrics.TransportCreateCalls.Increment("uncacheable") } // Get the TLS options for this client config