plaintextpackets / netprobe_lite

Simple internet performance tester written in Python
628 stars 127 forks source link

Division by 0 issue in presentation #51

Open plaintextpackets opened 5 months ago

plaintextpackets commented 5 months ago

2024-05-24 10:58:52 ZeroDivisionError: division by zero 2024-05-24 11:01:22 Traceback (most recent call last): 2024-05-24 11:01:22 File "/usr/local/lib/python3.11/wsgiref/handlers.py", line 137, in run 2024-05-24 11:01:22 self.result = application(self.environ, self.start_response) 2024-05-24 11:01:22 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-05-24 11:01:22 File "/usr/local/lib/python3.11/site-packages/prometheus_client/exposition.py", line 128, in prometheus_app 2024-05-24 11:01:22 status, headers, output = _bake_output(registry, accept_header, accept_encoding_header, params, disable_compression) 2024-05-24 11:01:22 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-05-24 11:01:22 File "/usr/local/lib/python3.11/site-packages/prometheus_client/exposition.py", line 104, in _bake_output 2024-05-24 11:01:22 output = encoder(registry) 2024-05-24 11:01:22 ^^^^^^^^^^^^^^^^^ 2024-05-24 11:01:22 File "/usr/local/lib/python3.11/site-packages/prometheus_client/openmetrics/exposition.py", line 21, in generate_latest 2024-05-24 11:01:22 for metric in registry.collect(): 2024-05-24 11:01:22 File "/usr/local/lib/python3.11/site-packages/prometheus_client/registry.py", line 97, in collect 2024-05-24 11:01:22 yield from collector.collect() 2024-05-24 11:01:22 File "/netprobe_lite/presentation.py", line 59, in collect 2024-05-24 11:01:22 average_latency = total_latency / len(stats_netprobe['stats']) 2024-05-24 11:01:22 ~~~~^~~~~~~~ 2024-05-24 11:01:22 ZeroDivisionError: division by zero

Mytutoyo1 commented 3 months ago

same Issue here, any news ?

sazeygit commented 3 months ago

Hello both, I was also getting this same error, after much head banging it emerged (in my case) the custom DNS I specified could not be reached for lookups due to firewall config. Once that was resolved, a container restart is all it took to get it behaving again.

Xian55 commented 3 months ago

Hello, i've been using the app for over 2 months, for the first time today i got an error

Traceback (most recent call last):
  File "/usr/local/lib/python3.12/wsgiref/handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/prometheus_client/exposition.py", line 128, in prometheus_app
    status, headers, output = _bake_output(registry, accept_header, accept_encoding_header, params, disable_compression)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/prometheus_client/exposition.py", line 104, in _bake_output
    output = encoder(registry)
             ^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/prometheus_client/openmetrics/exposition.py", line 21, in generate_latest
    for metric in registry.collect():
  File "/usr/local/lib/python3.12/site-packages/prometheus_client/registry.py", line 97, in collect
    yield from collector.collect()
  File "/netprobe_lite/presentation.py", line 60, in collect
    average_latency = total_latency / len(stats_netprobe['stats'])
                      ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero

Meanwhile the presentation / website shows ERR_CONNECTION_REFUSED.

One possible solution would be to sanitize the len(stats_netprobe['stats'])

my suggestion would be the following

total_latency = sum(stat['latency'] for stat in stats_netprobe['stats'])
count = len(stats_netprobe['stats'])

if count == 0:
    average_latency = 0  # or handle this case as per your requirements
else:
    average_latency = total_latency / count

# Continue with the rest of your code