Python
Installation
Currently Metlo’s Python Agent supports 4 servers:
- Django
- Flask
- FastAPI/Starlette
It can be installed from pypi
by running :
Configuration
Django
Once installed, Metlo’s middleware can be added by modifying middlewares list
(in the projects settings.py
) like so:
and configuring a METLO_CONFIG
attribute in the projects settings.py
like
this :
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.
Flask
Once installed, Metlo middleware can be added simply like :
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.
FastAPI/Starlette
Once installed, Metlo middleware can be added simply like :
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.