Skip to content

Commit

Permalink
add gauge metric and set its value
Browse files Browse the repository at this point in the history
  • Loading branch information
johnazariah committed Nov 18, 2024
1 parent 1218f14 commit 99bc8fc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
23 changes: 23 additions & 0 deletions internal/controllers/watchdog/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

apiv1 "github.com/Azure/eno/api/v1"
"github.com/Azure/eno/internal/manager"
"github.com/go-logr/logr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand All @@ -17,6 +18,19 @@ type watchdogController struct {
threshold time.Duration
}

func (c *watchdogController) getInputsExist(comp *apiv1.Composition, ctx context.Context) bool {
logger := logr.FromContextOrDiscard(ctx).WithValues("checkForMissingInputs: synthesizer access", comp.Generation)

syn := &apiv1.Synthesizer{}
syn.Name = comp.Spec.Synthesizer.Name
err := c.client.Get(ctx, client.ObjectKeyFromObject(syn), syn)
if err != nil {
logger.WithValues("synthesizerName", syn.Name).Error(err, "failed to get synthesizer for composition. Synthesizer may not exist. Presuming inputs are not missing.")
return true
}
return comp.InputsExist(syn)
}

func NewController(mgr ctrl.Manager, threshold time.Duration) error {
return ctrl.NewControllerManagedBy(mgr).
Named("watchdogController").
Expand All @@ -35,11 +49,15 @@ func (c *watchdogController) Reconcile(ctx context.Context, req ctrl.Request) (c
return ctrl.Result{}, err
}

var inputsMissing int
var pendingInit int
var pending int
var unready int
var terminal int
for _, comp := range list.Items {
if c.waitingOnInputs(&comp, ctx) {
inputsMissing++
}
if c.pendingInitialReconciliation(&comp) {
pendingInit++
}
Expand All @@ -54,6 +72,7 @@ func (c *watchdogController) Reconcile(ctx context.Context, req ctrl.Request) (c
}
}

waitingOnInputs.Set(float64(inputsMissing))
pendingInitialReconciliation.Set(float64(pendingInit))
stuckReconciling.Set(float64(pending))
pendingReadiness.Set(float64(unready))
Expand All @@ -62,6 +81,10 @@ func (c *watchdogController) Reconcile(ctx context.Context, req ctrl.Request) (c
return ctrl.Result{}, nil
}

func (c *watchdogController) waitingOnInputs(comp *apiv1.Composition, ctx context.Context) bool {
return !c.getInputsExist(comp, ctx) && time.Since(comp.CreationTimestamp.Time) > c.threshold
}

func (c *watchdogController) pendingInitialReconciliation(comp *apiv1.Composition) bool {
return !synthesisHasReconciled(comp.Status.CurrentSynthesis) &&
!synthesisHasReconciled(comp.Status.PreviousSynthesis) &&
Expand Down
9 changes: 8 additions & 1 deletion internal/controllers/watchdog/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ import (
)

var (
waitingOnInputs = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "eno_compositions_inputs_missing_total",
Help: "Number of compositions that are missing input resources",
},
)

pendingInitialReconciliation = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "eno_compositions_pending_initial_reconciliation",
Expand Down Expand Up @@ -36,5 +43,5 @@ var (
)

func init() {
metrics.Registry.MustRegister(pendingInitialReconciliation, stuckReconciling, pendingReadiness, terminalErrors)
metrics.Registry.MustRegister(waitingOnInputs, pendingInitialReconciliation, stuckReconciling, pendingReadiness, terminalErrors)
}

0 comments on commit 99bc8fc

Please sign in to comment.