Skip to main content

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 }}",
    "CLIENT_CLASS": "unfazed_prometheus.settings.PrometheusSettings",
    "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.

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unfazed_prometheus-0.0.1.tar.gz (35.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unfazed_prometheus-0.0.1-py2.py3-none-any.whl (8.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file unfazed_prometheus-0.0.1.tar.gz.

File metadata

  • Download URL: unfazed_prometheus-0.0.1.tar.gz
  • Upload date:
  • Size: 35.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.7

File hashes

Hashes for unfazed_prometheus-0.0.1.tar.gz
Algorithm Hash digest
SHA256 303665ea28e53d70772257bd9f928779abcfa0fb699f0d25a22b96bbc5214020
MD5 7ac0a1695037e08d0cd9caeb2e3ea001
BLAKE2b-256 f0c970f812a8f4ecd3bbd225b15830fe52cf410eb696b08724d12c7626d27f6a

See more details on using hashes here.

File details

Details for the file unfazed_prometheus-0.0.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for unfazed_prometheus-0.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e0d6fc32ea890db6e395f1179ebda3df8644daf0357d623fab9b30076476ab2d
MD5 f476c0b30db43a12b5ed918d20d0d738
BLAKE2b-256 58ed4fe65d27feba3e2dfc9ae8afab3d813f2b74369096d3c35a5a7993b40086

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page