-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathmetrics.go
123 lines (110 loc) · 3.74 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
Copyright 2023 The Multi-Cluster App Dispatcher Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package queuejob
import (
arbv1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/apis/controller/v1beta1"
"github.com/prometheus/client_golang/prometheus"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/klog/v2"
"reflect"
"runtime"
"sigs.k8s.io/controller-runtime/pkg/metrics"
"time"
)
var (
allocatableCapacityCpu = prometheus.NewGauge(prometheus.GaugeOpts{
Subsystem: "mcad",
Name: "allocatable_capacity_cpu",
Help: "Allocatable CPU Capacity (in millicores)",
})
allocatableCapacityMemory = prometheus.NewGauge(prometheus.GaugeOpts{
Subsystem: "mcad",
Name: "allocatable_capacity_memory",
Help: "Allocatable Memory Capacity",
})
allocatableCapacityGpu = prometheus.NewGauge(prometheus.GaugeOpts{
Subsystem: "mcad",
Name: "allocatable_capacity_gpu",
Help: "Allocatable GPU Capacity",
})
appWrappersCount = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Subsystem: "mcad",
Name: "appwrappers_count",
Help: "AppWrappers count per state",
}, []string{"state"})
)
func init() {
klog.V(10).Infof("Registering metrics")
metrics.Registry.MustRegister(
allocatableCapacityCpu,
allocatableCapacityMemory,
allocatableCapacityGpu,
appWrappersCount,
)
}
func updateMetricsLoop(controller *XController, stopCh <-chan struct{}) {
updateMetricsLoopGeneric(controller, stopCh, time.Minute*1, updateAllocatableCapacity)
updateMetricsLoopGeneric(controller, stopCh, time.Second*5, updateQueue)
}
func getFuncName(f interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
}
func updateMetricsLoopGeneric(controller *XController, stopCh <-chan struct{}, d time.Duration, updateFunc func(xController *XController)) {
ticker := time.NewTicker(d)
go func() {
updateFunc(controller)
for {
select {
case <-ticker.C:
if klog.V(10).Enabled() {
klog.Infof("[updateMetricsLoopGeneric] Update metrics loop tick: %v", getFuncName(updateFunc))
}
updateFunc(controller)
case <-stopCh:
if klog.V(10).Enabled() {
klog.Infof("[updateMetricsLoopGeneric] Exiting update metrics loop: %v", getFuncName(updateFunc))
}
ticker.Stop()
return
}
}
}()
}
func updateAllocatableCapacity(controller *XController) {
res := controller.GetAllocatableCapacity()
allocatableCapacityCpu.Set(res.MilliCPU)
allocatableCapacityMemory.Set(res.Memory)
allocatableCapacityGpu.Set(float64(res.GPU))
}
func updateQueue(controller *XController) {
awList, err := controller.appWrapperLister.List(labels.Everything())
if err != nil {
klog.Errorf("[updateQueue] Unable to obtain the list of AppWrappers: %+v", err)
return
}
stateToAppWrapperCount := map[arbv1.AppWrapperState]int{
arbv1.AppWrapperStateEnqueued: 0,
arbv1.AppWrapperStateActive: 0,
arbv1.AppWrapperStateDeleted: 0,
arbv1.AppWrapperStateFailed: 0,
arbv1.AppWrapperStateCompleted: 0,
arbv1.AppWrapperStateRunningHoldCompletion: 0,
}
for _, aw := range awList {
state := aw.Status.State
stateToAppWrapperCount[state]++
}
for state, count := range stateToAppWrapperCount {
appWrappersCount.WithLabelValues(string(state)).Set(float64(count))
}
}