-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix: do not use new metric instances every Go memstats collection #1792
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
Conversation
Signed-off-by: Ales Pour <[email protected]>
b141a7a
to
074bca8
Compare
Signed-off-by: Ales Pour <[email protected]>
0c8f9cb
to
e21d4eb
Compare
Signed-off-by: Ales Pour <[email protected]>
Signed-off-by: Ales Pour <[email protected]>
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 is looking good.
Could you please add some benchmarks? So that we can actually see if we have tangible improvements?
Benchmark
|
metric Metric | ||
} | ||
|
||
func (m *memStatsMetric) update(memStats *runtime.MemStats) Metric { |
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 is not concurrency-safe. Metrics collection can happen concurrently, and then all kind of weirdness will happen here.
Updating the metrics primitives that are meant for direct instrumentation just before a scrape is an anti-pattern. (The "reverse engineering" needed to calculate the counter increment is a good sign that you are working against the intention of the API here.) Scrapes can happen at any time and in particular concurrently. This PR introduces races. If you try to work around the concurrency problem by using mutexes, you might easily have a slower metrics collection than before this change. I also doubt that the allocation savings here really make a dent in the big picture. Encoding the metrics later to be sent out via the network is probably an order of magnitude more effort anyway. |
This PR changes how Go memstats are collected. Instead of creating new
ConstMetric
during every collection, it (re)uses instances ofGauge
orCounter
in order to reduce needless memory allocations.