HTTP API

当前稳定的 HTTP API 在 Prometheus 服务端上位于 /api/v1。任何非破坏性的新增功能都将在此端点下添加。

格式概述

API 响应格式是 JSON。每个成功的 API 请求都返回 2xx 状态码。

到达 API 处理器但无效的请求返回 JSON 错误对象和以下 HTTP 响应码之一:

  • 400 Bad Request (参数缺失或不正确时)。
  • 422 Unprocessable Entity (无法执行表达式时) (RFC4918)。
  • 503 Service Unavailable (查询超时或中断时)。

在到达 API 端点之前发生的错误可能会返回其他非 2xx 状态码。

如果存在不影响请求执行的错误,可能会返回一个警告数组。对于可能或可能不是误报的潜在查询问题,可能会返回一个额外的 info 级别注解数组。所有成功收集的数据都将返回在数据字段中。

JSON 响应的包络格式如下:

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

  // Only set if status is "error". The data field may still hold
  // additional data.
  "errorType": "<string>",
  "error": "<string>",

  // Only set if there were warnings while executing the request.
  // There will still be data in the data field.
  "warnings": ["<string>"],
  // Only set if there were info-level annotations while executing the request.
  "infos": ["<string>"]
}

通用占位符定义如下:

  • <rfc3339 | unix_timestamp>: 输入时间戳可以提供 RFC3339 格式或 Unix 时间戳(以秒为单位,带有可选的小数位以表示亚秒精度)。输出时间戳总是以 Unix 时间戳(以秒为单位)表示。
  • <series_selector>: Prometheus 时间序列选择器,如 http_requests_totalhttp_requests_total{method=~"(GET|POST)"},需要进行 URL 编码。
  • <duration>: Prometheus 浮点字面量中使用时间单位的子集。例如,5m 表示 5 分钟的持续时间。
  • <bool>: 布尔值 (字符串 truefalse)。

注意:可能重复的查询参数名称以 [] 结尾。

表达式查询

查询语言表达式可以在单个即时或一段时间范围内求值。以下部分描述了每种表达式查询的 API 端点。

即时查询

以下端点在单个时间点求值即时查询:

GET /api/v1/query
POST /api/v1/query

URL 查询参数

  • query=<string>: Prometheus 表达式查询字符串。
  • time=<rfc3339 | unix_timestamp>: 求值时间戳。可选。
  • timeout=<duration>: 求值超时时间。可选。默认为 -query.timeout 标志的值,且受其限制。
  • limit=<number>: 返回时间序列的最大数量。不影响标量或字符串,但会截断矩阵和向量的时间序列数量。可选。0 表示禁用。

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

您可以通过使用 POST 方法和 Content-Type: application/x-www-form-urlencoded 头部,直接在请求体中对这些参数进行 URL 编码。这对于指定可能超出服务器端 URL 字符限制的大型查询非常有用。

查询结果的 data 部分具有以下格式:

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

<value> 指的是查询结果数据,其格式根据 resultType 的不同而变化。请参阅表达式查询结果格式

以下示例在时间 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>: 查询分辨率步长,采用 duration 格式或秒数的浮点数。
  • timeout=<duration>: 求值超时时间。可选。默认为 -query.timeout 标志的值,且受其限制。
  • limit=<number>: 返回时间序列的最大数量。可选。0 表示禁用。

您可以通过使用 POST 方法和 Content-Type: application/x-www-form-urlencoded 头部,直接在请求体中对这些参数进行 URL 编码。这对于指定可能超出服务器端 URL 字符限制的大型查询非常有用。

查询结果的 data 部分具有以下格式:

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

有关 <value> 占位符的格式,请参阅范围向量结果格式

以下示例在 30 秒范围内求值表达式 up,查询分辨率为 15 秒。

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

格式化查询表达式

以下端点以美化的方式格式化 PromQL 表达式:

GET /api/v1/format_query
POST /api/v1/format_query

URL 查询参数

  • query=<string>: Prometheus 表达式查询字符串。

您可以通过使用 POST 方法和 Content-Type: application/x-www-form-urlencoded 头部,直接在请求体中对这些参数进行 URL 编码。这对于指定可能超出服务器端 URL 字符限制的大型查询非常有用。

查询结果的 data 部分是一个包含格式化查询表达式的字符串。请注意,格式化字符串中会删除所有注释。

以下示例格式化表达式 foo/bar

curl 'http://localhost:9090/api/v1/format_query?query=foo/bar'
{
   "status" : "success",
   "data" : "foo / bar"
}

将 PromQL 表达式解析为抽象语法树 (AST)

此端点是实验性的,将来可能会更改。它目前仅供 Prometheus 自己的 Web UI 使用,端点名称和返回的具体格式可能会因 Prometheus 版本而异。如果 UI 不再需要它,也可能会再次删除。

以下端点解析 PromQL 表达式并将其作为 JSON 格式的 AST (抽象语法树) 表示返回:

GET /api/v1/parse_query
POST /api/v1/parse_query

URL 查询参数

  • query=<string>: Prometheus 表达式查询字符串。

您可以通过使用 POST 方法和 Content-Type: application/x-www-form-urlencoded 头部,直接在请求体中对这些参数进行 URL 编码。这对于指定可能超出服务器端 URL 字符限制的大型查询非常有用。

查询结果的 data 部分是一个字符串,包含解析后的查询表达式的 AST。

以下示例解析表达式 foo/bar

curl 'http://localhost:9090/api/v1/parse_query?query=foo/bar'
{
   "data" : {
      "bool" : false,
      "lhs" : {
         "matchers" : [
            {
               "name" : "__name__",
               "type" : "=",
               "value" : "foo"
            }
         ],
         "name" : "foo",
         "offset" : 0,
         "startOrEnd" : null,
         "timestamp" : null,
         "type" : "vectorSelector"
      },
      "matching" : {
         "card" : "one-to-one",
         "include" : [],
         "labels" : [],
         "on" : false
      },
      "op" : "/",
      "rhs" : {
         "matchers" : [
            {
               "name" : "__name__",
               "type" : "=",
               "value" : "bar"
            }
         ],
         "name" : "bar",
         "offset" : 0,
         "startOrEnd" : null,
         "timestamp" : null,
         "type" : "vectorSelector"
      },
      "type" : "binaryExpr"
   },
   "status" : "success"
}

查询元数据

Prometheus 提供了一组 API 端点来查询时间序列及其标签的元数据。

注意:这些 API 端点可能会返回在选定时间范围内没有样本的时间序列的元数据,以及/或通过删除 API 端点标记为已删除样本的时间序列的元数据。额外返回的时间序列元数据的具体程度是实现细节,将来可能会更改。

按标签匹配器查找时间序列

以下端点返回与特定标签集匹配的时间序列列表。

GET /api/v1/series
POST /api/v1/series

URL 查询参数

  • match[]=<series_selector>: 重复的时间序列选择器参数,选择要返回的时间序列。必须至少提供一个 match[] 参数。
  • start=<rfc3339 | unix_timestamp>: 起始时间戳。
  • end=<rfc3339 | unix_timestamp>: 结束时间戳。
  • limit=<number>: 返回时间序列的最大数量。可选。0 表示禁用。

您可以通过使用 POST 方法和 Content-Type: application/x-www-form-urlencoded 头部,直接在请求体中对这些参数进行 URL 编码。这对于指定可能超出服务器端 URL 字符限制的大量或动态时间序列选择器非常有用。

查询结果的 data 部分包含一个对象列表,这些对象包含标识每个时间序列的标签名称/值对。

以下示例返回与选择器 upprocess_start_time_seconds{job="prometheus"} 匹配的所有时间序列:

curl -g 'http://localhost:9090/api/v1/series?' --data-urlencode 'match[]=up' --data-urlencode '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/labels
POST /api/v1/labels

URL 查询参数

  • start=<rfc3339 | unix_timestamp>: 起始时间戳。可选。
  • end=<rfc3339 | unix_timestamp>: 结束时间戳。可选。
  • match[]=<series_selector>: 重复的时间序列选择器参数,选择要从中读取标签名称的时间序列。可选。
  • limit=<number>: 返回时间序列的最大数量。可选。0 表示禁用。

JSON 响应的 data 部分是一个字符串标签名称列表。

示例:

curl 'localhost:9090/api/v1/labels'
{
    "status": "success",
    "data": [
        "__name__",
        "call",
        "code",
        "config",
        "dialer_name",
        "endpoint",
        "event",
        "goversion",
        "handler",
        "instance",
        "interval",
        "job",
        "le",
        "listener_name",
        "name",
        "quantile",
        "reason",
        "role",
        "scrape_job",
        "slice",
        "version"
    ]
}

查询标签值

以下端点返回给定标签名称的标签值列表:

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

URL 查询参数

  • start=<rfc3339 | unix_timestamp>: 起始时间戳。可选。
  • end=<rfc3339 | unix_timestamp>: 结束时间戳。可选。
  • match[]=<series_selector>: 重复的时间序列选择器参数,选择要从中读取标签值的时间序列。可选。
  • limit=<number>: 返回时间序列的最大数量。可选。0 表示禁用。

JSON 响应的 data 部分是一个字符串标签值列表。

此示例查询 http_status_code 标签的所有标签值:

curl http://localhost:9090/api/v1/label/http_status_code/values
{
   "status" : "success",
   "data" : [
      "200",
      "504"
   ]
}

标签名称可以选择使用 Values Escaping 方法进行编码,如果名称包含 / 字符,则这是必需的。要以这种方式编码名称:

  • 在标签前加上 U__
  • 字母、数字和冒号保持原样。
  • 将单个下划线转换为双下划线。
  • 对于所有其他字符,使用 UTF-8 码点作为十六进制整数,用下划线包围。因此 变为 _20_. 变为 _2e_

有关文本转义的更多信息,请参阅原始的 UTF-8 提案文档

此示例查询 http.status_code 标签的所有标签值:

curl http://localhost:9090/api/v1/label/U__http_2e_status_code/values
{
   "status" : "success",
   "data" : [
      "200",
      "404"
   ]
}

查询 Exemplars

这是实验性的,将来可能会更改。以下端点返回特定时间范围内有效 PromQL 查询的 exemplar 列表:

GET /api/v1/query_exemplars
POST /api/v1/query_exemplars

URL 查询参数

  • query=<string>: Prometheus 表达式查询字符串。
  • start=<rfc3339 | unix_timestamp>: 起始时间戳。
  • end=<rfc3339 | unix_timestamp>: 结束时间戳。
curl -g 'http://localhost:9090/api/v1/query_exemplars?query=test_exemplar_metric_total&start=2020-09-14T15:22:25.479Z&end=2020-09-14T15:23:25.479Z'
{
    "status": "success",
    "data": [
        {
            "seriesLabels": {
                "__name__": "test_exemplar_metric_total",
                "instance": "localhost:8090",
                "job": "prometheus",
                "service": "bar"
            },
            "exemplars": [
                {
                    "labels": {
                        "trace_id": "EpTxMJ40fUus7aGY"
                    },
                    "value": "6",
                    "timestamp": 1600096945.479
                }
            ]
        },
        {
            "seriesLabels": {
                "__name__": "test_exemplar_metric_total",
                "instance": "localhost:8090",
                "job": "prometheus",
                "service": "foo"
            },
            "exemplars": [
                {
                    "labels": {
                        "trace_id": "Olp9XHlq763ccsfa"
                    },
                    "value": "19",
                    "timestamp": 1600096955.479
                },
                {
                    "labels": {
                        "trace_id": "hCtjygkIHwAN9vs4"
                    },
                    "value": "20",
                    "timestamp": 1600096965.489
                }
            ]
        }
    ]
}

表达式查询结果格式

表达式查询可能在 data 部分的 result 属性中返回以下响应值。<sample_value> 占位符是数字样本值。JSON 不支持特殊的浮点值,如 NaNInf-Inf,因此样本值作为带引号的 JSON 字符串传输,而不是原始数字。

仅当响应中存在实验性的原生直方图时,才会显示键 "histogram""histograms"。其占位符 <histogram> 在下面的单独部分中详细解释。

范围向量

范围向量作为结果类型 matrix 返回。相应的 result 属性具有以下格式:

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

每个时间序列可以有 "values" 键,或 "histograms" 键,或两者都有。对于给定的时间戳,只会有一个浮点或直方图类型的样本。

时间序列按 metric 排序返回。sortsort_by_label 等函数对范围向量无效。

即时向量

即时向量作为结果类型 vector 返回。相应的 result 属性具有以下格式:

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

每个时间序列可以有 "value" 键,或 "histogram" 键,但不能两者都有。

除非使用 sortsort_by_label 等函数,否则不保证时间序列以任何特定顺序返回。

标量

标量结果作为结果类型 scalar 返回。相应的 result 属性具有以下格式:

[ <unix_time>, "<scalar_value>" ]

字符串

字符串结果作为结果类型 string 返回。相应的 result 属性具有以下格式:

[ <unix_time>, "<string_value>" ]

原生直方图

上面使用的 <histogram> 占位符格式如下。

请注意,原生直方图是一个实验性功能,以下格式可能仍然会改变。

{
  "count": "<count_of_observations>",
  "sum": "<sum_of_observations>",
  "buckets": [ [ <boundary_rule>, "<left_boundary>", "<right_boundary>", "<count_in_bucket>" ], ... ]
}

<boundary_rule> 占位符是一个介于 0 和 3 之间的整数,具有以下含义:

  • 0:“左开”(左边界不包含,右边界包含)
  • 1:“右开”(左边界包含,右边界不包含)
  • 2:“两边都开”(两边界都不包含)
  • 3:“两边都闭合”(两边界都包含)

请注意,在当前实现的 bucket 模式中,正向 bucket 是“左开”,负向 bucket 是“右开”,零 bucket(左边界为负,右边界为正)是“两边都闭合”。

目标

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

GET /api/v1/targets

活动目标和丢弃目标都默认包含在响应中。丢弃目标受 keep_dropped_targets 限制(如果设置)。labels 表示重新贴标后(relabeling 后)的标签集。discoveredLabels 表示服务发现期间检索到的未修改的标签(relabeling 前)。

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"
        },
        "scrapePool": "node"
      }
    ]
  }
}

state 查询参数允许调用者按活动目标或丢弃目标进行过滤(例如,state=activestate=droppedstate=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": []
  }
}

scrapePool 查询参数允许调用者按 scrape pool 名称进行过滤。

curl 'http://localhost:9090/api/v1/targets?scrapePool=node_exporter'

  "status": "success",
  "data": {
    "activeTargets": [
      {
        "discoveredLabels": {
          "__address__": "127.0.0.1:9091",
          "__metrics_path__": "/metrics",
          "__scheme__": "http",
          "job": "node_exporter"
        },
        "labels": {
          "instance": "127.0.0.1:9091",
          "job": "node_exporter"
        },
        "scrapePool": "node_exporter",
        "scrapeUrl": "http://127.0.0.1:9091/metrics",
        "globalUrl": "http://example-prometheus:9091/metrics",
        "lastError": "",
        "lastScrape": "2017-01-17T15:07:44.723715405+01:00",
        "lastScrapeDuration": 50688943,
        "health": "up"
      }
    ],
    "droppedTargets": []
  }
}

规则

/rules API 端点返回当前加载的告警规则和记录规则列表。此外,它还返回 Prometheus 实例触发的每条告警规则的当前活动告警。

由于 /rules 端点相对较新,它不具备与整个 API v1 相同的稳定性保证。

GET /api/v1/rules

URL 查询参数

  • type=alert|record: 仅返回告警规则(例如 type=alert)或记录规则(例如 type=record)。当参数缺失或为空时,不进行过滤。
  • rule_name[]=<string>: 仅返回具有给定规则名称的规则。如果参数重复,则返回具有任何提供的名称的规则。如果我们已过滤掉组中的所有规则,则不返回该组。当参数缺失或为空时,不进行过滤。
  • rule_group[]=<string>: 仅返回具有给定规则组名称的规则。如果参数重复,则返回具有任何提供的规则组名称的规则。当参数缺失或为空时,不进行过滤。
  • file[]=<string>: 仅返回具有给定文件路径的规则。如果参数重复,则返回具有任何提供的文件路径的规则。当参数缺失或为空时,不进行过滤。
  • exclude_alerts=<bool>: 仅返回规则,不返回活动告警。
  • match[]=<label_selector>: 仅返回配置标签满足标签选择器的规则。如果参数重复,则返回与任何一组标签选择器匹配的规则。请注意,匹配是针对每条规则定义中的标签,而不是模板展开后(对于告警规则)的值。可选。
  • group_limit=<number>: group_limit 参数允许您指定单个响应中返回的规则组数量的限制。如果规则组的总数超过指定的 group_limit 值,响应将包含一个 groupNextToken 属性。您可以在后续请求中使用此 groupNextToken 属性的值作为 group_next_token 参数,以便分页遍历剩余的规则组。groupNextToken 属性不会出现在最终响应中,这表明您已检索到所有可用的规则组。请注意,如果在分页过程中修改规则组,响应的一致性无法保证。
  • group_next_token: 当设置了 group_limit 属性时,在先前的请求中返回的分页令牌。分页令牌用于迭代分页遍历大量规则组。要使用 group_next_token 参数,还需要存在 group_limit 参数。如果在您分页遍历规则组时删除了与下一个令牌相符的规则组,将返回状态码为 400 的响应。
curl http://localhost:9090/api/v1/rules
{
    "data": {
        "groups": [
            {
                "rules": [
                    {
                        "alerts": [
                            {
                                "activeAt": "2018-07-04T20:27:12.60602144+02:00",
                                "annotations": {
                                    "summary": "High request latency"
                                },
                                "labels": {
                                    "alertname": "HighRequestLatency",
                                    "severity": "page"
                                },
                                "state": "firing",
                                "value": "1e+00"
                            }
                        ],
                        "annotations": {
                            "summary": "High request latency"
                        },
                        "duration": 600,
                        "health": "ok",
                        "labels": {
                            "severity": "page"
                        },
                        "name": "HighRequestLatency",
                        "query": "job:request_latency_seconds:mean5m{job=\"myjob\"} > 0.5",
                        "type": "alerting"
                    },
                    {
                        "health": "ok",
                        "name": "job:http_inprogress_requests:sum",
                        "query": "sum by (job) (http_inprogress_requests)",
                        "type": "recording"
                    }
                ],
                "file": "/rules.yaml",
                "interval": 60,
                "limit": 0,
                "name": "example"
            }
        ]
    },
    "status": "success"
}

告警

/alerts 端点返回所有活动告警的列表。

由于 /alerts 端点相对较新,它不具备与整个 API v1 相同的稳定性保证。

GET /api/v1/alerts
curl http://localhost:9090/api/v1/alerts
{
    "data": {
        "alerts": [
            {
                "activeAt": "2018-07-04T20:27:12.60602144+02:00",
                "annotations": {},
                "labels": {
                    "alertname": "my-alert"
                },
                "state": "firing",
                "value": "1e+00"
            }
        ]
    },
    "status": "success"
}

查询目标元数据

以下端点返回当前从目标 scrape 的指标元数据。这是实验性的,将来可能会更改。

GET /api/v1/targets/metadata

URL 查询参数

  • match_target=<label_selectors>: 通过标签集匹配目标的标签选择器。如果留空,则选择所有目标。
  • metric=<string>: 要检索元数据的指标名称。如果留空,则检索所有指标元数据。
  • limit=<number>: 最大匹配目标数。

查询结果的 data 部分包含一个对象列表,这些对象包含指标元数据和目标标签集。

以下示例返回标签 job="prometheus" 的前两个目标的 go_goroutines 指标的所有元数据条目。

curl -G http://localhost:9091/api/v1/targets/metadata \
    --data-urlencode 'metric=go_goroutines' \
    --data-urlencode 'match_target={job="prometheus"}' \
    --data-urlencode 'limit=2'
{
  "status": "success",
  "data": [
    {
      "target": {
        "instance": "127.0.0.1:9090",
        "job": "prometheus"
      },
      "type": "gauge",
      "help": "Number of goroutines that currently exist.",
      "unit": ""
    },
    {
      "target": {
        "instance": "127.0.0.1:9091",
        "job": "prometheus"
      },
      "type": "gauge",
      "help": "Number of goroutines that currently exist.",
      "unit": ""
    }
  ]
}

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

curl -G http://localhost:9091/api/v1/targets/metadata \
    --data-urlencode 'match_target={instance="127.0.0.1:9090"}'
{
  "status": "success",
  "data": [
    // ...
    {
      "target": {
        "instance": "127.0.0.1:9090",
        "job": "prometheus"
      },
      "metric": "prometheus_treecache_zookeeper_failures_total",
      "type": "counter",
      "help": "The total number of ZooKeeper failures.",
      "unit": ""
    },
    {
      "target": {
        "instance": "127.0.0.1:9090",
        "job": "prometheus"
      },
      "metric": "prometheus_tsdb_reloads_total",
      "type": "counter",
      "help": "Number of times the database reloaded block data from disk.",
      "unit": ""
    },
    // ...
  ]
}

查询指标元数据

它返回当前从目标 scrape 的指标元数据。但是,它不提供任何目标信息。这被认为是实验性的,将来可能会更改。

GET /api/v1/metadata

URL 查询参数

  • limit=<number>: 返回的最大指标数。
  • limit_per_metric=<number>: 每个指标返回的最大元数据条目数。
  • metric=<string>: 要过滤元数据的指标名称。如果留空,则检索所有指标元数据。

查询结果的 data 部分包含一个对象,其中每个键是指标名称,每个值是唯一元数据对象的列表,这些元数据对象在所有目标中对该指标名称暴露。

以下示例返回两个指标。请注意,指标 http_requests_total 在列表中有多个对象。至少有一个目标的 HELP 值与其他值不匹配。

curl -G http://localhost:9090/api/v1/metadata?limit=2
{
  "status": "success",
  "data": {
    "cortex_ring_tokens": [
      {
        "type": "gauge",
        "help": "Number of tokens in the ring",
        "unit": ""
      }
    ],
    "http_requests_total": [
      {
        "type": "counter",
        "help": "Number of HTTP requests",
        "unit": ""
      },
      {
        "type": "counter",
        "help": "Amount of HTTP requests",
        "unit": ""
      }
    ]
  }
}

以下示例仅返回每个指标的一个元数据条目。

curl -G http://localhost:9090/api/v1/metadata?limit_per_metric=1
{
  "status": "success",
  "data": {
    "cortex_ring_tokens": [
      {
        "type": "gauge",
        "help": "Number of tokens in the ring",
        "unit": ""
      }
    ],
    "http_requests_total": [
      {
        "type": "counter",
        "help": "Number of HTTP requests",
        "unit": ""
      }
    ]
  }
}

以下示例仅返回指标 http_requests_total 的元数据。

curl -G http://localhost:9090/api/v1/metadata?metric=http_requests_total
{
  "status": "success",
  "data": {
    "http_requests_total": [
      {
        "type": "counter",
        "help": "Number of HTTP requests",
        "unit": ""
      },
      {
        "type": "counter",
        "help": "Amount of HTTP requests",
        "unit": ""
      }
    ]
  }
}

Alertmanagers

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

GET /api/v1/alertmanagers

活动和丢弃的 Alertmanagers 都包含在响应中。

curl http://localhost:9090/api/v1/alertmanagers
{
  "status": "success",
  "data": {
    "activeAlertmanagers": [
      {
        "url": "http://127.0.0.1:9090/api/v1/alerts"
      }
    ],
    "droppedAlertmanagers": [
      {
        "url": "http://127.0.0.1:9093/api/v1/alerts"
      }
    ]
  }
}

状态

以下状态端点暴露当前的 Prometheus 配置。

配置

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

GET /api/v1/status/config

配置以 dump 的 YAML 文件形式返回。由于 YAML 库的限制,不包含 YAML 注释。

curl http://localhost:9090/api/v1/status/config
{
  "status": "success",
  "data": {
    "yaml": "<content of the loaded config file in YAML>",
  }
}

标志

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

GET /api/v1/status/flags

所有值的结果类型都是 string

curl http://localhost:9090/api/v1/status/flags
{
  "status": "success",
  "data": {
    "alertmanager.notification-queue-capacity": "10000",
    "alertmanager.timeout": "10s",
    "log.level": "info",
    "query.lookback-delta": "5m",
    "query.max-concurrency": "20",
    ...
  }
}

v2.2 新增

运行时信息

以下端点返回关于 Prometheus 服务端的各种运行时信息属性:

GET /api/v1/status/runtimeinfo

返回的值类型不同,取决于运行时属性的性质。

curl http://localhost:9090/api/v1/status/runtimeinfo
{
  "status": "success",
  "data": {
    "startTime": "2019-11-02T17:23:59.301361365+01:00",
    "CWD": "/",
    "hostname" : "DESKTOP-717H17Q",
    "serverTime": "2025-01-05T18:27:33Z",
    "reloadConfigSuccess": true,
    "lastConfigTime": "2019-11-02T17:23:59+01:00",
    "timeSeriesCount": 873,
    "corruptionCount": 0,
    "goroutineCount": 48,
    "GOMAXPROCS": 4,
    "GOGC": "",
    "GODEBUG": "",
    "storageRetention": "15d"
  }
}
注意:返回的具体运行时属性可能在 Prometheus 版本之间发生变化,恕不另行通知。

v2.14 新增

构建信息

以下端点返回关于 Prometheus 服务端的各种构建信息属性:

GET /api/v1/status/buildinfo

所有值的结果类型都是 string

curl http://localhost:9090/api/v1/status/buildinfo
{
  "status": "success",
  "data": {
    "version": "2.13.1",
    "revision": "cb7cbad5f9a2823a622aaa668833ca04f50a0ea7",
    "branch": "master",
    "buildUser": "julius@desktop",
    "buildDate": "20191102-16:19:59",
    "goVersion": "go1.13.1"
  }
}
注意:返回的具体构建属性可能在 Prometheus 版本之间发生变化,恕不另行通知。

v2.14 新增

TSDB 统计信息

以下端点返回关于 Prometheus TSDB 的各种基数统计信息:

GET /api/v1/status/tsdb

URL 查询参数

  • limit=<number>: 限制每组统计信息返回的项目数量。默认返回 10 个项目。

查询结果的 data 部分包含:

  • headStats: 提供关于 TSDB head block 的以下数据:
    • numSeries: 时间序列的数量。
    • chunkCount: chunk 的数量。
    • minTime: 当前最小时间戳,单位毫秒。
    • maxTime: 当前最大时间戳,单位毫秒。
  • seriesCountByMetricName: 提供指标名称及其时间序列数量的列表。
  • labelValueCountByLabelName: 提供标签名称及其值的数量的列表。
  • memoryInBytesByLabelName 提供标签名称及其使用的内存量(字节)的列表。内存使用量是通过将给定标签名称的所有值的长度相加计算得出的。
  • seriesCountByLabelPair 提供标签值对及其时间序列数量的列表。
curl http://localhost:9090/api/v1/status/tsdb
{
  "status": "success",
  "data": {
    "headStats": {
      "numSeries": 508,
      "chunkCount": 937,
      "minTime": 1591516800000,
      "maxTime": 1598896800143,
    },
    "seriesCountByMetricName": [
      {
        "name": "net_conntrack_dialer_conn_failed_total",
        "value": 20
      },
      {
        "name": "prometheus_http_request_duration_seconds_bucket",
        "value": 20
      }
    ],
    "labelValueCountByLabelName": [
      {
        "name": "__name__",
        "value": 211
      },
      {
        "name": "event",
        "value": 3
      }
    ],
    "memoryInBytesByLabelName": [
      {
        "name": "__name__",
        "value": 8266
      },
      {
        "name": "instance",
        "value": 28
      }
    ],
    "seriesCountByLabelValuePair": [
      {
        "name": "job=prometheus",
        "value": 425
      },
      {
        "name": "instance=localhost:9090",
        "value": 425
      }
    ]
  }
}

v2.15 新增

WAL 回放统计信息

以下端点返回关于 WAL 回放的信息:

GET /api/v1/status/walreplay
  • read: 已回放的 segment 数量。
  • total: 需要回放的总 segment 数量。
  • progress: 回放进度 (0 - 100%)。
  • state: 回放状态。可能的状态:
    • waiting: 等待回放开始。
    • in progress: 回放进行中。
    • done: 回放已完成。
curl http://localhost:9090/api/v1/status/walreplay
{
  "status": "success",
  "data": {
    "min": 2,
    "max": 5,
    "current": 40,
    "state": "in progress"
  }
}
注意:此端点在服务器被标记为 ready 之前可用,并实时更新以方便监控 WAL 回放进度。

v2.28 新增

TSDB 管理 API

这些 API 为高级用户提供数据库功能。除非设置了 --web.enable-admin-api,否则这些 API 不会启用。

快照

Snapshot 会将所有当前数据快照到 TSDB 数据目录下的 snapshots/<datetime>-<rand> 目录,并返回该目录作为响应。它会选择性地跳过只存在于 head block 且尚未压缩到磁盘的数据快照。

POST /api/v1/admin/tsdb/snapshot
PUT /api/v1/admin/tsdb/snapshot

URL 查询参数

  • skip_head=<bool>: 跳过 head block 中的数据。可选。
curl -XPOST http://localhost:9090/api/v1/admin/tsdb/snapshot
{
  "status": "success",
  "data": {
    "name": "20171210T211224Z-2be650b6d019eb54"
  }
}

快照现在位于 <data-dir>/snapshots/20171210T211224Z-2be650b6d019eb54

v2.1 新增,v2.9 起支持 PUT

删除时间序列

DeleteSeries 删除时间范围内选定时间序列的数据。实际数据仍存在于磁盘上,并在将来的压缩中清理,或者可以通过调用清理墓碑端点进行显式清理。

如果成功,返回 204

POST /api/v1/admin/tsdb/delete_series
PUT /api/v1/admin/tsdb/delete_series

URL 查询参数

  • match[]=<series_selector>: 重复的标签匹配器参数,选择要删除的时间序列。必须至少提供一个 match[] 参数。
  • start=<rfc3339 | unix_timestamp>: 起始时间戳。可选,默认为可能的最小时间。
  • end=<rfc3339 | unix_timestamp>: 结束时间戳。可选,默认为可能的最大时间。

不提及开始时间和结束时间将清除数据库中匹配时间序列的所有数据。

示例

curl -X POST \
  -g 'http://localhost:9090/api/v1/admin/tsdb/delete_series?match[]=up&match[]=process_start_time_seconds{job="prometheus"}'
注意:此端点将时间序列中的样本标记为已删除,但不一定会阻止受影响时间范围内的相关时间序列元数据在元数据查询中仍然返回(即使在清理墓碑之后)。元数据删除的具体程度是实现细节,将来可能会更改。

v2.1 新增,v2.9 起支持 PUT

清理墓碑

CleanTombstones 从磁盘中删除已删除的数据并清理现有墓碑。这可以在删除时间序列后使用以释放空间。

如果成功,返回 204

POST /api/v1/admin/tsdb/clean_tombstones
PUT /api/v1/admin/tsdb/clean_tombstones

此端点不接受参数或请求体。

curl -XPOST http://localhost:9090/api/v1/admin/tsdb/clean_tombstones

v2.1 新增,v2.9 起支持 PUT

远程写入接收器

Prometheus 可以配置为 Prometheus 远程写入协议的接收器。这不被认为是高效的样本摄入方式。请谨慎用于特定的低流量用例。它不适合替代通过 scrape 进行的摄入,并将 Prometheus 变成基于推送的指标收集系统。

通过设置 --web.enable-remote-write-receiver 来启用远程写入接收器。启用后,远程写入接收器端点是 /api/v1/write。更多详细信息请参阅此处

v2.33 新增

OTLP 接收器

Prometheus 可以配置为 OTLP 指标协议的接收器。这不被认为是高效的样本摄入方式。请谨慎用于特定的低流量用例。它不适合替代通过 scrape 进行的摄入。

通过设置 --web.enable-otlp-receiver 来启用 OTLP 接收器。启用后,OTLP 接收器端点是 /api/v1/otlp/v1/metrics

v2.47 新增

OTLP Delta

Prometheus 可以将来自 delta 临时性的传入指标转换为其累积等效指标。这是通过使用 OpenTelemetry Collector 的 deltatocumulative 实现的。

要启用,传递 --enable-feature=otlp-deltatocumulative 标志。

v3.2 新增

通知

以下端点提供关于 Prometheus 服务端自身的活动状态通知的信息。通知用于 Web UI。

这些端点是实验性的。它们将来可能会更改。

活动通知

/api/v1/notifications 端点返回所有当前活动通知的列表。

GET /api/v1/notifications

示例

curl http://localhost:9090/api/v1/notifications
{
  "status": "success",
  "data": [
    {
      "text": "Prometheus is shutting down and gracefully stopping all operations.",
      "date": "2024-10-07T12:33:08.551376578+02:00",
      "active": true
    }
  ]
}

v3.0 新增

实时通知

/api/v1/notifications/live 端点使用 Server-Sent Events 流式传输发生的实时通知。已删除的通知会以 active: false 发送。连接到此端点时,会发送活动通知。

GET /api/v1/notifications/live

示例

curl http://localhost:9090/api/v1/notifications/live
data: {
  "status": "success",
  "data": [
    {
      "text": "Prometheus is shutting down and gracefully stopping all operations.",
      "date": "2024-10-07T12:33:08.551376578+02:00",
      "active": true
    }
  ]
}

注意:如果达到最大订阅者数量,/notifications/live 端点将返回 204 No Content 响应。您可以使用 --web.max-notifications-subscribers 标志设置最大监听器数量,默认为 16。

GET /api/v1/notifications/live
204 No Content

v3.0 新增

本文档是开源的。请通过提交 issues 或 pull requests 帮助改进它。