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 packet loss stat zero division #51

Merged
merged 1 commit into from
Jun 29, 2023

Conversation

floatingstatic
Copy link
Contributor

If PacketsSent is 0 we end up doing zero division when calculating packet loss in pinger.Statistics() which ends up reporting as NaN. This can be problematic if one were to take the pinger.Statistics struct and marshal it into json, which will end up failing altogether due to having a value of NaN. Ref: golang/go#3480

Running the test I added in this branch without any other code change highlights this problem:

$ go test -v ./... -run ^TestStatisticsZeroDivision$
?   	github.com/prometheus-community/pro-bing/cmd/ping	[no test files]
=== RUN   TestStatisticsZeroDivision
    ping_test.go:492: Expected 0, got NaN
--- FAIL: TestStatisticsZeroDivision (0.00s)
FAIL
FAIL	github.com/prometheus-community/pro-bing	0.512s
FAIL

Signed-off-by: Jeremiah Millay <[email protected]>
Copy link
Contributor

@SuperQ SuperQ left a comment

Choose a reason for hiding this comment

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

Nice catch, thanks.

@SuperQ SuperQ merged commit 631cd38 into prometheus-community:main Jun 29, 2023
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.

2 participants