metrics: Reduce CPU cycles spent in Prometheus metric collection #3414
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Prior to this change, Prometheus description objects were re-created on
every collection cycle, even though label names change extremely rarely
in practice (perhaps even never on a given machine). This means that we
were unnecessarily causing CPU cycles. Using the benchmark in the
previous commit, we determine we're making collection at least 23%
faster, but based on production profiling data, we estimate this is
improving the Kubelet's baseline CPU usage by ~1.3% (potentially even
more since it also relieves the GC). This is because the benchmark shows
that all calls to
NewDesc
are completely disappearing from profilingdata, therefore stastically speaking reducing the kubelet's CPU time by
that much.
Production Kubelet CPU data: https://pprof.me/437533c/?filter_by_function=collectContainersInfo&search_string=collectContainersInfo
Before Optimizing: https://pprof.me/ac14fee/
After Optimizing: https://pprof.me/7ec5def/
Side note: We believe we can further improve CPU usage here, but we think it requires a new API in the
prometheus.NewConstMetric
functionality, that allows caching of the result ofMakeLabelPairs
. We'll open a feature request for this.This patch was created on the Polar Signals Let's Profile stream: https://www.youtube.com/watch?v=dkx-5vGCns8