Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Windows metric for process handle counts #21379

Closed
braydonk opened this issue May 1, 2023 · 2 comments
Closed

Add Windows metric for process handle counts #21379

braydonk opened this issue May 1, 2023 · 2 comments

Comments

@braydonk
Copy link
Contributor

braydonk commented May 1, 2023

Component(s)

receiver/hostmetrics

Is your feature request related to a problem? Please describe.

I would like a metric for a process' Handle Count in Windows.

Describe the solution you'd like

I would like to add a gauge metric in hostmetrics called process.handles, which represents the process' handle count at time of collection.

Each scrape, the receiver would do a NtQuerySystemInformation syscall with the info class SystemProcessInformation, which responds with information for all processes including each process' handle count. For each pid, we would get the handle count from the syscall results and record the datapoint.

Describe alternatives you've considered

I have opened shirou/gopsutil#1455 with an accompanying example PR to implement this in gopsutil. This would bring this metric in-line with the implementation of all other process metrics, but this has 2 major issues:

  1. The gopsutil API is inflexible for a piece of process information that is exclusive to a single platform like this. NumFDs is not the same as a process' handle count in Windows.
  2. This is extremely inefficient. It requires doing a full syscall for all handles on the system for every single process we scrape metrics for.

Additional context

This StackExchange answer contains a good summary of what handles are in Windows: https://superuser.com/questions/1065826/handles-vs-threads-vs-processes

NtQuerySystemInformation syscall docs: https://learn.microsoft.com/en-us/windows/win32/api/winternl/nf-winternl-ntquerysysteminformation
Specifically this sections of the syscall docs is relevant: https://learn.microsoft.com/en-us/windows/win32/api/winternl/nf-winternl-ntquerysysteminformation#systemprocessinformation

@braydonk braydonk added enhancement New feature or request needs triage New item requiring triage labels May 1, 2023
@github-actions
Copy link
Contributor

github-actions bot commented May 1, 2023

Pinging code owners:

See Adding Labels via Comments if you do not have permissions to add labels yourself.

@atoulme atoulme removed the needs triage New item requiring triage label May 3, 2023
@braydonk braydonk changed the title Windows support for process.open_file_descriptors Add Windows metric for process handle counts May 26, 2023
dmitryax pushed a commit that referenced this issue Jun 27, 2023
**Description:** <Describe what has changed.>
Adds a new Windows-exclusive metric called process.handles, which
represents the handle count of the given process. When enabled, the
receiver will make a WMI Query at the beginning of each scrape to update
the handle count for all processes on the system. If the metric is
enabled on a platform other than Windows, an error will be produced when
attempting to refresh handle counts. This matches the rough behaviour of
the Linux exclusive `process.open_file_descriptors`.

**Link to tracking Issue:** <Issue number if applicable>
#21379 
#12482

**Testing:** <Describe what testing was performed and which tests were
added.>
Ran the binary with the following configuration:
```
receivers:
  hostmetrics:
    collection_interval: 2s
    scrapers:
      cpu: {}
      disk: {}
      filesystem: {}
      load: {}
      memory: {}
      network: {}
      paging: {}
      process:
        mute_process_name_error: true
        metrics:
          process.handles:
            enabled: true
      processes: {}

exporters:
  file:
    path: x.json

service:
  pipelines:
    metrics:
      receivers: [hostmetrics]
      exporters: [file]
```

The following is an example result of a scrape with this configuration.
https://gist.github.com/braydonk/c97996272574319e03111dc79076a1bd
@braydonk
Copy link
Contributor Author

braydonk commented Jul 5, 2023

The process.handles metric is in v0.81! I will close this issue now.

@braydonk braydonk closed this as completed Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants