Prometheus http api 的 Python 包装器和一些用于指标处理的工具。
要安装最新版本:
pip install prometheus-api-client
要直接从此分支安装:
pip install https://github.com/4n4nd/prometheus-api-client-python/zipball/master
Prometheus 是云原生计算基金会项目,是一个系统和服务监控系统。它以给定的时间间隔从配置的目标收集指标(时间序列数据),评估规则表达式,显示结果,并且如果观察到某些条件为真,则可以触发警报。从 Prometheus 主机获得的原始时间序列数据有时很难解释。为了帮助更好地理解这些指标,我们为 Prometheus http API 创建了一个 Python 包装器,以便更轻松地进行指标处理和分析。
prometheus-api-client
库由多个模块组成,这些模块有助于连接到 Prometheus 主机、获取所需的指标并对时间序列数据执行各种聚合操作。
库的PrometheusConnect
模块可用于连接到 Prometheus 主机。该模块本质上是一个为从 Prometheus 主机收集指标而创建的类。它存储以下连接参数:
from prometheus_api_client import PrometheusConnect
prom = PrometheusConnect ( url = "<prometheus-host>" , disable_ssl = True )
# Get the list of all the metrics that the Prometheus host scrapes
prom . all_metrics ()
您还可以使用自定义查询获取特定指标的时间序列数据,如下所示:
prom = PrometheusConnect ()
my_label_config = { 'cluster' : 'my_cluster_id' , 'label_2' : 'label_2_value' }
prom . get_current_metric_value ( metric_name = 'up' , label_config = my_label_config )
# Here, we are fetching the values of a particular metric name
prom . custom_query ( query = "prometheus_http_requests_total" )
# Now, lets try to fetch the `sum` of the metrics
prom . custom_query ( query = "sum(prometheus_http_requests_total)" )
我们还可以使用自定义查询来获取特定时间间隔内的指标数据。例如,让我们尝试以 1 天为单位获取特定指标过去 2 天的数据:
# Import the required datetime functions
from prometheus_api_client . utils import parse_datetime
from datetime import timedelta
start_time = parse_datetime ( "2d" )
end_time = parse_datetime ( "now" )
chunk_size = timedelta ( days = 1 )
metric_data = prom . get_metric_range_data (
"up{cluster='my_cluster_id'}" , # this is the metric name and label config
start_time = start_time ,
end_time = end_time ,
chunk_size = chunk_size ,
)
有关PrometheusConnect
模块中包含的更多功能,请参阅此文档。
MetricsList
模块初始化 Metric 对象列表,用于作为 promql 查询的结果从 Prometheus 主机获取的指标。
# Import the MetricsList and Metric modules
from prometheus_api_client import PrometheusConnect , MetricsList , Metric
prom = PrometheusConnect ()
my_label_config = { 'cluster' : 'my_cluster_id' , 'label_2' : 'label_2_value' }
metric_data = prom . get_metric_range_data ( metric_name = 'up' , label_config = my_label_config )
metric_object_list = MetricsList ( metric_data ) # metric_object_list will be initialized as
# a list of Metric objects for all the
# metrics downloaded using get_metric query
# We can see what each of the metric objects look like
for item in metric_object_list :
print ( item . metric_name , item . label_config , " n " )
metric_object_list
中的每个项目都被初始化为Metric
类对象。让我们看一下metric_object_list
中的一个指标,以了解有关Metric
类的更多信息:
my_metric_object = metric_object_list [ 1 ] # one of the metrics from the list
print ( my_metric_object )
有关MetricsList
和Metrics
模块中包含的更多功能,请参阅此文档。
Metric
类还支持多种功能,例如添加、等于和绘制各种度量对象。
您可以为同一时间序列添加两个指标对象,如下所示:
metric_1 = Metric ( metric_data_1 )
metric_2 = Metric ( metric_data_2 )
metric_12 = metric_1 + metric_2 # will add the data in ``metric_2`` to ``metric_1``
# so if any other parameters are set in ``metric_1``
# will also be set in ``metric_12``
# (like ``oldest_data_datetime``)
重载运算符=,检查两个指标是否相同(无论数据如何,都是相同的时间序列)
metric_1 = Metric ( metric_data_1 )
metric_2 = Metric ( metric_data_2 )
print ( metric_1 == metric_2 ) # will print True if they belong to the same time-series
为度量时间序列绘制一个非常简单的线图:
from prometheus_api_client import PrometheusConnect , MetricsList , Metric
prom = PrometheusConnect ()
my_label_config = { 'cluster' : 'my_cluster_id' , 'label_2' : 'label_2_value' }
metric_data = prom . get_metric_range_data ( metric_name = 'up' , label_config = my_label_config )
metric_object_list = MetricsList ( metric_data )
my_metric_object = metric_object_list [ 1 ] # one of the metrics from the list
my_metric_object . plot ()
要执行数据分析和操作,使用 pandas DataFrame 表示数据通常会很有帮助。该库中有两个模块可用于处理提取到 DataFrame 中的原始指标。
MetricSnapshotDataFrame
模块将“当前指标值”数据转换为 DataFrame 表示形式, MetricRangeDataFrame
将“指标范围值”数据转换为 DataFrame 表示形式。这些类的用法示例如下:
import datetime as dt
from prometheus_api_client import PrometheusConnect , MetricSnapshotDataFrame , MetricRangeDataFrame
prom = PrometheusConnect ()
my_label_config = { 'cluster' : 'my_cluster_id' , 'label_2' : 'label_2_value' }
# metric current values
metric_data = prom . get_current_metric_value (
metric_name = 'up' ,
label_config = my_label_config ,
)
metric_df = MetricSnapshotDataFrame ( metric_data )
metric_df . head ()
""" Output:
+-------------------------+-----------------+------------+-------+
| __name__ | cluster | label_2 | timestamp | value |
+==========+==============+=================+============+=======+
| up | cluster_id_0 | label_2_value_2 | 1577836800 | 0 |
+-------------------------+-----------------+------------+-------+
| up | cluster_id_1 | label_2_value_3 | 1577836800 | 1 |
+-------------------------+-----------------+------------+-------+
"""
# metric values for a range of timestamps
metric_data = prom . get_metric_range_data (
metric_name = 'up' ,
label_config = my_label_config ,
start_time = ( dt . datetime . now () - dt . timedelta ( minutes = 30 )),
end_time = dt . datetime . now (),
)
metric_df = MetricRangeDataFrame ( metric_data )
metric_df . head ()
""" Output:
+------------+------------+-----------------+--------------------+-------+
| | __name__ | cluster | label_2 | value |
+-------------------------+-----------------+--------------------+-------+
| timestamp | | | | |
+============+============+=================+====================+=======+
| 1577836800 | up | cluster_id_0 | label_2_value_2 | 0 |
+-------------------------+-----------------+--------------------+-------+
| 1577836801 | up | cluster_id_1 | label_2_value_3 | 1 |
+-------------------------+-----------------+------------=-------+-------+
"""
有关prometheus-api-client
库中包含的更多功能,请参阅此文档。
PROM_URL="http://demo.robustperception.io:9090/" pytest
Prometheus Api 客户端使用预提交框架来维护代码 linting 和 python 代码样式。
AICoE-CI 将对每个拉取请求运行预提交检查。
我们鼓励我们的贡献者在贡献代码时遵循相同的模式。
我们希望保持相同的标准并维护代码以获得更好的质量和可读性。
预提交配置文件存在于存储库.pre-commit-config.yaml
中
它包含我们用于应用程序的不同代码样式和 linting 指南。
我们只需要在提出拉取请求之前运行预提交。
以下命令可用于运行预提交:
pre-commit run --all-files
如果您的系统中未安装预提交,可以使用以下命令进行安装: pip install pre-commit