headers: fail Build on missing native exports and assert MI/COM layouts (#2739)

Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
This commit is contained in:
Jan-Otto Kröpke
2026-10-16 02:32:29 +02:00
committed by GitHub
parent af2a7c3f6c
commit 2f5781ff13
43 changed files with 866 additions and 16 deletions
+4
View File
@@ -321,6 +321,10 @@ func newCollector[T any](logger *slog.Logger, query *Query, resultType CounterTy
return nil, fmt.Errorf("invalid result type: %v", resultType)
}
if err := Load(); err != nil {
return nil, err
}
var handle pdhQueryHandle
if query != nil {
+32
View File
@@ -55,6 +55,7 @@ import (
"unsafe"
"github.com/prometheus-community/windows_exporter/internal/headers/kernel32"
"github.com/prometheus-community/windows_exporter/internal/headers/lazyproc"
"golang.org/x/sys/windows"
)
@@ -292,8 +293,39 @@ var (
pdhGetRawCounterArrayW = libPdhDll.NewProc("PdhGetRawCounterArrayW")
pdhPdhGetCounterTimeBase = libPdhDll.NewProc("PdhGetCounterTimeBase")
pdhRemoveCounter = libPdhDll.NewProc("PdhRemoveCounter")
// The localized-name procs are optional and checked where they are used.
loadPdh = lazyproc.Once(libPdhDll,
pdhAddCounterW,
pdhAddEnglishCounterW,
pdhCloseQuery,
pdhCollectQueryData,
pdhCollectQueryDataWithTime,
pdhGetFormattedCounterValue,
pdhGetFormattedCounterArrayW,
pdhOpenQuery,
pdhValidatePathW,
pdhExpandWildCardPathW,
pdhGetCounterInfoW,
pdhGetRawCounterValue,
pdhGetRawCounterArrayW,
pdhPdhGetCounterTimeBase,
pdhRemoveCounter,
)
)
// Load checks that pdh.dll, and the kernel32 functions this package uses,
// export every function this package calls without a check of its own.
// [NewQuery] and the collector constructors call it, so a missing export
// fails Build instead of panicking on the first scrape.
func Load() error {
if err := loadPdh(); err != nil {
return err
}
return kernel32.Load()
}
// AddCounter adds the specified counter to the query. This is the internationalized version. Preferably, use the
// function AddEnglishCounter instead. hQuery is the query handle, which has been fetched by OpenQuery.
// szFullCounterPath is a full, internationalized counter path (this will differ per Windows language version).
+4
View File
@@ -39,6 +39,10 @@ type Query struct {
// NewQuery opens a query for [NewQueryCollector]. The caller must Close it
// after the collectors in it.
func NewQuery() (*Query, error) {
if err := Load(); err != nil {
return nil, err
}
var handle pdhQueryHandle
if ret := OpenQuery(0, 0, &handle); ret != ErrorSuccess {