TracesMetricsApp LogsCustom LogsProfiling

This guide walks you through setting up Application Performance Monitoring (APM) on a Python application. These instructions can also be found on the Installation page in your Middleware Account. View example code here .


  1. Middleware Host Agent (MW Agent), to install the MW Agent please see our installation guide.

  2. Python 3 version 3.8 or above recommended. Check your Python version:

python3 --version
  1. pip version 23.1.2 or above. Check your pip version:
pip --version

Step 1: Install Middleware Python APM Package

Run the following command in your terminal:

pip install middleware-apm

Step 2: Import Middleware Tracker

Add the following lines to the beginning of your application:

Python 3
import logging 
from middleware import MwTracker

Step 3: Container Variables (Optional)

Applications running in a container require an additional environment variable. If your application is not running in a container, move to Step 4.

For Docker containers, add the following environment variable to your application:

docker bridge gateway address is the IP address of the gateway between the Docker host and the bridge network, which is by default. Refer more about Docker bridge networking here

For Kubernetes, first identify the namespace where the MW Agent is running:

kubectl get service --all-namespaces | grep mw-service

Then add the following environment variable to your application deployment YAML file:


Step 4: Capture Application Data

Setup .ini File

Create a .ini file based on the features you want to observe. service_name and access_token are required for the tracker to send data to Middleware.


# The name of your application as service-name, as it will appear in the UI to filter out your data.
service_name = {APM-SERVICE-NAME}

# This Token binds the Python Agent's data and profiling data to your account.
access_token = {YOUR-ACCESS-TOKEN}

# The service name, where Middleware Agent is running, in case of K8s.
mw_agent_service =

# Distributed traces for your application (false = disabled).
collect_traces = true

# Collection of metrics for your application (false = disabled).
collect_metrics = true

# Collection of logs for your application (false = disabled).
collect_logs = true

# Collection of profiling data for your application (false = disabled).
collect_profiling = true

Enable Custom Logs

To ingest custom logs, utilize the following functions based on desired log severity levels:

Python 3"info sample")
logging.warning("Sample Warning Log") 
logging.error("Sample Error Log.", extra={'tester': 'Alex'})

Stack Errors

Use tracker.record_error() method to record a stack trace when an error occurs:

Python 3
    not_possible = 12/0
except ZeroDivisionError as e:

Step 5: Start Your Project

After deploying your application, run the following command to start your project:

MIDDLEWARE_CONFIG_FILE=./middleware.ini middleware-apm run python
Need assistance or want to learn more about Middleware? Contact us at support[at]