Skip to content

Commit cce4e69

Browse files
author
Rafal Szypulka
committed
Enhance InfiniBand device handling and add tests
Signed-off-by: Rafal Szypulka <rafal@datacrunch.io>
1 parent b401dcf commit cce4e69

2 files changed

Lines changed: 77 additions & 4 deletions

File tree

collector/infiniband_linux.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,20 +158,25 @@ func (c *infinibandCollector) pushCounter(ch chan<- prometheus.Metric, name stri
158158
}
159159

160160
func (c *infinibandCollector) Update(ch chan<- prometheus.Metric) error {
161-
devices, err := c.fs.InfiniBandClass()
161+
deviceNames, err := c.fs.InfiniBandClassDevices()
162162
if err != nil {
163163
if errors.Is(err, os.ErrNotExist) {
164164
c.logger.Debug("infiniband statistics not found, skipping")
165165
return ErrNoData
166166
}
167-
return fmt.Errorf("error obtaining InfiniBand class info: %w", err)
167+
return fmt.Errorf("error obtaining InfiniBand device list: %w", err)
168168
}
169169

170-
for _, device := range devices {
171-
if c.deviceFilter.ignored(device.Name) {
170+
for _, name := range deviceNames {
171+
if c.deviceFilter.ignored(name) {
172172
continue
173173
}
174174

175+
device, err := c.fs.InfiniBandDevice(name)
176+
if err != nil {
177+
return fmt.Errorf("error obtaining InfiniBand device %q info: %w", name, err)
178+
}
179+
175180
infoDesc := prometheus.NewDesc(
176181
prometheus.BuildFQName(namespace, c.subsystem, "info"),
177182
"Non-numeric data from /sys/class/infiniband/<device>, value is always 1.",

collector/infiniband_linux_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2026 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
//go:build !noinfiniband
15+
16+
package collector
17+
18+
import (
19+
"io"
20+
"log/slog"
21+
"os"
22+
"path/filepath"
23+
"testing"
24+
25+
"github.com/prometheus/client_golang/prometheus"
26+
)
27+
28+
func TestInfiniBandDeviceFilterBeforeRead(t *testing.T) {
29+
sys := t.TempDir()
30+
included := filepath.Join(sys, "class", "infiniband", "mlx5_7")
31+
excluded := filepath.Join(sys, "class", "infiniband", "mlx5_5")
32+
33+
if err := os.MkdirAll(filepath.Join(included, "ports"), 0o755); err != nil {
34+
t.Fatal(err)
35+
}
36+
if err := os.WriteFile(filepath.Join(included, "fw_ver"), []byte("28.47.1088\n"), 0o644); err != nil {
37+
t.Fatal(err)
38+
}
39+
// The excluded device deliberately lacks fw_ver and ports. Reading it would fail.
40+
if err := os.MkdirAll(excluded, 0o755); err != nil {
41+
t.Fatal(err)
42+
}
43+
44+
oldSysPath := *sysPath
45+
oldInclude := *infinibandDeviceInclude
46+
oldExclude := *infinibandDeviceExclude
47+
t.Cleanup(func() {
48+
*sysPath = oldSysPath
49+
*infinibandDeviceInclude = oldInclude
50+
*infinibandDeviceExclude = oldExclude
51+
})
52+
*sysPath = sys
53+
*infinibandDeviceInclude = ""
54+
*infinibandDeviceExclude = "^mlx5_5$"
55+
56+
collector, err := NewInfiniBandCollector(slog.New(slog.NewTextHandler(io.Discard, nil)))
57+
if err != nil {
58+
t.Fatal(err)
59+
}
60+
61+
ch := make(chan prometheus.Metric, 1)
62+
if err := collector.Update(ch); err != nil {
63+
t.Fatalf("excluded device was read: %v", err)
64+
}
65+
if len(ch) != 1 {
66+
t.Fatalf("expected one metric from the included device, got %d", len(ch))
67+
}
68+
}

0 commit comments

Comments
 (0)