Monitoring on-premises Python applications running in individual Docker container

You can configure the Python data collector to monitor on-premises Python applications running in individual Docker container. During data collector deployment, the server information must be provided so that the data collector can be configured to connect to the appropriate server. The server information is provided as a configuration package for downloading from the IBM Cloud Pak console.

Before you begin

Procedure

To configure the Python data collector in individual Docker container mode, do the following steps:

  1. Update the Dockerfile to add the following lines to install Python data collector for your Python application. Add the following lines in Dockerfile:

    ADD ibm-cloud-apm-dc-configpack.tar /application/root/path
    RUN mv /application/root/path/ibm-cloud-apm-dc-configpack /application/root/path/etc
    ADD ibm_python_datacollector.tar.gz /application/root/path
    RUN pip install --no-index --find-links=/application/root/path/python_dc ibm_python_dc
    
  2. From Monitoring V2019.4.0, you can use the Python data collector to monitor Python applications running on the web server uWSGI V1.9.0 or later versions. But some configurations are needed to make the Python data collector work well.

    • If you start uWSGI without threads, the threads that are generated by your application and the Python data collector will never run and thus the Python data collector cannot work normally. You must enable uWSGI threads by adding the --enable-threads option in the uwsgi command. This option is applied automatically when you specify the --threadsoption to configure the number of threads.
    • If you run the uwsgi command with the --master option, Python scripts and modules are preloaded in the parent master process, and worker processes are forked from the parent master process. In addition, background threads that are created in the master process are killed in worker processes. To make the Python data collector work normally, you must add the --lazy-apps option in the uwsgi command to use the lazy loading mode.

    Command example:

    uwsgi --enable-threads --master --lazy-apps --processes 4 --http :8002 --wsgi-file=flask_hello.py --callable app
    

    Note: You can also add the options in a .ini configuration file and run the uwsgi command with the .ini file. For more information, see uWSGI documents.

  3. Integrate the installed data collector in your Python application:

    • If your application is based on Django V1.10 or later versions, open settings.py of your Django application, and add the following content into first line of the MIDDLEWARE section in that file:

       'ibm_python_dc.kpg_dc_django.ResourceMiddleware',
      
    • If your application is based on Django V1.9 or older versions, add the following content into the first line of the MIDDLEWARE_CLASS section in that file:

      'ibm_python_dc.kpg_dc_django.ResourceMiddleware',
      
    • If your application is based on Flask, add the Python data collector wsgi middleware in your Python application file, for example, if you run export FLASK_APP=run.py, then edit the run.py file and ensure that the Python data collector wsgi middleware are added in front of other middlewares. Example:

      from flask import Flask
         from flask_restful import Api
         from api.board import Article
         from api.auth import Login, Register, RefreshToken
         from middleware import Test
         from werkzeug.middleware.dispatcher import DispatcherMiddleware
      
         api.add_resource(Login, '/login')
         api.add_resource(Register, '/register')
      
         from ibm_python_dc.kpg_dc_wsgi import ResourceMiddleware
         app.wsgi_app = ResourceMiddleware(app.wsgi_app)
      
         app.wsgi_app = DispatcherMiddleware(serve_frontend, {
           '/test': test,
           '/admin': admin,
         })
      
         ......
         ......
      
  4. Build the new Docker image.

  5. Start your Python application with the new created Docker image.