推特 阿里云技术文档正文

HTTP API_promQL参考_时序数据库 Prometheus® 版_时序时空数据库TSDB

admin 阿里云技术文档 2020-02-11 207 0
阿里云服务器优惠

HTTP API

HTTP API

在Prometheus服务器上的/ api / v1路径下可以访问当前稳定的HTTP API。

格式概述

API返回是JSON格式。每个成功的API请求都将返回2xx的状态码。
所有无效的请求,API会返回一个JSON错误对象,并返回以下HTTP响应码:

  • 400 Bad Request。当参数错误或者缺失时。
  • 422 Unprocessable Entity。当一个表达式不能被执行时。
  • 503 Service Unavailable。当查询超时或者中断时。

JSON返回格式如下所示:

  1. {
  2. "status": "success" | "error",
  3. "data": <data>,
  4. // Only set if status is "error". The data field may still hold
  5. // additional data.
  6. "errorType": "<string>",
  7. "error": "<string>",
  8. // Only if there were warnings while executing the request.
  9. // There will still be data in the data field.
  10. "warnings": ["<string>"]
  11. }

输入时间戳可以为RFC3339格式或者Unix时间戳格式。输出时间戳以Unix时间戳方式呈现。

<series_selector>占位符提供Prometheus时间序列选择器http_requests_total或者http_requests_total{method=~"^GET|POST$"},需要在URL中编码传输。

占位符指的是[0-9] + [smhdwy]形式的Prometheus持续时间字符串。 例如,5m表示5分钟的持续时间。
占位符引用布尔值(字符串true和false)。

表达式查询

通过 HTTP API 我们可以分别通过 /api/v1/query/api/v1/query_range 查询 PromQL 表达式当前或者一定时间范围内的计算结果。

瞬时查询

瞬时向量的http restful api查询:

  1. GET /api/v1/query
  2. POST /api/v1/query

url查询参数:

  • query = :Prometheus表达式查询字符串。
  • time= :执行时间戳。 可缺省。
  • timeout = :执行超时时间设置,默认由-query.timeout标志设置。

如果省略time参数,则使用当前服务器时间。

您可以使用POST方法和Content-Type:application / x-www-form-urlencoded标头直接在请求正文中对这些参数进行URL编码。 在指定可能违反服务器端URL字符限制的大型查询时,这是非常有用的。
查询结果的数据部分具有以下格式:

  1. {
  2. "resultType": "matrix" | "vector" | "scalar" | "string",
  3. "result": <value>
  4. }

是指查询结果数据,其具有不同的格式,具体取决于resultType。 请参阅表达式查询结果格式

下面例子执行了在2015-07-01T20:10:51.781Z时刻up表达式:

  1. $ curl 'http://localhost:9090/api/v1/query?query=up&time=2015-07-01T20:10:51.781Z'
  2. {
  3. "status" : "success",
  4. "data" : {
  5. "resultType" : "vector",
  6. "result" : [
  7. {
  8. "metric" : {
  9. "__name__" : "up",
  10. "job" : "prometheus",
  11. "instance" : "localhost:9090"
  12. },
  13. "value": [ 1435781451.781, "1" ]
  14. },
  15. {
  16. "metric" : {
  17. "__name__" : "up",
  18. "job" : "node",
  19. "instance" : "localhost:9100"
  20. },
  21. "value" : [ 1435781451.781, "0" ]
  22. }
  23. ]
  24. }
  25. }

区间数据查询

使用 QUERY_RANGE API 我们则可以直接查询 PromQL 表达式在一段时间返回内的计算结果。

  1. GET /api/v1/query_range
  2. POST /api/v1/query_range

url查询参数:

  • query = :Prometheus表达式查询字符串。
  • start = :开始时间戳。
  • end= :结束时间戳。
  • step = :查询时间步长,范围时间内每step秒执行一次。
  • timeout = :超时设置。 可缺省。 默认为-query.timeout标志的值并受其限制。

您可以使用POST方法和Content-Type:application / x-www-form-urlencoded标头直接在请求正文中对这些参数进行URL编码。 在指定可能违反服务器端URL字符限制的大型查询时,这是非常有用的。
查询结果的数据部分具有以下格式:

  1. {
  2. "resultType": "matrix",
  3. "result": <value>
  4. }

有关占位符的格式,请参阅区间向量结果格式。
下面例子评估的查询条件up,且30s范围的查询,步长是15s。

  1. $ 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'
  2. {
  3. "status" : "success",
  4. "data" : {
  5. "resultType" : "matrix",
  6. "result" : [
  7. {
  8. "metric" : {
  9. "__name__" : "up",
  10. "job" : "prometheus",
  11. "instance" : "localhost:9090"
  12. },
  13. "values" : [
  14. [ 1435781430.781, "1" ],
  15. [ 1435781445.781, "1" ],
  16. [ 1435781460.781, "1" ]
  17. ]
  18. },
  19. {
  20. "metric" : {
  21. "__name__" : "up",
  22. "job" : "node",
  23. "instance" : "localhost:9091"
  24. },
  25. "values" : [
  26. [ 1435781430.781, "0" ],
  27. [ 1435781445.781, "0" ],
  28. [ 1435781460.781, "1" ]
  29. ]
  30. }
  31. ]
  32. }
  33. }

元数据查询

通过标签匹配器找到metric列表

以下表达式返回与特定标签集匹配的时间序列列表:
url查询参数:

  • match [] = :选择器是series_selector。这个参数个数必须大于等于1。
  • start = :开始时间戳。
  • end= :结束时间戳。

您可以使用POST方法和Content-Type:application / x-www-form-urlencoded标头直接在请求正文中对这些参数进行URL编码。 在指定可能违反服务器端URL字符限制的大型或动态数量的系列选择器时,这是非常有用的。
查询结果的数据部分包含一个对象列表,这些对象包含标识每个系列的标签名称/值对。
以下示例返回与选择器up或process_start_time_seconds {job =“prometheus”}匹配的所有时间序列数据:

  1. $ curl -g 'http://localhost:9090/api/v1/series?' -d 'match[]=up' -d 'match[]=process_start_time_seconds{job="prometheus"}'
  2. {
  3. "status" : "success",
  4. "data" : [
  5. {
  6. "__name__" : "up",
  7. "job" : "prometheus",
  8. "instance" : "localhost:9090"
  9. },
  10. {
  11. "__name__" : "up",
  12. "job" : "node",
  13. "instance" : "localhost:9091"
  14. },
  15. {
  16. "__name__" : "process_start_time_seconds",
  17. "job" : "prometheus",
  18. "instance" : "localhost:9090"
  19. }
  20. ]
  21. }

获取标签名称

标签名称的http restful api查询:

  1. GET /api/v1/labels
  2. POST /api/v1/labels

JSON响应的数据部分是字符串标签名称的列表。
例如:

  1. $ curl 'localhost:9090/api/v1/labels'
  2. {
  3. "status": "success",
  4. "data": [
  5. "__name__",
  6. "call",
  7. "code",
  8. "config",
  9. "dialer_name",
  10. "endpoint",
  11. "event",
  12. "goversion",
  13. "handler",
  14. "instance",
  15. "interval",
  16. "job",
  17. "le",
  18. "listener_name",
  19. "name",
  20. "quantile",
  21. "reason",
  22. "role",
  23. "scrape_job",
  24. "slice",
  25. "version"
  26. ]
  27. }

查询标签值

下面这个例子,返回了带有指定标签的标签值列表:

  1. GET /api/v1/label/<label_name>/values

这个返回JSON结果的data部分是带有label_name=job的值列表:

  1. $ curl http://localhost:9090/api/v1/label/job/values
  2. {
  3. "status" : "success",
  4. "data" : [
  5. "node",
  6. "prometheus"
  7. ]
  8. }

表达式查询结果格式

表达式查询结果,在data部分的result部分中,返回下面的数据。\<sample_value\>占位符有数值样本值。JSON不支持特殊浮点值,例如:NaN, Inf-Inf。因此样本值返回结果是字符串,不是原生的数值。

区间向量

区间向量查询返回的result类型是一个matrix矩阵。下面返回的结果是result部分的数据格式:

  1. [
  2. {
  3. "metric": { "<label_name>": "<label_value>", ... },
  4. "values": [ [ <unix_time>, "<sample_value>" ], ... ]
  5. },
  6. ...
  7. ]

瞬时矢量

瞬时向量查询返回result类型是vector。下面是result部分的数据格式

  1. [
  2. {
  3. "metric": { "<label_name>": "<label_value>", ... },
  4. "value": [ <unix_time>, "<sample_value>" ]
  5. },
  6. ...
  7. ]

标量

标量查询返回result类型是scalar。下面是result部分的数据格式:

  1. [ <unix_time>, "<scalar_value>" ]

字符串

字符串查询返回的result类型是string。下面是result部分的数据格式:

  1. [ <unix_time>, "<string_value>" ]

Targets目标

通过以下路径可返回当前状态下prometheus的目标发现的概述。

  1. GET /api/v1/targets

激活目标和删除目标都是响应的一部分。 标签表示重新标记发生后的标签集。 discoveredLabels表示在发生重新标记之前在服务发现期间检索到的未修改标签。

  1. $ curl http://localhost:9090/api/v1/targets
  2. {
  3. "status": "success",
  4. "data": {
  5. "activeTargets": [
  6. {
  7. "discoveredLabels": {
  8. "__address__": "127.0.0.1:9090",
  9. "__metrics_path__": "/metrics",
  10. "__scheme__": "http",
  11. "job": "prometheus"
  12. },
  13. "labels": {
  14. "instance": "127.0.0.1:9090",
  15. "job": "prometheus"
  16. },
  17. "scrapeUrl": "http://127.0.0.1:9090/metrics",
  18. "lastError": "",
  19. "lastScrape": "2017-01-17T15:07:44.723715405+01:00",
  20. "health": "up"
  21. }
  22. ],
  23. "droppedTargets": [
  24. {
  25. "discoveredLabels": {
  26. "__address__": "127.0.0.1:9100",
  27. "__metrics_path__": "/metrics",
  28. "__scheme__": "http",
  29. "job": "node"
  30. },
  31. }
  32. ]
  33. }
  34. }

规则

/ rules API端点返回当前加载的警报和记录规则列表。 此外,它还返回由每个警报规则的Prometheus实例触发的当前活动警报。
由于/ rules端点为新开发端点,因此它没有像API/v1一样的稳定性保证。

  1. GET /api/v1/rules
  1. $ curl http://localhost:9090/api/v1/rules
  2. {
  3. "data": {
  4. "groups": [
  5. {
  6. "rules": [
  7. {
  8. "alerts": [
  9. {
  10. "activeAt": "2018-07-04T20:27:12.60602144+02:00",
  11. "annotations": {
  12. "summary": "High request latency"
  13. },
  14. "labels": {
  15. "alertname": "HighRequestLatency",
  16. "severity": "page"
  17. },
  18. "state": "firing",
  19. "value": 1
  20. }
  21. ],
  22. "annotations": {
  23. "summary": "High request latency"
  24. },
  25. "duration": 600,
  26. "health": "ok",
  27. "labels": {
  28. "severity": "page"
  29. },
  30. "name": "HighRequestLatency",
  31. "query": "job:request_latency_seconds:mean5m{job=\"myjob\"} > 0.5",
  32. "type": "alerting"
  33. },
  34. {
  35. "health": "ok",
  36. "name": "job:http_inprogress_requests:sum",
  37. "query": "sum(http_inprogress_requests) by (job)",
  38. "type": "recording"
  39. }
  40. ],
  41. "file": "/rules.yaml",
  42. "interval": 60,
  43. "name": "example"
  44. }
  45. ]
  46. },
  47. "status": "success"
  48. }

报警

/ alerts端点返回所有活动警报的列表。
由于/ alerts端点为新开发端点,因此它没有像API/v1一样的稳定性保证。

  1. GET /api/v1/alerts
  1. $ curl http://localhost:9090/api/v1/alerts
  2. {
  3. "data": {
  4. "alerts": [
  5. {
  6. "activeAt": "2018-07-04T20:27:12.60602144+02:00",
  7. "annotations": {},
  8. "labels": {
  9. "alertname": "my-alert"
  10. },
  11. "state": "firing",
  12. "value": 1
  13. }
  14. ]
  15. },
  16. "status": "success"
  17. }

查询目标元数据

以下端点返回相关target抓取的metric的元数据。 实验中,将来可能会发生变化。

  1. GET /api/v1/targets/metadata

url查询参数:

  • match_target = :通过标签集匹配目标的标签选择器。 如果缺省则选择所有目标。
  • metric = :用于检索元数据的metric。 如果缺省,则检索所有metric元数据。
  • limit = :要匹配的最大目标数。

查询结果的数据部分包括一个含有metric元数据和目标标签集对象列表。
以下示例从前两个目标返回go_goroutines指标的所有元数据条目,标签为job =“prometheus”。

  1. curl -G http://localhost:9091/api/v1/targets/metadata \
  2. --data-urlencode 'metric=go_goroutines' \
  3. --data-urlencode 'match_target={job="prometheus"}' \
  4. --data-urlencode 'limit=2'
  5. {
  6. "status": "success",
  7. "data": [
  8. {
  9. "target": {
  10. "instance": "127.0.0.1:9090",
  11. "job": "prometheus"
  12. },
  13. "type": "gauge",
  14. "help": "Number of goroutines that currently exist.",
  15. "unit": ""
  16. },
  17. {
  18. "target": {
  19. "instance": "127.0.0.1:9091",
  20. "job": "prometheus"
  21. },
  22. "type": "gauge",
  23. "help": "Number of goroutines that currently exist.",
  24. "unit": ""
  25. }
  26. ]
  27. }

以下示例返回标签instance=“127.0.0.1:9090”的所有目标的所有metric的元数据。

  1. curl -G http://localhost:9091/api/v1/targets/metadata \
  2. --data-urlencode 'match_target={instance="127.0.0.1:9090"}'
  3. {
  4. "status": "success",
  5. "data": [
  6. // ...
  7. {
  8. "target": {
  9. "instance": "127.0.0.1:9090",
  10. "job": "prometheus"
  11. },
  12. "metric": "prometheus_treecache_zookeeper_failures_total",
  13. "type": "counter",
  14. "help": "The total number of ZooKeeper failures.",
  15. "unit": ""
  16. },
  17. {
  18. "target": {
  19. "instance": "127.0.0.1:9090",
  20. "job": "prometheus"
  21. },
  22. "metric": "prometheus_tsdb_reloads_total",
  23. "type": "counter",
  24. "help": "Number of times the database reloaded block data from disk.",
  25. "unit": ""
  26. },
  27. // ...
  28. ]
  29. }

Alertmanagers

以下端点返回Prometheus alertmanager发现的当前状态概述:

  1. GET /api/v1/alertmanagers

激活和丢弃的Alertmanagers都是响应的一部分。

  1. $ curl http://localhost:9090/api/v1/alertmanagers
  2. {
  3. "status": "success",
  4. "data": {
  5. "activeAlertmanagers": [
  6. {
  7. "url": "http://127.0.0.1:9090/api/v1/alerts"
  8. }
  9. ],
  10. "droppedAlertmanagers": [
  11. {
  12. "url": "http://127.0.0.1:9093/api/v1/alerts"
  13. }
  14. ]
  15. }
  16. }

状态

以下状态端点显示当前的Prometheus配置。

配置

以下端点返回当前加载的配置文件:

  1. GET /api/v1/status/config

配置作为转储的YAML文件返回。 由于YAML库的限制,不包括YAML注释。

  1. $ curl http://localhost:9090/api/v1/status/config
  2. {
  3. "status": "success",
  4. "data": {
  5. "yaml": "<content of the loaded config file in YAML>",
  6. }
  7. }

标志

以下端点返回Prometheus配置的标志值:

  1. GET /api/v1/status/flags

所有值都以“字符串”的形式出现。

  1. $ curl http://localhost:9090/api/v1/status/flags
  2. {
  3. "status": "success",
  4. "data": {
  5. "alertmanager.notification-queue-capacity": "10000",
  6. "alertmanager.timeout": "10s",
  7. "log.level": "info",
  8. "query.lookback-delta": "5m",
  9. "query.max-concurrency": "20",
  10. ...
  11. }
  12. }

TSDB管理API (公测期间暂不开发)

这些是为高级用户公开数据库功能的API。 除非设置了—web.enable-admin-api,否则不会启用这些API。
我们还公开了一个gRPC API,其定义可以在这里找到。 实验中,将来可能会发生变化。

快照

快照会将所有当前数据的快照创建到TSDB数据目录下的snapshots / - 中,并将该目录作为响应返回。 它可以选择跳过仅存在于head block中但尚未压缩到磁盘的快照数据。

  1. POST /api/v1/admin/tsdb/snapshot?skip_head=<bool>
  2. PUT /api/v1/admin/tsdb/snapshot?skip_head=<bool>
  1. $ curl -XPOST http://localhost:9090/api/v1/admin/tsdb/snapshot
  2. {
  3. "status": "success",
  4. "data": {
  5. "name": "20171210T211224Z-2be650b6d019eb54"
  6. }
  7. }

快照现在位于 / snapshots / 20171210T211224Z-2be650b6d019eb54

删除系列

DeleteSeries删除时间范围内所选系列的数据。 实际数据仍然存在于磁盘上,并在将来的压缩中清除,或者可以通过点击Clean Tombstones端点来明确清理。
如果成功,则返回204。

  1. POST /api/v1/admin/tsdb/delete_series
  2. PUT /api/v1/admin/tsdb/delete_series

网址查询参数:

  • match [] = :选择要删除的系列的重复标签匹配器参数。 必须至少提供一个match []参数。
  • start = :开始时间戳。 可选,默认为最短可能时间。
  • end= :结束时间戳。 可选,默认为最长可能时间。

开始和结束时间缺省将清除数据库中匹配系列的所有数据。
例:

  1. $ curl -X POST \
  2. -g 'http://localhost:9090/api/v1/admin/tsdb/delete_series?match[]=up&match[]=process_start_time_seconds{job="prometheus"}'

Clean Tombstones

Clean Tombstones从磁盘中删除已删除的数据并清理现有的逻辑。 clean Tombstones删除系列后使用以释放空间。
如果成功,则返回204。

  1. POST /api/v1/admin/tsdb/clean_tombstones
  2. PUT /api/v1/admin/tsdb/clean_tombstones

此端点不需要参数和正文。

  1. $ curl -XPOST http://localhost:9090/api/v1/admin/tsdb/clean_tombstones
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

评论

-----