Prometheus metrics for Unfazed
Project description
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:
...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file unfazed_prometheus-0.0.3.tar.gz.
File metadata
- Download URL: unfazed_prometheus-0.0.3.tar.gz
- Upload date:
- Size: 36.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c8528a11e017e37e9219b91dae300dfe24d96d1c25324f25b9a20700d403d095
|
|
| MD5 |
b5a0402588327aa98a9e9fa891c86fb2
|
|
| BLAKE2b-256 |
41047a7308304a83d2eff3a41bea9032156b0ad3914ada184dec9010ce9940e1
|
File details
Details for the file unfazed_prometheus-0.0.3-py2.py3-none-any.whl.
File metadata
- Download URL: unfazed_prometheus-0.0.3-py2.py3-none-any.whl
- Upload date:
- Size: 10.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fbde49b840bf4e8e0b8840f28d7cb36f2cd0d533c5d02a4d3ed7204af7959555
|
|
| MD5 |
d229a196549b24fae2a6a5e3eba69ee6
|
|
| BLAKE2b-256 |
938b49855a85566601636e011004c9436a94e6e860b09b19613b1b5337d92f4c
|