In today’s article, I’ll briefly explain how to implement APM in the Python Django application framework. For details on how to install Elasticsearch, Kibana, and APM Server, see my previous article “Application Performance Monitoring/Management (APM) Practices.” In this article, we will briefly introduce some of the areas to be aware of.

 

How to configure

We can open Kibaba:

Click the Add APM button above:

Find Django in APM Agents. We follow the above requirements for configuration. If you’re not familiar with it, here’s a sample application:

git clone https://github.com/liu-xiao-guo/weather_app_django
Copy the code

Let’s follow the instructions above to download and look at the file settings.py. Here is part of the contents of the file:

settings.py

ELASTIC_APM = { # Set required service name. Allowed characters: # a-z, A-Z, 0-9, -, _, and space 'SERVICE_NAME': 'python-django', # Use if APM Server requires a token 'SECRET_TOKEN': '', # Set custom APM Server URL (default: http://localhost:8200) 'SERVER_URL': 'http://localhost:8200', 'DEBUG': True } # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable For production # See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = '_%8&aaez4#5%6)$$#rxdbyv5)(he7(2&@_d77_00tehjkrj$n2' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = False ALLOWED_HOSTS = ['localhost', '127.0.0.1] # Application definition INSTALLED_APPS = [' elasticapm. Contrib. Django', 'the django. Contrib. Admin, 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'weather' ] MIDDLEWARE = [ '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', 'elasticapm.contrib.django.middleware.TracingMiddleware' ]Copy the code

The key point above is:

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = ['localhost', '127.0.0.1']
Copy the code

I need to set DEBUG to false, otherwise it will not send transaction and error messages to the APM Server.

Run Elasticserch, Kibana, and APM Server and you can see things like:

Click on the python – django:

Click on POST weather.views.index above: