Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add registry metrics collectors #983

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions controllers/hcpvaultsecretsapp_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"

"github.com/hashicorp/vault-secrets-operator/credentials"
"github.com/hashicorp/vault-secrets-operator/credentials/hcp"
Expand Down Expand Up @@ -302,9 +303,16 @@ func (r *HCPVaultSecretsAppReconciler) updateStatus(ctx context.Context, o *secr
// SetupWithManager sets up the controller with the Manager.
func (r *HCPVaultSecretsAppReconciler) SetupWithManager(mgr ctrl.Manager, opts controller.Options) error {
r.referenceCache = newResourceReferenceCache()
ctrlmetrics.Registry.MustRegister(
newResourceReferenceCacheCollector(r.referenceCache, "hcpvaultsecretsapp"),
)

if r.BackOffRegistry == nil {
r.BackOffRegistry = NewBackOffRegistry()
}
ctrlmetrics.Registry.MustRegister(
newBackoffRegistryCacheCollector(r.BackOffRegistry, "hcpvaultsecretsapp"),
)

return ctrl.NewControllerManagedBy(mgr).
For(&secretsv1beta1.HCPVaultSecretsApp{}).
Expand Down
79 changes: 79 additions & 0 deletions controllers/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import (
"time"

"github.com/cenkalti/backoff/v4"
"github.com/prometheus/client_golang/prometheus"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/hashicorp/vault-secrets-operator/internal/metrics"
)

type ResourceKind int
Expand Down Expand Up @@ -49,6 +52,7 @@ type ResourceReferenceCache interface {
Get(ResourceKind, client.ObjectKey) []client.ObjectKey
Remove(ResourceKind, client.ObjectKey) bool
Prune(ResourceKind, client.ObjectKey) int
Len() int
}

var _ ResourceReferenceCache = (*resourceReferenceCache)(nil)
Expand Down Expand Up @@ -80,6 +84,20 @@ type resourceReferenceCache struct {
mu sync.RWMutex
}

func (c *resourceReferenceCache) Len() int {
c.mu.RLock()
defer c.mu.RUnlock()

var l int
for _, scope := range c.m {
for _, refs := range scope {
l += len(refs)
}
}

return l
}

// Set references of kind for referrer.
func (c *resourceReferenceCache) Set(kind ResourceKind, referrer client.ObjectKey, references ...client.ObjectKey) {
c.mu.Lock()
Expand Down Expand Up @@ -274,6 +292,13 @@ func (r *BackOffRegistry) Get(objKey client.ObjectKey) (*BackOff, bool) {
return entry, !ok
}

func (r *BackOffRegistry) Len() int {
r.mu.RLock()
defer r.mu.RUnlock()

return len(r.m)
}

// BackOff is a wrapper around backoff.BackOff that does not implement
// BackOff.Reset, since elements in BackOffRegistry are meant to be ephemeral.
type BackOff struct {
Expand Down Expand Up @@ -304,3 +329,57 @@ func NewBackOffRegistry(opts ...backoff.ExponentialBackOffOpts) *BackOffRegistry
opts: opts,
}
}

// registryCacheCollector provides a prometheus.Collector for ClientCache metrics.
type registryCacheCollector struct {
cache ResourceReferenceCache
size float64
lenDesc *prometheus.Desc
}

func (c registryCacheCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.lenDesc
}

func (c registryCacheCollector) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(c.lenDesc, prometheus.GaugeValue, float64(c.cache.Len()))
}

func newResourceReferenceCacheCollector(cache ResourceReferenceCache, name string) prometheus.Collector {
metricsFQNClientCacheLength := prometheus.BuildFQName(
metrics.Namespace, "rsc_ref_cache", metrics.NameLength)
return &registryCacheCollector{
cache: cache,
lenDesc: prometheus.NewDesc(
metricsFQNClientCacheLength,
"Number of object references in the cache.",
nil, map[string]string{"name": name}),
}
}

// backoffRegistryCacheCollector
type backoffRegistryCacheCollector struct {
cache *BackOffRegistry
size float64
lenDesc *prometheus.Desc
}

func (c backoffRegistryCacheCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.lenDesc
}

func (c backoffRegistryCacheCollector) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(c.lenDesc, prometheus.GaugeValue, float64(c.cache.Len()))
}

func newBackoffRegistryCacheCollector(cache *BackOffRegistry, name string) prometheus.Collector {
metricsFQNClientCacheLength := prometheus.BuildFQName(
metrics.Namespace, "backoff_registry_cache", metrics.NameLength)
return &backoffRegistryCacheCollector{
cache: cache,
lenDesc: prometheus.NewDesc(
metricsFQNClientCacheLength,
"Number of backoff objects in the cache.",
nil, map[string]string{"name": name}),
}
}
2 changes: 1 addition & 1 deletion controllers/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func Test_resourceReferenceCache(t *testing.T) {
{
action: 0,
referrer: referrer1,
references: []client.ObjectKey{reference1},
references: []client.ObjectKey{reference1, reference1, reference1},
},
},
m: refCacheMap{
Expand Down
5 changes: 5 additions & 0 deletions controllers/vaultauth_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
"sigs.k8s.io/controller-runtime/pkg/predicate"

secretsv1beta1 "github.com/hashicorp/vault-secrets-operator/api/v1beta1"
Expand Down Expand Up @@ -241,6 +242,10 @@ func (r *VaultAuthReconciler) handleFinalizer(ctx context.Context, o *secretsv1b
// SetupWithManager sets up the controller with the Manager.
func (r *VaultAuthReconciler) SetupWithManager(mgr ctrl.Manager) error {
r.referenceCache = newResourceReferenceCache()
ctrlmetrics.Registry.MustRegister(
newResourceReferenceCacheCollector(r.referenceCache, "vaultauth"),
)

return ctrl.NewControllerManagedBy(mgr).
For(&secretsv1beta1.VaultAuth{}).
Watches(
Expand Down
9 changes: 8 additions & 1 deletion controllers/vaultdynamicsecret_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/builder"
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"

ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down Expand Up @@ -596,9 +597,15 @@ func (r *VaultDynamicSecretReconciler) renewLease(
// SetupWithManager sets up the controller with the Manager.
func (r *VaultDynamicSecretReconciler) SetupWithManager(mgr ctrl.Manager, opts controller.Options) error {
r.referenceCache = newResourceReferenceCache()
ctrlmetrics.Registry.MustRegister(
newResourceReferenceCacheCollector(r.referenceCache, "vaultdynamicsecret"),
)
if r.BackOffRegistry == nil {
r.BackOffRegistry = NewBackOffRegistry()
}
ctrlmetrics.Registry.MustRegister(
newBackoffRegistryCacheCollector(r.BackOffRegistry, "vaultdynamicsecret"),
)

r.ClientFactory.RegisterClientCallbackHandler(
vault.ClientCallbackHandler{
Expand Down
8 changes: 8 additions & 0 deletions controllers/vaultpkisecret_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"

secretsv1beta1 "github.com/hashicorp/vault-secrets-operator/api/v1beta1"
"github.com/hashicorp/vault-secrets-operator/consts"
Expand Down Expand Up @@ -347,8 +348,15 @@ func (r *VaultPKISecretReconciler) handleDeletion(ctx context.Context, o *secret

func (r *VaultPKISecretReconciler) SetupWithManager(mgr ctrl.Manager, opts controller.Options) error {
r.referenceCache = newResourceReferenceCache()
ctrlmetrics.Registry.MustRegister(
newResourceReferenceCacheCollector(r.referenceCache, "vaultpkisecret"),
)

if r.BackOffRegistry == nil {
r.BackOffRegistry = NewBackOffRegistry()
ctrlmetrics.Registry.MustRegister(
newBackoffRegistryCacheCollector(r.BackOffRegistry, "vaultpkisecret"),
)
}
return ctrl.NewControllerManagedBy(mgr).
For(&secretsv1beta1.VaultPKISecret{}).
Expand Down
9 changes: 9 additions & 0 deletions controllers/vaultstaticsecret_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/log"
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
"sigs.k8s.io/controller-runtime/pkg/source"

"github.com/cenkalti/backoff/v4"
Expand Down Expand Up @@ -500,9 +501,17 @@ func (r *VaultStaticSecretReconciler) streamStaticSecretEvents(ctx context.Conte

func (r *VaultStaticSecretReconciler) SetupWithManager(mgr ctrl.Manager, opts controller.Options) error {
r.referenceCache = newResourceReferenceCache()
ctrlmetrics.Registry.MustRegister(
newResourceReferenceCacheCollector(r.referenceCache, "vaultstaticsecret"),
)

if r.BackOffRegistry == nil {
r.BackOffRegistry = NewBackOffRegistry()
}
ctrlmetrics.Registry.MustRegister(
newBackoffRegistryCacheCollector(r.BackOffRegistry, "vaultstaticsecret"),
)

r.SourceCh = make(chan event.GenericEvent)
r.eventWatcherRegistry = newEventWatcherRegistry()

Expand Down
Loading