Skip to content

Commit

Permalink
Fix MR state metrics when MRs are deleted
Browse files Browse the repository at this point in the history
Signed-off-by: ezgidemirel <[email protected]>
  • Loading branch information
ezgidemirel committed May 3, 2024
1 parent 8641eb2 commit d0da68d
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions pkg/statemetrics/mr_state_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/prometheus/client_golang/prometheus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
Expand Down Expand Up @@ -81,18 +82,20 @@ type MRStateRecorder struct {
log logging.Logger
interval time.Duration
managedList resource.ManagedList
gvk schema.GroupVersionKind

metrics *MRStateMetrics
}

// NewMRStateRecorder returns a new MRStateRecorder which records the state of managed resources.
func NewMRStateRecorder(client client.Client, log logging.Logger, metrics *MRStateMetrics, managedList resource.ManagedList, interval time.Duration) *MRStateRecorder {
func NewMRStateRecorder(client client.Client, log logging.Logger, metrics *MRStateMetrics, managedList resource.ManagedList, gvk schema.GroupVersionKind, interval time.Duration) *MRStateRecorder {
return &MRStateRecorder{
client: client,
log: log,
metrics: metrics,
managedList: managedList,
interval: interval,
gvk: gvk,
}
}

Expand All @@ -104,12 +107,8 @@ func (r *MRStateRecorder) Record(ctx context.Context, mrList resource.ManagedLis
}

mrs := mrList.GetItems()
if len(mrs) == 0 {
return nil
}

label := mrs[0].GetObjectKind().GroupVersionKind().String()
r.metrics.Exists.WithLabelValues(label).Set(float64(len(mrs)))
label := prometheus.Labels{"gvk": r.gvk.String()}
r.metrics.Exists.With(label).Set(float64(len(mrs)))

var numReady, numSynced float64 = 0, 0
for _, o := range mrs {
Expand All @@ -122,8 +121,8 @@ func (r *MRStateRecorder) Record(ctx context.Context, mrList resource.ManagedLis
}
}

r.metrics.Ready.WithLabelValues(label).Set(numReady)
r.metrics.Synced.WithLabelValues(label).Set(numSynced)
r.metrics.Ready.With(label).Set(numReady)
r.metrics.Synced.With(label).Set(numSynced)

return nil
}
Expand Down

0 comments on commit d0da68d

Please sign in to comment.