Skip to content

Commit b611e37

Browse files
authored
Feat: Add Guardrails usage metrics (depends on #513) (#514)
* Add basic guardrails metrics * Document guardrailmetriccreation function
1 parent c9cece7 commit b611e37

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

controllers/gorch/guardrailsorchestrator_controller.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package gorch
1818

1919
import (
2020
"context"
21+
"github.com/trustyai-explainability/trustyai-service-operator/controllers/metrics"
22+
"strconv"
2123
"time"
2224

2325
routev1 "github.com/openshift/api/route/v1"
@@ -67,6 +69,19 @@ func ControllerSetUp(mgr manager.Manager, ns, configmap string, recorder record.
6769
}).SetupWithManager(mgr)
6870
}
6971

72+
// createOrchestratorCreationMetrics collects and publishes metrics related to new-created Guardrails Orchestrators
73+
func createOrchestratorCreationMetrics(orchestrator *gorchv1alpha1.GuardrailsOrchestrator) {
74+
// Update the Prometheus metrics for each task in the tasklist
75+
labels := make(map[string]string)
76+
labels["orchestrator_namespace"] = orchestrator.Namespace
77+
labels["using_built_in_detectors"] = strconv.FormatBool(orchestrator.Spec.EnableBuiltInDetectors)
78+
labels["using_sidecar_gateway"] = strconv.FormatBool(orchestrator.Spec.SidecarGatewayConfig != nil)
79+
80+
// create/update metric counter
81+
counter := metrics.GetOrCreateGuardrailsOrchestratorCounter(labels)
82+
counter.Inc()
83+
}
84+
7085
// Reconcile is part of the main kubernetes reconciliation loop which aims to
7186
// move the current state of the cluster closer to the desired state.
7287
// TODO(user): Modify the Reconcile function to compare the state specified by
@@ -103,6 +118,8 @@ func (r *GuardrailsOrchestratorReconciler) Reconcile(ctx context.Context, req ct
103118
log.Error(err, "Failed to update GuardrailsOrchestrator status during initialization")
104119
return ctrl.Result{}, err
105120
}
121+
122+
createOrchestratorCreationMetrics(orchestrator)
106123
}
107124

108125
if !controllerutil.ContainsFinalizer(orchestrator, finalizerName) {

controllers/metrics/metrics.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ func GetOrCreateEvalCounter(labels map[string]string) prometheus.Counter {
3939
return getOrCreateCounter("eval", "Number of times this task has been scheduled", labels)
4040
}
4141

42+
func GetOrCreateGuardrailsOrchestratorCounter(labels map[string]string) prometheus.Counter {
43+
return getOrCreateCounter("guardrails_orchestrators", "Number of guardrails orchestrators that have been created", labels)
44+
}
45+
4246
// GetOrCreateCounter returns a prometheus.Counter with the given name, help, and labels.
4347
// If a counter with that name and labels already exists, it returns the existing one.
4448
// Otherwise, it creates, registers, and returns a new counter.

0 commit comments

Comments
 (0)