From 23397701ff1ad787e0a2852720fb5aa38495f50f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Fri, 20 Mar 2026 20:27:02 +0100 Subject: [PATCH] pdh/registry: fix panic when T is a struct type (#2366) (#2367) Co-authored-by: Kayla Ondracek <112117836+kondracek-nr@users.noreply.github.com> --- internal/pdh/registry/collector.go | 2 +- internal/pdh/registry/perflib_test.go | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/internal/pdh/registry/collector.go b/internal/pdh/registry/collector.go index a868e977..f4200f74 100644 --- a/internal/pdh/registry/collector.go +++ b/internal/pdh/registry/collector.go @@ -53,7 +53,7 @@ func NewCollector[T any](object string, _ []string) (*Collector, error) { counters: make(map[string]Counter), } - valueType := reflect.TypeFor[T]().Elem() + valueType := reflect.TypeFor[T]() if f, ok := valueType.FieldByName("Name"); ok { if f.Type.Kind() == reflect.String { diff --git a/internal/pdh/registry/perflib_test.go b/internal/pdh/registry/perflib_test.go index ecc74359..c3340b4d 100644 --- a/internal/pdh/registry/perflib_test.go +++ b/internal/pdh/registry/perflib_test.go @@ -21,6 +21,22 @@ import ( "testing" ) +// TestNewCollectorStructTypeParam guards against a regression where +// reflect.TypeFor[T]().Elem() panicked when T is a plain struct (not a pointer). +// See https://github.com/prometheus-community/windows_exporter/issues/2365 +func TestNewCollectorStructTypeParam(t *testing.T) { + type systemCounterValues struct { + Name string + + ProcessorQueueLength float64 `perfdata:"Processor Queue Length"` + } + + _, err := NewCollector[systemCounterValues]("System", nil) + if err != nil { + t.Skipf("skipping: failed to create collector: %v", err) + } +} + func BenchmarkQueryPerformanceData(b *testing.B) { for b.Loop() { _, _ = QueryPerformanceData("Global", "")