OpenMetrics 和 Prometheus 数据规范

OpenMetrics 是云原生计算基金会(CNCF)项目,旨在标准化云原生应用程序中暴露指标的过程。OpenMetrics 的规范是公开的,可以在github上阅读。本文简要介绍与数据模型相关的规范,有助于你学习Prometheus及相关的监控系统。

Prometheus Exposition Format

OpenMetrics规范基于Prometheus Exposition Format的。它是一种简单的、基于文本的、人类可读的数据格式,在社区中被广泛采用。围绕Prometheus暴露指标数据格式有数百种集成,甚至一些其他监控工具也已经直接使用它。然而,在名称中加入"Prometheus"使其难以获得竞争对手或其他相关方的支持。OpenMetrics希望通过基于厂商中立和开放数据格式原则来改变现状,这看起来像是策略性的公关行动,但它已经得到了一些大型组织的支持。所以看起来它有一个美好的未来。

数据模型

OpenMetrics中展示的度量数据大致如下:、

shell 复制代码
# TYPE acme_http_router_request_seconds summary
# UNIT acme_http_router_request_seconds seconds
# HELP acme_http_router_request_seconds Latency though all of ACME's HTTP request router.
acme_http_router_request_seconds_sum{path="/api/v1",method="GET"} 9036.32
acme_http_router_request_seconds_count{path="/api/v1",method="GET"} 807283.0
acme_http_router_request_seconds_created{path="/api/v1",method="GET"} 1605281325.0
acme_http_router_request_seconds_sum{path="/api/v2",method="POST"} 479.3
acme_http_router_request_seconds_count{path="/api/v2",method="POST"} 34.0
acme_http_router_request_seconds_created{path="/api/v2",method="POST"} 1605281325.0
# TYPE go_goroutines gauge
# HELP go_goroutines Number of goroutines that currently exist.
go_goroutines 69
# TYPE process_cpu_seconds counter
# UNIT process_cpu_seconds seconds
# HELP process_cpu_seconds Total user and system CPU time spent in seconds.
process_cpu_seconds_total 4.20072246e+06
# EOF

这种格式的最佳特性之一是可读性。我们简要介绍注释部分,分别为type, unit, help:

  • 使用TYPE,定义metric.type和标签。
  • 通过UNIT,定义了在该MetricFamily中使用的单位。
  • HELP是用于人类可读的度量族的描述。应该是较短字符串,以便可以用作工具提示。

度量类型

counter

计数器是对离散事件进行计数的度量类型。它必须有一个称为total的值,并且不会随时间推移而减少。计数器可用于计算错误或请求的数量、所花费的cpu时间或其他度量累积的数据。

shell 复制代码
# TYPE process_cpu_seconds counter
# UNIT process_cpu_seconds seconds
# HELP process_cpu_seconds Total user and system CPU time spent in seconds.
process_cpu_seconds_total 4.20072246e+06

gauge

Gauge是最简单的度量类型之一。它由可以随时间变化的单一值组成。与计数器相反,Gauge可以在任何给定的时间改变其值,可以增加或减少其值。

shell 复制代码
# TYPE go_goroutines gauge
# HELP go_goroutines Number of goroutines that currently exist.
go_goroutines 69

histogram

直方图用于测量离散事件并将其分成桶。常见的例子是请求持续时间和I/O请求大小。直方图MetricPoints必须至少有一个桶。Count、created和sum值是可选的。

shell 复制代码
# TYPE request_duration histogram
# HELP request_duration help
request_duration_bucket{le="0"} 0
request_duration_bucket{le="0.00000000001"} 0
request_duration_bucket{le="0.0000000001"} 0
request_duration_bucket{le="1e-04"} 0
request_duration_bucket{le="1.1e-4"} 0
request_duration_bucket{le="1.1e-3"} 0
request_duration_bucket{le="1.1e-2"} 0
request_duration_bucket{le="1"} 0
request_duration_bucket{le="1e+05"} 0
request_duration_bucket{le="10000000000"} 0
request_duration_bucket{le="100000000000.0"} 0
request_duration_bucket{le="+Inf"} 3
request_duration_count 3
request_duration_sum 2
# EOF

summary

摘要与直方图非常相似,当直方图计算成本较高时可以使用摘要。我们可以使用sum、count和created值来创建摘要。

python 复制代码
# TYPE acme_http_router_request_seconds summary
# UNIT acme_http_router_request_seconds seconds
# HELP acme_http_router_request_seconds Latency though all of ACME's HTTP request router.
acme_http_router_request_seconds_sum{path="/api/v1",method="GET"} 9036.32
acme_http_router_request_seconds_count{path="/api/v1",method="GET"} 807283.0
acme_http_router_request_seconds_created{path="/api/v1",method="GET"} 1605281325.0
acme_http_router_request_seconds_sum{path="/api/v2",method="POST"} 479.3
acme_http_router_request_seconds_count{path="/api/v2",method="POST"} 34.0
acme_http_router_request_seconds_created{path="/api/v2",method="POST"} 1605281325.0

info

信息度量用于公开在进程生命周期内不应更改的文本信息。常见的例子是应用程序的版本、修订控制提交和编译器的版本。

信息度量的MetricPoint包含LabelSet。Info MetricPoint的标签集的标签名不能与其Metric的标签集的标签名相同。Info可用于对值不随时间变化的enum进行编码,例如网络接口的类型。

Metrics Endpoint

该标准的实现者必须以OpenMetrics格式公开度量标准。为记录的端点的HTTP Get请求提供响应,该端点应命名为/metrics。此端点将被应用端定期消费,它必须能够为应用端调用响应有意义的数据。

总结

本文介绍了OpenMetrics数据格式规范,理解这些规范有助于我们进一步学习prometheus,实现对各类应用或系统的监控。

相关推荐
阿里云云原生1 天前
研发视角的新突破:当 AI Coding 工具集成全域运维诊断,排查线上故障只需 3 分钟
云原生
小猿姐2 天前
唯品会大规模数据库云原生实践:基于 KubeBlocks 管理数千实例的统一运维之路
运维·elasticsearch·云原生
阿里云云原生2 天前
AgentTeams 和 Claude Tag 都进入群聊模式,是新范式还是新叙事?
云原生·agent
阿里云云原生3 天前
Higress v2.2.3 发布:正式入驻 CNCF Sandbox,AI Gateway 与 Ingress 迁移能力双向加固
云原生
阿里云云原生4 天前
香港站【企业 AI Agent 工程化实战专场】来啦,邀您7月9日见!
云原生·agent
阿里云云原生4 天前
研发域与运维域的“数字握手”:通过 Agentic Skills 实现 DevOps 全链路自动化
云原生
SRETalk8 天前
Zabbix、Prometheus、Grafana、Nightingale,四个监控如何选型?
zabbix·grafana·prometheus·nightingale
阿里云云原生8 天前
AI 开发新常态:当 Cursor、Claude、Codex 并行,如何统一管理散落的 Skill 资产?
云原生·ai编程
探索云原生8 天前
K8s 1.36 这个 GA 特性,把 initContainer 拉模型的 hack 干掉了
ai·云原生·kubernetes
Java之美8 天前
从edge-trigger到level-trigger,谈谈 Kubernetes controller 的开发范式
云原生