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

ZeroDivisionError in cpu_linux.py #610

Closed
ivasic opened this issue Oct 14, 2019 · 2 comments
Closed

ZeroDivisionError in cpu_linux.py #610

ivasic opened this issue Oct 14, 2019 · 2 comments
Milestone

Comments

@ivasic
Copy link
Contributor

ivasic commented Oct 14, 2019

Describe the bug:

When deploying my Django app on AWS Beanstalk the APM agent throws ZeroDivisionError with the trace:

ZeroDivisionError: float division by zero
  File "elasticapm/utils/threading.py", line 79, in run
    rval = self._function(*self._args, **self._kwargs)
  File "elasticapm/metrics/base_metrics.py", line 97, in collect
    for data in metricset.collect():
  File "elasticapm/metrics/base_metrics.py", line 195, in collect
    self.before_collect()
  File "elasticapm/metrics/sets/cpu_linux.py", line 72, in before_collect
    cpu_usage_ratio = delta["cpu_usage"] / delta["cpu_total"]

To Reproduce

I'm not able to consistently reproduce this behavior but the problem obviously seems that the cpu_total read from the stats file is zero. It may be due to the newly launched docker container that has just started up.

One way to reproduce this (although in my local tests it rarely happens):

  • Launch a new python:3.7 container
  • Run any django manage.py command as CMD via Dockerfile

Expected behavior:

No ZeroDivisionError exception is thrown

Environment (please complete the following information)

  • OS: Linux
  • Python version: 3.7.4 (happens on 3.7.3 as well)
  • Framework and version [e.g. Django 2.1]: Django 2.2.6
  • APM Server version: v7.4.0
  • Agent version: 5.2.1

Additional context

I managed to fix this by catching this exception and setting the ration to 0 which I think makes sense.

try:
    cpu_usage_ratio = delta["cpu_usage"] / delta["cpu_total"]
except ZeroDivisionError:
    cpu_usage_ratio = 0

Similar problem happens with cpu_process_percent which I solved the same way.
I can open up a pull request (with a corresponding test case in cpu_linux_test.py?) if that makes it easier.

Thanks,
Ivan

@beniwohli
Copy link
Contributor

Hi @ivasic

great catch, thanks for opening the issue! If you're up for it, a pull request would be great! The approach you outlined above looks sensible to me.

@basepi
Copy link
Contributor

basepi commented Oct 16, 2019

Fixed in #611

@lreuven lreuven added this to the 7.5 milestone Nov 17, 2019
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

No branches or pull requests

4 participants