> ## Documentation Index
> Fetch the complete documentation index at: https://mw-docs.middleware.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Python

> Python APM - Setup & Installation Docs | Middleware

<a target="_blank" href="https://pypi.org/project/middleware-apm/">
  <span
    style={{
    background: "url(https://img.shields.io/pypi/v/middleware-apm) no-repeat",
    width: "125px",
    height: "20px",
    display: "inline-block",
}}
  />
</a>

| Traces | Metrics | App Logs | Custom Logs | Profiling |
| :----: | :-----: | :------: | :---------: | :-------: |
|    ✅   |    ✅    |     ✅    |      ✅      |     ✅     |

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](https://github.com/middleware-labs/demo-apm/tree/master/python).

# Prerequisites

<Steps>
  <Step title="Infra Agent">
    Infrastructure Agent (Infra Agent). To install the Infra Agent, see our [Installation Guide](https://docs.middleware.io/docs/agent-installation/overview).
  </Step>

  <Step title="Python Version">
    `Python 3 version 3.8` or above. Check your Python version with the following command:

    ```bash Shell theme={null}
    python3 --version
    ```
  </Step>

  <Step title="Pip Version">
    `pip version 23.1.2` or above. Check your pip version with the following command:

    ```bash Shell theme={null}
    pip --version
    ```
  </Step>
</Steps>

# Install

### Step 1: Install `Python` APM Package

Run the following command in your terminal:

```python Shell theme={null}
pip install middleware-apm
```

Check if the `middleware-apm` has been installed with the following command:

```python Shell theme={null}
pip list
```

### Step 2: Import Middleware Tracker

Add the following lines to the beginning of your application:

```python Python 3 theme={null}
import logging 
 
from middleware import MwTracker
tracker=MwTracker()
```

### Step 3: Container Variables \[Optional]

#### Docker

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

Add the following environment variable to your application:

```bash Shell theme={null}
MW_AGENT_SERVICE=<DOCKER_BRIDGE_GATEWAY_ADDRESS>
```

<Note> 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](https://docs.docker.com/network/network-tutorial-standalone/) </Note>

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

```Dockerfile Dockerfile theme={null}
RUN middleware-bootstrap -a install
```

#### Kubernetes

Identify the namespace where the Infra Agent is running:

```bash Shell theme={null}
kubectl get service --all-namespaces | grep mw-service
```

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

```bash Shell theme={null}
MW_AGENT_SERVICE=mw-service.mw-agent-ns.svc.cluster.local
```

### Step 4: Capture Application Data

#### Step 4a: Setup `middleware.ini` File

Create a `middleware.ini` file based on the features you want to observe and place it at the root of your app directory. Specify the location of the `middleware.ini` file with the `MIDDLEWARE_CONFIG_FILE` environment variable.

<Note>`service_name` and `access_token` are required for the tracker to send data to Middleware.</Note>

```python .ini theme={null}
[middleware.common]

# 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 Infra Agent is running, in case of K8s.
mw_agent_service = mw-service.mw-agent-ns.svc.cluster.local

# 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
```

#### Step 4b: Enable Custom Logs

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

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

#### Step 4c: Stack Errors

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

```python Python 3 theme={null}
try:
    not_possible = 12/0
except ZeroDivisionError as e:
    tracker.record_error(e)
```

### Step 5: Deploy Your Django App \[Optional]

<Note> If you are not using the Django framework in your Python application, proceed to [Step 6](/apm-configuration/python#step-6-start-your-project). </Note>

#### Step 5a: Instrument Your App

Add the following to your `main()` function in `manage.py`:

```python3 Python theme={null}
tracker.django_instrument()
```

#### Step 5b: Start Your Django Project

Initialize your Django project with the following command:

```python Python theme={null}
DJANGO_SETTINGS_MODULE='mysite.settings' middleware-apm run python manage.py runserver
```

#### Step 5c: Start Your Django Project

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

<Warning> If the `middleware.ini` file is not in your root directory, add `MIDDLEWARE_CONFIG_FILE=./path/to/middleware.ini` to the below command </Warning>

```python Python theme={null}
middleware-apm run python app.py
```

### Step 6: Start Your Project

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

```shell Shell theme={null}
middleware-apm run python app.py
```

# Continuous Profiling

Continuous profiling captures real-time performance insights from your application to enable rapid identification of resource allocation, bottlenecks, and more. Navigate to the [Continuous Profiling](https://docs.middleware.io/workflow/profiling#continuous-profiling) section to learn more about using Continuous Profiling with the Python APM.

<Note> Need assistance or want to learn more about Middleware? Contact us at support\[at]middleware.io. </Note>
