fix(cpu_info,diskdrive): stop probing WMI during Build (#2648)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
This commit is contained in:
Jan-Otto Kröpke
2026-10-10 16:09:43 +02:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 651daf0fee
commit a31fae00d1
4 changed files with 38 additions and 10 deletions
+3 -5
View File
@@ -151,11 +151,9 @@ func (c *Collector) Build(_ *slog.Logger, miSession *mi.Session) error {
c.miQuery = miQuery
c.miSession = miSession
var dst []miProcessor
if err := c.miSession.Query(&dst, mi.NamespaceRootCIMv2, c.miQuery, 0); err != nil {
return fmt.Errorf("WMI query failed: %w", err)
}
// Win32_Processor exists on every supported Windows version, so Build does not probe it.
// WMI can time out shortly after boot. A probe would disable the collector until restart,
// while a failing Collect only fails the affected scrapes.
return nil
}
@@ -18,10 +18,15 @@
package cpu_info_test
import (
"log/slog"
"testing"
"time"
"github.com/prometheus-community/windows_exporter/internal/collector/cpu_info"
"github.com/prometheus-community/windows_exporter/internal/mi"
"github.com/prometheus-community/windows_exporter/internal/utils/testutils"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
)
func BenchmarkCollector(b *testing.B) {
@@ -31,3 +36,14 @@ func BenchmarkCollector(b *testing.B) {
func TestCollector(t *testing.T) {
testutils.TestCollector(t, cpu_info.New, nil)
}
// A WMI failure during Build must not disable the collector until restart, so
// Build must not query WMI. The uninitialized session fails every query.
func TestBuildDoesNotQueryWMI(t *testing.T) {
c := cpu_info.New(nil)
t.Cleanup(func() { require.NoError(t, c.Close()) })
require.NoError(t, c.Build(slog.New(slog.DiscardHandler), &mi.Session{}))
require.ErrorIs(t, c.Collect(make(chan prometheus.Metric, 64), time.Second), mi.ErrNotInitialized)
}
+3 -5
View File
@@ -127,11 +127,9 @@ func (c *Collector) Build(logger *slog.Logger, miSession *mi.Session) error {
c.miQuery = miQuery
c.miSession = miSession
var dst []diskDrive
if err := c.miSession.Query(&dst, mi.NamespaceRootCIMv2, c.miQuery, 0); err != nil {
return fmt.Errorf("WMI query failed: %w", err)
}
// Win32_DiskDrive exists on every supported Windows version, so Build does not probe it.
// WMI can time out shortly after boot. A probe would disable the collector until restart,
// while a failing Collect only fails the affected scrapes.
return nil
}
@@ -18,10 +18,15 @@
package diskdrive_test
import (
"log/slog"
"testing"
"time"
"github.com/prometheus-community/windows_exporter/internal/collector/diskdrive"
"github.com/prometheus-community/windows_exporter/internal/mi"
"github.com/prometheus-community/windows_exporter/internal/utils/testutils"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
)
func BenchmarkCollector(b *testing.B) {
@@ -31,3 +36,14 @@ func BenchmarkCollector(b *testing.B) {
func TestCollector(t *testing.T) {
testutils.TestCollector(t, diskdrive.New, nil)
}
// A WMI failure during Build must not disable the collector until restart, so
// Build must not query WMI. The uninitialized session fails every query.
func TestBuildDoesNotQueryWMI(t *testing.T) {
c := diskdrive.New(nil)
t.Cleanup(func() { require.NoError(t, c.Close()) })
require.NoError(t, c.Build(slog.New(slog.DiscardHandler), &mi.Session{}))
require.ErrorIs(t, c.Collect(make(chan prometheus.Metric, 64), time.Second), mi.ErrNotInitialized)
}