Prometheus 是一个开源的监控系统,其核心在于通过 metrics(指标) 实现对系统资源的量化分析。以下是编写 Prometheus 指标的关键要素:
📌 指标格式规范
命名规则
- 采用
name{label="value", label="value"}
的格式 - 支持
counter
(计数器)、gauge
(仪表盘)、histogram
(直方图)、summary
(摘要)等数据类型 - 避免使用特殊字符,推荐使用下划线
_
代替空格(如cpu_usage_seconds_total
)
- 采用
标签(Labels)
- 标签用于对指标进行分类,例如
job="api_server"
- 标签是可选的,但建议为指标添加有意义的标签以提高可读性
- 标签用于对指标进行分类,例如
数据类型示例
- Counter:
http_requests_total{method="post"} 42
- Gauge:
node_cpu_seconds_total{mode="idle"} 100
- Histogram:
request_latency_seconds{method="get"} 0.123
- Summary:
request_duration_seconds{method="post"} 0.456
- Counter:
📚 编写实践建议
- 使用清晰的命名约定,例如
service_name
+metric_type
+description
- 对指标进行分类管理,避免重复命名
- 通过
#
注释说明指标含义,例如:# 该指标记录 HTTP 服务的请求延迟 http_request_latency_seconds{job="web"} 0.5
- 遵循 Prometheus 官方文档 的最佳实践
📈 图表与监控
通过 Prometheus 的 graph
功能,可以将指标可视化为趋势图、热力图等。例如:
📖 扩展阅读
如需深入学习 Prometheus 的指标系统,推荐访问:
Prometheus 指标详解
了解更多关于指标类型和采集方法的细节,请参考官方文档。