Skip to content

Commit 8baf776

Browse files
committed
Resolve comments
Signed-off-by: win5923 <[email protected]>
1 parent 6fa17fc commit 8baf776

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

ray-operator/controllers/ray/metrics/ray_cluster_metrics.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,35 @@ import (
44
"strconv"
55

66
"github.com/prometheus/client_golang/prometheus"
7-
"github.com/prometheus/client_golang/prometheus/promauto"
87
"sigs.k8s.io/controller-runtime/pkg/metrics"
8+
9+
rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1"
10+
"github.com/ray-project/kuberay/ray-operator/controllers/ray/utils"
911
)
1012

11-
var rayClustersCreatedCounter = promauto.NewCounterVec(
13+
var rayClustersCreatedCounter = prometheus.NewCounterVec(
1214
prometheus.CounterOpts{
1315
Name: "ray_clusters_created_total",
1416
Help: "The total number of RayClusters created",
1517
},
1618
[]string{"namespace", "created_by_ray_job", "created_by_ray_service"},
1719
)
1820

19-
// CreatedRayClustersCounterInc increments the counter for RayClusters created
20-
func CreatedRayClustersCounterInc(namespace string, createdByRayJob bool, createdByRayService bool) {
21-
rayClustersCreatedCounter.WithLabelValues(namespace, strconv.FormatBool(createdByRayJob), strconv.FormatBool(createdByRayService)).Inc()
22-
}
23-
2421
func registerRayClusterMetrics() {
2522
// Register custom metrics with the global prometheus registry
2623
metrics.Registry.MustRegister(rayClustersCreatedCounter)
2724
}
25+
26+
// CreatedRayClustersCounterInc increments the counter for ray_clusters_created_total metric based on the creator CRD type.
27+
func CreatedRayClustersCounterInc(enabled bool, namespace string, instance *rayv1.RayCluster) {
28+
if !enabled {
29+
return
30+
}
31+
32+
creatorCRDType := utils.GetCRDType(instance.Labels[utils.RayOriginatedFromCRDLabelKey])
33+
34+
isfromRayJob := creatorCRDType == utils.RayJobCRD
35+
isfromRayService := creatorCRDType == utils.RayServiceCRD
36+
37+
rayClustersCreatedCounter.WithLabelValues(namespace, strconv.FormatBool(isfromRayJob), strconv.FormatBool(isfromRayService)).Inc()
38+
}

ray-operator/controllers/ray/metrics/ray_cluster_metrics_test.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,38 @@ import (
55
"testing"
66

77
"github.com/prometheus/client_golang/prometheus/testutil"
8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
10+
rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1"
11+
"github.com/ray-project/kuberay/ray-operator/controllers/ray/utils"
812
)
913

1014
func TestCreatedRayClustersCounterInc(t *testing.T) {
11-
CreatedRayClustersCounterInc("default", true, false)
12-
CreatedRayClustersCounterInc("default", false, true)
13-
CreatedRayClustersCounterInc("test", false, false)
14-
CreatedRayClustersCounterInc("test", false, false)
15+
CreatedRayClustersCounterInc(true, "default", &rayv1.RayCluster{})
16+
CreatedRayClustersCounterInc(true, "default", &rayv1.RayCluster{})
17+
CreatedRayClustersCounterInc(true, "test", &rayv1.RayCluster{})
18+
CreatedRayClustersCounterInc(true, "test", &rayv1.RayCluster{
19+
ObjectMeta: metav1.ObjectMeta{
20+
Labels: map[string]string{
21+
utils.RayOriginatedFromCRDLabelKey: "RayJob",
22+
},
23+
},
24+
})
25+
CreatedRayClustersCounterInc(true, "test", &rayv1.RayCluster{
26+
ObjectMeta: metav1.ObjectMeta{
27+
Labels: map[string]string{
28+
utils.RayOriginatedFromCRDLabelKey: "RayService",
29+
},
30+
},
31+
})
1532

1633
expected := `
1734
# HELP ray_clusters_created_total The total number of RayClusters created
1835
# TYPE ray_clusters_created_total counter
19-
ray_clusters_created_total{created_by_ray_job="true",created_by_ray_service="false",namespace="default"} 1
20-
ray_clusters_created_total{created_by_ray_job="false",created_by_ray_service="true",namespace="default"} 1
21-
ray_clusters_created_total{created_by_ray_job="false",created_by_ray_service="false",namespace="test"} 2
36+
ray_clusters_created_total{created_by_ray_job="false",created_by_ray_service="false",namespace="default"} 2
37+
ray_clusters_created_total{created_by_ray_job="false",created_by_ray_service="false",namespace="test"} 1
38+
ray_clusters_created_total{created_by_ray_job="true",created_by_ray_service="false",namespace="test"} 1
39+
ray_clusters_created_total{created_by_ray_job="false",created_by_ray_service="true",namespace="test"} 1
2240
`
2341
if err := testutil.CollectAndCompare(rayClustersCreatedCounter, strings.NewReader(expected)); err != nil {
2442
t.Errorf("unexpected collecting result:\n%s", err)

ray-operator/controllers/ray/raycluster_controller.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -740,21 +740,11 @@ func (r *RayClusterReconciler) reconcilePods(ctx context.Context, instance *rayv
740740
// Create head Pod if it does not exist.
741741
logger.Info("reconcilePods: Found 0 head Pods; creating a head Pod for the RayCluster.")
742742

743-
if r.EnableMetrics {
744-
creatorCRDType := getCreatorCRDType(*instance)
745-
// Increment the counter for ray_clusters_created_total metric based on the creator CRD type.
746-
if creatorCRDType == utils.RayClusterCRD {
747-
metrics.CreatedRayClustersCounterInc(instance.Namespace, false, false)
748-
} else if creatorCRDType == utils.RayJobCRD {
749-
metrics.CreatedRayClustersCounterInc(instance.Namespace, true, false)
750-
} else if creatorCRDType == utils.RayServiceCRD {
751-
metrics.CreatedRayClustersCounterInc(instance.Namespace, false, true)
752-
}
753-
}
754-
755743
if err := r.createHeadPod(ctx, *instance); err != nil {
756744
return errstd.Join(utils.ErrFailedCreateHeadPod, err)
757745
}
746+
747+
metrics.CreatedRayClustersCounterInc(r.EnableMetrics, instance.Namespace, instance)
758748
} else if len(headPods.Items) > 1 { // This should never happen. This protects against the case that users manually create headpod.
759749
correctHeadPodName := instance.Name + "-head"
760750
headPodNames := make([]string, len(headPods.Items))

0 commit comments

Comments
 (0)