1. HTTP API
在prometheus服务器上,我们可以通过访问/api/v1来查询prometheus状态,也就是http://localhost:9090/api/v1
curl http://localhost:9090/api/v1
<a href="/api/v1/">Moved Permanently</a>
不过我们这样是没办法查询到数据的,毕竟这是接口的地址,但是出现这个输出就说明接口是可以访问的
1.1. 接口的格式
这个API返回是JSON格式。每个请求成功的返回值都是以2xx
开头的编码。
到达API处理的无效请求,返回一个JSON错误对象,并返回下面的错误码:
-
400 Bad Request
。当参数错误或者丢失时。 -
422 Unprocessable Entity
。当一个表达式不能被执行时。 -
503 Service Unavailable
。当查询超时或者中断时。
1.2. 表达式查询
查询语言表达式可以在瞬时向量或者范围向量中执行。
-
Instant queries(即时查询):
GET /api/v1/query
POST /api/v1/query
URL查询参数:
-
query=<string>
: Prometheus表达式查询字符串。 -
time=<rfc3339 | unix_timestamp>
: 执行时间戳,可选项,缺省表示当前服务器时间 -
timeout=<duration>
: 执行超时时间设置,默认由-query.timeout
标志设置
查询结果
{ "resultType": "matrix" | "vector" | "scalar" | "string", "result": <value> }
比如:下面例子执行了在时刻是
2015-07-01T20:10:51.781Z
的up
表达式:$ curl 'http://localhost:9090/api/v1/query?query=up&time=2015-07-01T20:10:51.781Z' { "status": "success", "data":{ "resultType": "vector", "result" : [ { "metric" : { "__name__" : "up", "job" : "prometheus", "instance" : "localhost:9090" }, "value": [ 1435781451.781, "1" ] }, { "metric" : { "__name__" : "up", "job" : "node", "instance" : "localhost:9100" }, "value" : [ 1435781451.781, "0" ] } ] } }
-
-
范围查询
GET /api/v1/query_range
POST /api/v1/query_range
URL查询参数
-
query=<string>
: Prometheus表达式查询字符串。 -
start=<rfc3339 | unix_timestamp>
: 开始时间戳。 -
end=<rfc3339 | unix_timestamp>
: 结束时间戳。 -
step=<duration | float>
: 查询时间步长,范围时间内每step秒执行一次。 -
timeout=<duration>
: 执行超时时间设置,默认由-query.timeout
标志设置
下面查询结果格式的
data
部分:{ "resultType": "matrix", "result": <value> }
下面例子评估的查询条件
up
,且30s范围的查询,步长是15s。$ curl 'http://localhost:9090/api/v1/query_range?query=up&start=2015-07-01T20:10:30.781Z&end=2015-07-01T20:11:00.781Z&step=15s' { "status" : "success", "data" : { "resultType" : "matrix", "result" : [ { "metric" : { "__name__" : "up", "job" : "prometheus", "instance" : "localhost:9090" }, "values" : [ [ 1435781430.781, "1" ], [ 1435781445.781, "1" ], [ 1435781460.781, "1" ] ] }, { "metric" : { "__name__" : "up", "job" : "node", "instance" : "localhost:9091" }, "values" : [ [ 1435781430.781, "0" ], [ 1435781445.781, "0" ], [ 1435781460.781, "1" ] ] } ] } }
-
1.3. 查询元数据
-
通过标签匹配器找到度量指标列表
下面例子返回了度量指标列表 且不返回时间序列数据值。
GET /api/v1/series
URL查询参数:
-
match[]=<series_selector>
: 选择器是series_selector。这个参数个数必须大于等于1. -
start=<rfc3339 | unix_timestamp>
: 开始时间戳。 -
end=<rfc3339 | unix_timestamp>
: 结束时间戳。
返回结果的
data
部分,是由key-value键值对的对象列表组成的。下面这个例子返回时间序列数据, 选择器是
up
或者process_start_time_seconds{job="prometheus"}
$ curl -g 'http://localhost:9090/api/v1/series?match[]=up&match[]=process_start_time_seconds{job="prometheus"}' { "status" : "success", "data" : [ { "__name__" : "up", "job" : "prometheus", "instance" : "localhost:9090" }, { "__name__" : "up", "job" : "node", "instance" : "localhost:9091" }, { "__name__" : "process_start_time_seconds", "job" : "prometheus", "instance" : "localhost:9090" } ] }
-
-
查询标签值
下面这个例子,返回了带有指定标签的标签值列表
GET /api/v1/label//values
这个返回JSON结果的
data
部分是带有label_name=job的值列表:$ curl http://localhost:9090/api/v1/label/job/values { "status" : "success", "data" : [ "node", "prometheus" ] }
-
删除时间序列
下面的例子,是从Prometheus服务中删除匹配的度量指标和标签列表:
DELETE /api/v1/series
URL查询参数
-
match[]=<series_selector>
: 删除符合series_selector匹配器的时间序列数据。参数个数必须大于等于1.
返回JSON数据中的
data
部分有以下的格式{ "numDeleted": <number of deleted series> }
下面的例子删除符合度量指标名称是
up
或者时间序为process_start_time_seconds{job="prometheus"}
:$ curl -XDELETE -g 'http://localhost:9090/api/v1/series?match[]=up&match[]=process_start_time_seconds{job="prometheus"}' { "status" : "success", "data" : { "numDeleted" : 3 } }
-
为了方便大家学习,请大家加我的微信,我会把大家加到微信群(微信群的二维码会经常变)和qq群821119334,问题答案云原生技术课堂,有问题可以一起讨论
-
个人微信
640.jpeg -
腾讯课堂
640-20200506145837072.jpeg -
微信公众号
640-20200506145842007.jpeg -
专题讲座
2020 CKA考试视频 真题讲解 https://www.bilibili.com/video/BV167411K7hp
2020 CKA考试指南 https://www.bilibili.com/video/BV1sa4y1479B/
2020年 5月CKA考试真题 https://mp.weixin.qq.com/s/W9V4cpYeBhodol6AYtbxIA
网友评论