prometheus-community / pro-bing

A library for creating continuous probers
MIT License
309 stars 52 forks source link

Fix packet loss stat zero division #51

Closed floatingstatic closed 1 year ago

floatingstatic commented 1 year ago

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: https://github.com/golang/go/issues/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