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 }}",
    "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.2.tar.gz (35.4 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.2-py2.py3-none-any.whl (8.8 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

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

File hashes

Hashes for unfazed_prometheus-0.0.2.tar.gz
Algorithm Hash digest
SHA256 201043e8a460ff6730920d656051da9db0df3b6c28cae7f7d73d0b7ca18eb1dd
MD5 da08a238329dd978ce8dee1595839d12
BLAKE2b-256 4811e11e6f619e64aa955f814ab2f1ffc4dd9ae52b262d85467413469eaad76e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for unfazed_prometheus-0.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 bb041181b14652fb18765783536375c102d5803b6f5ab11f4e6227017d421c5f
MD5 ebc30f2b9e14d417f35b8e7a635d3eb1
BLAKE2b-256 f6610334a354d0e408d2aa0285fbb305501f0ece898b49f66ff0e6a51c1112d1

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