Skip to content

Commit

Permalink
set metric on errors
Browse files Browse the repository at this point in the history
Signed-off-by: Will Kutler <wkutler@redhat.com>
  • Loading branch information
willkutler committed Dec 5, 2022
1 parent 7ca12ad commit 6d3ee40
Showing 1 changed file with 37 additions and 13 deletions.
50 changes: 37 additions & 13 deletions controllers/configurationpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,29 @@ var policyEvalCounter = prometheus.NewCounterVec(
[]string{"name"},
)

// var policyUserErrorsGauge = prometheus.NewGaugeVec(
// prometheus.GaugeOpts{
// Name: "common_related_objects",
// Help: "A gauge vector of related objects managed by multiple policies.",
// },
// []string{
// "relatedObject",
// "policy",
// },
// )
var policyUserErrorsGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "policy_user_errors",
Help: "The number of user errors encountered while processing policies",
},
[]string{
"policy",
"template",
"type",
},
)

var policySystemErrorsGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "policy_system_errors",
Help: "The number of system errors encountered while processing policies",
},
[]string{
"policy",
"template",
"type",
},
)

var compareObjSecondsCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Expand All @@ -130,6 +143,8 @@ func init() {
metrics.Registry.MustRegister(policyEvalCounter)
metrics.Registry.MustRegister(compareObjSecondsCounter)
metrics.Registry.MustRegister(compareObjEvalCounter)
metrics.Registry.MustRegister(policySystemErrorsGauge)
metrics.Registry.MustRegister(policyUserErrorsGauge)
}

// SetupWithManager sets up the controller with the Manager.
Expand Down Expand Up @@ -192,6 +207,8 @@ func (r *ConfigurationPolicyReconciler) Reconcile(ctx context.Context, request c
_ = policyEvalCounter.DeleteLabelValues(request.Name)
_ = compareObjEvalCounter.DeletePartialMatch(prometheus.Labels{"config_policy_name": request.Name})
_ = compareObjSecondsCounter.DeletePartialMatch(prometheus.Labels{"config_policy_name": request.Name})
_ = policyUserErrorsGauge.DeletePartialMatch(prometheus.Labels{"template": request.Name})
_ = policySystemErrorsGauge.DeletePartialMatch(prometheus.Labels{"template": request.Name})
}

return reconcile.Result{}, nil
Expand Down Expand Up @@ -616,7 +633,6 @@ func (r *ConfigurationPolicyReconciler) handleObjectTemplates(plc policyv1.Confi
relatedObjects := []policyv1.RelatedObject{}
parentStatusUpdateNeeded := false

//TODO-METRIC - user error
validationErr := ""
if plc.Spec == nil {
validationErr = "Policy does not have a Spec specified"
Expand All @@ -637,6 +653,9 @@ func (r *ConfigurationPolicyReconciler) handleObjectTemplates(plc policyv1.Confi

r.checkRelatedAndUpdate(plc, relatedObjects, oldRelated, statusChanged)

parent := plc.OwnerReferences[0]
policyUserErrorsGauge.WithLabelValues(parent.Name, plc.GetName(), "invalid-template").Add(1)

return
}

Expand Down Expand Up @@ -1635,8 +1654,11 @@ func (r *ConfigurationPolicyReconciler) getMapping(
kind := afterPrefix[0:(strings.Index(afterPrefix, "\" "))]
mappingErrMsg = "couldn't find mapping resource with kind " + kind +
", please check if you have CRD deployed"
// TODO-METRIC - user error - missing CRD

log.Error(err, "Could not map resource, do you have the CRD deployed?", "kind", kind)

parent := policy.OwnerReferences[0]
policyUserErrorsGauge.WithLabelValues(parent.Name, policy.GetName(), "no-object-CRD").Add(1)
}

errMsg := err.Error()
Expand Down Expand Up @@ -2457,8 +2479,10 @@ func (r *ConfigurationPolicyReconciler) addForUpdate(policy *policyv1.Configurat
if k8serrors.IsConflict(err) {
policyLog.Error(err, "Tried to re-update status before previous update could be applied, retrying next loop")
} else if err != nil {
// TODO-METRIC - system error - can't send a status update
policyLog.Error(err, "Could not update status, retrying next loop")

parent := policy.OwnerReferences[0]
policySystemErrorsGauge.WithLabelValues(parent.Name, policy.GetName(), "status-update-failed").Add(1)
}
}

Expand Down

0 comments on commit 6d3ee40

Please sign in to comment.