honeybadger-io / honeybadger-python

Send Python and Django errors to Honeybadger.
https://www.honeybadger.io/
MIT License
15 stars 25 forks source link

incorrectly assuming dev environment #134

Closed dotysan closed 1 year ago

dotysan commented 1 year ago

Steps to reproduce.

git clone --branch dotysan git@github.com:dotysan/honeybadger-python.git
cd honeybadger-python
export HONEYBADGER_API_KEY='{your_api_key}'
make django

Now that Django is up and running, then go to http://localhost:8000/api/div and whammo!

Django version 2.2.24, using settings 'honeybadger_example.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Internal Server Error: /api/div
Traceback (most recent call last):
  File ".venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File ".venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File ".venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "examples/django_app/app/views.py", line 24, in buggy_div
    return JsonResponse({'result': a / b})
ZeroDivisionError: float division by zero
[20/Jan/2023 20:16:40] "GET /api/div HTTP/1.1" 500 67330

You get a 500. But nothing is sent to app.honeybadger.io. What am I doing wrong?

Front logo Front conversations

dotysan commented 1 year ago

Solution is to set HONEYBADGER_FORCE_REPORT_DATA=True. Why isn't this mentioned in the quickstart?!?

joshuap commented 1 year ago

@dotysan glad you figured it out! It is mentioned (see the note under "Install Honeybadger"): https://github.com/honeybadger-io/honeybadger-python#install-honeybadger

dotysan commented 1 year ago

This occurrs when HONEYBADGER_ENVIRONMENT is not set. I.e. 'production' by default.

joshuap commented 1 year ago

This occurrs when HONEYBADGER_ENVIRONMENT is not set. I.e. 'production' by default.

I see—thanks for clarifying. @subzero10 @Kelvin4664 can you take a look at this next week? We should be reporting exceptions by default if there is no environment specified.

subzero10 commented 1 year ago

I started looking at the code and soon realized that the current implementation appears to be correct. So, I created a sample repo and tried it out. If you look at the code, I only set the api key. The environment is set to production by default and the exception is reported to Honeybadger without any additional config.

image image

@dotysan, I looked into the stack trace above and it seems you are running on Django. For Django setups, we automatically set the environment to development if DEBUG is found in settings. Could this be the reason your exceptions are not reported by default?

dotysan commented 1 year ago

@subzero10 Oh my, you found it! honeybadger/contrib/django.py#L102

And your own example sets DEBUG. examples/django_app/honeybadger_example/settings.py#L26

That explains things. However...

Here's my workflow. I'm a new user of hb. I already have DEV and PROD environments. First thing I'm going to do is install it in DEV and see what happens. Nowhere in the intro does it say Django's DEBUG will disable hb. And frankly, I disagree with that logic.

Anyhoo, thanks for looking into this. I had reviewed the code elsewhere in here but somehow missed django.py.

subzero10 commented 1 year ago

Hey @dotysan, I'm glad that we found the root of the issue. You are right, this is not documented anywhere. I will update our documentation to include this note.

As for the logic behind it, I'm not the original author of this piece of code and at first I was surprised to see it. However, I can understand why it's there. The package tries to make the best decision when it comes to the default config. So, it sees that Django is set on debug mode, indicating this is not a production environment, hence it makes sense not to set Honeybadger config in production.