Integration Guide - Python (Django) SDK

Django middleware to log incoming REST API calls to Moesif’s error analysis platform.

Source Code on GitHub

Package on PyPI

This SDK uses the Requests library and will work for Python 2.7 — 3.5.

How to install

pip install moesifdjango

How to use

In your settings.py file in your Django project directory, please add moesifdjango.middleware.moesif_middleware to the MIDDLEWARE array.

Because of middleware execution order, it is best to add moesifdjango middleware below SessionMiddleware and AuthenticationMiddleware, because they add useful session data that enables deeper error analysis. On the other hand, if you have other middleware that modified response before going out, you may choose to place Moesif middleware above the middleware modifying response. This allows Moesif to see the modifications to the response data and see closer to what is going over the wire.

MIDDLEWARE = [
    ...
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'moesifdjango.middleware.moesif_middleware'
    ...
]

Add MOESIF_MIDDLEWARE to your settings file.


MOESIF_MIDDLEWARE = {
    'APPLICATION_ID': 'Your Application ID Found in Settings on Moesif',
    ...
    # other options see below.
}

You can find your Application Id from Moesif Dashboard -> Top Right Menu -> App Setup

Configuration options

APPLICATION_ID

(required), string, is obtained via your Moesif Account, this is required.

SKIP

(optional) (request, response) => boolean, a function that takes a request and a response, and returns true if you want to skip this particular event.

IDENTIFY_USER

(optional) (request, response) => string, a function that takes a request and a response, and returns a string that is the user id used by your system. While Moesif identify users automatically, and this middleware try to use the standard Django request.user.username, if your set up is very different from the standard implementations, it would be helpful to provide this function.

GET_SESSION_TOKEN

(optional) (request, response) => string, a function that takes a request and a response, and returns a string that is the session token for this event. Again, Moesif tries to get the session token automatically, but if you setup is very different from standard, this function will be very help for tying events together, and help you replay the events.

MASK_EVENT_MODEL

(optional) (EventModel) => EventModel, a function that takes an EventModel and returns an EventModel with desired data removed. Use this if you prefer to write your own mask function than use the string based filter options: REQUEST_BODY_MASKS, REQUEST_HEADER_MASKS, RESPONSE_BODY_MASKS, & RESPONSE_HEADER_MASKS. The return value must be a valid EventModel required by Moesif data ingestion API. For details regarding EventModel please see the Moesif Python API Documentation.

REQUEST_HEADER_MASKS

(deprecated), string[], is a list of strings for headers that you want to hide from Moesif. Will be removed in future version. Replaced by the function based ‘MASK_EVENT_MODEL’ for additional flexibility.

REQUEST_BODY_MASKS

(deprecated), string[], is a list of key values in the body that you want to hide from Moesif. All key values in the body will be recursively removed before sending to Moesif. Will be removed in future version. Replaced by the function based ‘MASK_EVENT_MODEL’ for additional flexibility.

RESPONSE_HEADER_MASKS

(deprecated), string[], performs the same function for response headers. Will be removed in future version. Replaced by the function based ‘MASK_EVENT_MODEL’ for additional flexibility.

RESPONSE_BODY_MASKS

(deprecated), string[], performs the same task for response body. Will be removed in future version. Replaced by the function based ‘MASK_EVENT_MODEL’ for additional flexibility.

Example:

def identifyUser(req, res):
    # if your setup do not use the standard request.user.username
    # return the user id here
    return "user_id_1"

def should_skip(req, res):
    if "healthprobe" in req.path:
        return True
    else:
        return False

def get_token(req, res):
    # if your setup do not use the standard Django method for
    # setting session tokens. do it here.
    return "token"

def mask_event(eventmodel):
    # do something to remove sensitive fields
    # be sure not to remove any required fields.
    return eventmodel

MOESIF_MIDDLEWARE = {
    'APPLICATION_ID': 'Your application id',
    'LOCAL_DEBUG': False,
    'IDENTIFY_USER': identifyUser,
    'GET_SESSION_TOKEN': get_token,
    'SKIP': should_skip,
    'MASK_EVENT_MODEL': mask_event,
}

How to run tests

  1. Manually clone the git repo
  2. Install moesifdjango middleware as in directions above.
  3. Copy test.py to your local directory for tests.
  4. Invoke python manage.py test.

Other integrations

To view more more documentation on integration options, please visit the Integration Options Documentation.

Leave a Comment