Python prometheus client get metrics

prometheus-api-client

A Python wrapper for the Prometheus http api and some tools for metrics processing.

Installation

To install the latest release:

pip install prometheus-api-client

To install directly from this branch:

pip install https://github.com/4n4nd/prometheus-api-client-python/zipball/master

Getting Started

Usage

Prometheus, a Cloud Native Computing Foundation project, is a systems and service monitoring system. It collects metrics (time series data) from configured targets at given intervals, evaluates rule expressions, displays the results, and can trigger alerts if some condition is observed to be true. The raw time series data obtained from a Prometheus host can sometimes be hard to interpret. To help better understand these metrics we have created a Python wrapper for the Prometheus http api for easier metrics processing and analysis.

The prometheus-api-client library consists of multiple modules which assist in connecting to a Prometheus host, fetching the required metrics and performing various aggregation operations on the time series data.

Connecting and Collecting Metrics from a Prometheus host

The PrometheusConnect module of the library can be used to connect to a Prometheus host. This module is essentially a class created for the collection of metrics from a Prometheus host. It stores the following connection parameters:

  • url — (str) url for the prometheus host
  • headers – (dict) A dictionary of http headers to be used to communicate with the host. Example:
  • disable_ssl – (bool) If set to True, will disable ssl certificate verification for the http requests made to the prometheus host
 You can also fetch the time series data for a specific metric using custom queries as follows:
  We can also use custom queries for fetching the metric data in a specific time interval. For example, let's try to fetch the past 2 days of data for a particular metric in chunks of 1 day:
  For more functions included in the PrometheusConnect module, refer to this documentation.

Understanding the Metrics Data Fetched

The MetricsList module initializes a list of Metric objects for the metrics fetched from a Prometheus host as a result of a promql query.

     Each of the items in the metric_object_list are initialized as a Metric class object. Let's look at one of the metrics from the metric_object_list to learn more about the Metric class:
 For more functions included in the MetricsList and Metrics module, refer to this documentation.

Additional Metric Functions

The Metric class also supports multiple functions such as adding, equating and plotting various metric objects.

Adding Metrics

You can add add two metric objects for the same time-series as follows:

Equating Metrics

Overloading operator =, to check whether two metrics are the same (are the same time-series regardless of their data)

Plotting Metric Objects

Plot a very simple line graph for the metric time series:

To perform data analysis and manipulation, it is often helpful to have the data represented using a pandas DataFrame. There are two modules in this library that can be used to process the raw metrics fetched into a DataFrame.

The MetricSnapshotDataFrame module converts «current metric value» data to a DataFrame representation, and the MetricRangeDataFrame converts «metric range values» data to a DataFrame representation. Example usage of these classes can be seen below:

           For more functions included in the prometheus-api-client library, please refer to this documentation.

Running tests

Code Styling and Linting

Prometheus Api client uses pre-commit framework to maintain the code linting and python code styling.
The AICoE-CI would run the pre-commit check on each pull request.
We encourage our contributors to follow the same pattern, while contributing to the code.
we would like to keep the same standard and maintain the code for better quality and readability.

The pre-commit configuration file is present in the repository .pre-commit-config.yaml
It contains the different code styling and linting guide which we use for the application.

we just need to run pre-commit before raising a Pull Request.
Following command can be used to run the pre-commit:
pre-commit run —all-files

If pre-commit is not installed in your system, it can be install with : pip install pre-commit

Источник

Мониторинг микросервисов Flask с помощью Prometheus

Пара строк кода и ваше приложение генерирует метрики, вау!

Для того что бы понять как работает prometheus_flask_exporter достаточно минимального примера:

from flask import Flask from prometheus_flask_exporter import PrometheusMetrics app = Flask(__name__) metrics = PrometheusMetrics(app) @app.route('/') def main(): return 'OK'

Это все что нужно для начала! Добавив импорт и строку для инициализации PrometheusMetrics, вы получите метрики продолжительности запросов и счетчики запросов, отображаемые в конечной точке /metrics приложения Flask, в котором оно зарегистрировано, а также все метрики по умолчанию, которые вы получаете из базовой клиентской библиотеки Prometheus.

Вы можете найти простой в использовании пример в репозитории GitHub, который запускает экземпляр Prometheus и Grafana вместе с демонстрационным приложением для генерации метрик, которые будут выглядеть примерно так:

Вы также найдете список показателей в README примера, которые отображаются на панели управления, вместе с запросами Prometheus, которые заполняют панели.

Настройка

В библиотеке есть множество опций конфигурации, посмотрите в README проекта их примеры с кратким объяснением.

Базовая конфигурация показана вверху. Просто создайте экземпляр PrometheusMetrics, назовем его metrics, а затем с его помощью определите дополнительные метрики, которые вы хотите собирать, декорировав функции:

Счетчики подсчитывают вызовы, а остальные собирают метрики в зависимости от продолжительности этих вызовов. Вы можете определить метки для каждого из них, потенциально используя свойства запроса или ответа. Например:

from flask import Flask, request from prometheus_flask_exporter import PrometheusMetrics app = Flask(__name__) # group by endpoint rather than path metrics = PrometheusMetrics(app, group_by='endpoint') @app.route('/collection/:collection_id/item/:item_id') @metrics.counter( 'cnt_collection', 'Number of invocations per collection', labels=< 'collection': lambda: request.view_args['collection_id'], 'status': lambda resp: resp.status_code >) def get_item_from_collection(collection_id, item_id): pass

В приведенном выше примере нажатие на конечную точку /collection/10002/item/76 приведет к увеличению счетчика, например cnt_collection , плюс вы получите метрики по умолчанию (для каждой конечной точки в этом примере ) из библиотеки по умолчанию:

  • flask_http_request_duration_seconds — Продолжительность HTTP-запроса в секундах для всех запросов Flask по методу, пути и статусу
  • flask_http_request_total — Общее количество HTTP-запросов по методам и статусам

Есть варианты пропустить отслеживание определенных конечных точек, зарегистрировать дополнительные метрики по умолчанию или пропустить те, что указаны выше, или применить одну и ту же настраиваемую метрику к нескольким конечным точкам. Ознакомьтесь с README проекта, чтобы узнать, что доступно.

app = Flask(__name__) metrics = PrometheusMetrics(app) @app.route('/') def main(): pass # requests tracked by default @app.route('/skip') @metrics.do_not_track() def skip(): pass # default metrics are not collected # custom metric to be applied to multiple endpoints common_counter = metrics.counter( 'by_endpoint_counter', 'Request count by endpoints', labels= ) @app.route('/common/one') @common_counter def endpoint_one(): pass # tracked by the custom and the default metrics @app.route('/common/two') @common_counter def endpoint_two(): pass # also tracked by the custom and the default metrics # register additional default metrics metrics.register_default( metrics.counter( 'by_path_counter', 'Request count by request paths', labels= ) )

В библиотеке есть удобные расширения для популярных многопроцессорных библиотек, таких как uWSGI и Gunicorn. Вы также можете найти небольшие примеры целевых вариантов использования, в том числе многопроцессорных.

Сбор метрик

Как упоминалось выше, библиотека по умолчанию предоставляет конечную точку /metrics в приложении Flask, которая может служить целью для сборщика Prometheus.

В приведенном выше примере с дашбордом вы можете нацелить свой Prometheus на приложение Flask с настройками по умолчанию с такой конфигурацией:

scrape_configs: - job_name: 'example' dns_sd_configs: - names: ['app'] port: 5000 type: A refresh_interval: 5s

Смотрите полный пример в репозитории GitHub. Это предполагает, что Prometheus может найти ваши экземпляры приложения Flask на http://app:5000/metrics, где имя домена приложения может потенциально разрешаться на несколько IP-адресов, например, при работе в Kubernetes или Docker Swarm.

Если такое раскрытие конечной точки метрик вам не подходит, возможно, потому что вы не хотите разрешать внешний доступ к ней, вы можете легко отключить ее, передав path=None при создании экземпляра PrometheusMetrics.

from flask import Flask, request from prometheus_flask_exporter import PrometheusMetrics app = Flask(__name__) metrics = PrometheusMetrics(app, path=None) . metrics.start_http_server(5099)

Затем вы можете использовать start_http_server(port), чтобы открыть эту конечную точку на другом HTTP-порту, 5099 в приведенном выше примере. В качестве альтернативы, если вас устраивает то, что конечная точка находится в том же приложении Flask, но вам нужно изменить ее путь от /metrics, вы можете либо передать другой URI в качестве параметра пути, либо использовать register_endpoint(..), чтобы установить это позже.

Ссылки

  • rycus86/prometheus_flask_exporter — Этот экспортер флаконов для Prometheus
  • prometheus-flask-exporter README — Использование, примеры и параметры конфигурации
  • prometheus-flask-exporter examples — Примеры настройки мониторинга приложений Flask разными способами
  • prometheus-flask-exporter on PyPI — Этот проект на PyPI
  • prometheus/client_python — Официальная клиентская библиотека Prometheus для Python

Если вы решите попробовать, не стесняйтесь откройте issue на GitHub или оставлять свои комментарии, отзывы и предложения!

Источник

Читайте также:  Java файлы во всех папках
Оцените статью