avoid ZeroDevisionError exception when swap memory is 0 (#1345)

* avoid devidedByZero exception when sawp memory is 0

* lint

Co-authored-by: Srikanth Chekuri <srikanth.chekuri92@gmail.com>
This commit is contained in:
Yulin Li 2022-09-28 02:11:32 +08:00 committed by GitHub
parent fc98f0832e
commit 9d8228f82f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 1 deletions

View File

@ -420,7 +420,9 @@ class SystemMetricsInstrumentor(BaseInstrumentor):
if hasattr(system_swap, metric):
self._system_swap_utilization_labels["state"] = metric
yield Observation(
getattr(system_swap, metric) / system_swap.total,
getattr(system_swap, metric) / system_swap.total
if system_swap.total
else 0,
self._system_swap_utilization_labels.copy(),
)