ApplicationInsightsMiddleware Class

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 ...
       'botbuilder.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 ...
       'botbuilder.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:

  • client: This is an instance of the <xref:applicationinsights.TelemetryClient> type, which will submit telemetry to the same instrumentation key, and will parent each telemetry item to the current request.

  • request: This is the <xref:applicationinsights.channel.contracts.RequestData> instance for the current request. You can modify properties on this object during the handling of the current request. It will be submitted when the request has finished.

  • context: This is the <xref:applicationinsights.channel.TelemetryContext> object for the current ApplicationInsights sender.

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.

Inheritance
builtins.object
ApplicationInsightsMiddleware

Constructor

ApplicationInsightsMiddleware(get_response=None)

Parameters

get_response
default value: None

Methods

process_exception
process_request
process_response
process_template_response
process_view

process_exception

process_exception(request, exception)

Parameters

request
Required
exception
Required

process_request

process_request(request)

Parameters

request
Required

process_response

process_response(request, response)

Parameters

request
Required
response
Required

process_template_response

process_template_response(request, response)

Parameters

request
Required
response
Required

process_view

process_view(request, view_func, view_args, view_kwargs)

Parameters

request
Required
view_func
Required
view_args
Required
view_kwargs
Required