Skip to main content

Moesif Middleware for Python Django

Project description

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.

Django middleware style and setup changed drastically from version 1.10.

For Django 1.10 or newer, the please add Middleware this way:

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

For Django 1.9 or older, please add Middleware this way:

MIDDLEWARE_CLASSES = [
    ...
    'moesifdjango.middleware_pre19.MoesifMiddlewarePre19',
    ...
    # other middlewares
]

Also, add MOESIF_MIDDLEWARE to your settings.py 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.

``GET_METADATA``

(optional) (request, response) => dictionary, getMetadata is a function that returns an object that allows you to add custom metadata that will be associated with the event. The metadata must be a dictionary that can be converted to JSON. For example, you may want to save a VM instance_id, a trace_id, or a tenant_id with the request.

``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

def get_metadata(req, res):
    return {
        'foo': '12345',
        'bar': '23456',
    }


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,
    'GET_METADATA': get_metadata,
}

Example

An example Moesif integration based on quick start tutorials of Django and Django Rest Framework: Moesif Django Example

Other integrations

To view more more documentation on integration options, please visit `the Integration Options Documentation <https://www.moesif.com/docs/getting-started/integration-options/>`__.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

moesifdjango-1.5.3.tar.gz (23.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

moesifdjango-1.5.3-py2.py3-none-any.whl (30.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file moesifdjango-1.5.3.tar.gz.

File metadata

  • Download URL: moesifdjango-1.5.3.tar.gz
  • Upload date:
  • Size: 23.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.9.1 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/2.7.16

File hashes

Hashes for moesifdjango-1.5.3.tar.gz
Algorithm Hash digest
SHA256 7fed34cdb7ab9457022c1cb50d438f04d2870ab516a005308a100d570f2bede5
MD5 89ec7659ea08f387266722d62c66c672
BLAKE2b-256 e77483ee88b1e69e1e6fd769af15a8c157c2aa6a0b20e57cf9570b5be05e496c

See more details on using hashes here.

File details

Details for the file moesifdjango-1.5.3-py2.py3-none-any.whl.

File metadata

  • Download URL: moesifdjango-1.5.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 30.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.9.1 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/2.7.16

File hashes

Hashes for moesifdjango-1.5.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7654bdf0ea2dec8084e8c6dd0913d249bd7937ecea8c92d14936f47b7bf13f24
MD5 7e66528197ed72dc5a21af62addbb699
BLAKE2b-256 4f62f184974d0d49c9c612266ddfb2fb5546a10cacc0705dae1e0af489edef58

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page