process: fix fallback to V1 collector (#1667)

This commit is contained in:
Jan-Otto Kröpke
2024-10-03 23:44:36 +02:00
committed by GitHub
parent 79baf9921d
commit 2a9a11bd01
5 changed files with 159 additions and 120 deletions

View File

@@ -1,11 +1,18 @@
package process_test
import (
"io"
"log/slog"
"sync"
"testing"
"time"
"github.com/alecthomas/kingpin/v2"
"github.com/prometheus-community/windows_exporter/internal/collector/process"
"github.com/prometheus-community/windows_exporter/internal/testutils"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
"github.com/yusufpapurcu/wmi"
)
func BenchmarkProcessCollector(b *testing.B) {
@@ -15,3 +22,49 @@ func BenchmarkProcessCollector(b *testing.B) {
// No context name required as collector source is WMI
testutils.FuncBenchmarkCollector(b, process.Name, process.NewWithFlags)
}
func TestProcessCollector(t *testing.T) {
t.Setenv("WINDOWS_EXPORTER_PERF_COUNTERS_ENGINE", "pdh")
var (
metrics []prometheus.Metric
err error
)
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
c := process.New(nil)
ch := make(chan prometheus.Metric, 10000)
wmiClient := &wmi.Client{
AllowMissingFields: true,
}
wmiClient.SWbemServicesClient, err = wmi.InitializeSWbemServices(wmiClient)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, c.Close(logger))
})
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
for metric := range ch {
metrics = append(metrics, metric)
}
}()
require.NoError(t, c.Build(logger, wmiClient))
time.Sleep(1 * time.Second)
require.NoError(t, c.Collect(nil, logger, ch))
close(ch)
wg.Wait()
require.NotEmpty(t, metrics)
}