applicationinsights.django module

ApplicationInsightsMiddleware class

class applicationinsights.django.ApplicationInsightsMiddleware(get_response=None)[source]

This class is a Django middleware that automatically enables request and exception telemetry. Django versions 1.7 and newer are supported.

To enable, add this class to your settings.py file in MIDDLEWARE_CLASSES (pre-1.10) or MIDDLEWARE (1.10 and newer):

# If on Django < 1.10
MIDDLEWARE_CLASSES = [
    # ... or whatever is below for you ...
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    # ... or whatever is above for you ...
    'applicationinsights.django.ApplicationInsightsMiddleware',   # Add this middleware to the end
]

# If on Django >= 1.10
MIDDLEWARE = [
    # ... or whatever is below for you ...
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    # ... or whatever is above for you ...
    'applicationinsights.django.ApplicationInsightsMiddleware',   # Add this middleware to the end
]

And then, add the following to your settings.py file:

APPLICATION_INSIGHTS = {
    # (required) Your Application Insights instrumentation key
    'ikey': "00000000-0000-0000-0000-000000000000",

    # (optional) By default, request names are logged as the request method
    # and relative path of the URL.  To log the fully-qualified view names
    # instead, set this to True.  Defaults to False.
    'use_view_name': True,

    # (optional) To log arguments passed into the views as custom properties,
    # set this to True.  Defaults to False.
    'record_view_arguments': True,

    # (optional) Exceptions are logged by default, to disable, set this to False.
    'log_exceptions': False,

    # (optional) Events are submitted to Application Insights asynchronously.
    # send_interval specifies how often the queue is checked for items to submit.
    # send_time specifies how long the sender waits for new input before recycling
    # the background thread.
    'send_interval': 1.0, # Check every second
    'send_time': 3.0, # Wait up to 3 seconds for an event

    # (optional, uncommon) If you must send to an endpoint other than the
    # default endpoint, specify it here:
    'endpoint': "https://dc.services.visualstudio.com/v2/track",
}

Once these are in place, each request will have an appinsights object placed on it. This object will have the following properties:

These properties will be present even when DEBUG is True, but it may not submit telemetry unless debug_ikey is set in APPLICATION_INSIGHTS, above.

LoggingHandler class

class applicationinsights.django.LoggingHandler(*args, **kwargs)[source]

This class is a LoggingHandler that uses the same settings as the Django middleware to configure the telemetry client. This can be referenced from LOGGING in your Django settings.py file. As an example, this code would send all Django log messages–WARNING and up–to Application Insights:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        # The application insights handler is here
        'appinsights': {
            'class': 'applicationinsights.django.LoggingHandler',
            'level': 'WARNING'
        }
    },
    'loggers': {
        'django': {
            'handlers': ['appinsights'],
            'level': 'WARNING',
            'propagate': True,
        }
    }
}

# You will need this anyway if you're using the middleware.
# See the middleware documentation for more information on configuring
# this setting:
APPLICATION_INSIGHTS = {
    'ikey': '00000000-0000-0000-0000-000000000000'
}

create_client function

applicationinsights.django.create_client()[source]

Returns an applicationinsights.TelemetryClient instance using the instrumentation key and other settings found in the current Django project’s settings.py file.