Python

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.

Prerequisites

  1. Python Version 3.8+: Verify with python3 --version
  2. Pip Version 23.1.2+: Verify with pip --version

Installation

1 Setup Virtual Environment

2 Install Middleware Python APM Package

To install Middleware Python APM Package run the command in your terminal:

3 Install OpenTelemetry automatic instrumentation libraries:

Run the command to install instrumentation library:

The middleware-bootstrap -a install command reads through the list of packages installed in your active site-packages folder, and installs the corresponding instrumentation libraries for these packages, if applicable. For example, if you already installed the flask package, running middleware-bootstrap -a install will install opentelemetry-instrumentation-flask for you. The Middleware Python agent will use monkey patching to modify functions in these libraries at runtime.

Ensure library is installed properly with command:

For Containerized Application add command in dockerfile:

Instrumentation

To begin with instrumentation, you will need to obtain your API key from Middleware App. Additionally, ensure that the Middleware agent is installed and properly configured as per your application Host Platform and Operating System.

Based on your application requirements, you can choose one of the following 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. It requires to set environments only.

  2. Tracker Function Based Instrumentation: If you require more control over the instrumentation process, this method involves using a tracker function to integrate APM directly into your application’s code.

Method 1: Python Zero-Code Instrumentation

Zero code instrumentation allows you to monitor your Python application without modifying the application code. This can be achieved by setting environment variables that configure the application with Middleware Application Performance Monitoring (APM) tool.

Example:

You can set the following environment variables in your shell to auto instrument:

For Advanced Configuration explore all configuration options with environments for the Middleware APM.

Automatic instrumentation with Python uses a Python agent that can be attached to any Python application. This agent primarily uses monkey patching to modify library functions at runtime, allowing for the capture of telemetry data from many popular libraries and frameworks.

Method 2: Python Tracker Function Based Instrumentation

If you prefer more control and want to integrate APM directly into your application, you can use a middleware function like mw_tracker. This method requires adding a specific function call in your application's code .

Example: Using mw_tracker Function from Middleware

Run Your Application

To run your application, use the following command:

The MW_TRACKER=True is required if mw_tracker() function is used for instrumentation.

Configuration

Advanced Configuration

Use Attributes and Environment Variables to configure Middleware APM.
This table shows the available configuration settings along with the corresponding environment variables that can be used instead.

Config AttributeEnvironment VariableDescriptionExample
access_tokenMW_API_KEYToken required for authentication.access_token = "whkvkobudfitutobptgonaezuxpjjypnejbb"
service_nameMW_SERVICE_NAME or OTEL_SERVICE_NAMEThe name of your application and it will appear in the UI to filter your data.service_name = "MyPythonServer"
collect_tracesMW_APM_COLLECT_TRACESFlag to enable or disable trace collection. Defaults to True.collect_traces = True
collect_metricsMW_APM_COLLECT_METRICSFlag to enable or disable metrics collection. Defaults to True.collect_metrics = False
collect_logsMW_APM_COLLECT_LOGSFlag to enable or disable log collection. Defaults to True.collect_logs = True
collect_profilingMW_APM_COLLECT_PROFILINGFlag to enable or disable profiling collection. Defaults to False.collect_profiling = True
log_levelMW_LOG_LEVEL or OTEL_LOG_LEVELSets the logging level (INFO, DEBUG, WARNING, ERROR). Defaults to INFOlog_level = "DEBUG"
mw_agent_serviceMW_AGENT_SERVICEAddress of the middleware agent service. Defaults to localhost.Set mw_agent_service="172.17.0.1" for Docker
or mw_agent_service="mw-service.mw-agent-ns.svc.cluster.local" for Kubernetes Application.
targetMW_TARGET or OTEL_EXPORTER_OTLP_ENDPOINTTarget endpoint for the OTLP exporter. Defaults to http://localhost:9319.target = "https://myapp.middleware.io:443"
custom_resource_attributesMW_CUSTOM_RESOURCE_ATTRIBUTESCustom resource attributes for enhanced telemetry context.custom_resource_attributes="call_id=12345678, request_id=987654321"
otel_propagatorsMW_PROPAGATORS or OTEL_PROPAGATORSPropagators for context propagation. Defaults to b3.otel_propagators = "b3,tracecontext"
console_exporterMW_CONSOLE_EXPORTERFlag to enable console exporter. Dumps telemetry data to console. Defaults to False.console_exporter = True
debug_log_fileMW_DEBUG_LOG_FILEFlag to enable debug logging to file. Only used if console_exporter is enabled. Defaults to False.debug_log_file = True
project_nameMW_PROJECT_NAMEName of the project for identification.project_name = "TestingProject"
sample_rateMW_SAMPLE_RATESample rate for telemetry data from (0 to 1). AlwaysOn (1), AlwaysOff (0), or a TraceIdRatio as 1/N. Defaults to 1.sample_rate = 0.5
detectorsMW_DETECTORSDetectors for AWS, Azure, GCP, or environment variables.detectors = [DETECT_AWS_LAMBDA, DETECT_GCP]
MW_DETECTORS = "aws_lambda,gcp"

All OpenTelemetry environment variables are supported and take the highest priority.

Host Based Configuration

If you are deploying the host using Kubernetes or Docker you will need to set the mw_agent_service to the location of the mw-agent.

This variable can be ignored when the agent is running on the same host as the application.

Docker
Kubernetes

Add the mw_agent_service or export environment variable to your application:

The DOCKER_BRIDGE_GATEWAY_ADDRESS is the IP address of the gateway between the Docker host and bridge network. This is 172.17.0.1 by default. Learn more about Docker bridge networking here

Add the following command to your Dockerfile after the pip install command:

Identify the namespace where the Infra Agent is running:

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

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:

Framework Specific Configurations

Specific frameworks require different configurations when being run, which can be found below:

Django
Gunicorn
Uvicorn

Explore more OpenTelemetry supported libraries here.

Sending Custom Data

Custom Metrics

Create and use a meter to send custom metrics:

Custom Traces

Create and use a tracer to send custom spans:

Custom Logs

Utilize logging method to send logs with a given priority. It is recommended to integrate these calls function inside your existing logger:

Custom Attributes

All custom attributes collected will be available as filtering and grouping functions inside of the Middleware platform.

Using similar methods from the above Traces and Logs sections you can attach custom attributes to any trace, span or log:

Adding Stack Traces

Use record_exception() method to record a stack trace when an exception occurs:

Continuous Profiling

By default the Python APM captures Continuous Profiling data which provides real-time performance insights from your application to enable rapid identification of resource allocation, bottlenecks, and more. Navigate to the Continuous Profiling section to learn more about using Continuous Profiling with Middleware.

To enable continous profiling for your python application. Install required profiling dependencies with below command:

Enable profiling with collect_profiling or export MW_APM_COLLECT_PROFILING=True

Example application

Below is a sample python flask server application.

Start the server

Troubleshooting and Debugging

To help troubleshoot and debug issues with your application's telemetry, you can enable specific settings in the mw_tracker function or use environment variables. These settings allow you to export telemetry data to the console, save them to log files, or debug basic information logs.

Toggling Basic Information Logs

Toggle info logs on or off to focus on debugging specific issues:

Exporting Telemetry Data to the Console

When debugging in a dev environment it may be useful to view the telemetry data directly in the console. Setting the console_exporter to true will print telemetry data like traces, metrics, and logs in your terminal or console.

Saving Telemetry Data to Log Files

You may save telemetry data to log files by enabling the debug_log_file setting. The logs will be written to respective files such as mw-traces.log, mw-metrics.log, and mw-logs.log.

Python package installation failure

The Python package installs require gcc and gcc-c++, which you may need to install if you’re running a slim version of Linux, such as CentOS.

CentOS
Debian/Ubuntu
Alpine

Missing Parent Span

Try tunning batch export interval using OTel Environment variable

For more info on Batch Span Processor here

Need assistance or want to learn more about Middleware? Contact our support team in Slack.