-
Notifications
You must be signed in to change notification settings - Fork 24
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
[cpu/windows] Switch to performance counters #192
[cpu/windows] Switch to performance counters #192
Conversation
fd55440
to
7e067ac
Compare
go.mod
Outdated
@@ -4,7 +4,7 @@ go 1.22.8 | |||
|
|||
require ( | |||
github.com/docker/docker v26.1.5+incompatible | |||
github.com/elastic/elastic-agent-libs v0.9.13 | |||
github.com/elastic/elastic-agent-libs v0.17.3-0.20241112062438-5ba501c7ca8c |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will be updated once elastic/elastic-agent-libs#252 is merged and released
7e067ac
to
fdb61e0
Compare
442bd63
to
b9277da
Compare
b9277da
to
695ac10
Compare
PR is up for review! |
metric/cpu/metrics.go
Outdated
|
||
type OptionFunc func(*option) | ||
|
||
func WithPerformanceCounter() OptionFunc { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume performance counters is just a windows thing? If so, we could add a comment at the top of this function that states that it only applies to windows.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup. That's better
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Putting Windows in the option name would be even more obvious.
Also, consider that we are probably going to want to make this the default once we are satisfied with it. Probably just deleting the option would work once we do that but something to consider for the near future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed again, LGTM. I'm learning quite a bit about Windows internals through your PR chain.
This PR is a split from #192. It aims to simplify the query building for performance counters. Using a global variable might create problems if multiple modules (`cpu` and `core`) are trying to initialise it at a same time. `sync.Once` adds unnecessary CPU overhead (makes it 4x slower). Instead, we can initialise the performance counters in `New(...)` and return error to the caller (i.e. metricbeat).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, optional refactor.
metric/cpu/metrics_windows.go
Outdated
if m.query == nil { | ||
return defaultGet() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit. Just to improve readability and testing. I think it would be nice if Get
was something like:
func Get(m *Monitor) (CPUMetrics, error) {
if m.query == nil {
return getUsingSystemTimes()
}
return getUsingPerformanceCounters(m)
}
That way it is explicit that the only thing Get does is pick which implementation to use, and it is easy to test the 2 implementations independently.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it. Thanks!
Add conditional switching to performance counters for
cpu
/core
metricset collectionsThis PR add an option
WithPerformanceCounter
, which uses performance counter for CPU data collection, when enabled.Default behaviour will be the existing implementation.
How to use performance counters?
It's simple. Something like following:
Why performance counters?
From elastic/beats#40926,
This is a major limitation for such systems and it leads to inconsistent data.
Testing
Test results are reported on the linked issue.
Fixes elastic/beats#40926
Note:
WithPerformanceCounter
option is only effective for windows and is ineffective if used by other OS'.