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 label user to process collector #1472

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

aburtasov
Copy link
Contributor

@aburtasov aburtasov commented May 8, 2024

Added label "user" to process collector. Using WIN32_LoggedOnUser and Win32_SessionProcess

Example metric with new label:

HELP windows_process_cpu_time_total Returns elapsed time that all of the threads of this process used the processor to execute instructions by mode (privileged, user).
TYPE windows_process_cpu_time_total counter
windows_process_cpu_time_total{creating_process_id="12244",mode="privileged",process="powershell",process_id="17512",user="MYDOMAIN\aburtasov"} 0.296875
windows_process_cpu_time_total{creating_process_id="12244",mode="privileged",process="powershell",process_id="19688",user="MYDOMAIN\aburtasov"} 14.296875
windows_process_cpu_time_total{creating_process_id="12244",mode="privileged",process="powershell",process_id="25612",user="MYDOMAIN\aburtasov"} 0.484375

Signed-off-by: Andrey Burtasov <andrey.a.burtasov@gmail.com>
@aburtasov aburtasov requested a review from a team as a code owner May 8, 2024 10:52
@jkroepke jkroepke added this to the v0.26.0 milestone May 11, 2024
Copy link
Member

@jkroepke jkroepke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this PR on my local system and it works.

However, I see that the collector is 10 times slower compared to master branch.

master:
windows_exporter_collector_duration_seconds{collector="process"} 0.0121184

PR:
windows_exporter_collector_duration_seconds{collector="process"} 0.1610989

Thats to much. I have provide some suggestions, I hope they help. After apply the suggestion, verify locally, if everything still works as expected.


err := wmi.Query(win32LoggedOnUserQuery, &users)
if err != nil {
fmt.Println(err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the error occurs, the error should be returned as return value. Currently, there is just printed and the function continue, result into more errors.


err = wmi.Query(win32SessionProcessQuery, &sessionProcesses)
if err != nil {
fmt.Println(err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the error occurs, the error should be returned as return value. Currently, there is just printed and the function continue, result into more errors.

matchps := reps.FindStringSubmatch(sesessionProcess.Dependent)
number := matchps[1]

processMap[number] = fullname
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a break here. If we found the user, we the break the search. This saves unnecessary extra loops.

Suggested change
processMap[number] = fullname
processMap[number] = fullname
break

@@ -261,6 +278,7 @@ type WorkerProcess struct {

func (c *collector) Collect(ctx *types.ScrapeContext, ch chan<- prometheus.Metric) error {
data := make([]perflibProcess, 0)
processMap := getUserProcess()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the error returned, add a log warning here

like:

Suggested change
processMap := getUserProcess()
processMap, err := getUserProcess()
if err != nil {
_ = level.Warn(c.logger).Log("msg", "Could not query users for processes", "err", err)
}

@@ -301,6 +319,7 @@ func (c *collector) Collect(ctx *types.ScrapeContext, ch chan<- prometheus.Metri
processName,
pid,
cpid,
processMap[pid],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a fail-safe here, in case processMap[pid] may not hit, we have a panic here. Add a check, if the keys exists and return unknown

Copy link
Member

@jkroepke jkroepke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this PR on my local system and it works.

However, I see that the collector is 10 times slower compared to master branch.

master:
windows_exporter_collector_duration_seconds{collector="process"} 0.0121184

PR:
windows_exporter_collector_duration_seconds{collector="process"} 0.1610989

Thats to much. I have provide some suggestions, I hope they help. After apply the suggestion, verify locally, if everything still works as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants