Search code examples
pythongunicornsentryraven

Adding a hook to Gunicorn for raven-python (sentry client)


I want to know where i would put this into either my code or gunicorn's in order to get raven running. http://raven.readthedocs.org/en/latest/config/django.html#gunicorn


Solution

  • Bit late but anyway :)

    You need to add this into your Gunicorn config file. For example when you spin up gunicorn_django you can pass it a -c (--config) argument which takes a path to python file.

    Gunicorn will use this file to load configuration settings not passed as arguments, like workers and log paths etc etc. But you can also include functions gunicorn will call at certain points of the processes life cycle. This is where you would put the raven setup, according to the Raven docs.

    For example:

    $ gunicorn_django -c /path/to/gunicorn_settings.py
    

    The file could contain the following:

    workers = 2
    bind = 'unix:/tmp/my_project_name.sock'  # Binds to a unix socket rather than ip/port
    errorlog = '/path/to/logs/gunicorn.error.log'
    
    def when_ready(server):
        from django.core.management import call_command
        call_command('validate')
    

    Be careful to ensure your DJANGO_SETTINGS_MODULE is exported correctly otherwise call_command('validate') will throw a SystemExit and your process will fail to start.

    You can read more on Gunicorn config files at: http://docs.gunicorn.org/en/latest/configure.html