Prometheus 第4章:基本概念
2026/8/13大约 8 分钟
Prometheus · 第 4 章(合并)
由原课程小节笔记合并,便于连续阅读。
4.1 prometheus基本概念-sample数据点
本节重点介绍 : prometheus 基本概念
- point 时序中单一数据点的数据结构,大小
- 标签和标签组
- sample 时序曲线中的一个点
prometheus 基本概念
Point 数据点
- 源码位置 D:\nyy_work\go_path\src\github.com\prometheus\prometheus\promql\value.go
// Point represents a single data point for a given timestamp.
type Point struct {
T int64
V float64
}- 具体含义: 一个时间戳和一个value组合成的数据点
- size:16byte: 包含 1个8byte int64时间戳和1个8byte float64 value
- 举例图片

Label 标签
- 源码位置 D:\nyy_work\go_path\src\github.com\prometheus\prometheus\pkg\labels\labels.go
type Label struct {
Name, Value string
}- 一对label 比如
cpu="0"mode: "user" - 举例图片

Labels 标签组
- 源码位置 D:\nyy_work\go_path\src\github.com\prometheus\prometheus\pkg\labels\labels.go
type Labels []Label- 是Label切片的别名
- 就是 一个指标的所有tag values
- 举例图片

sample 数据点
- 源码位置 D:\nyy_work\go_path\src\github.com\prometheus\prometheus\promql\value.go
// Sample is a single sample belonging to a metric.
type Sample struct {
Point
Metric labels.Labels
}- sample代表一个数据点
- 举例图片


本节重点总结 : prometheus 基本概念
- point 时序中单一数据点的数据结构,大小 8+8=16byte
- 标签和标签组 key-value的字符串
- sample 时序曲线中的一个点
4.2 prometheus四种查询类型
本节重点介绍 : prometheus 四种查询类型
- 4种查询类型
- vector
- matrix
- scalar
- string
- instant query 对应vector
- range query 对应matrix
prometheus四种查询类型
- 文档地址
- 查询类型源码地址 D:\nyy_work\go_path\src\github.com\prometheus\prometheus\promql\parser\value.go
// The valid value types.
const (
ValueTypeNone ValueType = "none"
ValueTypeVector ValueType = "vector"
ValueTypeScalar ValueType = "scalar"
ValueTypeMatrix ValueType = "matrix"
ValueTypeString ValueType = "string"
)即时向量 Instant vector : 一组时间序列,每个时间序列包含一个样本,所有样本共享相同的时间戳
- vector 向量 源码位置 D:\nyy_work\go_path\src\github.com\prometheus\prometheus\promql\value.go
// Vector is basically only an alias for model.Samples, but the
// contract is that in a Vector, all Samples have the same timestamp.
type Vector []Sample- vector 向量,是samples的别名,但是所有sample具有相同timestamp ,常用作instant_query的结果
- 在prometheus页面上就是table查询 ,对应查询接口 /api/v1/query
- 举例图片

范围向量 Range vector : 一组时间序列,一段时间的结果
- 在prometheus页面上就是graph查询 ,对应查询接口 /api/v1/query_range
- 返回的结果是Matrix 矩阵,源码位置 D:\nyy_work\go_path\src\github.com\prometheus\prometheus\promql\value.go
// Matrix is a slice of Series that implements sort.Interface and
// has a String method.
type Matrix []Series- Matrix是series的切片 Series源码位置 D:\nyy_work\go_path\src\github.com\prometheus\prometheus\promql\value.go
// Series is a stream of data points belonging to a metric.
type Series struct {
Metric labels.Labels `json:"metric"`
Points []Point `json:"values"`
}- series 是标签组+Points的组合
- 举例图片

标量 Scalar 一个简单的数字浮点值
- 举例图片

String 一个简单的字符串值;目前未使用
本节重点总结 : prometheus 四种查询类型
- 4种查询类型
- vector 一个时刻的结果
- matrix 一段时间的结果
- scalar 浮点数
- string
- instant query 对应vector
- range query 对应matrix
4.3 四种标签匹配模式
本节重点介绍 : prometheus 四种标签匹配模式
- 4种查询类型
=等于!=不等于=~正则匹配!~正则非匹配
四种标签匹配模式
=等于- 查询举例: cpu第一个核并且是用户态的数据 node_cpu_seconds_total{mode="user",cpu="0"}
- 查询举例: go_gc_duration_seconds{quantile="0.75"}

!=不等于- 查询举例: 非lo网卡的接收字节数 node_network_receive_bytes_total{device!="lo"}
- 查询举例:

=~正则匹配- 查询: 挂载点以/run开头的文件系统剩余字节数 node_filesystem_avail_bytes{mountpoint=~"^/run.*"}
- 查询: prometheus_http_requests_total{handler=~"/api.*"}

!~正则非匹配- 查询: 块设备名字不包含vda的读字节数 node_disk_read_bytes_total{device!~".vda."}
- 查询: prometheus_http_requests_total{code!~".*00"}

__name__也是个标签,可以匹配metrics- 查询 {__name__="go.*",quantile=".0."} 等价于 go_gc_duration_seconds{quantile=~".0."}

本节重点介绍 : prometheus 四种标签匹配模式
4种查询类型
=等于!=不等于=~正则匹配!~正则非匹配
=,!=不需要正则,速度最快
4种可以自由组合
标签的key要明确给出
__name__也是个标签,可以匹配metricspromql中查询没数据,大多是标签匹配的问题
4.4 四种数据类型
本节重点介绍 : prometheus 四种数据类型
- 四种数据类型
- gauge 当前值
- counter 计数器
- histogram 直方图样本观测
- summary 摘要
四种数据类型
gauge 当前值
- 举例 go_info{instance="localhost:9090", job="prometheus", version="go1.16.7"}
- 类似的info信息,看时序的结果值=1 意义不大
- 主要是看标签的key和value go.1.16.7 ,关注一下
- 举例 go_memstats_heap_alloc_bytes

counter 计数器
- 代表一个累积指标单调递增计数器
- 使用rate 查看qps rate(prometheus_http_requests_total[1m])
- 使用increase 查看增量 increase(prometheus_http_requests_total[10s])

histogram 直方图样本观测
- 通常之类的东西请求持续时间或响应大小和计数它们配置的桶中

- 它还提供所有观察值的总和
# http所有接口 总的95分位值
# sum/count 可以算平均值
prometheus_http_request_duration_seconds_sum/ prometheus_http_request_duration_seconds_count
# histogram_quantile(0.95, sum(rate(prometheus_http_request_duration_seconds_bucket[5m])) by (le,handler))
histogram_quantile(0.95, sum(rate(prometheus_http_request_duration_seconds_bucket[1m])) by (le))
# range_query接口的95分位值
histogram_quantile(0.95, sum(rate(prometheus_http_request_duration_seconds_bucket{handler="/api/v1/query_range"}[5m])) by (le))summary 摘要会采样观察值
- 通常是请求持续时间和响应大小之类的东西
- 尽管它还提供了观测值的总数和所有观测值的总和
# gc耗时
# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0.000135743
go_gc_duration_seconds{quantile="0.25"} 0.000872805
go_gc_duration_seconds{quantile="0.5"} 0.000965516
go_gc_duration_seconds{quantile="0.75"} 0.001055636
go_gc_duration_seconds{quantile="1"} 0.006464756
# summary 平均值
go_gc_duration_seconds_sum /go_gc_duration_seconds_count利用 sum/count 算平均值 :histogram 和summary 都适用
- go_gc_duration_seconds_sum/go_gc_duration_seconds_count 算平均值
本节重点介绍 : prometheus 四种数据类型
- 四种数据类型
- gauge 当前值 最简单,看标签
- counter 计数器 多用在请求计数,cpu统计
- histogram 直方图样本观测 :服务端算分位值
- summary 摘要:客户端算分位值
- 利用 sum/count 算平均值 :histogram 和summary 都适用
4.5 时间范围选择器
本节重点介绍 :
- 时间范围选择器的正确用法
- prometheus查询返回13位毫秒时间戳
范围向量选择器 Range Vector Selectors
- 范围矢量的工作方式与即时矢量一样,不同之处在于它们从当前即时中选择了一定范围的样本。语法上,将持续时间附加在[]向量选择器末尾的方括号()中,以指定应为每个结果范围向量元素提取多远的时间值。
- 只能作用在
counter上
时间范围
ms -毫秒
s -秒
m - 分钟
h - 小时
d -天-假设一天总是24小时
w -周-假设一周始终为7天
y -年-假设一年始终为365天- 时间范围不能脱离rate等函数,不然会报错
直接查询报错 promhttp_metric_handler_requests_total[1m]
Error executing query: invalid expression type "range vector" for range query, must be Scalar or instant Vector需要叠加一个非聚合函数 如 rate irate delta idelta sum 等
- 计算网卡入流量
rate(promhttp_metric_handler_requests_total[1m])
时间范围 ,不能低于采集间隔
- 采集8秒 ,查询3秒则无数据
- rate(promhttp_metric_handler_requests_total[3s])

prometheus返回的都是毫秒时间戳
- 10位代表秒时间戳
- 13位代表毫秒时间戳
- 举例图片

本节重点总结 :
- 时间范围选择器的正确用法
- 时间范围 ,不能低于采集间隔
- prometheus查询返回13位毫秒时间戳
4.6 实用promql介绍
本节重点介绍 : prometheus promql简单的总结
- topk 最值
- absent nodata报警
- offset 同环比
- 分位值histogram_quantile
- 成功的/总的 = 成功率
- agg_over_time 横向的聚合
实用功能总结
查询函数文档
举例
agg 去掉/保留 label ,分布情况
- 去掉举例:
sum without(code) (rate(prometheus_http_requests_total[2m] ) ) 
- 保留举例:
sum by(code) (rate(prometheus_http_requests_total[2m] ) ) 
topk bottomK 看top
- 举例:查看容器cpu使用率top5
topk(5,prometheus_http_response_size_bytes_bucket) 
- 最小的 bottomk(5,prometheus_http_response_size_bytes_bucket)
同环比 相减
- 举例:qps环比1小时 掉10
sum (rate(prometheus_http_requests_total[2m] offset 1h) ) - sum (rate(prometheus_http_requests_total[2m] ) )
absent nodata报警
- ==1代表absent生效
- 举例:
absent(abc_def)==1
分位值histogram_quantile
- 举例查看apiserver 请求延迟90分位
histogram_quantile(0.90, sum(rate(prometheus_http_request_duration_seconds_bucket[5m])) by (le))
两组series关联 成功率百分比
- 举例:apiserver 请求成功率
100* ( sum(prometheus_http_requests_total{code=~"2.*|3.*"})/ sum(prometheus_http_requests_total) ) 
agg_over_time 给所有ts的value做agg 横向agg
- 举例查看一天的alert
avg_over_time(go_goroutines [24h])
本节重点总结 : prometheus promql简单的总结
- topk 最值
- absent nodata报警
- 分位值histogram_quantile
- offset 同环比
- 成功的/总的 = 成功率
- agg_over_time 横向的聚合