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

2 Pip Version 23.1.2+

Verify your pip version with

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

1b. Activate the virtual environment

Linux/MacOS
Windows

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:

To enable continuous code profiling for deeper CPU and memory insights, install with the profiling extra:

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:

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:

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.

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:

  1. Zero-code instrumentation: This method allows you to instrument your application without modifying any code. It is ideal for quick setups and minimal code.

  2. 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.

Zero-code instrumentation
Function-based instrumentation

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:

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:

Step 5: Run Your Python Application with the Agent

To run your application, use the following command:

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:

Django
Gunicorn
Uvicorn

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:

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.

Start the server

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].