Skip to content

Commit

Permalink
Refactor: move metrics for checkAndUpdateResource
Browse files Browse the repository at this point in the history
Putting this in the function itself means that the function could be
called from multiple places, and the metrics would still be recorded.

Signed-off-by: Justin Kulikauskas <jkulikau@redhat.com>
(cherry picked from commit 406ccf7)
  • Loading branch information
JustinKuli authored and magic-mirror-bot[bot] committed Sep 7, 2023
1 parent 420c6d3 commit 8bcab5a
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions controllers/configurationpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1738,8 +1738,6 @@ func (r *ConfigurationPolicyReconciler) handleSingleObj(
compType := strings.ToLower(string(objectT.ComplianceType))
mdCompType := strings.ToLower(string(objectT.MetadataComplianceType))

before := time.Now().UTC()

throwSpecViolation, msg, triedUpdate, updatedObj := r.checkAndUpdateResource(
obj, compType, mdCompType, remediation,
)
Expand All @@ -1749,19 +1747,6 @@ func (r *ConfigurationPolicyReconciler) handleSingleObj(
result.events = append(result.events, objectTmplEvalEvent{false, reasonWantFoundNoMatch, ""})
}

duration := time.Now().UTC().Sub(before)
seconds := float64(duration) / float64(time.Second)
compareObjSecondsCounter.WithLabelValues(
obj.policy.Name,
obj.namespace,
fmt.Sprintf("%s.%s", obj.gvr.Resource, obj.name),
).Add(seconds)
compareObjEvalCounter.WithLabelValues(
obj.policy.Name,
obj.namespace,
fmt.Sprintf("%s.%s", obj.gvr.Resource, obj.name),
).Inc()

if throwSpecViolation {
var resultReason, resultMsg string

Expand Down Expand Up @@ -2524,6 +2509,23 @@ func (r *ConfigurationPolicyReconciler) checkAndUpdateResource(
"policy", obj.policy.Name, "name", obj.name, "namespace", obj.namespace, "resource", obj.gvr.Resource,
)

// Time the function, and record it in a metric
before := time.Now().UTC()
defer func() {
duration := time.Now().UTC().Sub(before)
seconds := float64(duration) / float64(time.Second)
compareObjSecondsCounter.WithLabelValues(
obj.policy.Name,
obj.namespace,
fmt.Sprintf("%s.%s", obj.gvr.Resource, obj.name),
).Add(seconds)
compareObjEvalCounter.WithLabelValues(
obj.policy.Name,
obj.namespace,
fmt.Sprintf("%s.%s", obj.gvr.Resource, obj.name),
).Inc()
}()

if obj.existingObj == nil {
log.Info("Skipping update: Previous object retrieval from the API server failed")

Expand Down

0 comments on commit 8bcab5a

Please sign in to comment.