Django Instrumentation

This feature requires AppSignal for Python version 0.1.0 or higher.
This feature requires Django version 1.10 or higher.

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design.

Installation

Don't forget to install the AppSignal for Python package in your application first.

First, install the opentelemetry-instrumentation-django package. To add it to your project, add the following line to your requirements.txt file:

Shell
# requirements.txt opentelemetry-instrumentation-django

To instrument queries performed via the Django ORM, install the instrumentation for the SQL database that your application uses:

The AppSignal for Python integration will automatically use this instrumentation when the corresponding package is installed. To disable this instrumentation without uninstalling the package, use the disable_default_instrumentations configuration option.

Development setup

Django requires some extra setup, as it's usually the first thing started in an app.

As shown in the example below, the appsignal module needs to be imported in the manage.py file. Then in the main method, the appsignal.start method needs to be called to initialize the instrumentation configured in the __appsignal__.py file.

Python
# manage.py #!/usr/bin/env python """Django's command-line utility for administrative tasks.""" import os import sys # Add this import statement import appsignal def main(): """Run administrative tasks.""" os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<YOUR_APP_NAME_HERE>.settings') # Add this method call appsignal.start() try: from django.core.management import execute_from_command_line except ImportError as exc: raise ImportError( "Couldn't import Django. Are you sure it's installed and " "available on your PYTHONPATH environment variable? Did you " "forget to activate a virtual environment?" ) from exc execute_from_command_line(sys.argv) if __name__ == '__main__': main()

Production setup (WSGI/ASGI)

In production, it is recommended to run Django using a WSGI or ASGI server, such as Gunicorn or Uvicorn. In this scenario, the manage.py file that is modified in the development setup above is never called.

To instrument your application when started as a WSGI or ASGI server, add the following to your project's wsgi.py file, if using a WSGI server:

Python
# wsgi.py import os # Add this import statement import appsignal from django.core.wsgi import get_wsgi_application os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_app.settings') # Add this method call appsignal.start() application = get_wsgi_application()

Or to your project's asgi.py file, if using an ASGI server:

Python
# asgi.py import os # Add this import statement import appsignal from django.core.asgi import get_asgi_application os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_app.settings') # Add this method call appsignal.start() application = get_asgi_application()

Features

By default, the Django instrumentation reports incoming request parameters, like query strings and routing parameters.

Do not send Personal Identifiable Information (PII) to AppSignal. You must ensure that PII (such as personal names, email addresses, passwords, etc.) is filtered before data is sent to AppSignal. If you must identify a person, consider using a user ID, hash or pseudonymized identifier instead.