Skip to content

Commit

Permalink
process: fix metric labels for multiple process instances
Browse files Browse the repository at this point in the history
Signed-off-by: Jan-Otto Kröpke <[email protected]>
  • Loading branch information
jkroepke committed Dec 9, 2024
1 parent c465d0f commit 456277d
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 5 deletions.
6 changes: 3 additions & 3 deletions internal/collector/performancecounter/performancecounter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand All @@ -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(
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion internal/collector/performancecounter/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
1 change: 1 addition & 0 deletions pkg/collector/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion tools/end-to-end-test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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" `
Expand Down

0 comments on commit 456277d

Please sign in to comment.