Python
Django

Django Web Application

On this page, we help you install ShortLoop SDK in your Django web application, so that it can automatically sample traffic.

Requirements

Python version - 3.7 or higher

Django version - 3.0 or higher.

Install

Install ShortLoop Python SDK.

$ pip install shortloop-python==0.1.8

Configure

To configure the SDK, you need to add it as a middleware in your application. This can be done in your Django settings module (normally at <project_name>/settings.py).

MIDDLEWARE = [
    ...
    'shortloop_python.integrations.ShortLoopDjangoMiddleware',
    ...
    ...
]

Django applies middleware in the order it's defined in MIDDLEWARE (top-down). Put ShortLoop's middleware as high as possible in the middleware chain to get the most accurate traffic data.

Import the SDK from installed package on top of your Django settings module.

import shortloop_python

Initialize SDK with options - ShortloopEndpoint and ApplicationName as shown in below example and register the middleware.

shortloop_python.init(
  url="https://shortloop.company-name.com",       # the shortloop url for your org. (Provided by ShortLoop team.)
  application_name="service-name",                # your application name here
  auth_key="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", # ShortLoop Auth Key. (Provided by ShortLoop team.)
  environment="your-environment"                  # for e.g stage or prod
)

Configure : Masking sensitive headers.

To mask sensitive headers, provide an array as mask_headers field as shown in below example.

Note that mask_headers field is optional and headers are case insensitive.

shortloop_python.init(
  ...
  ...
  environment="your-environment",                 # for e.g stage or prod
  mask_headers=["cookie", "x-auth"]               # [Optional]. The following headers values will be masked at the SDK level itself. (Will not be sent to the server)
)

Configure : Samples capturing frequency

By default; ShortLoop SDK will capture a small percentage of traffic; so that the performance impact is almost nil. But for certain use-cases; or non production environments; traffic capturing can be configured to 'always'; in which case SDK will try to capture 100% of the Traffic.

shortloop_python.init(
  ...
  ...
  environment="your-environment",                 
  mask_headers=["cookie", "x-auth"],               
  capture="always"                                # Requesting SDK to try to capture all the API calls.
)

Setting the above property; doesn't guarantees that all the API calls will be captures. SDK will try to capture most of the Traffic; but if the traffic increases; the Samples will be dropped.

⚠️

It is not recommended to set the above property in high traffic production environments which are performance and latency sensitive.

Verify

Quickly test if project is running after configuring SDK.

⚠️

Run command maybe custom to your project.

python3 manage.py runserver

Django launches two processes (opens in a new tab) for hot reload. SDK gets initialized once for each process. This will not cause any issues, except there will be duplicate SDK logs on application boot. You can disable hot reload by giving --noreload to the run command.

After the changes, redeploy your Django Application.

Feel free to email for a quick support, reporting bug or improvements suggestions. Sumit B Mulchandani (sumit@shortloop.dev)

Copyright © 2023 ShortLoop, Inc. All rights reserved.