python客户端库的prometheus二进制格式度量数据结构

prometheus_metrics_proto的Python项目详细描述


prometheus_metrics_proto包提供prometheus protobuf数据 结构和一组帮助生成普罗米修斯的帮助函数 协议缓冲区格式度量并序列化它们以准备 网络传输。

度量的收集和摘要分位数的管理 直方图存储桶不在 这个包裹。

使用prometheus_metrics_proto的项目示例如下 ^使用它的{a1} 在二进制格式化程序中。

prometheus_metrics_proto使用的协议缓冲区规范是 从普罗米修斯回购协议中获得。

安装

$ pip install prometheus_metrics_proto

背景

创建能被普罗米修斯吸收的指标相对简单, 但需要知道它们是如何构成的。

普罗米修斯服务器希望在 抓取暴露协议缓冲区格式数据的终结点。

MetricFamily对象是保存度量名称的容器,帮助 字符串和Metric对象。每个MetricFamily在同一个 说明必须有唯一的名称。

Metric对象是特定度量的单个实例的容器 键入。有效的度量类型有counter、gauge、histogram和summary。每个 Metric在同一个MetricFamily中必须有一个唯一的 LabelPair字段。这通常被称为多维度量。

示例

prometheus_metrics_proto包提供帮助函数来帮助 生成普罗米修斯度量对象。

下面的示例演示如何使用这些函数来构造度量 并将它们编码成适合在 回应。

#!/usr/bin/env python'''
This script demonstrates the high level helper functions used to assist
creating various metrics kinds as well as how to encode the metrics into
a form that can be sent to Prometheus server.
'''importprometheus_metrics_protoaspmpdefmain():# Define some labels that we want added to all metrics. These labels are# independent of the instance labels that define a metric as unique.# These could be used to add hostname, app name, etc.const_labels={'host':'examplehost','app':'my_app'}# Create a Counter metric to track logged in users. This counter has# 5 separate instances.# We'll make use of the optional const_labels argument to add extra# constant labels.# We will also add a timestamp to the metric instances.# We will request that the labels be sorted.cm=pmp.create_counter('logged_users_total','Logged users in the application.',(({'country':"sp","device":"desktop"},520),({'country':"us","device":"mobile"},654),({'country':"uk","device":"desktop"},1001),({'country':"de","device":"desktop"},995),({'country':"zh","device":"desktop"},520),),timestamp=True,const_labels=const_labels,ordered=True)# Create a Gauge metric, similar to the counter above.gm=pmp.create_gauge('logged_users_total','Logged users in the application.',(({'country':"sp","device":"desktop"},520),({'country':"us","device":"mobile"},654),({'country':"uk","device":"desktop"},1001),({'country':"de","device":"desktop"},995),({'country':"zh","device":"desktop"},520),),timestamp=True,const_labels=const_labels,ordered=True)# Now lets create a Summary and Histogram metric object. These forms# of metrics are slightly more complicated.## Remember, the collection of metrics and the management of Summary# Quantiles and Histogram Buckets are outside the scope of# functionality provided by this package.## The following examples assume they are taking the data values from# a management library that can also emit the sum and count fields# expected for both Summary and Histogram metrics.# Create a Summary metric. The values for a summary are slightly# different to a Counter or Gauge. They are composed of a dict representing# the various quantile values of the metric. The count and sum are# expected to be present in this dict.sm=pmp.create_summary('request_payload_size_bytes','Request payload size in bytes.',(({'route':'/'},{0.5:4.0,0.9:5.2,0.99:5.2,'sum':25.2,'count':4}),({'route':'/data'},{0.5:4.0,0.9:5.2,0.99:5.2,'sum':25.2,'count':4}),),timestamp=True,const_labels=const_labels,ordered=True)# Create a Histogram metric. The values for a histogram are slightly# different to a Counter or Gauge. They are composed of a dict representing# the various bucket values of the metric. The cumulative count and sum# values are expected to be present in this dict.## Libraries manageing buckets typically have add a POS_INF upper bound to# catch values beyond the largest bucket bound. Simulate this behavior in# the data below.POS_INF=float("inf")hm=pmp.create_histogram('request_latency_seconds','Request latency in seconds.',(({'route':'/'},{5.0:3,10.0:2,15.0:1,POS_INF:0,'count':6,'sum':46.0}),({'route':'/data'},{5.0:3,10.0:2,15.0:1,POS_INF:0,'count':6,'sum':46.0}),),timestamp=True,const_labels=const_labels,ordered=True)# Serialize a sequence of metrics into a payload suitable for network# transmission.input_metrics=(cm,gm,sm,hm)payload=pmp.encode(*input_metrics)assertisinstance(payload,bytes)# De-serialize the payload into a sequence of MetricsFamily objects.recovered_metrics=pmp.decode(payload)# Confirm that the round trip re-produced the same number of metrics# and that the metrics are identical.assertlen(recovered_metrics)==len(input_metrics)forrecovered_metric,input_metricinzip(recovered_metrics,input_metrics):assertrecovered_metric==input_metricformetricininput_metrics:print(metric)if__name__=='__main__':main()

如果您只想直接访问prometheus协议缓冲区对象 自己生成实例,只需将它们从包中导入为 如下:

fromprometheus_metrics_protoimport(COUNTER,GAUGE,SUMMARY,HISTOGRAM,Bucket,Counter,Gauge,Histogram,LabelPair,Metric,MetricFamily,Summary,Quantile)

许可证

普罗米修斯度量原型是在麻省理工学院的许可下发布的。

开发

使用:

检查代码样式
(myenv) $ make style

运行单元测试。

$ make test

使用:

检查代码覆盖率
(myenv) $ make coverage

然后打开results查看覆盖范围。

项目已放置代码存根(prometheus_metrics_pb2.py), 由google protocol buffers代码生成工具生成,在source下 控制。

如果以后需要重新生成此文件,请执行以下步骤:

(myenv) $ make regenerate

释放过程

以下步骤用于制作新的软件版本:

  • 确保__init__.py中的版本标签已更新。

  • 创建分发。这个项目产生了一个称为纯 Python轮。此软件包仅支持python3。

    make dist
  • 测试分布。这包括创建一个虚拟环境,安装 它的分布和运行测试。这些步骤已被捕获 为了方便使用makefile规则。

    make dist.test
  • 上传到pypi。

    make dist.upload
  • 创建repo标签并将其推送到github。

    git tag YY.MM.MICRO -m "A meaningful release tag comment"
    git tag  # check release tag is in list
    git push --tags origin master
    • Github将在以下位置创建发布tarball:

      https://github.com/{username}/{repo}/tarball/{tag}.tar.gz
      

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java为什么@DELETE REST不起作用?   带有JPA2的java表值参数。1和Hibernate,Sql Server   如何将Java类添加到Xamarin VS2017项目   绘制多边形时出现java空指针异常   java Apache WebClient 303状态未重定向   java如何用一组字符串数组从数据库中获取数据   java是否可以使用Google Drive API向文件中添加脚本?   java组织。阿帕奇。贾斯珀。JspC jar文件下载   java在整个JSON映射中将单个值作为JSON流   通过命令行将文件输入到java   java rs.next()总是返回false   java标记异常,通知调用方利用异常消息   java Spring YML数组属性为空