Prometheus Http API访问

半兽人 发表于: 2019-07-23   最后更新时间: 2020-10-20 17:30:19  
{{totalSubscript}} 订阅, 4,373 游览

HTTP API

在Prometheus服务上/api/v1版本api是稳定版。

格式概述

这个API返回是JSON格式。每个请求成功的返回值都是以2xx开头的编码。

到达API处理的无效请求,返回一个JSON错误对象,并返回下面的错误码:

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

在请求到达API之前,其他非2xx的错误码可能会被返回。

JSON返回格式如下所示:

{
"status": "success" | "error",
"data": <data>,

// 如果status是"error", 这个数据字段还会包括下面的数据
"errorType": "<string>",
"error": "<string>"
}

输入时间戳可以被RFC3339格式或者Unix时间戳提供。输出时间戳以Unix时间戳的方式呈现。

查询参数名称可以用[]中括号重复次数。
<series_selector>占位符提供像http_requests_total或者http_requests_total{method=~"^GET|POST$"}的Prometheus时间序列选择器,并需要在URL中编码传输。

<duration>占位符涉及到[0-9]-[smhdwy]。例如:5m表示5分钟的持续时间。

表达式查询

查询语言表达式可以在瞬时向量或者范围向量中执行。

Instant queries(即时查询)

瞬时向量的http restful api查询:

GET /api/v1/query

URL查询参数:

  • query=<string>: Prometheus表达式查询字符串。
  • time=<rfc3339 | uninx_timestamp>: 执行时间戳,可选项。
  • timeout=<duration>: 执行超时时间设置,默认由-query.timeout标志设置

如果time缺省,则用当前服务器时间表示执行时刻。

这个查询结果的data部分有下面格式:

{
 "resultType": "matrix" | "vector" | "scalar" | "string",
 "result": <value>
}

是一个查询结果数据,依赖于这个resultType格式, 不同的结果类型,则会有不同的结果数据格式。见表达式查询结果格式

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

$ 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

URL查询参数

  • query=<string>: Prometheus表达式查询字符串。
  • start=<rfc3339 | unix_timestamp>: 开始时间戳。
  • end=<rfc3339 | unix_timestamp>: 结束时间戳。
  • step=<duration>: 查询时间步长,范围时间内每step秒执行一次。

下面查询结果格式的data部分:

{
    "resultType": "matrix",
    "result": <value>
}

对于<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" ]
            ]
         }
      ]
   }
}

查询元数据

通过标签匹配器找到度量指标列表

下面例子返回了度量指标列表 且不返回时间序列数据值。

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
   }
}

表达式查询结果格式

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

范围向量

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

[
  {
    "metric": { "<label_name>": "<label_value>", ... },
    "values": [ [ <unix_time>, "<sample_value>" ], ... ]
  },
  ...
]

瞬时向量

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

[
  {
    "metric": { "<label_name>": "<label_value>", ... },
    "value": [ <unix_time>, "<sample_value>" ]
  },
  ...
]

Scalars标量

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

[ , "" ]

字符串

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

[ , "" ]

Targets目标

这个API是实验性的,暂不翻译。

Alertmanagers

这个API也是实验性的,暂不翻译

更新于 2020-10-20
在线,1小时前登录

查看Prometheus更多相关的文章或提一个关于Prometheus的问题,也可以与我们一起分享文章