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

Fix potential arithmetic errors in CPU usage calculator #29

Merged
merged 1 commit into from
May 9, 2023

Conversation

gjcairo
Copy link
Contributor

@gjcairo gjcairo commented May 9, 2023

Fix potential arithmetic errors in CPU usage calculator.

Motivation:

In a project using swift-metrics-extras, we noticed that the CPU usage metric would sometimes be -nan, causing issues when encoding to JSON (which our metrics backend does). The lack of logs in swift-metrics(-extras) makes it hard to know for sure why this was happening, but it's possible that if the usage calculator was called in quick succession, the number of uptime ticks (for both the system and the process) could be the same as the previous invocation, causing a 0.0/0.0 division which results in NaN.

Modifications:

Added some extra checks and tests to make sure that these operations are safer.

Result:

Hopefully no more `NaN's.

Copy link
Member

@ktoso ktoso left a comment

Choose a reason for hiding this comment

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

LGTM, yeah avoiding NaN sounds like a good idea here. LGTM

@ktoso ktoso merged commit c71e170 into main May 9, 2023
12 checks passed
@ktoso ktoso deleted the cpu-usage-fix branch May 9, 2023 14:30
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