Merge pull request #117295 from aojea/transport_cache_metrics

add new metric for the internal client-go cache size

Kubernetes-commit: 64af2d93e5b97e41bb078e2fa81923fda6681154
This commit is contained in:
Kubernetes Publisher 2023-05-11 08:59:02 -07:00
commit 23a9e82799
4 changed files with 43 additions and 4 deletions

4
go.mod
View File

@ -24,7 +24,7 @@ require (
golang.org/x/time v0.3.0 golang.org/x/time v0.3.0
google.golang.org/protobuf v1.30.0 google.golang.org/protobuf v1.30.0
k8s.io/api v0.0.0-20230506223117-f3a0f2ed177a 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/klog/v2 v2.100.1
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f
k8s.io/utils v0.0.0-20230209194617-a36077c30491 k8s.io/utils v0.0.0-20230209194617-a36077c30491
@ -60,5 +60,5 @@ require (
replace ( replace (
k8s.io/api => k8s.io/api v0.0.0-20230506223117-f3a0f2ed177a 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
) )

4
go.sum
View File

@ -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= 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 h1:8sUE9zRxWwhC0FmKr0+Jtm6wgnL3ljlVJOT94jSVsO4=
k8s.io/api v0.0.0-20230506223117-f3a0f2ed177a/go.mod h1:/fu24lnfAhrloAI7EhcGTa0fXXQH5r4rUqEQMW9endY= 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-20230510175910-f3f375c6bf8b h1:WdIXsJtB+R/npU8V6XSRtCtBx0qSDvyrUhCQnHg/Fhw=
k8s.io/apimachinery v0.0.0-20230508165628-e7958c5fe270/go.mod h1:jF849JXyKVKRC0O62ZBSygt6qOSEYju8i90sKd1mx4g= 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 h1:7WCHKK6K8fNhTqfBhISHQ97KrnJNFZMcQvKp7gP/tmg=
k8s.io/klog/v2 v2.100.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0= k8s.io/klog/v2 v2.100.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f h1:2kWPakN3i/k81b0gvD5C5FJ2kxm1WrQFanWchyKuqGg= k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f h1:2kWPakN3i/k81b0gvD5C5FJ2kxm1WrQFanWchyKuqGg=

View File

@ -64,6 +64,17 @@ type RetryMetric interface {
IncrementRetry(ctx context.Context, code string, method string, host string) 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 ( var (
// ClientCertExpiry is the expiry time of a client certificate // ClientCertExpiry is the expiry time of a client certificate
ClientCertExpiry ExpiryMetric = noopExpiry{} ClientCertExpiry ExpiryMetric = noopExpiry{}
@ -85,6 +96,12 @@ var (
// RequestRetry is the retry metric that tracks the number of // RequestRetry is the retry metric that tracks the number of
// retries sent to the server. // retries sent to the server.
RequestRetry RetryMetric = noopRetry{} 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. // RegisterOpts contains all the metrics to register. Metrics may be nil.
@ -98,6 +115,8 @@ type RegisterOpts struct {
RequestResult ResultMetric RequestResult ResultMetric
ExecPluginCalls CallsMetric ExecPluginCalls CallsMetric
RequestRetry RetryMetric RequestRetry RetryMetric
TransportCacheEntries TransportCacheMetric
TransportCreateCalls TransportCreateCallsMetric
} }
// Register registers metrics for the rest client to use. This can // Register registers metrics for the rest client to use. This can
@ -131,6 +150,12 @@ func Register(opts RegisterOpts) {
if opts.RequestRetry != nil { if opts.RequestRetry != nil {
RequestRetry = opts.RequestRetry 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{} type noopRetry struct{}
func (noopRetry) IncrementRetry(context.Context, string, string, string) {} func (noopRetry) IncrementRetry(context.Context, string, string, string) {}
type noopTransportCache struct{}
func (noopTransportCache) Observe(int) {}
type noopTransportCreateCalls struct{}
func (noopTransportCreateCalls) Increment(string) {}

View File

@ -27,6 +27,7 @@ import (
utilnet "k8s.io/apimachinery/pkg/util/net" utilnet "k8s.io/apimachinery/pkg/util/net"
"k8s.io/apimachinery/pkg/util/wait" "k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/metrics"
) )
// TlsTransportCache caches TLS http.RoundTrippers different configurations. The // 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 // Ensure we only create a single transport for the given TLS options
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
defer metrics.TransportCacheEntries.Observe(len(c.transports))
// See if we already have a custom transport for this config // See if we already have a custom transport for this config
if t, ok := c.transports[key]; ok { if t, ok := c.transports[key]; ok {
metrics.TransportCreateCalls.Increment("hit")
return t, nil return t, nil
} }
metrics.TransportCreateCalls.Increment("miss")
} else {
metrics.TransportCreateCalls.Increment("uncacheable")
} }
// Get the TLS options for this client config // Get the TLS options for this client config