diff --git a/internal/collector/performancecounter/performancecounter.go b/internal/collector/performancecounter/performancecounter.go index 1095f737f..0527e0cfc 100644 --- a/internal/collector/performancecounter/performancecounter.go +++ b/internal/collector/performancecounter/performancecounter.go @@ -129,7 +129,7 @@ func (c *Collector) Build(logger *slog.Logger, _ *mi.Session) error { for i, object := range c.config.Objects { if object.Name == "" { - return fmt.Errorf("object name is required") + return errors.New("object name is required") } if object.Object == "" { @@ -145,8 +145,8 @@ func (c *Collector) Build(logger *slog.Logger, _ *mi.Session) error { } names = append(names, object.Name) - counters := make([]string, 0, len(object.Counters)) + for j, counter := range object.Counters { if counter.Metric == "" { c.config.Objects[i].Counters[j].Metric = c.sanitizeMetricName( @@ -239,7 +239,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) error { ) } - return nil + return errors.Join(errs...) } func (c *Collector) collectObject(ch chan<- prometheus.Metric, perfDataObject Object) error { diff --git a/internal/collector/performancecounter/performancecounter_test_test.go b/internal/collector/performancecounter/performancecounter_test_test.go index c83d57793..6d482c4bb 100644 --- a/internal/collector/performancecounter/performancecounter_test_test.go +++ b/internal/collector/performancecounter/performancecounter_test_test.go @@ -144,6 +144,7 @@ windows_performancecounter_processor_information_processor_time\{core="0,0",stat logger := slog.New(slog.NewTextHandler(io.Discard, nil)) err := perfDataCollector.Build(logger, nil) + if tc.buildErr != "" { require.ErrorContains(t, err, tc.buildErr) diff --git a/internal/collector/performancecounter/types.go b/internal/collector/performancecounter/types.go index efe8c12ff..a01d56918 100644 --- a/internal/collector/performancecounter/types.go +++ b/internal/collector/performancecounter/types.go @@ -18,7 +18,7 @@ package performancecounter import "github.com/prometheus-community/windows_exporter/internal/perfdata" type Object struct { - Name string `json:"name" yaml:"name"` + Name string `json:"name" yaml:"name"` Object string `json:"object" yaml:"object"` Instances []string `json:"instances" yaml:"instances"` Counters []Counter `json:"counters" yaml:"counters"` diff --git a/pkg/collector/collect.go b/pkg/collector/collect.go index a2b580236..233bd27a7 100644 --- a/pkg/collector/collect.go +++ b/pkg/collector/collect.go @@ -206,6 +206,7 @@ func (c *Collection) collectCollector(ch chan<- prometheus.Metric, logger *slog. if err != nil && !errors.Is(err, perfdata.ErrNoData) && !errors.Is(err, types.ErrNoData) { loggerFn := logger.Warn + if errors.Is(err, perfdata.ErrPerformanceCounterNotInitialized) || errors.Is(err, mi.MI_RESULT_INVALID_NAMESPACE) { err = fmt.Errorf("%w. Check application logs from initialization pharse for more information", err) } diff --git a/tools/end-to-end-test.ps1 b/tools/end-to-end-test.ps1 index 85e5bc9cb..49de5b5a8 100644 --- a/tools/end-to-end-test.ps1 +++ b/tools/end-to-end-test.ps1 @@ -26,7 +26,7 @@ $exporter_proc = Start-Process ` -PassThru ` -FilePath ..\windows_exporter.exe ` -ArgumentList "--log.level=debug","--web.disable-exporter-metrics","--collectors.enabled=[defaults],cpu_info,textfile,process,pagefile,performancecounter,scheduled_task,tcp,udp,time,system,service,logical_disk,printer,os,net,memory,logon,cache","--collector.process.include=explorer.exe","--collector.scheduled_task.include=.*GAEvents","--collector.service.include=Themes","--collector.textfile.directories=$($textfile_dir)",@" ---collector.performancecounter.objects="[{\"object\":\"Processor Information\",\"instances\":[\"*\"],\"instance_label\":\"core\",\"counters\":[{\"name\":\"% Processor Time\",\"metric\":\"windows_performancecounter_processor_information_processor_time\",\"labels\":{\"state\":\"active\"}},{\"name\":\"% Idle Time\",\"metric\":\"windows_performancecounter_processor_information_processor_time\",\"labels\":{\"state\":\"idle\"}}]},{\"object\":\"Memory\",\"counters\":[{\"name\":\"Cache Faults/sec\",\"type\":\"counter\"}]}]" +--collector.performancecounter.objects="[{\"name\":\"cpu\",\"object\":\"Processor Information\",\"instances\":[\"*\"],\"instance_label\":\"core\",\"counters\":[{\"name\":\"% Processor Time\",\"metric\":\"windows_performancecounter_processor_information_processor_time\",\"labels\":{\"state\":\"active\"}},{\"name\":\"% Idle Time\",\"metric\":\"windows_performancecounter_processor_information_processor_time\",\"labels\":{\"state\":\"idle\"}}]},{\"name\":\"memory\",\"object\":\"Memory\",\"counters\":[{\"name\":\"Cache Faults/sec\",\"type\":\"counter\"}]}]" "@ ` -WindowStyle Hidden ` -RedirectStandardOutput "$($temp_dir)/windows_exporter.log" `