diff --git a/cmd/vGPUmonitor/metrics.go b/cmd/vGPUmonitor/metrics.go index ea4b4bf0b4..94601bbdc2 100644 --- a/cmd/vGPUmonitor/metrics.go +++ b/cmd/vGPUmonitor/metrics.go @@ -211,9 +211,8 @@ func sendLegacyMetric(ch chan<- prometheus.Metric, desc *prometheus.Desc, valueT } } -// Describe is implemented with DescribeByCollect. That's possible because the -// Collect method will always return the same two metrics with the same two -// descriptors. +// Describe sends all the metrics descriptors that the collector might use. +// These descriptors are used by the Prometheus registry to register the metrics. func (cc ClusterManagerCollector) Describe(ch chan<- *prometheus.Desc) { ch <- hostGPUdesc ch <- ctrvGPUdesc @@ -221,6 +220,8 @@ func (cc ClusterManagerCollector) Describe(ch chan<- *prometheus.Desc) { ch <- hostGPUUtilizationdesc ch <- ctrDeviceMemorydesc ch <- ctrDeviceUtilizationdesc + ch <- ctrDeviceLastKernelDesc + ch <- ctrDeviceMigInfo ch <- ctrDeviceMemoryContextDesc ch <- ctrDeviceMemoryModuleDesc ch <- ctrDeviceMemoryBufferDesc diff --git a/cmd/vGPUmonitor/metrics_test.go b/cmd/vGPUmonitor/metrics_test.go new file mode 100644 index 0000000000..2c31e4d221 --- /dev/null +++ b/cmd/vGPUmonitor/metrics_test.go @@ -0,0 +1,70 @@ +/* +Copyright 2024 The HAMi 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 main + +import ( + "testing" + + "github.com/prometheus/client_golang/prometheus" + "k8s.io/client-go/informers" + "k8s.io/client-go/kubernetes/fake" + + "github.com/Project-HAMi/HAMi/pkg/monitor/nvidia" + "github.com/Project-HAMi/HAMi/pkg/util" +) + +func TestDescribeCollectSync(t *testing.T) { + reg := prometheus.NewPedanticRegistry() + + t.Setenv(util.NodeNameEnvName, "test-node") + client := fake.NewSimpleClientset() + informerFactory := informers.NewSharedInformerFactory(client, 0) + podLister := informerFactory.Core().V1().Pods().Lister() + + c := &ClusterManager{ + Zone: "test-zone", + LegacyMetrics: false, + PodLister: podLister, + containerLister: &nvidia.ContainerLister{}, + } + cc := ClusterManagerCollector{ClusterManager: c} + + if err := reg.Register(cc); err != nil { + t.Fatalf("Failed to register ClusterManagerCollector (non-legacy): %v", err) + } + + if _, err := reg.Gather(); err != nil { + t.Errorf("Gather failed (non-legacy): %v", err) + } + + regLegacy := prometheus.NewPedanticRegistry() + cLegacy := &ClusterManager{ + Zone: "test-zone-legacy", + LegacyMetrics: true, + PodLister: podLister, + containerLister: &nvidia.ContainerLister{}, + } + initLegacyDescriptors() + ccLegacy := ClusterManagerCollector{ClusterManager: cLegacy} + + if err := regLegacy.Register(ccLegacy); err != nil { + t.Fatalf("Failed to register ClusterManagerCollector (legacy): %v", err) + } + if _, err := regLegacy.Gather(); err != nil { + t.Errorf("Gather failed (legacy): %v", err) + } +}