Skip to content

Commit

Permalink
Instrument client object refs metrics
Browse files Browse the repository at this point in the history
Adds a new gauge metric with the cache_key label.

E.g:
 vso_client_factory_client_refs{cache_key="kubernetes-f42f026de00efe6e8aff24"} 1
  • Loading branch information
benashz committed Jun 12, 2024
1 parent 3377c25 commit 4d3e597
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
NameRequestsTotal = "requests_total"
NameRequestsErrorsTotal = "requests_errors_total"
NameTaintedClients = "tainted_clients"
NameClientRefs = "client_refs"
)

var ResourceStatus = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Expand Down
23 changes: 23 additions & 0 deletions internal/vault/client_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ type cachingClientFactory struct {
requestCounterVec *prometheus.CounterVec
requestErrorCounterVec *prometheus.CounterVec
taintedClientGauge *prometheus.GaugeVec
clientRefGauge *prometheus.GaugeVec
revokeOnEvict bool
pruneStorageOnEvict bool
ctrlClient ctrlclient.Client
Expand Down Expand Up @@ -299,6 +300,9 @@ func (m *cachingClientFactory) onClientEvict(ctx context.Context, client ctrlcli
}

m.removeClientLock(cacheKey)
if m.clientRefGauge != nil {
m.clientRefGauge.DeleteLabelValues(cacheKey.String())
}
}

// Restore will attempt to restore a Client from storage. If storage is not enabled then no restoration will take place.
Expand Down Expand Up @@ -590,6 +594,11 @@ func (m *cachingClientFactory) updateClientStatsAfterGet(ctx context.Context, ca
"creationTimestamp", c.Stat().CreationTimestamp(),
"reason", decrementReason,
)
if m.clientRefGauge != nil {
m.clientRefGauge.WithLabelValues(cacheKey.String()).Set(
float64(c.Stat().RefCount()),
)
}
// send the client to the cache pruner.
m.orphanPrunerClientCh <- c
}
Expand All @@ -604,6 +613,11 @@ func (m *cachingClientFactory) updateClientStatsAfterGet(ctx context.Context, ca
"creationTimestamp", c.Stat().CreationTimestamp(),
"reason", incrementReason,
)
if m.clientRefGauge != nil {
m.clientRefGauge.WithLabelValues(cacheKey.String()).Set(
float64(c.Stat().RefCount()),
)
}
}
}
}
Expand Down Expand Up @@ -1004,6 +1018,14 @@ func NewCachingClientFactory(ctx context.Context, client ctrlclient.Client, cach
metrics.LabelCacheKey,
},
),
clientRefGauge: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: metricsFQNClientRefs,
Help: "Client factory number of object references",
}, []string{
metrics.LabelCacheKey,
},
),
}

if config.CollectClientCacheMetrics {
Expand All @@ -1016,6 +1038,7 @@ func NewCachingClientFactory(ctx context.Context, client ctrlclient.Client, cach
factory.requestCounterVec,
factory.requestErrorCounterVec,
factory.taintedClientGauge,
factory.clientRefGauge,
clientFactoryOperationTimes,
)
}
Expand Down
3 changes: 3 additions & 0 deletions internal/vault/client_factory_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ var (
metricsFQNClientFactoryTaintedClients = prometheus.BuildFQName(
metrics.Namespace, subsystemClientFactory, metrics.NameTaintedClients)

metricsFQNClientRefs = prometheus.BuildFQName(
metrics.Namespace, subsystemClientFactory, metrics.NameClientRefs)

// TODO: update to use Native Histograms once it is no longer an experimental Prometheus feature
// ref: https://github.com/prometheus/prometheus/milestone/10
clientFactoryOperationTimes = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Expand Down

0 comments on commit 4d3e597

Please sign in to comment.