Skip to content
This repository was archived by the owner on Feb 18, 2025. It is now read-only.

Commit 696fb51

Browse files
authored
Add a metric of AppWrappers counts per phase, step and priority (#52)
* Add appWrappersCount metric * Make go-mod-tidy happy
1 parent 87cb347 commit 696fb51

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.20
55
require (
66
github.com/onsi/ginkgo/v2 v2.11.0
77
github.com/onsi/gomega v1.27.10
8+
github.com/prometheus/client_golang v1.16.0
89
gopkg.in/inf.v0 v0.9.1
910
k8s.io/api v0.28.3
1011
k8s.io/apimachinery v0.28.3
@@ -42,7 +43,6 @@ require (
4243
github.com/modern-go/reflect2 v1.0.2 // indirect
4344
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
4445
github.com/pkg/errors v0.9.1 // indirect
45-
github.com/prometheus/client_golang v1.16.0 // indirect
4646
github.com/prometheus/client_model v0.4.0 // indirect
4747
github.com/prometheus/common v0.44.0 // indirect
4848
github.com/prometheus/procfs v0.10.1 // indirect

internal/controller/dispatch_logic.go

+20-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ package controller
1919
import (
2020
"context"
2121
"sort"
22+
"strconv"
2223
"time"
2324

2425
v1 "k8s.io/api/core/v1"
2526
"k8s.io/apimachinery/pkg/fields"
2627
"sigs.k8s.io/controller-runtime/pkg/client"
2728

2829
mcadv1beta1 "github.com/project-codeflare/mcad/api/v1beta1"
30+
"github.com/prometheus/client_golang/prometheus"
2931
)
3032

3133
// Compute available cluster capacity
@@ -78,12 +80,21 @@ func (r *AppWrapperReconciler) listAppWrappers(ctx context.Context) (map[int]Wei
7880
}
7981
requests := map[int]Weights{} // total request per priority level
8082
queue := []*mcadv1beta1.AppWrapper{} // queued appWrappers
83+
84+
appWrapperCount := map[phaseStepPriority]int{}
85+
8186
for _, appWrapper := range appWrappers.Items {
8287
// get phase from cache if available as reconciler cache may be lagging
8388
phase, step := r.getCachedPhase(&appWrapper)
89+
priority := int(appWrapper.Spec.Priority)
90+
key := phaseStepPriority{phase, step, priority}
91+
if _, exists := appWrapperCount[key]; !exists {
92+
appWrapperCount[key] = 0
93+
}
94+
appWrapperCount[key]++
8495
// make sure to initialize weights for every known priority level
85-
if requests[int(appWrapper.Spec.Priority)] == nil {
86-
requests[int(appWrapper.Spec.Priority)] = Weights{}
96+
if requests[priority] == nil {
97+
requests[priority] = Weights{}
8798
}
8899
if step != mcadv1beta1.Idle {
89100
// use max request among AppWrapper request and total request of non-terminated AppWrapper pods
@@ -109,6 +120,13 @@ func (r *AppWrapperReconciler) listAppWrappers(ctx context.Context) (map[int]Wei
109120
queue = append(queue, &copy)
110121
}
111122
}
123+
// update AppWrapper count metric
124+
appWrappersCount.Reset()
125+
for key, count := range appWrapperCount {
126+
appWrappersCount.With(
127+
prometheus.Labels{"phase": string(key.phase), "step": string(key.step), "priority": strconv.Itoa(key.priority)},
128+
).Set(float64(count))
129+
}
112130
// propagate reservations at all priority levels to all levels below
113131
assertPriorities(requests)
114132
// order AppWrapper queue based on priority and precedence (creation time)

internal/controller/metrics.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Copyright 2023 IBM Corporation.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package controller
18+
19+
import (
20+
mcadv1beta1 "github.com/project-codeflare/mcad/api/v1beta1"
21+
"github.com/prometheus/client_golang/prometheus"
22+
"sigs.k8s.io/controller-runtime/pkg/metrics"
23+
)
24+
25+
type phaseStepPriority struct {
26+
phase mcadv1beta1.AppWrapperPhase
27+
step mcadv1beta1.AppWrapperStep
28+
priority int
29+
}
30+
31+
var (
32+
appWrappersCount = prometheus.NewGaugeVec(prometheus.GaugeOpts{
33+
Subsystem: "mcad",
34+
Name: "appwrappers_count",
35+
Help: "AppWrappers count per phase, step and priority",
36+
}, []string{"phase", "step", "priority"})
37+
)
38+
39+
func init() {
40+
metrics.Registry.MustRegister(
41+
appWrappersCount,
42+
)
43+
}

0 commit comments

Comments
 (0)