Unfazed Prometheus
Installation
pip install unfazed-prometheus
Quick Start
Add Settings
# settings.py
import socket
UNFAZED_PROMETHEUS_SETTINGS = {
"HOSTNAME": socket.gethostname(),
"PROJECT": "{{ project_name }}",
"PROMETHEUS_MULTIPROC_DIR": "/prometheus",
}
# add lifespan
UNFAZED_SETTINGS = {
# ... other settings
"LIFESPAN": ["unfazed_prometheus.lifespan.PrometheusLifespan"],
}
Monitor Request
all you need to do is add the middleware to the middleware list.
UNFAZED_SETTINGS = {
# ... other settings
"MIDDLEWARE": [
"unfazed_prometheus.middleware.common.PrometheusHttpRequestMiddleware",
],
}
Monitor Database using Tortoise ORM
all you need to do is to use the unfazed_prometheus.database.tortoise.mysql database engine.
UNFAZED_SETTINGS = {
"DATABASE": {
"CONNECTIONS": {
"default": {
"ENGINE": "unfazed_prometheus.database.tortoise.mysql",
"CREDENTIALS": {
"HOST": "mysql",
"PORT": 3306,
"USER": "app",
"PASSWORD": "app",
"DATABASE": "app",
},
}
},
},
}
Monitor Cache
all you need to do is to use the unfazed_prometheus.cache.backends.default.PrometheusDefaultBackend cache backend.
other cache backend:
unfazed_prometheus.cache.backends.namespace.PrometheusNamespaceBackendunfazed_prometheus.cache.backends.serializer.PrometheusSerializerBackend
UNFAZED_SETTINGS = {
"CACHE": {
"default": {
"BACKEND": "unfazed_prometheus.cache.backends.default.PrometheusDefaultBackend",
"LOCATION": "redis://redis:6379",
"OPTIONS": {
"decode_responses": True,
"max_connections": 1000,
},
}
},
}
Monitor Function
use prometheus agent.monitor_function decorator.
from unfazed_prometheus import agent
@agent.monitor_function
def my_function(a: int, b: int) -> int:
return a + b
Monitor API
use prometheus agent.monitor_api decorator.
from unfazed_prometheus import agent
@agent.monitor_api("/api/v1/users")
async def get_users():
resp = await asyncrequests.get("https://api.github.com/users")
return resp.json()
Monitor Exception
unfazed_prometheus will monitor exceptions through other monitor decorators. all you need to do is to let the exception be raised or raise it yourself.
from module import CustomException
@agent.monitor_function
def devide(a: int, b: int) -> float:
if b == 0:
raise CustomException("b is 0")
return a / b
or just let the exception be raised.
@agent.monitor_function
def devide(a: int, b: int) -> float:
return a / b
unfazed_prometheus will automatically collect the exception and count the total number of exceptions.
connect to prometheus server
in the live env, it's better to connect to prometheus server use another service other than the unfazed server.
example code see scripts/prometheus_client.py
Advanced
if the default metrics and decorators cannot meet your needs, meta_monitor may help.
from prometheus_client import Counter, Histogram
from unfazed_prometheus import meta_monitor
counter = Counter(
"my_counter",
"my_counter_description",
["label1", "label2"],
)
my_monitor = meta_monitor(
counter_handler=counter,
counter_labels=["foo", "bar"],
)
@my_monitor
def my_function(a: int, b: int) -> int:
return a + b
meta_monitor signature:
def meta_monitor(
counter_handler: t.Optional[Counter] = None,
hist_handler: t.Optional[Histogram] = None,
exc_handler: t.Optional[Counter] = None,
counter_labels: t.Optional[Labels] = None,
hist_labels: t.Optional[Labels] = None,
exc_labels: t.Optional[Labels] = None,
) -> Decorator:
...
Metadata
Release files for unfazed-prometheus 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| unfazed_prometheus-0.0.3.tar.gz | 36.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| unfazed_prometheus-0.0.3-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 46.5 kB
Release files / unfazed_prometheus-0.0.3.tar.gz
| Download URL | unfazed_prometheus-0.0.3.tar.gz |
|---|---|
| Size | 36.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c8528a11e017e37e9219b91dae300dfe24d96d1c25324f25b9a20700d403d095
|
|
BLAKE2b-256 checksum How to use checksums |
41047a7308304a83d2eff3a41bea9032156b0ad3914ada184dec9010ce9940e1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.2
|
Release files / unfazed_prometheus-0.0.3-py2.py3-none-any.whl
| Download URL | unfazed_prometheus-0.0.3-py2.py3-none-any.whl |
|---|---|
| Size | 10.0 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
fbde49b840bf4e8e0b8840f28d7cb36f2cd0d533c5d02a4d3ed7204af7959555
|
|
BLAKE2b-256 checksum How to use checksums |
938b49855a85566601636e011004c9436a94e6e860b09b19613b1b5337d92f4c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.2
|