- 4种查询类型
- vector
- matrix
- scalar
- string
- instant query 对应 vector
- range query 对应 matrix
prometheus四种查询类型
- 文档地址
- 查询类型源码地址
prometheus-release-2.29/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 向量 源码位置
prometheus-release-2.29/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 矩阵,源码位置
prometheus-release-2.29/promql/value.go
// Matrix is a slice of Series that implements sort.Interface and
// has a String method.
type Matrix []Series
- Matrix是Series的切片 Series源码位置
prometheus-release-2.29/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 一个简单的数字浮点值
在 Prometheus 中,scalar(标量)通常在以下几种情况下使用:
作为单个数值的表示:当您需要获取一个单一的、静态的数值,而不是一个随时间变化的时间序列时,可以使用标量。例如,获取当前系统的总内存大小。
计算表达式的结果:在一些复杂的查询和表达式中,可能会得到一个标量作为最终的计算结果。
与其他数据类型进行比较和运算:可以将标量与时间序列数据进行比较或参与运算,以实现特定的监控和分析逻辑。
例如,如果您有一个自定义的计算,得出了一个代表特定阈值的标量值,然后将其与某个指标的实时值进行比较,以确定是否触发警报。
假设您定义了一个标量 threshold = 80 ,然后可以将其与某个指标 cpu_usage 进行比较,如 cpu_usage > threshold ,以判断 CPU 使用率是否超过了设定的阈值。
String 一个简单的字符串值
在 PromQL 中,字符串(String)的使用场景相对较少,主要可能在以下几种情况中使用:
作为标签值:标签的值可以是字符串类型,用于对指标进行分类和描述。例如,http_requests_total{method="GET", status="200", endpoint="/api"} 中的 method、status 和 endpoint 的值都是字符串。
在某些特定的函数或操作中,可能会处理包含字符串的标签值。例如,使用 label_replace 函数对标签值进行字符串操作和替换。
在与外部系统或工具集成时,可能会将 Prometheus 的数据以字符串的形式进行传递或处理。
总体来说,PromQL 主要侧重于对数值型的指标数据进行查询和计算,字符串的直接操作相对较少。

评论区