Installation

Currently Metlo’s Python Agent supports 4 servers:

  • Django
  • Flask
  • FastAPI/Starlette

It can be installed from pypi by running :

Shell
pip install metlo

Configuration

Django

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

Python
MIDDLEWARE = [
    ...,
    "metlo.django.MetloDjango",
]

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

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

Python
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 :

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

Python
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 :

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

Python
app.use_middleware(
    ASGIMiddleware,
    "https://app.metlo.com",
    "<YOUR_METLO_API_KEY>",
    disabled=<Boolean, default False>
)