Skip to main content

Generic set of metrics for Python applications.

Project description

metrics-python

Generic set of metrics for Python applications.

Labels

Common labels like app, env, cluster, component, role, etc. is added to the metrics using the scrape config. Adding these metrics is not a responsibility we have in the metrics-python package.

Monitoring of periodic and cron jobs

metrics-python is a utility library that leverages the prometheus-client to gather and present metrics for Prometheus. As Prometheus operates on a pull-based model rather than a push-based one, collecting metrics from short-lived jobs can be problematic.

While the push-gateway component addresses this issue, it comes with its own set of drawbacks.

Monitoring cron jobs using Prometheus and AlertManager poses additional challenges due to the inability to interpret cron expressions in PromQL.

As a result, metrics-python does not support periodic or cron job monitoring.

Application info

Some properties from the application is not added as metric labels by default by the scrape config. One example is the application version. metrics-python has a util to expose labels like this to Prometheus.

from metrics_python.generics.info import expose_application_info

expose_application_info(version="your-application-version")

ASGI

metrics-python contains an ASGI middleware to measure request/response durations and sizes.

Starlette

from starlette.middleware import Middleware
from metrics_python.asgi import ASGIMiddleware

app = Starlette(
    middleware=[Middleware(ASGIMiddleware)]
)

fastapi

from metrics_python.asgi import ASGIMiddleware

app = FastAPI()
app.add_middleware(ASGIMiddleware)

Django

Cache

Cache metrics can be observed by adding patch_caching() to your settings file.

from metrics_python.django.cache import patch_caching

patch_caching()

Middleware

The execution of middlewares can be observed by adding patch_middlewares() to your settings file.

from metrics_python.django.middleware import patch_middlewares

patch_middlewares()

Signals

The execution of signals can be observed by adding patch_signals() to your settings file.

from metrics_python.django.signals import patch_signals

patch_signals()

Views

View processing, request and response sizes can be measured using the MetricsMiddleware.

MIDDLEWARE = [
    ...
    # It is important to place the MetricsMiddleware before the CommonMiddleware.
    "metrics_python.django.middleware.MetricsMiddleware",
    "django.middleware.common.CommonMiddleware",
]

Query count and duration in views

Database query count, duration, and duplicate queries can be observed by adding the QueryCountMiddleware. Add the middleware as early as possible in the list of middlewares to observe queries executed by other middlewares.

MIDDLEWARE = [
    ...
    "metrics_python.django.middleware.QueryCountMiddleware",
]

Query count and duration in Celery tasks

Database metrics can also be observed in Celery. Execute setup_celery_database_metrics bellow setup_celery_metrics, look into the Celery section of this document for more information.

from metrics_python.django.celery import setup_celery_database_metrics

setup_celery_database_metrics()

Postgres database connection metrics

The get_new_connection method in the PostgreSQL database connection engine can be observed by using a custom connection engine from metrics-python.

DATABASES = {
    "default": {
        "ENGINE": 'metrics_python.django.postgres_engine',
        ...
    }
}

Celery

To setup Celery monitoring, import and execute setup_celery_metrics as early as possible in your application to connect Celery signals. This is usually done in the settings.py file in Django applications.

from metrics_python.celery import setup_celery_metrics

setup_celery_metrics()

django-api-decorator

To measure request durations to views served by django-api-decorator, add the DjangoAPIDecoratorMetricsMiddleware.

MIDDLEWARE = [
    ...
    "metrics_python.django_api_decorator.DjangoAPIDecoratorMetricsMiddleware",
]

django-ninja

To measure request durations to views served by django-ninja, add the DjangoNinjaMetricsMiddleware.

MIDDLEWARE = [
    ...
    "metrics_python.django_ninja.DjangoNinjaMetricsMiddleware",
]

GraphQL

Strawberry

The Prometheus extension needs to be added to the schema to instrument GraphQL operations.

import strawberry
from metrics_python.graphql.strawberry import PrometheusExtension

schema = strawberry.Schema(
    Query,
    extensions=[
        PrometheusExtension,
    ],
)

Graphene

metrics-python has a Graphene middleware to instrument GraphQL operations. Add the middleware to Graphene by changing the GRAPHENE config in settings.py.

GRAPHENE = {
    ...
    "MIDDLEWARE": ["metrics_python.graphql.graphene.MetricsMiddleware"],
}

Gunicorn

To setup Gunicorn monitoring, add the Prometheus logger (to measure request durations) and add the worker state signals to the gunicorn config.

from metrics_python.generics.workers import export_worker_busy_state

logger_class = "metrics_python.gunicorn.Prometheus"

def pre_request(worker: Any, req: Any) -> None:
    export_worker_busy_state(worker_type="gunicorn", busy=True)


def post_request(worker: Any, req: Any, environ: Any, resp: Any) -> None:
    export_worker_busy_state(worker_type="gunicorn", busy=False)


def post_fork(server: Any, worker: Any) -> None:
    export_worker_busy_state(worker_type="gunicorn", busy=False)

Release new version

We use release-please from Google to relese new versions, this is done automatically.

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

metrics_python-0.3.1.tar.gz (65.9 kB view details)

Uploaded Source

Built Distribution

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

metrics_python-0.3.1-py3-none-any.whl (39.7 kB view details)

Uploaded Python 3

File details

Details for the file metrics_python-0.3.1.tar.gz.

File metadata

  • Download URL: metrics_python-0.3.1.tar.gz
  • Upload date:
  • Size: 65.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.3 Linux/6.11.0-1018-azure

File hashes

Hashes for metrics_python-0.3.1.tar.gz
Algorithm Hash digest
SHA256 574ca62cae2a0329d68d8a493757eae66ed7cb004cdfc8bbfd82d22d90b80ba2
MD5 6654865ccbce5f013f4eae394fc01b9f
BLAKE2b-256 d05aa55f3b7bb699f1cb83738ea55b122f5274574a3f309ce58de32dd90163f6

See more details on using hashes here.

File details

Details for the file metrics_python-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: metrics_python-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 39.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.3 Linux/6.11.0-1018-azure

File hashes

Hashes for metrics_python-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ec319c3bd3a3b6a160de8bc54d21eb91752e964b3e404f470e22e7643ef50545
MD5 2e4db8773755c01e647b06df82112463
BLAKE2b-256 20798245f495ffbf3e17e6f919c174ef951c82e1a51172005104a3501734ed6b

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