Gunicorn log errors in Google Cloud Platform

2/4/2019

I am running a Django REST API on Google Kubernetes Engine, using Gunicorn for my WSGI server. When my application encounters a 500 server error, the Python stack trace is not showing up in the GCP Logging console (in the "GKE Container" resource). In a different Django project of mine which uses Daphne for the ASGI/WSGI server, this traceback is being properly logged. Even more strange, the Gunicorn application has properly logged errors before, just a few weeks ago. Those errors appear in the Error Reporting console as well.

To be clear, this is the type of information that I would like to see in the GCP logs:

Internal Server Error: /v1/user/errant-endpoint
Traceback (most recent call last):
  File "/path/to/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)

  ...

  File "/path/to/project/file.py", line 176, in my_file
    print(test)
NameError: name 'test' is not defined

For the Gunicorn project, some of the Python tracebacks are logged, like this one when Gunicorn is started:

/usr/local/lib/python3.6/site-packages/django/db/models/fields/__init__.py:1421: RuntimeWarning: DateTimeField User.last_login received a naive datetime (2019-02-04 05:49:47.530648) while time zone support is active.

With 500 errors, however, only the HTTP info is logged:

[04/Feb/2019:06:03:58 +0000] "POST /v1/errant-endpoint HTTP/1.1" 500

I've looked up the resources for setting up Stackdriver Logging and Stackdriver Error Reporting, but neither of these seem to apply because 1) that only appears to work when you want to explicitly log your errors (as in client.report_exception()), and 2) Error Reporting has caught previous errors without my setting up, so it seems to be possible without having to install those client libraries.

There are so many variables at play here that I'm not sure where to start. I may not have provided enough information here in order to properly diagnose this (docker setup, kubernetes configuration, etc.), but I figured I may be misunderstanding something fundamental about this process, and that someone could be so kind as to enlighten me.

EDIT:

I found in the GKE documentation how to ensure that the Stackdriver logging is enabled for my cluster. Still no luck.

UPDATE (partial solution):

I set DEBUG = True in settings.py, which prompted the errors to start being logged in Stackdriver Logging and Error Reporting. I've gone ahead and done this for my canary environment, but it's less than ideal, since it exposes some of my backend code. Still not sure why this works for my other GCP project without running in debug mode.

-- Michael Hays
django
google-cloud-platform
google-kubernetes-engine
gunicorn
python

0 Answers