Python
This guide walks you through setting up Application Performance Monitoring (APM) on a Python application. You can also check the Installation page in your Middleware Account for these instructions. To test the Python APM feature, use this example code.
Prerequisites
Before you start, make sure you have:
1 Python Version 3.8+
Verify your python version with
python3 --version
2 Pip Version 23.1.2+
Verify your pip version with
pip --version
3 Network access
Your application host must have outbound network access to your Middleware.io MW_TARGET URL (typically on port 443 for HTTPS).
Installation steps
Follow these steps to install and set up the Middleware.io Python APM agent.
Step 1: Set Up a Python Virtual Environment
Isolate your project's dependencies in a virtual environment:
1a. Create a new virtual environment named myenv
python3 -m venv myenv
1b. Activate the virtual environment
source myenv/bin/activate
.\myenv\Scripts\activate
Your terminal prompt will show (myenv)
once activated.
Step 2: Install the Middleware.io Python APM Package
To install the Middleware Python APM Package into your active virtual environment, run this command in your terminal:
pip install middleware-io
To enable continuous code profiling for deeper CPU and memory insights, install with the profiling extra:
pip install middleware-io[profiling]
Enable profiling with collect_profiling
or export MW_APM_COLLECT_PROFILING=True
. Navigate to the Continuous Profiling section to learn more about using Continuous Profiling with Middleware.
Step 3: Install the OpenTelemetry Automatic Instrumentation
Our agent uses the OpenTelemetry Python Contrib for automatic instrumentation. This lets it collect traces and metrics from popular Python libraries (e.g., Flask, Django, FastAPI, Requests, SQLAlchemy) without code changes. Use the middleware-bootstrap
command below to install the necessary OpenTelemetry instrumentation packages based on your project's dependencies:
middleware-bootstrap -a install
This middleware-bootstrap -a install
command reads the list of packages installed in your active site-packages
folder, and installs the corresponding instrumentation libraries for these packages. For example, if you already installed a flask
package, running the middleware-bootstrap -a install
command will install the opentelemetry-instrumentation-flask
package.
Run this command to verify that the library has been installed properly:
pip list | grep -i your-apm-package-name
If you're building a Docker image, run the middleware-bootstrap -a install
command after installing your application's requirements.txt and the middleware-io package.
# Example Dockerfile Snippet FROM python:3.9-slim-buster # Set working directory WORKDIR /app # Copy requirements file and install dependencies COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt
Step 4: Instrumentation
To begin instrumentation, you need your API key from the Middleware App. You must also ensure that the Middleware agent is installed and properly configured per your application host platform and OS. Depending on your application requirements, there are two methods for instrumentation:
Zero-code instrumentation: This method allows you to instrument your application without modifying any code. It is ideal for quick setups and minimal code.
Function-based instrumentation: This method is ideal if you want more control over the instrumentation process. The method involves using a
tracker function
to integrate the APM agent into your application’s code.
Implement zero-code instrumentation by setting the environment variables in a way such that the agent will auto-instrument your application. For example, you can set the following environment variables in your shell to auto instrument:
export MW_API_KEY='<MW_API_KEY>' export MW_TARGET='https://<MW_UID>.middleware.io:443' export MW_SERVICE_NAME='MyFlaskServer' middleware-run python app.py
If you prefer more control and want to integrate APM directly into your application, you can use a middleware function called the mw_tracker
. This method requires adding a specific function call in your application's code. For example, if you have an application named main.py
using the flask package
, the command to use the mw_tracker
to instrument your application will look like:
main.py
from flask import Flask # Import the mw_tracker from middleware to your app from middleware import mw_tracker, MWOptions, record_exception, DETECT_AWS_EC2 mw_tracker( MWOptions( access_token="<MW_API_KEY>", target="https://<MW_UID>.middleware.io:443", console_exporter=True, debug_log_file=True,
Step 5: Run Your Python Application with the Agent
To run your application, use the following command:
MW_TRACKER=True middleware-run python app.py
The MW_TRACKER=True
is required if you used the mw_tracker() function for instrumentation in step 4 above.
Framework-specific runs
Specific frameworks require different configurations when being run:
export DJANGO_SETTINGS_MODULE='mysite.settings' middleware-run python manage.py runserver
export MW_API_KEY='<MW_API_KEY>' export MW_TARGET='https://<MW_UID>.middleware.io:443' export DJANGO_SETTINGS_MODULE='demo.settings' middleware-run gunicorn -c conf/gunicorn.conf.py --workers=4 --bind 0.0.0.0:8000 --timeout 120 demo.wsgi
export MW_API_KEY='<MW_API_KEY>' export MW_TARGET='https://<MW_UID>.middleware.io:443' middleware-run uvicorn main:app --host localhost --port 5002
Explore more OpenTelemetry supported libraries here.
Serverless Configuration
If you are running your Python application in a serverless setup without the mw-agent, the MW_API_KEY
and MW_TARGET
variables are required when running the application:
export MW_API_KEY='<MW_API_KEY>' export MW_TARGET='https://<MW_UID>.middleware.io:443' middleware-run python app.py
Verifying Data Ingestion
Once your application is running with the Middleware.io agent, allow a few minutes for the data to appear.
- Log in to your Middleware.io Dashboard.
- Navigate to the "APM" section.
- Find your application using the MW_SERVICE_NAME you configured (e.g., my-web-store-frontend).
- Explore the different views: Service Overview, Traces, Metrics, Logs, and Profiling (if enabled).
If you don't see data appearing in your Middleware dashboard, check the troubleshooting page for details on how to fix this.
Example application
Below is a sample python flask server application.
main.py
import logging from middleware import mw_tracker, MWOptions, record_exception, DETECT_AWS_EC2 mw_tracker( MWOptions( access_token="<MW_API_KEY>", target="https://<MW_UID>.middleware.io:443", console_exporter=True, debug_log_file=True, service_name="MyPythonServer", otel_propagators = "b3,tracecontext",
Start the server
MW_SERVICE_NAME="flask-server" middleware-run python app.py
To further configure the installed Python APM (e.g., send custom traces and logs, deploy the APM using docker or kubernetes, and customize the APM's behaviour using environment variables), follow the steps in the configuration page.
Want to learn more about Middleware? Contact our support team at [email protected].