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

# Python

### Installation

Currently Metlo's Python Agent supports 4 servers:

* Django
* Flask
* FastAPI/Starlette

It can be installed from `pypi` by running :

```bash Shell theme={null}
pip install metlo
```

### Configuration

#### Django

Once installed, Metlo's middleware can be added by modifying middlewares list
(in the projects `settings.py`) like so:

```py Python theme={null}
MIDDLEWARE = [
    ...,
    "metlo.django.MetloDjango",
]
```

and configuring a `METLO_CONFIG` attribute in the projects `settings.py` like
this :

```py Python theme={null}
METLO_CONFIG = {
    "API_KEY": "<YOUR_METLO_API_KEY>",
    "METLO_HOST": "https://app.metlo.com"
}
```

Optional Parameters:

* COLLECTOR\_PORT: Collector port used to capture traces. Defaults to 8081
* BACKEND\_PORT: Backend port used by Metlo.
* LOG\_LEVEL: The log level Metlo should log at. The debug levels and above correspond to values used by the python logging module. A special `trace` level is also present, that provides verbose logging info. Can be
  * trace (5)
  * debug (10)
  * info  (20) - Default
  * warn  (30)
  * error (40)
* ENCRYPTION\_KEY: Key used to encrypt sensitive data, such as User session keys
* GET\_USER: A function that takes in the ASGI scope parameter and returns the user for that request. By default, Metlo collects no information about user of a request.
* REJECT\_RESPONSE: Function that produces a reponse when Metlo is set to block malicious requests. Expected to return an object of the starlette Response type.
* MAX\_BODY: Max size of request/response bodies that Metlo should capture. Defaults to 5 KB
* DISABLED: Used to disable request blocking by Metlo.

#### Disabling during testing

METLO\_CONFIG takes an optional key-value pair for disabling metlo during testing
or development. It can be added as ("DISABLED": boolean). It defaults to `False`
so that metlo captures your traces by default.

```py Python theme={null}
METLO_CONFIG = {
    "API_KEY": "<YOUR_METLO_API_KEY>",
    "METLO_HOST": "https://app.metlo.com",
    "DISABLED": <Boolean, default False>
}
```

#### Flask

Once installed, Metlo middleware can be added simply like :

```py Python theme={null}
from flask import Flask

from metlo.flask import MetloFlask

app = Flask(__name__)
MetloFlask(app, "https://app.metlo.com", "<YOUR_METLO_API_KEY>")
```

The Flask Middleware takes the flask app, Metlo collector url, and the Metlo API
Key as parameters.

Metlo for Flask can be configured further with the following parameters:

* BACKEND\_PORT: Backend port used by Metlo.
* COLLECTOR\_PORT: Collector port used to capture traces. Defaults to 8081
* LOG\_LEVEL: The log level Metlo should log at. The debug levels and above correspond to values used by the python logging module. A special `trace` level is also present, that provides verbose logging info. Can be
* trace (5)
* debug (10)
* info (20) - Default
* warn (30)
* error (40)
* ENCRYPTION\_KEY: Key used to encrypt sensitive data, such as User session keys
* GET\_USER: A function that takes in the ASGI scope parameter and returns the user for that request. By default, Metlo collects no information about user of a request.
* REJECT\_RESPONSE: Function that produces a reponse when Metlo is set to block malicious requests. Expected to return an object of the starlette Response type.
* MAX\_BODY: Max size of request/response bodies that Metlo should capture. Defaults to 5 KB
* disabled: Used to disable request blocking by Metlo.

#### Disabling during testing

Metlo can take an optional named parameter (as "disabled":boolean) for disabling
metlo during testing.

```py Python theme={null}
MetloFlask(app, "https://app.metlo.com", "<YOUR_METLO_API_KEY>", disabled=<Boolean, default False>)
```

#### FastAPI/Starlette

Once installed, Metlo middleware can be added simply like :

```py Python theme={null}
from fastapi import FastAPI

from metlo.fastapi import ASGIMiddleware

app = FastAPI()

app.add_middleware(
    ASGIMiddleware,
    host="https://app.metlo.com",
    api_key="<YOUR_METLO_API_KEY>",
)

...
```

The FastAPI/Starlette Middleware takes the Metlo collector url, and the Metlo API
Key as keyword arguments. Optional arguments can then be further configured as:

* backend\_port: Backend port used by Metlo.
* collector\_port: Collector port used to capture traces. Defaults to 8081
* log\_level: The log level Metlo should log at. The debug levels and above correspond to values used by the python logging module. A special `trace` level is also present, that provides verbose logging info. Can be
  * trace (5)
  * debug (10)
  * info  (20) - Default
  * warn  (30)
  * error (40)
* encryption\_key: Key used to encrypt sensitive data, such as User session keys
* get\_user A function that takes in the ASGI scope parameter and returns the user for that request. By default, Metlo collects no information about user of a request.
* reject\_response: Function that produces a reponse when Metlo is set to block malicious requests. Expected to return an object of the starlette Response type.
* max\_body: Max size of request/response bodies that Metlo should capture. Defaults to 5 KB
* disable: Used to disable request blocking by Metlo.

#### Disabling during testing

Metlo can take an optional named parameter (as "disabled":boolean) for disabling
metlo during testing.

```py Python theme={null}
app.use_middleware(
    ASGIMiddleware,
    "https://app.metlo.com",
    "<YOUR_METLO_API_KEY>",
    disabled=<Boolean, default False>
)
```
