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代码中的类型冲突   谷歌应用引擎Java还是Python?   如何将java bean传递到jsp页面,以便jqQrid使用json显示?   在编译kotlin代码时,kotlin编译器如何处理java代码?   java不准确地更改JTextPane中的文本颜色   反应式编程AWS SDK v2 SdkAsyncHttpClient使用Java 11 Java实现。网http HttpClient sendAsync   在Spring AMQP中,java根据队列的消费者计数来消费队列   java在ArrayList的add()方法中创建新对象会导致内存泄漏。我能做什么不同的事?   未将java BufferedReader特定行追加到字符串   用于聊天程序格式化的java JavaFX 2文本区   java如何从netbeans项目生成exexutable文件?   swing如何在Java中使用JButton操作调整JWindow的宽度和高度?   java有没有办法在spring boot中使用jasypt aes加密和解密?   java通过使用泛型如何将映射作为集合传递给方法?   java如何替换不推荐使用的构造函数DynamoDBMapperFieldModel