本文已经收录在 Prometheus 合集 你真的会 Prometheus 查询吗?--PromQL 合集 中。
Target
这个接口会返回 Prometheus Target 发现的当前状态。
默认情况下 activeTargets 和 droppedTargets 都是请求响应的一部分。
discoveredLabels 表示在进行 Label 重新标记发生之前,服务发现找到的未修改的原始 Label。
labels 表示 Label 重新标记后的结果。
$ curl http://localhost:9090/api/v1/targets{ "status": "success", "data": { "activeTargets": [ { "discoveredLabels": { "__address__": "127.0.0.1:9090", "__metrics_path__": "/metrics", "__scheme__": "http", "job": "prometheus" }, "labels": { "instance": "127.0.0.1:9090", "job": "prometheus" }, "scrapePool": "prometheus", "scrapeUrl": "http://127.0.0.1:9090/metrics", "globalUrl": "http://example-prometheus:9090/metrics", "lastError": "", "lastScrape": "2017-01-17T15:07:44.723715405+01:00", "lastScrapeDuration": 0.050688943, "health": "up", "scrapeInterval": "1m", "scrapeTimeout": "10s" } ], "droppedTargets": [ { "discoveredLabels": { "__address__": "127.0.0.1:9100", "__metrics_path__": "/metrics", "__scheme__": "http", "__scrape_interval__": "1m", "__scrape_timeout__": "10s", "job": "node" }, } ] }}
复制代码
对于上边的这个查询,可以添加一个查询参数 state ,这个参数可以指定对应的 Target 状态,比如 state=active, state=dropped, state=any 。当然,这是个可选参数,不是必须的。
注意,对于过滤掉的目标,仍然返回一个空数组,里边的值会丢掉。比如:
$ curl 'http://localhost:9090/api/v1/targets?state=active'{ "status": "success", "data": { "activeTargets": [ { "discoveredLabels": { "__address__": "127.0.0.1:9090", "__metrics_path__": "/metrics", "__scheme__": "http", "job": "prometheus" }, "labels": { "instance": "127.0.0.1:9090", "job": "prometheus" }, "scrapePool": "prometheus", "scrapeUrl": "http://127.0.0.1:9090/metrics", "globalUrl": "http://example-prometheus:9090/metrics", "lastError": "", "lastScrape": "2017-01-17T15:07:44.723715405+01:00", "lastScrapeDuration": 50688943, "health": "up" } ], "droppedTargets": [] }}
复制代码
查询结果格式
我们通过表达式对 Prometheus 进行查询,会将返回的值放在结果的 data 部分。
<sample_value> 是数值的例子。
JSON 不支持特殊浮点值,比如 NaN 、Inf 、-Inf ,所以样本值是作为引用的 JSON 字符串而不是原始数字传输的。
范围向量
范围向量返回的结果类型是 matrix 。对应的 result 属性的格式如下:
[ { "metric": { "<label_name>": "<label_value>", ... }, "values": [ [ <unix_time>, "<sample_value>" ], ... ] }, ...]
复制代码
瞬时向量
瞬时向量返回的结果类型是 vector 。对应的 result 属性的格式如下:
[ { "metric": { "<label_name>": "<label_value>", ... }, "value": [ <unix_time>, "<sample_value>" ] }, ...]
复制代码
标量
标量返回的结果类型是 scalar 。对应的 result 属性的格式如下:
[ <unix_time>, "<scalar_value>" ]
复制代码
字符串
字符串返回的结果类型是 string 。对应的 result 属性的格式如下:
[ <unix_time>, "<string_value>" ]
复制代码
评论