Skip to content

Commit

Permalink
feat(rrset): Add custom metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony TREUILLIER <[email protected]>
  • Loading branch information
antrema committed Oct 30, 2024
1 parent cdd4558 commit 79dfe9d
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 2 deletions.
2 changes: 1 addition & 1 deletion config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ resources:
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
#- ../certmanager
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
#- ../prometheus
- ../prometheus

patches:
- path: manager_env_patch.yaml
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/joeig/go-powerdns/v3 v3.14.1
github.com/onsi/ginkgo/v2 v2.20.0
github.com/onsi/gomega v1.34.1
github.com/prometheus/client_golang v1.20.0
k8s.io/apimachinery v0.31.0
k8s.io/client-go v0.31.0
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8
Expand Down Expand Up @@ -38,12 +39,12 @@ require (
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.20.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
Expand Down
49 changes: 49 additions & 0 deletions internal/controller/pdns_metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package controller

import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
)

var (
rrsetsStatusesMetric = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "rrsets_status",
Help: "Statuses of RRsets processed",
},
[]string{"fqdn", "type", "status", "name", "namespace"},
)
)

func updateRrsetsMetrics(fqdn, rrsetType, rrsetStatus, name, namespace string) {
rrsetsStatusesMetric.With(map[string]string{
"fqdn": fqdn,
"type": rrsetType,
"status": rrsetStatus,
"name": name,
"namespace": namespace,
}).Set(1)
}
func removeRrsetMetrics(name, namespace string) {
rrsetsStatusesMetric.DeletePartialMatch(
map[string]string{
"namespace": namespace,
"name": name,
},
)
}

//nolint:unparam
func getMetricWithLabels(rrsetFQDN, rrsetType, rrsetStatus, rrsetName, rrsetNamespace string) float64 {
return testutil.ToFloat64(rrsetsStatusesMetric.With(prometheus.Labels{
"fqdn": rrsetFQDN,
"type": rrsetType,
"status": rrsetStatus,
"name": rrsetName,
"namespace": rrsetNamespace,
}))
}

func countMetrics() int {
return testutil.CollectAndCount(rrsetsStatusesMetric)
}
21 changes: 21 additions & 0 deletions internal/controller/rrset_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/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/metrics"

dnsv1alpha1 "github.com/orange-opensource/powerdns-operator/api/v1alpha1"
)
Expand All @@ -40,6 +41,11 @@ type RRsetReconciler struct {
PDNSClient PdnsClienter
}

func init() {
// Register custom metrics with the global prometheus registry
metrics.Registry.MustRegister(rrsetsStatusesMetric)
}

// +kubebuilder:rbac:groups=dns.cav.enablers.ob,resources=rrsets,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=dns.cav.enablers.ob,resources=rrsets/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=dns.cav.enablers.ob,resources=rrsets/finalizers,verbs=update
Expand Down Expand Up @@ -77,6 +83,8 @@ func (r *RRsetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
log.Error(err, "Failed to remove finalizer")
return ctrl.Result{}, err
}
// Remove resource metrics
removeRrsetMetrics(rrset.Name, rrset.Namespace)
}
// Race condition when creating Zone+RRset at the same time
// RRset is not created because Zone is not created yet
Expand Down Expand Up @@ -119,6 +127,9 @@ func (r *RRsetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
log.Error(err, "Failed to remove finalizer")
return ctrl.Result{}, err
}
// Remove resource metrics
removeRrsetMetrics(rrset.Name, rrset.Namespace)

//nolint:ineffassign
lastUpdateTime = &metav1.Time{Time: time.Now().UTC()}
}
Expand All @@ -129,6 +140,9 @@ func (r *RRsetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl

// We cannot exit previously (at the early moments of reconcile), because we have to allow deletion process
if isInFailedStatus {
// Update resource metrics
updateRrsetsMetrics(getRRsetName(rrset), rrset.Spec.Type, *rrset.Status.SyncStatus, rrset.Name, rrset.Namespace)

return ctrl.Result{}, nil
}

Expand All @@ -151,6 +165,10 @@ func (r *RRsetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
log.Error(err, "unable to patch RRSet status")
return ctrl.Result{}, err
}

// Update resource metrics
updateRrsetsMetrics(getRRsetName(rrset), rrset.Spec.Type, *rrset.Status.SyncStatus, rrset.Name, rrset.Namespace)

return ctrl.Result{}, nil
}

Expand Down Expand Up @@ -195,6 +213,9 @@ func (r *RRsetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
return ctrl.Result{}, err
}

// Metrics calculation
updateRrsetsMetrics(getRRsetName(rrset), rrset.Spec.Type, *rrset.Status.SyncStatus, rrset.Name, rrset.Namespace)

return ctrl.Result{}, nil
}

Expand Down
Loading

0 comments on commit 79dfe9d

Please sign in to comment.