Django
http://djangoproject.comOverview
Django is a Python web framework.
Integration
Django can be enhanced by AlertGrid in various scenarios including:
- Enhancing error handling
- Scheduled task monitoring
1. Enhancing error handling
By default Django can notify project administrators via email each time an unexpected exception was detected in an application. This is very helpful but also has some limitations:
- Multiple emails can be sent for one problem which leads to 'email avalanche'
- Email with error details can land in a spam folder or stay as unread for too long
- For crucial components email notification can be too slow or silent
To enhance standard Django notification mechanisms by AlertGrid features one should:
Add the following middleware class to a project:import urllib
from django.conf import settings
ALERT_GRID_ENDPOINT = 'http://hq.alert-grid.com/save-signal/'
class AlertGridMiddleware(object):
'''
Middleware class which provides complete integration
with AlertGrid (http://alert-grid.com/).
Requirements:
This middleware class requires following settings defined
in a settings.py:
ALERT_GRID_API_ID - AlertGrid's api key (can be found in
Administration->Account Settings)
ALERT_GRID_RECEIVER_NAME - name of a receiver created for
this purpose
As any other middleware this one has to be added to a
MIDDLEWARE_CLASSES section in settings.py
'''
def _compose_data(self, request, exception):
'''
Creates dictionary object with variables to be sent
as an AlertGrid's Signal.
Thanks to this middleware one can easily enable
instant alerting functionality provided by AlertGrid
in any django project.
'''
data = dict()
data['api_id'] = settings.ALERT_GRID_API_ID
data['receiver_name'] = settings.ALERT_GRID_RECEIVER_NAME
data['ERROR_MESSAGE'] = exception.message
return data
def process_exception(self, request, exception):
'''
Launched each time exception was raised in application.
This method does following actions:
- composes data which will be send to AlertGrid
- sends Signal to AlertGrid
This method returns None
'''
data = self._compose_data(request, exception)
opener = urllib.FancyURLopener({})
f = opener.open(ALERT_GRID_ENDPOINT, urllib.urlencode(data))
#contiune
return None
Modify the settings.py file
In a settings.py file you need to register AlertGridMiddleware:
MIDDLEWARE_CLASSES = (
...
'ReferenceTo.middleware.AlertGridMiddleware',
)and add the following settings:
ALERT_GRID_API_ID = 'api_id for AlertGrid account' ALERT_GRID_RECEIVER_NAME = 'MyDjangoProjectName'
2. Scheduled task monitoring
To monitor scheduled tasks written in Python you need to:
- Send a Signal to AlertGrid each time your script finishes executing - this is as easy as adding a short piece of code at the end of your script.
- Build a Workflow in AlertGrid that will check incoming parameters or check if the Signal from your script was received in an expected timeframe - thanks to this you can receive notification when your scheduled task reports some interesting situation (processing too much data, executing for too long) or simply when - for some reason - your scheduled task has failed to complete.
The image below presents an example Workflow configured to send alerts if no Signal was received from a scheduled task - we use this Workflow to check if some cleaning utils were launched for AlertGrid.