pdh: added logging, if PDH CStatus is not valid (#2203)

This commit is contained in:
Jan-Otto Kröpke
2025-09-07 13:31:29 +02:00
committed by GitHub
parent fcf21bb600
commit f1772a742f
76 changed files with 132 additions and 116 deletions

View File

@@ -20,6 +20,7 @@ package pdh
import (
"errors"
"fmt"
"log/slog"
"reflect"
"slices"
"strconv"
@@ -47,6 +48,7 @@ type Collector struct {
handle pdhQueryHandle
totalCounterRequested bool
mu sync.RWMutex
logger *slog.Logger
nameIndexValue int
metricsTypeIndexValue int
@@ -67,13 +69,13 @@ type Counter struct {
FieldIndexSecondValue int
}
func NewCollector[T any](resultType CounterType, object string, instances []string) (*Collector, error) {
func NewCollector[T any](logger *slog.Logger, resultType CounterType, object string, instances []string) (*Collector, error) {
valueType := reflect.TypeFor[T]()
return NewCollectorWithReflection(resultType, object, instances, valueType)
return NewCollectorWithReflection(logger, resultType, object, instances, valueType)
}
func NewCollectorWithReflection(resultType CounterType, object string, instances []string, valueType reflect.Type) (*Collector, error) {
func NewCollectorWithReflection(logger *slog.Logger, resultType CounterType, object string, instances []string, valueType reflect.Type) (*Collector, error) {
var handle pdhQueryHandle
if ret := OpenQuery(0, 0, &handle); ret != ErrorSuccess {
@@ -94,6 +96,7 @@ func NewCollectorWithReflection(resultType CounterType, object string, instances
handle: handle,
totalCounterRequested: slices.Contains(instances, InstanceTotal),
mu: sync.RWMutex{},
logger: logger,
nameIndexValue: -1,
metricsTypeIndexValue: -1,
}
@@ -369,6 +372,12 @@ func (c *Collector) collectWorkerRaw() {
for _, item := range items {
if item.RawValue.CStatus != CstatusValidData && item.RawValue.CStatus != CstatusNewData {
c.logger.Debug("skipping counter item with invalid data status",
slog.String("counter", counter.Name),
slog.String("instance", windows.UTF16PtrToString(item.SzName)),
slog.Uint64("status", uint64(item.RawValue.CStatus)),
)
continue
}

View File

@@ -18,6 +18,7 @@
package pdh_test
import (
"log/slog"
"testing"
"github.com/prometheus-community/windows_exporter/internal/pdh"
@@ -58,7 +59,7 @@ type processFull struct {
}
func BenchmarkTestCollector(b *testing.B) {
performanceData, err := pdh.NewCollector[processFull](pdh.CounterTypeRaw, "Process", []string{"*"})
performanceData, err := pdh.NewCollector[processFull](slog.New(slog.DiscardHandler), pdh.CounterTypeRaw, "Process", []string{"*"})
require.NoError(b, err)
var data []processFull

View File

@@ -18,6 +18,7 @@
package pdh_test
import (
"log/slog"
"testing"
"time"
@@ -45,7 +46,7 @@ func TestCollector(t *testing.T) {
t.Run(tc.object, func(t *testing.T) {
t.Parallel()
performanceData, err := pdh.NewCollector[process](pdh.CounterTypeRaw, tc.object, tc.instances)
performanceData, err := pdh.NewCollector[process](slog.New(slog.DiscardHandler), pdh.CounterTypeRaw, tc.object, tc.instances)
require.NoError(t, err)
time.Sleep(100 * time.Millisecond)